Windows NT 4.0 source code leak
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.

136 lines
3.0 KiB

4 years ago
  1. /* --------------------------------------------------------------------
  2. File : sdict2.cxx
  3. Title : Simple dictionary.
  4. Description :
  5. History :
  6. -------------------------------------------------------------------- */
  7. #include <precomp.hxx>
  8. #include <sdict2.hxx>
  9. SIMPLE_DICT2::SIMPLE_DICT2 (
  10. )
  11. {
  12. int iDictSlots;
  13. ALLOCATE_THIS(SIMPLE_DICT2);
  14. cDictSlots = INITIALDICT2SLOTS;
  15. DictKeys = InitialDictKeys;
  16. DictItems = InitialDictItems;
  17. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  18. {
  19. DictKeys [iDictSlots] = (void *) 0;
  20. DictItems[iDictSlots] = (void *) 0;
  21. }
  22. }
  23. SIMPLE_DICT2::~SIMPLE_DICT2 (
  24. )
  25. {
  26. if (DictKeys != InitialDictKeys)
  27. {
  28. ASSERT(DictItems != InitialDictItems);
  29. delete DictKeys;
  30. delete DictItems;
  31. }
  32. }
  33. int
  34. SIMPLE_DICT2::Insert (
  35. void * Key,
  36. void * Item
  37. )
  38. {
  39. int iDictSlots;
  40. void * * NewDictKeys;
  41. void * * NewDictItems;
  42. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  43. {
  44. if (DictKeys[iDictSlots] == (void *) 0)
  45. {
  46. DictKeys[iDictSlots] = Key;
  47. DictItems[iDictSlots] = Item;
  48. return(0);
  49. }
  50. }
  51. // Otherwise, we need to expand the size of the dictionary.
  52. NewDictKeys = (void * *)
  53. new unsigned char [sizeof(void *) * cDictSlots * 2];
  54. NewDictItems = (void * *)
  55. new unsigned char [sizeof(void *) * cDictSlots * 2];
  56. if (NewDictKeys == (void *) 0)
  57. return(-1);
  58. if (NewDictItems == (void *) 0)
  59. return(-1);
  60. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  61. {
  62. NewDictKeys[iDictSlots] = DictKeys[iDictSlots];
  63. NewDictItems[iDictSlots] = DictItems[iDictSlots];
  64. }
  65. cDictSlots *= 2;
  66. NewDictKeys[iDictSlots] = Key;
  67. NewDictItems[iDictSlots] = Item;
  68. for (iDictSlots++; iDictSlots < cDictSlots; iDictSlots++)
  69. {
  70. NewDictKeys[iDictSlots] = (void *) 0;
  71. NewDictItems[iDictSlots] = (void *) 0;
  72. }
  73. if (DictKeys != InitialDictKeys)
  74. {
  75. ASSERT(DictItems != InitialDictItems);
  76. delete DictKeys;
  77. delete DictItems;
  78. }
  79. DictKeys = NewDictKeys;
  80. DictItems = NewDictItems;
  81. return(0);
  82. }
  83. void *
  84. SIMPLE_DICT2::Delete (
  85. void * Key
  86. )
  87. {
  88. int iDictSlots;
  89. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  90. {
  91. if (DictKeys[iDictSlots] == Key)
  92. {
  93. void * Item = DictItems[iDictSlots];
  94. DictKeys [iDictSlots] = (void *) 0;
  95. DictItems[iDictSlots] = (void *) 0;
  96. return Item;
  97. }
  98. }
  99. return((void *) 0);
  100. }
  101. void *
  102. SIMPLE_DICT2::Find (
  103. void * Key
  104. )
  105. {
  106. int iDictSlots;
  107. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  108. {
  109. if (DictKeys[iDictSlots] == Key)
  110. {
  111. return(DictItems[iDictSlots]);
  112. }
  113. }
  114. return((void *) 0);
  115. }