1
0

tire.inl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "tire.h"
  2. #define HOLDER
  3. #define __TEMPLATE template<typename Type, typename Container>
  4. #define TEMPLATE(rtype) __TEMPLATE rtype
  5. #define ADT Tire<Type, Container>
  6. #define NODE ADT::Node
  7. #define ITERATOR ADT::iterator
  8. #define INDEX(x) ((const int)(x))
  9. inline unsigned int lowbit(unsigned int x) {
  10. return x & -x;
  11. }
  12. inline int bit_weight(unsigned int x) {
  13. x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
  14. x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  15. x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f);
  16. x = (x & 0x00ff00ff) + ((x >> 8) & 0x00ff00ff);
  17. // x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff);
  18. return (int)x;
  19. }
  20. // #pragma region Tire<Type>::Node
  21. TEMPLATE(HOLDER) NODE::Node() {
  22. }
  23. TEMPLATE(HOLDER) NODE::~Node() {
  24. // lowbit 取最低非零位
  25. // 汉明重量 计算最低非零位位置
  26. for (unsigned int bmap = bitmap; bmap != 0;) {
  27. unsigned int x = lowbit(bmap);
  28. int y = bit_weight(x - 1u);
  29. delete next[y];
  30. next[y] = nullptr;
  31. bmap -= x;
  32. }
  33. }
  34. // #pragma endregion
  35. // #pragma region Tire<Type>
  36. TEMPLATE(HOLDER) ADT::Tire() {
  37. _is_subtree = false;
  38. }
  39. TEMPLATE(HOLDER) ADT::Tire(Node* root) {
  40. _root = root;
  41. _is_subtree = true;
  42. }
  43. TEMPLATE(HOLDER) ADT::~Tire() {
  44. if (_is_subtree == false) {
  45. delete _root;
  46. }
  47. }
  48. TEMPLATE(typename ITERATOR) ADT::find(const std::string& key) {
  49. Node* curr = _root;
  50. Node* next = nullptr;
  51. for (const char& ch : key) {
  52. {
  53. // 高 4 位
  54. unsigned int h = ((unsigned char)ch >> 4) & 0x0fu;
  55. next = curr->next[h];
  56. if (next == nullptr) {
  57. return end();
  58. }
  59. curr = next;
  60. }
  61. {
  62. // 低 4 位
  63. unsigned int l = ch & 0x0fu;
  64. next = curr->next[l];
  65. if (next == nullptr) {
  66. return end();
  67. }
  68. curr = next;
  69. }
  70. }
  71. return curr->value == nullptr ? end() : iterator(curr->value);
  72. }
  73. TEMPLATE(Type&) ADT::operator[](const std::string& key) {
  74. return *find(key);
  75. }
  76. TEMPLATE(typename ITERATOR) ADT::insert(const std::string& key, Type& value) {
  77. Node* curr = _root;
  78. Node* next = nullptr;
  79. for (const char& ch : key) {
  80. {
  81. // 高 4 位
  82. unsigned int h = ((unsigned char)ch >> 4) & 0x0fu;
  83. next = curr->next[h];
  84. if (next == nullptr) {
  85. curr->bitmap |= 0x01u << h;
  86. curr = curr->next[h] = new Node();
  87. }
  88. else {
  89. curr = next;
  90. }
  91. }
  92. {
  93. // 低 4 位
  94. unsigned int l = ch & 0x0fu;
  95. next = curr->next[l];
  96. if (next == nullptr) {
  97. curr->bitmap |= 0x01u << l;
  98. curr = curr->next[l] = new Node();
  99. }
  100. else {
  101. curr = next;
  102. }
  103. }
  104. }
  105. iterator it = _container.append(value);
  106. curr->value = &it;
  107. return it;
  108. }
  109. TEMPLATE(size_t) ADT::erase(const std::string& key) {
  110. Node* curr = _root;
  111. Node* next = nullptr;
  112. for (const char& ch : key) {
  113. {
  114. // 高 4 位
  115. unsigned int h = ((unsigned char)ch >> 4) & 0x0fu;
  116. next = curr->next[h];
  117. if (next == nullptr) {
  118. return 0;
  119. }
  120. curr = next;
  121. }
  122. {
  123. // 低 4 位
  124. unsigned int l = ch & 0x0fu;
  125. next = curr->next[l];
  126. if (next == nullptr) {
  127. return 0;
  128. }
  129. curr = next;
  130. }
  131. }
  132. if (curr->value == nullptr) {
  133. return 0;
  134. }
  135. else {
  136. _container.erase(curr->value);
  137. curr->value = nullptr;
  138. return 1;
  139. }
  140. }
  141. TEMPLATE(ADT&) ADT::set_prefix(string& key) {
  142. Node* curr = _root;
  143. Node* next = nullptr;
  144. for (const char& ch : key) {
  145. {
  146. // 高 4 位
  147. unsigned int h = ((unsigned char)ch >> 4) & 0x0fu;
  148. next = curr->next[h];
  149. if (next == nullptr) {
  150. curr->bitmap |= 0x01u << h;
  151. curr = curr->next[h] = new Node();
  152. }
  153. else {
  154. curr = next;
  155. }
  156. }
  157. {
  158. // 低 4 位
  159. unsigned int l = ch & 0x0fu;
  160. next = curr->next[l];
  161. if (next == nullptr) {
  162. curr->bitmap |= 0x01u << l;
  163. curr = curr->next[l] = new Node();
  164. }
  165. else {
  166. curr = next;
  167. }
  168. }
  169. }
  170. return Tire(curr);
  171. }
  172. TEMPLATE(typename ITERATOR) ADT::begin() { return _is_subtree == false ? _container.begin() : _container.end(); }
  173. TEMPLATE(typename ITERATOR) ADT::end() { return _container.end(); }
  174. TEMPLATE(typename ITERATOR) ADT::rbegin() { return _is_subtree == false ? _container.rbegin() : _container.rend(); }
  175. TEMPLATE(typename ITERATOR) ADT::rend() { return _container.rend(); }
  176. // #pragma endregion
  177. #undef HOLDER
  178. #undef __TEMPLATE
  179. #undef TEMPLATE
  180. #undef ADT
  181. #undef NODE
  182. #undef ITERATOR