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.

194 lines
4.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1990 - 1999
  6. //
  7. // File: sdict2.cxx
  8. //
  9. //--------------------------------------------------------------------------
  10. /* --------------------------------------------------------------------
  11. File : sdict2.cxx
  12. Title : Simple dictionary.
  13. Description :
  14. History :
  15. -------------------------------------------------------------------- */
  16. #include <precomp.hxx>
  17. #include <sdict2.hxx>
  18. SIMPLE_DICT2::SIMPLE_DICT2 (
  19. )
  20. {
  21. unsigned int iDictSlots;
  22. ALLOCATE_THIS(SIMPLE_DICT2);
  23. cDictSlots = INITIALDICT2SLOTS;
  24. DictKeys = InitialDictKeys;
  25. DictItems = InitialDictItems;
  26. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  27. {
  28. DictKeys [iDictSlots] = (void *) 0;
  29. DictItems[iDictSlots] = (void *) 0;
  30. }
  31. }
  32. SIMPLE_DICT2::~SIMPLE_DICT2 (
  33. )
  34. {
  35. if (DictKeys != InitialDictKeys)
  36. {
  37. ASSERT(DictItems != InitialDictItems);
  38. delete DictKeys;
  39. delete DictItems;
  40. }
  41. }
  42. int
  43. SIMPLE_DICT2::Insert (
  44. void * Key,
  45. void * Item
  46. )
  47. {
  48. unsigned int iDictSlots;
  49. void * * NewDictKeys;
  50. void * * NewDictItems;
  51. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  52. {
  53. if (DictKeys[iDictSlots] == (void *) 0)
  54. {
  55. DictKeys[iDictSlots] = Key;
  56. DictItems[iDictSlots] = Item;
  57. return(0);
  58. }
  59. }
  60. // Otherwise, we need to expand the size of the dictionary.
  61. NewDictKeys = (void * *)
  62. new unsigned char [sizeof(void *) * cDictSlots * 2];
  63. NewDictItems = (void * *)
  64. new unsigned char [sizeof(void *) * cDictSlots * 2];
  65. if (NewDictKeys == (void *) 0)
  66. {
  67. if (NewDictItems)
  68. delete NewDictItems;
  69. return(-1);
  70. }
  71. if (NewDictItems == (void *) 0)
  72. {
  73. delete NewDictKeys;
  74. return(-1);
  75. }
  76. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  77. {
  78. NewDictKeys[iDictSlots] = DictKeys[iDictSlots];
  79. NewDictItems[iDictSlots] = DictItems[iDictSlots];
  80. }
  81. cDictSlots *= 2;
  82. NewDictKeys[iDictSlots] = Key;
  83. NewDictItems[iDictSlots] = Item;
  84. for (iDictSlots++; iDictSlots < cDictSlots; iDictSlots++)
  85. {
  86. NewDictKeys[iDictSlots] = (void *) 0;
  87. NewDictItems[iDictSlots] = (void *) 0;
  88. }
  89. if (DictKeys != InitialDictKeys)
  90. {
  91. ASSERT(DictItems != InitialDictItems);
  92. delete DictKeys;
  93. delete DictItems;
  94. }
  95. DictKeys = NewDictKeys;
  96. DictItems = NewDictItems;
  97. return(0);
  98. }
  99. void *
  100. SIMPLE_DICT2::Delete (
  101. void * Key
  102. )
  103. {
  104. unsigned int iDictSlots;
  105. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  106. {
  107. if (DictKeys[iDictSlots] == Key)
  108. {
  109. void * Item = DictItems[iDictSlots];
  110. DictKeys [iDictSlots] = (void *) 0;
  111. DictItems[iDictSlots] = (void *) 0;
  112. return Item;
  113. }
  114. }
  115. return((void *) 0);
  116. }
  117. void *
  118. SIMPLE_DICT2::Find (
  119. void * Key
  120. )
  121. {
  122. unsigned int iDictSlots;
  123. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  124. {
  125. if (DictKeys[iDictSlots] == Key)
  126. {
  127. return(DictItems[iDictSlots]);
  128. }
  129. }
  130. return((void *) 0);
  131. }
  132. void
  133. SIMPLE_DICT2::Update (
  134. void * Key,
  135. void *Item
  136. )
  137. {
  138. unsigned int iDictSlots;
  139. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  140. {
  141. if (DictKeys[iDictSlots] == Key)
  142. {
  143. DictItems[iDictSlots] = Item ;
  144. return;
  145. }
  146. }
  147. ASSERT(0) ;
  148. }
  149. void *SIMPLE_DICT2::Next (DictionaryCursor &cursor, BOOL fRemove)
  150. {
  151. for ( ; cursor < cDictSlots; cursor++)
  152. {
  153. if (DictKeys[cursor])
  154. {
  155. if (fRemove)
  156. {
  157. DictKeys[cursor] = 0;
  158. }
  159. return(DictItems[cursor++]);
  160. }
  161. }
  162. cursor = Nil;
  163. return(Nil);
  164. }