diff --git a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs index e3923458..2a0885a9 100644 --- a/src/Numerics/LinearAlgebra/Complex/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/DenseVector.cs @@ -106,7 +106,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex public DenseVector(Vector other) : this(other.Count) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index d3265cfc..2b30d279 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -123,7 +123,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex public SparseVector(Vector other) : this(new SparseVectorStorage(other.Count)) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs index 749ce364..6c955b0e 100644 --- a/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/DenseVector.cs @@ -106,7 +106,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 public DenseVector(Vector other) : this(other.Count) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index 914fc87d..c8ff0aa3 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -123,7 +123,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 public SparseVector(Vector other) : this(new SparseVectorStorage(other.Count)) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Double/DenseVector.cs b/src/Numerics/LinearAlgebra/Double/DenseVector.cs index c27ec628..b634acb7 100644 --- a/src/Numerics/LinearAlgebra/Double/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/DenseVector.cs @@ -106,7 +106,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public DenseVector(Vector other) : this(other.Count) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index e38ddde7..bf0079a1 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -123,7 +123,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double public SparseVector(Vector other) : this(new SparseVectorStorage(other.Count)) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Generic/Vector.cs b/src/Numerics/LinearAlgebra/Generic/Vector.cs index 6e6eeeaa..7b17d423 100644 --- a/src/Numerics/LinearAlgebra/Generic/Vector.cs +++ b/src/Numerics/LinearAlgebra/Generic/Vector.cs @@ -150,7 +150,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic public Vector Clone() { var result = CreateVector(Count); - Storage.CopyTo(result.Storage, skipClearing: true); + Storage.CopyToUnchecked(result.Storage, skipClearing: true); return result; } @@ -173,16 +173,6 @@ namespace MathNet.Numerics.LinearAlgebra.Generic throw new ArgumentNullException("target"); } - if (ReferenceEquals(this, target) || ReferenceEquals(Storage, target.Storage)) - { - return; - } - - if (Count != target.Count) - { - throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); - } - Storage.CopyTo(target.Storage); } diff --git a/src/Numerics/LinearAlgebra/Single/DenseVector.cs b/src/Numerics/LinearAlgebra/Single/DenseVector.cs index 91ab2584..918341d0 100644 --- a/src/Numerics/LinearAlgebra/Single/DenseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/DenseVector.cs @@ -106,7 +106,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single public DenseVector(Vector other) : this(other.Count) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 67f65f14..3c89e27b 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -123,7 +123,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single public SparseVector(Vector other) : this(new SparseVectorStorage(other.Count)) { - other.Storage.CopyTo(Storage, skipClearing: true); + other.Storage.CopyToUnchecked(Storage, skipClearing: true); } /// diff --git a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs index b6c2afb5..628027f2 100644 --- a/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs @@ -59,13 +59,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage Array.Clear(Data, index, count); } - /// Parameters assumed to be validated already. - public override void CopyTo(VectorStorage target, bool skipClearing = false) + internal override void CopyToUnchecked(VectorStorage target, bool skipClearing = false) { var denseTarget = target as DenseVectorStorage; if (denseTarget != null) { - CopyTo(denseTarget); + CopyToUnchecked(denseTarget); return; } @@ -77,7 +76,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopyTo(DenseVectorStorage target) + void CopyToUnchecked(DenseVectorStorage target) { if (ReferenceEquals(this, target)) { @@ -93,26 +92,25 @@ namespace MathNet.Numerics.LinearAlgebra.Storage Array.Copy(Data, 0, target.Data, 0, Data.Length); } - public override void CopySubVectorTo(VectorStorage target, + internal override void CopySubVectorToUnchecked(VectorStorage target, int sourceIndex, int targetIndex, int count, bool skipClearing = false) { var denseTarget = target as DenseVectorStorage; if (denseTarget != null) { - CopySubVectorTo(denseTarget, sourceIndex, targetIndex, count); + CopySubVectorToUnchecked(denseTarget, sourceIndex, targetIndex, count); return; } // FALL BACK - base.CopySubVectorTo(target, sourceIndex, targetIndex, count, skipClearing); + base.CopySubVectorToUnchecked(target, sourceIndex, targetIndex, count, skipClearing); } - void CopySubVectorTo(DenseVectorStorage target, + void CopySubVectorToUnchecked(DenseVectorStorage target, int sourceIndex, int targetIndex, int count) { - ValidateSubVectorRange(target, sourceIndex, targetIndex, count); Array.Copy(Data, sourceIndex, target.Data, targetIndex, count); } } diff --git a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs index 7e7a6053..91ba3a08 100644 --- a/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs @@ -180,13 +180,12 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - /// Parameters assumed to be validated already. - public override void CopyTo(VectorStorage target, bool skipClearing = false) + internal override void CopyToUnchecked(VectorStorage target, bool skipClearing = false) { var sparseTarget = target as SparseVectorStorage; if (sparseTarget != null) { - CopyTo(sparseTarget); + CopyToUnchecked(sparseTarget); return; } @@ -206,7 +205,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopyTo(SparseVectorStorage target) + void CopyToUnchecked(SparseVectorStorage target) { if (ReferenceEquals(this, target)) { @@ -230,16 +229,14 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - public override void CopySubVectorTo(VectorStorage target, + internal override void CopySubVectorToUnchecked(VectorStorage target, int sourceIndex, int targetIndex, int count, bool skipClearing = false) { - ValidateSubVectorRange(target, sourceIndex, targetIndex, count); - var sparseTarget = target as SparseVectorStorage; if (sparseTarget != null) { - CopySubVectorTo(sparseTarget, sourceIndex, targetIndex, count, skipClearing); + CopySubVectorToUnchecked(sparseTarget, sourceIndex, targetIndex, count, skipClearing); return; } @@ -263,7 +260,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - void CopySubVectorTo(SparseVectorStorage target, + void CopySubVectorToUnchecked(SparseVectorStorage target, int sourceIndex, int targetIndex, int count, bool skipClearing) { diff --git a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs b/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs index 2993a0b9..ac156ed6 100644 --- a/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs +++ b/src/Numerics/LinearAlgebra/Storage/VectorStorage.cs @@ -166,8 +166,27 @@ namespace MathNet.Numerics.LinearAlgebra.Storage return hash; } - /// Parameters assumed to be validated already. - public virtual void CopyTo(VectorStorage target, bool skipClearing = false) + public void CopyTo(VectorStorage target, bool skipClearing = false) + { + if (target == null) + { + throw new ArgumentNullException("target"); + } + + if (ReferenceEquals(this, target)) + { + return; + } + + if (Length != target.Length) + { + throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); + } + + CopyToUnchecked(target, skipClearing); + } + + internal virtual void CopyToUnchecked(VectorStorage target, bool skipClearing = false) { for (int i = 0; i < Length; i++) { @@ -175,7 +194,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage } } - public virtual void CopySubVectorTo(VectorStorage target, + public void CopySubVectorTo(VectorStorage target, int sourceIndex, int targetIndex, int count, bool skipClearing = false) { @@ -186,6 +205,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage ValidateSubVectorRange(target, sourceIndex, targetIndex, count); + CopySubVectorToUnchecked(target, sourceIndex, targetIndex, count, skipClearing); + } + + internal virtual void CopySubVectorToUnchecked(VectorStorage target, + int sourceIndex, int targetIndex, int count, + bool skipClearing = false) + { if (ReferenceEquals(this, target)) { var tmp = new T[count];