From 7904a2c181850990ba4f03a3a969ef8b15807f25 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Thu, 13 Oct 2011 00:50:53 +0200 Subject: [PATCH] Sparse Linear Algebra: make unchecked setter privately accessible II --- .../LinearAlgebra/Complex/SparseVector.cs | 37 +++++++++++-------- .../LinearAlgebra/Complex32/SparseVector.cs | 37 +++++++++++-------- .../LinearAlgebra/Double/SparseVector.cs | 37 +++++++++++-------- .../LinearAlgebra/Single/SparseVector.cs | 37 +++++++++++-------- 4 files changed, 84 insertions(+), 64 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs index a3d69ea2..ef67a10e 100644 --- a/src/Numerics/LinearAlgebra/Complex/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex/SparseVector.cs @@ -1290,22 +1290,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex // Item already exist at itemIndex if (value == Complex.Zero) { - // Value is zero. Let's delete it from Values and Indices array - for (var i = itemIndex + 1; i < NonZerosCount; i++) - { - _nonZeroValues[i - 1] = _nonZeroValues[i]; - _nonZeroIndices[i - 1] = _nonZeroIndices[i]; - } - - NonZerosCount -= 1; - - // 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)) - { - Array.Resize(ref _nonZeroValues, NonZerosCount); - Array.Resize(ref _nonZeroIndices, NonZerosCount); - } + RemoveAtUnchecked(itemIndex); } else { @@ -1350,6 +1335,26 @@ namespace MathNet.Numerics.LinearAlgebra.Complex NonZerosCount += 1; } + private void RemoveAtUnchecked(int itemIndex) + { + // Value is zero. Let's delete it from Values and Indices array + for (var i = itemIndex + 1; i < NonZerosCount; i++) + { + _nonZeroValues[i - 1] = _nonZeroValues[i]; + _nonZeroIndices[i - 1] = _nonZeroIndices[i]; + } + + NonZerosCount -= 1; + + // 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)) + { + Array.Resize(ref _nonZeroValues, NonZerosCount); + Array.Resize(ref _nonZeroIndices, NonZerosCount); + } + } + /// /// Calculates the amount with which to grow the storage array's if they need to be /// increased in size. diff --git a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs index a99010fe..0b6858aa 100644 --- a/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Complex32/SparseVector.cs @@ -1320,22 +1320,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 // Item already exist at itemIndex if (value == Complex32.Zero) { - // Value is zero. Let's delete it from Values and Indices array - for (var i = itemIndex + 1; i < NonZerosCount; i++) - { - _nonZeroValues[i - 1] = _nonZeroValues[i]; - _nonZeroIndices[i - 1] = _nonZeroIndices[i]; - } - - NonZerosCount -= 1; - - // 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)) - { - Array.Resize(ref _nonZeroValues, NonZerosCount); - Array.Resize(ref _nonZeroIndices, NonZerosCount); - } + RemoveAtUnchecked(itemIndex); } else { @@ -1380,6 +1365,26 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32 NonZerosCount += 1; } + private void RemoveAtUnchecked(int itemIndex) + { + // Value is zero. Let's delete it from Values and Indices array + for (var i = itemIndex + 1; i < NonZerosCount; i++) + { + _nonZeroValues[i - 1] = _nonZeroValues[i]; + _nonZeroIndices[i - 1] = _nonZeroIndices[i]; + } + + NonZerosCount -= 1; + + // 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)) + { + Array.Resize(ref _nonZeroValues, NonZerosCount); + Array.Resize(ref _nonZeroIndices, NonZerosCount); + } + } + /// /// Calculates the amount with which to grow the storage array's if they need to be /// increased in size. diff --git a/src/Numerics/LinearAlgebra/Double/SparseVector.cs b/src/Numerics/LinearAlgebra/Double/SparseVector.cs index e051f388..85836244 100644 --- a/src/Numerics/LinearAlgebra/Double/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Double/SparseVector.cs @@ -1350,22 +1350,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double // Item already exist at itemIndex if (value == 0.0) { - // Value is zero. Let's delete it from Values and Indices array - for (var i = itemIndex + 1; i < NonZerosCount; i++) - { - _nonZeroValues[i - 1] = _nonZeroValues[i]; - _nonZeroIndices[i - 1] = _nonZeroIndices[i]; - } - - NonZerosCount -= 1; - - // 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)) - { - Array.Resize(ref _nonZeroValues, NonZerosCount); - Array.Resize(ref _nonZeroIndices, NonZerosCount); - } + RemoveAtUnchecked(itemIndex); } else { @@ -1410,6 +1395,26 @@ namespace MathNet.Numerics.LinearAlgebra.Double NonZerosCount += 1; } + private void RemoveAtUnchecked(int itemIndex) + { + // Value is zero. Let's delete it from Values and Indices array + for (var i = itemIndex + 1; i < NonZerosCount; i++) + { + _nonZeroValues[i - 1] = _nonZeroValues[i]; + _nonZeroIndices[i - 1] = _nonZeroIndices[i]; + } + + NonZerosCount -= 1; + + // 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)) + { + Array.Resize(ref _nonZeroValues, NonZerosCount); + Array.Resize(ref _nonZeroIndices, NonZerosCount); + } + } + /// /// Calculates the amount with which to grow the storage array's if they need to be /// increased in size. diff --git a/src/Numerics/LinearAlgebra/Single/SparseVector.cs b/src/Numerics/LinearAlgebra/Single/SparseVector.cs index b428530c..a77a7599 100644 --- a/src/Numerics/LinearAlgebra/Single/SparseVector.cs +++ b/src/Numerics/LinearAlgebra/Single/SparseVector.cs @@ -1360,22 +1360,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single // Item already exist at itemIndex if (value == 0.0) { - // Value is zero. Let's delete it from Values and Indices array - for (var i = itemIndex + 1; i < NonZerosCount; i++) - { - _nonZeroValues[i - 1] = _nonZeroValues[i]; - _nonZeroIndices[i - 1] = _nonZeroIndices[i]; - } - - NonZerosCount -= 1; - - // 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)) - { - Array.Resize(ref _nonZeroValues, NonZerosCount); - Array.Resize(ref _nonZeroIndices, NonZerosCount); - } + RemoveAtUnchecked(itemIndex); } else { @@ -1420,6 +1405,26 @@ namespace MathNet.Numerics.LinearAlgebra.Single NonZerosCount += 1; } + private void RemoveAtUnchecked(int itemIndex) + { + // Value is zero. Let's delete it from Values and Indices array + for (var i = itemIndex + 1; i < NonZerosCount; i++) + { + _nonZeroValues[i - 1] = _nonZeroValues[i]; + _nonZeroIndices[i - 1] = _nonZeroIndices[i]; + } + + NonZerosCount -= 1; + + // 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)) + { + Array.Resize(ref _nonZeroValues, NonZerosCount); + Array.Resize(ref _nonZeroIndices, NonZerosCount); + } + } + /// /// Calculates the amount with which to grow the storage array's if they need to be /// increased in size.