From 101864e798cce7a8a9fd2fd3a2d5c89d0db6ebb5 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Tue, 11 Oct 2011 21:30:16 +0200 Subject: [PATCH] Sparse Linear Algebra: make unchecked setter privately accessible. gh-20. --- .../LinearAlgebra/Complex/SparseVector.cs | 57 ++++++++++--------- .../LinearAlgebra/Complex32/SparseVector.cs | 57 ++++++++++--------- .../LinearAlgebra/Double/SparseVector.cs | 57 ++++++++++--------- .../LinearAlgebra/Single/SparseVector.cs | 57 ++++++++++--------- 4 files changed, 116 insertions(+), 112 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index 4dbe5825..a087a64a 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -1181,7 +1181,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex NonZerosCount -= 1; - // Check if the storage needs to be shrink. This is reasonable to do if + // Check if the storage needs to be shrink. This is reasonable to do if // there are a lot of non-zero elements and storage is two times bigger if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) { @@ -1196,39 +1196,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex } else { - if (value == Complex.Zero) + if (value != Complex.Zero) { - return; + InsertAtUnchecked(~itemIndex, index, value); } + } + } - itemIndex = ~itemIndex; // Index where to put new value - - // Check if the storage needs to be increased - if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) - { - // Value and Indices arrays are completely full so we increase the size - var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); - Array.Resize(ref _nonZeroValues, size); - Array.Resize(ref _nonZeroIndices, size); - } + private void InsertAtUnchecked(int itemIndex, int index, Complex value) + { + // Check if the storage needs to be increased + if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) + { + // Value and Indices arrays are completely full so we increase the size + var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); + Array.Resize(ref _nonZeroValues, size); + Array.Resize(ref _nonZeroIndices, size); + } - // Move all values (with an position larger than index) in the value array - // to the next position - // move all values (with an position larger than index) in the columIndices - // array to the next position - for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) - { - _nonZeroValues[i + 1] = _nonZeroValues[i]; - _nonZeroIndices[i + 1] = _nonZeroIndices[i]; - } + // Move all values (with an position larger than index) in the value array + // to the next position + // Move all values (with an position larger than index) in the columIndices + // array to the next position + for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) + { + _nonZeroValues[i + 1] = _nonZeroValues[i]; + _nonZeroIndices[i + 1] = _nonZeroIndices[i]; + } - // Add the value and the column index - _nonZeroValues[itemIndex] = value; - _nonZeroIndices[itemIndex] = index; + // Add the value and the column index + _nonZeroValues[itemIndex] = value; + _nonZeroIndices[itemIndex] = index; - // increase the number of non-zero numbers by one - NonZerosCount += 1; - } + // increase the number of non-zero numbers by one + NonZerosCount += 1; } /// diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index f18393e0..ef79608b 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -1211,7 +1211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 NonZerosCount -= 1; - // Check if the storage needs to be shrink. This is reasonable to do if + // Check if the storage needs to be shrink. This is reasonable to do if // there are a lot of non-zero elements and storage is two times bigger if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) { @@ -1226,39 +1226,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 } else { - if (value == Complex32.Zero) + if (value != Complex32.Zero) { - return; + InsertAtUnchecked(~itemIndex, index, value); } + } + } - itemIndex = ~itemIndex; // Index where to put new value - - // Check if the storage needs to be increased - if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) - { - // Value and Indices arrays are completely full so we increase the size - var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); - Array.Resize(ref _nonZeroValues, size); - Array.Resize(ref _nonZeroIndices, size); - } + private void InsertAtUnchecked(int itemIndex, int index, Complex32 value) + { + // Check if the storage needs to be increased + if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) + { + // Value and Indices arrays are completely full so we increase the size + var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); + Array.Resize(ref _nonZeroValues, size); + Array.Resize(ref _nonZeroIndices, size); + } - // Move all values (with an position larger than index) in the value array - // to the next position - // move all values (with an position larger than index) in the columIndices - // array to the next position - for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) - { - _nonZeroValues[i + 1] = _nonZeroValues[i]; - _nonZeroIndices[i + 1] = _nonZeroIndices[i]; - } + // Move all values (with an position larger than index) in the value array + // to the next position + // Move all values (with an position larger than index) in the columIndices + // array to the next position + for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) + { + _nonZeroValues[i + 1] = _nonZeroValues[i]; + _nonZeroIndices[i + 1] = _nonZeroIndices[i]; + } - // Add the value and the column index - _nonZeroValues[itemIndex] = value; - _nonZeroIndices[itemIndex] = index; + // Add the value and the column index + _nonZeroValues[itemIndex] = value; + _nonZeroIndices[itemIndex] = index; - // increase the number of non-zero numbers by one - NonZerosCount += 1; - } + // increase the number of non-zero numbers by one + NonZerosCount += 1; } /// diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index 911dbe64..60eb51bf 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -1241,7 +1241,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double NonZerosCount -= 1; - // Check if the storage needs to be shrink. This is reasonable to do if + // Check if the storage needs to be shrink. This is reasonable to do if // there are a lot of non-zero elements and storage is two times bigger if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) { @@ -1256,39 +1256,40 @@ namespace MathNet.Numerics.LinearAlgebra.Double } else { - if (value == 0.0) + if (value != 0.0) { - return; + InsertAtUnchecked(~itemIndex, index, value); } + } + } - itemIndex = ~itemIndex; // Index where to put new value - - // Check if the storage needs to be increased - if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) - { - // Value and Indices arrays are completely full so we increase the size - var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); - Array.Resize(ref _nonZeroValues, size); - Array.Resize(ref _nonZeroIndices, size); - } + private void InsertAtUnchecked(int itemIndex, int index, double value) + { + // Check if the storage needs to be increased + if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) + { + // Value and Indices arrays are completely full so we increase the size + var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); + Array.Resize(ref _nonZeroValues, size); + Array.Resize(ref _nonZeroIndices, size); + } - // Move all values (with an position larger than index) in the value array - // to the next position - // move all values (with an position larger than index) in the columIndices - // array to the next position - for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) - { - _nonZeroValues[i + 1] = _nonZeroValues[i]; - _nonZeroIndices[i + 1] = _nonZeroIndices[i]; - } + // Move all values (with an position larger than index) in the value array + // to the next position + // Move all values (with an position larger than index) in the columIndices + // array to the next position + for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) + { + _nonZeroValues[i + 1] = _nonZeroValues[i]; + _nonZeroIndices[i + 1] = _nonZeroIndices[i]; + } - // Add the value and the column index - _nonZeroValues[itemIndex] = value; - _nonZeroIndices[itemIndex] = index; + // Add the value and the column index + _nonZeroValues[itemIndex] = value; + _nonZeroIndices[itemIndex] = index; - // increase the number of non-zero numbers by one - NonZerosCount += 1; - } + // increase the number of non-zero numbers by one + NonZerosCount += 1; } /// diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index 3b88434c..7e9e6c7d 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -1251,7 +1251,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single NonZerosCount -= 1; - // Check if the storage needs to be shrink. This is reasonable to do if + // Check if the storage needs to be shrink. This is reasonable to do if // there are a lot of non-zero elements and storage is two times bigger if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) { @@ -1266,39 +1266,40 @@ namespace MathNet.Numerics.LinearAlgebra.Single } else { - if (value == 0.0) + if (value != 0.0) { - return; + InsertAtUnchecked(~itemIndex, index, value); } + } + } - itemIndex = ~itemIndex; // Index where to put new value - - // Check if the storage needs to be increased - if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) - { - // Value and Indices arrays are completely full so we increase the size - var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); - Array.Resize(ref _nonZeroValues, size); - Array.Resize(ref _nonZeroIndices, size); - } + private void InsertAtUnchecked(int itemIndex, int index, float value) + { + // Check if the storage needs to be increased + if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) + { + // Value and Indices arrays are completely full so we increase the size + var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); + Array.Resize(ref _nonZeroValues, size); + Array.Resize(ref _nonZeroIndices, size); + } - // Move all values (with an position larger than index) in the value array - // to the next position - // move all values (with an position larger than index) in the columIndices - // array to the next position - for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) - { - _nonZeroValues[i + 1] = _nonZeroValues[i]; - _nonZeroIndices[i + 1] = _nonZeroIndices[i]; - } + // Move all values (with an position larger than index) in the value array + // to the next position + // Move all values (with an position larger than index) in the columIndices + // array to the next position + for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) + { + _nonZeroValues[i + 1] = _nonZeroValues[i]; + _nonZeroIndices[i + 1] = _nonZeroIndices[i]; + } - // Add the value and the column index - _nonZeroValues[itemIndex] = value; - _nonZeroIndices[itemIndex] = index; + // Add the value and the column index + _nonZeroValues[itemIndex] = value; + _nonZeroIndices[itemIndex] = index; - // increase the number of non-zero numbers by one - NonZerosCount += 1; - } + // increase the number of non-zero numbers by one + NonZerosCount += 1; } ///