OrderedHashTable.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file OrderedHashTable.h
  13. @brief The OrderedHashTable<T> template class. */
  14. namespace kNet
  15. {
  16. template<typename T, typename HashFunc>
  17. class OrderedHashTable
  18. {
  19. public:
  20. struct Node
  21. {
  22. Node()
  23. :next(0), prev(0), hashChain(0), used(false)
  24. {
  25. }
  26. T value;
  27. bool used;
  28. Node *next;
  29. Node *prev;
  30. Node *hashChain;
  31. };
  32. explicit OrderedHashTable(size_t numElems)
  33. :root(0), numElemsMask(numElems-1), size(0)
  34. {
  35. data = new Node[numElems];
  36. root = 0;
  37. }
  38. ~OrderedHashTable()
  39. {
  40. delete[] data;
  41. }
  42. size_t Size() const { return size; }
  43. void Insert(const T &value)
  44. {
  45. int idx = HashFunc::Hash(value, numElemsMask);
  46. if (data[idx].used)
  47. Remove(&data[idx]);
  48. data[idx].value = value;
  49. if (root)
  50. {
  51. root->prev = &data[idx];
  52. data[idx].next = root;
  53. }
  54. else
  55. data[idx].next = 0;
  56. data[idx].prev = 0;
  57. data[idx].used = true;
  58. data[idx].hashChain = 0; ///\todo Support item chains.
  59. root = &data[idx];
  60. ++size;
  61. }
  62. void Clear()
  63. {
  64. while(size > 0)
  65. Remove(First());
  66. }
  67. void PopFront()
  68. {
  69. Remove(First());
  70. }
  71. template<typename KeyType>
  72. Node *Find(const KeyType &key)
  73. {
  74. int idx = HashFunc::Hash(key, numElemsMask);
  75. if (data[idx].used)
  76. return &data[idx];
  77. else
  78. return 0;
  79. }
  80. template<typename KeyType>
  81. void Remove(const KeyType &key)
  82. {
  83. int idx = HashFunc::Hash(key, numElemsMask);
  84. assert(data[idx].used);
  85. Remove(&data[idx]);
  86. }
  87. void Remove(Node *node)
  88. {
  89. if (!node)
  90. return;
  91. assert(node->used);
  92. if (node == root)
  93. root = node->next;
  94. node->used = false;
  95. if (node->prev)
  96. node->prev->next = node->next;
  97. if (node->next)
  98. node->next->prev = node->prev;
  99. --size;
  100. assert(root || size == 0);
  101. }
  102. Node *FrontNode()
  103. {
  104. return root;
  105. }
  106. T *Front()
  107. {
  108. assert(root || size == 0);
  109. return root ? &root->value : 0;
  110. }
  111. Node *First()
  112. {
  113. assert(root || size == 0);
  114. return root;
  115. }
  116. Node *Next(Node *node)
  117. {
  118. return node->next;
  119. }
  120. private:
  121. Node *root;
  122. Node *data;
  123. size_t numElemsMask;
  124. size_t size;
  125. void operator =(const OrderedHashTable &); ///< \todo Implement.
  126. OrderedHashTable(const OrderedHashTable &); ///< \todo Implement.
  127. };
  128. } // ~kNet