Browse Source

Sparse Linear Algebra: make unchecked setter privately accessible. gh-20.

la-knuth
Christoph Ruegg 15 years ago
parent
commit
101864e798
  1. 57
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  2. 57
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  3. 57
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  4. 57
      src/Numerics/LinearAlgebra/Single/SparseVector.cs

57
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -1181,7 +1181,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
NonZerosCount -= 1; 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 // there are a lot of non-zero elements and storage is two times bigger
if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2))
{ {
@ -1196,39 +1196,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
} }
else else
{ {
if (value == Complex.Zero) if (value != Complex.Zero)
{ {
return; InsertAtUnchecked(~itemIndex, index, value);
} }
}
}
itemIndex = ~itemIndex; // Index where to put new value private void InsertAtUnchecked(int itemIndex, int index, Complex value)
{
// Check if the storage needs to be increased // Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count))
{ {
// Value and Indices arrays are completely full so we increase the size // Value and Indices arrays are completely full so we increase the size
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count);
Array.Resize(ref _nonZeroValues, size); Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _nonZeroIndices, size); Array.Resize(ref _nonZeroIndices, size);
} }
// Move all values (with an position larger than index) in the value array // Move all values (with an position larger than index) in the value array
// to the next position // to the next position
// move all values (with an position larger than index) in the columIndices // Move all values (with an position larger than index) in the columIndices
// array to the next position // array to the next position
for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) for (var i = NonZerosCount - 1; i > itemIndex - 1; i--)
{ {
_nonZeroValues[i + 1] = _nonZeroValues[i]; _nonZeroValues[i + 1] = _nonZeroValues[i];
_nonZeroIndices[i + 1] = _nonZeroIndices[i]; _nonZeroIndices[i + 1] = _nonZeroIndices[i];
} }
// Add the value and the column index // Add the value and the column index
_nonZeroValues[itemIndex] = value; _nonZeroValues[itemIndex] = value;
_nonZeroIndices[itemIndex] = index; _nonZeroIndices[itemIndex] = index;
// increase the number of non-zero numbers by one // increase the number of non-zero numbers by one
NonZerosCount += 1; NonZerosCount += 1;
}
} }
/// <summary> /// <summary>

57
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -1211,7 +1211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
NonZerosCount -= 1; 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 // there are a lot of non-zero elements and storage is two times bigger
if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2))
{ {
@ -1226,39 +1226,40 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
} }
else else
{ {
if (value == Complex32.Zero) if (value != Complex32.Zero)
{ {
return; InsertAtUnchecked(~itemIndex, index, value);
} }
}
}
itemIndex = ~itemIndex; // Index where to put new value private void InsertAtUnchecked(int itemIndex, int index, Complex32 value)
{
// Check if the storage needs to be increased // Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count))
{ {
// Value and Indices arrays are completely full so we increase the size // Value and Indices arrays are completely full so we increase the size
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count);
Array.Resize(ref _nonZeroValues, size); Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _nonZeroIndices, size); Array.Resize(ref _nonZeroIndices, size);
} }
// Move all values (with an position larger than index) in the value array // Move all values (with an position larger than index) in the value array
// to the next position // to the next position
// move all values (with an position larger than index) in the columIndices // Move all values (with an position larger than index) in the columIndices
// array to the next position // array to the next position
for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) for (var i = NonZerosCount - 1; i > itemIndex - 1; i--)
{ {
_nonZeroValues[i + 1] = _nonZeroValues[i]; _nonZeroValues[i + 1] = _nonZeroValues[i];
_nonZeroIndices[i + 1] = _nonZeroIndices[i]; _nonZeroIndices[i + 1] = _nonZeroIndices[i];
} }
// Add the value and the column index // Add the value and the column index
_nonZeroValues[itemIndex] = value; _nonZeroValues[itemIndex] = value;
_nonZeroIndices[itemIndex] = index; _nonZeroIndices[itemIndex] = index;
// increase the number of non-zero numbers by one // increase the number of non-zero numbers by one
NonZerosCount += 1; NonZerosCount += 1;
}
} }
/// <summary> /// <summary>

