Source code of Windows XP (NT5)
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.

128 lines
3.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) Microsoft Corporation, 1991 - 1997
  4. //
  5. // File: occarray.cxx
  6. //
  7. // Contents: Occurrence array
  8. //
  9. // Classes: CSparseOccArray
  10. //
  11. // History: 20-Jun-96 SitaramR Created
  12. //
  13. //----------------------------------------------------------------------------
  14. #include <pch.cxx>
  15. #pragma hdrstop
  16. #include "occarray.hxx"
  17. //+---------------------------------------------------------------------------
  18. //
  19. // Member: CSparseOccArray::CSparseOccArray, public
  20. //
  21. // Synopsis: Constructor
  22. //
  23. // Arguments: [size] -- the size of the initial array. If no parameter is
  24. // passed in, this defaults to OCCARRAY_SIZE.
  25. //
  26. // History: 1-Dec-97 dlee created
  27. //
  28. //----------------------------------------------------------------------------
  29. CSparseOccArray::CSparseOccArray(ULONG size)
  30. : _aPidOcc( size )
  31. {
  32. } //CSparseOccArray
  33. //+---------------------------------------------------------------------------
  34. //
  35. // Member: CSparseOccArray::Get, public
  36. //
  37. // Synopsis: Returns a reference to the occurrence count for a propid
  38. //
  39. // Arguments: [pid] -- Property id
  40. //
  41. // History: 1-Dec-97 dlee created
  42. //
  43. //----------------------------------------------------------------------------
  44. OCCURRENCE & CSparseOccArray::Get( ULONG pid )
  45. {
  46. //
  47. // Look it up in the array. Grab the pointer so we don't use the
  48. // non-const version of operator []
  49. //
  50. SPidOcc * pItems = (SPidOcc *) _aPidOcc.GetPointer();
  51. unsigned cItems = _aPidOcc.Count();
  52. for ( ULONG i = 0; i < cItems; i++ )
  53. if ( pid == pItems[i].pid )
  54. return pItems[i].occ;
  55. // not found; assume the occurrence count is 1 and add it to the array
  56. SPidOcc & item = _aPidOcc[ i ];
  57. item.pid = pid;
  58. item.occ = 1;
  59. return item.occ;
  60. } //Get
  61. //+---------------------------------------------------------------------------
  62. //
  63. // Member: CSparseOccArray::Set
  64. //
  65. // Synopsis: Sets the occurrence for the pid
  66. //
  67. // Arguments: [pid] -- Property id
  68. // [occ] -- Occurrence to set
  69. //
  70. // History: 1-Dec-97 dlee created
  71. //
  72. //----------------------------------------------------------------------------
  73. void CSparseOccArray::Set( ULONG pid, OCCURRENCE occ )
  74. {
  75. Win4Assert( occ > 0 );
  76. if ( occ > 1 )
  77. {
  78. //
  79. // First try to update an existing entry for the pid
  80. //
  81. SPidOcc * pItems = (SPidOcc *) _aPidOcc.GetPointer();
  82. unsigned cItems = _aPidOcc.Count();
  83. for ( ULONG i = 0; i < cItems; i++ )
  84. {
  85. if ( pid == pItems[i].pid )
  86. {
  87. Win4Assert( occ > pItems[i].occ );
  88. pItems[i].occ = occ;
  89. return;
  90. }
  91. }
  92. //
  93. // This is a linear algorithm -- if we hit this assert for normal
  94. // files rethink the design.
  95. //
  96. Win4Assert( i < 500 );
  97. //
  98. // Add the new pid
  99. //
  100. SPidOcc & item = _aPidOcc[ i ];
  101. item.pid = pid;
  102. item.occ = occ;
  103. }
  104. } //Set