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.

131 lines
2.9 KiB

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