57
src/Numerics/LinearAlgebra/Double/SparseVector.cs

@ -1241,7 +1241,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
NonZerosCount -= 1; 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 // there are a lot of non-zero elements and storage is two times bigger
if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2))
{ {
@ -1256,39 +1256,40 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
if (value == 0.0) if (value != 0.0)
{ {
return; InsertAtUnchecked(~itemIndex, index, value);
} }
}
}
itemIndex = ~itemIndex; // Index where to put new value private void InsertAtUnchecked(int itemIndex, int index, double value)
{
// Check if the storage needs to be increased // Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count))
{ {
// Value and Indices arrays are completely full so we increase the size // Value and Indices arrays are completely full so we increase the size
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count);
Array.Resize(ref _nonZeroValues, size); Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _nonZeroIndices, size); Array.Resize(ref _nonZeroIndices, size);
} }
// Move all values (with an position larger than index) in the value array // Move all values (with an position larger than index) in the value array
// to the next position // to the next position
// move all values (with an position larger than index) in the columIndices // Move all values (with an position larger than index) in the columIndices
// array to the next position // array to the next position
for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) for (var i = NonZerosCount - 1; i > itemIndex - 1; i--)
{ {
_nonZeroValues[i + 1] = _nonZeroValues[i]; _nonZeroValues[i + 1] = _nonZeroValues[i];
_nonZeroIndices[i + 1] = _nonZeroIndices[i]; _nonZeroIndices[i + 1] = _nonZeroIndices[i];
} }
// Add the value and the column index // Add the value and the column index
_nonZeroValues[itemIndex] = value; _nonZeroValues[itemIndex] = value;
_nonZeroIndices[itemIndex] = index; _nonZeroIndices[itemIndex] = index;
// increase the number of non-zero numbers by one // increase the number of non-zero numbers by one
NonZerosCount += 1; NonZerosCount += 1;
}
} }
/// <summary> /// <summary>

57
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -1251,7 +1251,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
NonZerosCount -= 1; 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 // there are a lot of non-zero elements and storage is two times bigger
if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2)) if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2))
{ {
@ -1266,39 +1266,40 @@ namespace MathNet.Numerics.LinearAlgebra.Single
} }
else else
{ {
if (value == 0.0) if (value != 0.0)
{ {
return; InsertAtUnchecked(~itemIndex, index, value);
} }
}
}
itemIndex = ~itemIndex; // Index where to put new value private void InsertAtUnchecked(int itemIndex, int index, float value)
{
// Check if the storage needs to be increased // Check if the storage needs to be increased
if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count)) if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count))
{ {
// Value and Indices arrays are completely full so we increase the size // Value and Indices arrays are completely full so we increase the size
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count); var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count);
Array.Resize(ref _nonZeroValues, size); Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _nonZeroIndices, size); Array.Resize(ref _nonZeroIndices, size);
} }
// Move all values (with an position larger than index) in the value array // Move all values (with an position larger than index) in the value array
// to the next position // to the next position
// move all values (with an position larger than index) in the columIndices // Move all values (with an position larger than index) in the columIndices
// array to the next position // array to the next position
for (var i = NonZerosCount - 1; i > itemIndex - 1; i--) for (var i = NonZerosCount - 1; i > itemIndex - 1; i--)
{ {
_nonZeroValues[i + 1] = _nonZeroValues[i]; _nonZeroValues[i + 1] = _nonZeroValues[i];
_nonZeroIndices[i + 1] = _nonZeroIndices[i]; _nonZeroIndices[i + 1] = _nonZeroIndices[i];
} }
// Add the value and the column index // Add the value and the column index
_nonZeroValues[itemIndex] = value; _nonZeroValues[itemIndex] = value;
_nonZeroIndices[itemIndex] = index; _nonZeroIndices[itemIndex] = index;
// increase the number of non-zero numbers by one // increase the number of non-zero numbers by one
NonZerosCount += 1; NonZerosCount += 1;
}
} }
/// <summary> /// <summary>

Loading…
Cancel
Save