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.

138 lines
2.7 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1997
  5. //
  6. // File: sdict.cxx
  7. //
  8. // Contents: simple dictionary
  9. //
  10. // History: 28-Feb-97 SophiaC Created.
  11. //
  12. //----------------------------------------------------------------------------
  13. #include <iis.hxx>
  14. SIMPLE_DICT::SIMPLE_DICT()
  15. {
  16. cDictSize = 0;
  17. iNextItem = 0;
  18. cDictSlots = INITIALDICTSLOTS;
  19. DictSlots = InitialDictSlots;
  20. memset(DictSlots, 0, sizeof(void *) * cDictSlots);
  21. }
  22. SIMPLE_DICT::~SIMPLE_DICT()
  23. {
  24. if (DictSlots != InitialDictSlots)
  25. delete DictSlots;
  26. }
  27. int
  28. SIMPLE_DICT::Insert (
  29. void *Item
  30. )
  31. {
  32. int iDictSlots;
  33. void * * NewDictSlots;
  34. for (iDictSlots = 0; iDictSlots < cDictSlots; iDictSlots++)
  35. if (DictSlots[iDictSlots] == NULL)
  36. {
  37. DictSlots[iDictSlots] = Item;
  38. cDictSize += 1;
  39. return(iDictSlots);
  40. }
  41. // If we fell through to here, it must mean that the dictionary is
  42. // full; hence we need to allocate more space and copy the old
  43. // dictionary into it.
  44. NewDictSlots = (void * *) new char[sizeof(void *)*cDictSlots*2];
  45. if (!NewDictSlots)
  46. return(-1);
  47. memcpy(NewDictSlots, DictSlots, sizeof(void *) * cDictSlots);
  48. memset(NewDictSlots+iDictSlots, 0, sizeof(void *) * cDictSlots);
  49. if (DictSlots != InitialDictSlots)
  50. delete DictSlots;
  51. DictSlots = NewDictSlots;
  52. cDictSlots *= 2;
  53. DictSlots[iDictSlots] = Item;
  54. cDictSize += 1;
  55. return(iDictSlots);
  56. }
  57. void *
  58. SIMPLE_DICT::Find (
  59. int Key
  60. )
  61. {
  62. if (Key >= cDictSlots)
  63. return(NULL);
  64. return(DictSlots[Key]);
  65. }
  66. void *
  67. SIMPLE_DICT::DeleteItemByBruteForce(
  68. void * Item
  69. )
  70. {
  71. if (Item == 0)
  72. {
  73. return (0);
  74. }
  75. for (int i = 0; i < cDictSlots; i++)
  76. {
  77. if (DictSlots[i] == Item)
  78. {
  79. DictSlots[i] = NULL;
  80. cDictSize -= 1;
  81. return (Item);
  82. }
  83. }
  84. return (0);
  85. }
  86. void *
  87. SIMPLE_DICT::Delete (
  88. int Key
  89. )
  90. {
  91. void *Item;
  92. if (Key >= cDictSlots)
  93. {
  94. return(NULL);
  95. }
  96. Item = DictSlots[Key];
  97. ASSERT((DictSlots[Key]));
  98. cDictSize -= 1;
  99. DictSlots[Key] = NULL;
  100. return(Item);
  101. }
  102. void *
  103. SIMPLE_DICT::Next (
  104. )
  105. {
  106. for ( ; iNextItem < cDictSlots; iNextItem++)
  107. {
  108. if (DictSlots[iNextItem])
  109. return(DictSlots[iNextItem++]);
  110. }
  111. iNextItem = NULL;
  112. return(NULL);
  113. }