Leaked source code of windows server 2003
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.

202 lines
4.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1990 - 1999
  6. //
  7. // File: sdict.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. /* --------------------------------------------------------------------
  11. File : sdict.cxx
  12. Title : A simple dictionary.
  13. Description :
  14. History :
  15. mikemon ??-??-?? Beginning of time.
  16. mikemon 10-30-90 Fixed the dictionary size.
  17. -------------------------------------------------------------------- */
  18. #include <precomp.hxx>
  19. SIMPLE_DICT::SIMPLE_DICT()
  20. {
  21. ALLOCATE_THIS(SIMPLE_DICT);
  22. cDictSize = 0;
  23. cDictSlots = INITIALDICTSLOTS;
  24. DictSlots = InitialDictSlots;
  25. memset(DictSlots, 0, sizeof(void *) * cDictSlots);
  26. }
  27. SIMPLE_DICT::~SIMPLE_DICT()
  28. {
  29. if (DictSlots != InitialDictSlots)
  30. delete DictSlots;
  31. }
  32. unsigned int
  33. SIMPLE_DICT::Insert (
  34. void *Item
  35. )
  36. {
  37. unsigned int iDictSlots;
  38. BOOL Res;
  39. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  40. if (DictSlots[iDictSlots] == Nil)
  41. {
  42. DictSlots[iDictSlots] = Item;
  43. cDictSize += 1;
  44. return(iDictSlots);
  45. }
  46. // If we fell through to here, it must mean that the dictionary is
  47. // full; hence we need to allocate more space and copy the old
  48. // dictionary into it.
  49. Res = ExpandToSize(cDictSlots * 2);
  50. if (!Res)
  51. return (-1);
  52. DictSlots[iDictSlots] = Item;
  53. cDictSize += 1;
  54. return(iDictSlots);
  55. }
  56. void *
  57. SIMPLE_DICT::DeleteItemByBruteForce(
  58. void * Item
  59. )
  60. {
  61. if (Item == 0)
  62. {
  63. return (0);
  64. }
  65. for (unsigned int i = 0; i < cDictSlots; i++)
  66. {
  67. if (DictSlots[i] == Item)
  68. {
  69. DictSlots[i] = Nil;
  70. cDictSize -= 1;
  71. return (Item);
  72. }
  73. }
  74. return (0);
  75. }
  76. void *
  77. SIMPLE_DICT::Delete (
  78. unsigned int Key
  79. )
  80. {
  81. void *Item;
  82. if (Key >= cDictSlots)
  83. {
  84. return(Nil);
  85. }
  86. Item = DictSlots[Key];
  87. ASSERT((DictSlots[Key]));
  88. cDictSize -= 1;
  89. DictSlots[Key] = Nil;
  90. return(Item);
  91. }
  92. unsigned int
  93. SIMPLE_DICT::ExpandToSize (
  94. unsigned int Size
  95. )
  96. {
  97. void * * NewDictSlots;
  98. if (Size <= cDictSlots)
  99. return cDictSlots; // cDictSlots is simply a quick non-zero value
  100. NewDictSlots = (void * *) new char[sizeof(void *) * Size];
  101. if (!NewDictSlots)
  102. return(0);
  103. RpcpMemoryCopy(NewDictSlots, DictSlots, sizeof(void *) * cDictSlots);
  104. RpcpMemorySet(NewDictSlots + cDictSlots, 0, sizeof(void *) * (Size - cDictSlots));
  105. if (DictSlots != InitialDictSlots)
  106. delete DictSlots;
  107. DictSlots = NewDictSlots;
  108. cDictSlots = Size;
  109. return cDictSlots; // cDictSlots is simply a quick non-zero value
  110. }
  111. void
  112. SIMPLE_DICT::DeleteAll (
  113. void
  114. )
  115. {
  116. RpcpMemorySet(DictSlots, 0, cDictSlots * sizeof(void *));
  117. }
  118. void *
  119. SIMPLE_DICT::Next (DictionaryCursor &cursor
  120. )
  121. {
  122. for ( ; cursor < cDictSlots; cursor++)
  123. {
  124. if (DictSlots[cursor])
  125. return(DictSlots[cursor++]);
  126. }
  127. cursor = Nil;
  128. return(Nil);
  129. }
  130. void *SIMPLE_DICT::RemoveNext(DictionaryCursor &cursor)
  131. {
  132. for ( ; cursor < cDictSlots; cursor++)
  133. {
  134. if (DictSlots[cursor])
  135. {
  136. void *Item = DictSlots[cursor];
  137. cDictSize -= 1;
  138. DictSlots[cursor] = Nil;
  139. cursor++;
  140. return(Item);
  141. }
  142. }
  143. cursor = Nil;
  144. return(Nil);
  145. }
  146. void *SIMPLE_DICT::NextWithKey (DictionaryCursor &cursor, unsigned int *Key)
  147. {
  148. for ( ; cursor < cDictSlots; cursor++)
  149. {
  150. if (DictSlots[cursor])
  151. {
  152. *Key = cursor;
  153. return(DictSlots[cursor++]);
  154. }
  155. }
  156. cursor = Nil;
  157. return(Nil);
  158. }