Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.8 KiB

  1. //===== Copyright � 1996-2006, Valve Corporation, All rights reserved. ======//
  2. //
  3. //===========================================================================//
  4. #include "tier1/sparsematrix.h"
  5. // memdbgon must be the last include file in a .cpp file!!!
  6. #include "tier0/memdbgon.h"
  7. void CSparseMatrix::AdjustAllRowIndicesAfter( int nStartRow, int nDelta )
  8. {
  9. // now, we need to offset the starting position of all subsequent rows by -1 to compensate for the removal of this element
  10. for( int nOtherRow = nStartRow + 1 ; nOtherRow < Height(); nOtherRow++ )
  11. {
  12. m_rowDescriptors[nOtherRow].m_nDataIndex += nDelta;
  13. }
  14. }
  15. void CSparseMatrix::SetDimensions( int nNumRows, int nNumCols )
  16. {
  17. m_nNumRows = nNumRows;
  18. m_nNumCols = nNumCols;
  19. m_entries.SetCount( 0 );
  20. m_rowDescriptors.SetCount( m_nNumRows );
  21. // and set all rows to be empty
  22. for( int i = 0; i < m_nNumRows; i++ )
  23. {
  24. m_rowDescriptors[i].m_nNonZeroCount = 0;
  25. m_rowDescriptors[i].m_nDataIndex = 0;
  26. }
  27. m_nHighestRowAppendedTo = -1;
  28. }
  29. void CSparseMatrix::SetElement( int nRow, int nCol, float flValue )
  30. {
  31. Assert( nCol < m_nNumCols );
  32. int nCount = m_rowDescriptors[nRow].m_nNonZeroCount;
  33. bool bValueIsZero = ( flValue == 0.0 );
  34. int nFirstEntryIndex = m_rowDescriptors[nRow].m_nDataIndex;
  35. if ( nCount )
  36. {
  37. NonZeroValueDescriptor_t *pValue = &( m_entries[nFirstEntryIndex] );
  38. int i;
  39. for( i = 0; i < nCount; i++ )
  40. {
  41. int nIdx = pValue->m_nColumnNumber;
  42. if ( nIdx == nCol ) // we found it!
  43. {
  44. if ( !bValueIsZero )
  45. {
  46. // we want to overwrite the existing value
  47. pValue->m_flValue = flValue;
  48. }
  49. else
  50. {
  51. // there is a non-zero element currently at this position. We need to remove it
  52. // and we need to remove its storage.
  53. m_rowDescriptors[nRow].m_nNonZeroCount--;
  54. m_entries.Remove( nFirstEntryIndex + i );
  55. // now, we need to offset the starting position of all subsequent rows by -1 to compensate for the removal of this element
  56. AdjustAllRowIndicesAfter( nRow, -1 );
  57. }
  58. return;
  59. }
  60. if ( nIdx > nCol )
  61. {
  62. break;
  63. }
  64. pValue++;
  65. }
  66. // we did not find an entry for this cell. If we were writing zero, fine - we are
  67. // done, otherwise insert
  68. if (! bValueIsZero )
  69. {
  70. m_rowDescriptors[nRow].m_nNonZeroCount++;
  71. NonZeroValueDescriptor_t newValue;
  72. newValue.m_nColumnNumber = nCol;
  73. newValue.m_flValue = flValue;
  74. if ( i == nCount ) // need to append
  75. {
  76. m_entries.InsertAfter( nFirstEntryIndex + nCount - 1, newValue );
  77. }
  78. else
  79. {
  80. m_entries.InsertBefore( nFirstEntryIndex + i, newValue );
  81. }
  82. // now, we need to offset the starting position of all subsequent rows by -1 to compensate for the addition of this element
  83. AdjustAllRowIndicesAfter( nRow, +1 );
  84. }
  85. }
  86. else
  87. {
  88. // row is empty. We may need to insert
  89. if ( ! bValueIsZero )
  90. {
  91. m_rowDescriptors[nRow].m_nNonZeroCount++;
  92. NonZeroValueDescriptor_t newValue;
  93. newValue.m_nColumnNumber = nCol;
  94. newValue.m_flValue = flValue;
  95. m_entries.InsertBefore( nFirstEntryIndex, newValue );
  96. AdjustAllRowIndicesAfter( nRow, +1 );
  97. }
  98. }
  99. }
  100. void CSparseMatrix::FinishedAppending( void )
  101. {
  102. // set all pointers to space for subsequent rows to the right value
  103. for( int i = m_nHighestRowAppendedTo + 1 ; i < Height(); i++ )
  104. {
  105. m_rowDescriptors[i].m_nDataIndex = m_entries.Count();
  106. }
  107. }
  108. void CSparseMatrix::AppendElement( int nRow, int nColumn, float flValue )
  109. {
  110. if ( flValue != 0.0 )
  111. {
  112. if ( m_nHighestRowAppendedTo != nRow )
  113. {
  114. Assert( nRow > m_nHighestRowAppendedTo );
  115. for( int i = m_nHighestRowAppendedTo + 1; i <= nRow; i++ )
  116. {
  117. m_rowDescriptors[i].m_nDataIndex = m_entries.Count();
  118. }
  119. }
  120. m_nHighestRowAppendedTo = nRow;
  121. m_rowDescriptors[nRow].m_nNonZeroCount++;
  122. NonZeroValueDescriptor_t newDesc;
  123. newDesc.m_nColumnNumber = nColumn;
  124. newDesc.m_flValue = flValue;
  125. m_entries.AddToTail( newDesc );
  126. }
  127. }