tire.inl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. }
  38. TEMPLATE(HOLDER) ADT::~Tire() {
  39. delete _root;
  40. }
  41. TEMPLATE(typename ITERATOR) ADT::find(const std::string& key) {
  42. Node* curr = _root;
  43. Node* next = nullptr;
  44. for (const char& ch : key) {
  45. {
  46. // 高 4 位
  47. unsigned int h = ((unsigned char)ch >> 4) & 0x0fu;
  48. next = curr->next[h];
  49. if (next == nullptr) {
  50. return end();
  51. }
  52. curr = next;
  53. }
  54. {
  55. // 低 4 位
  56. unsigned int l = ch & 0x0fu;
  57. next = curr->next[l];
  58. if (next == nullptr) {
  59. return end();
  60. }
  61. curr = next;
  62. }
  63. }
  64. return curr->value == nullptr ? end() : iterator(curr->value);
  65. }
  66. TEMPLATE(Type&) ADT::operator[](const std::string& key) {
  67. return *find(key);
  68. }
  69. TEMPLATE(typename ITERATOR) ADT::insert(const std::string& key, Type& value) {
  70. Node* curr = _root;
  71. Node* next = nullptr;
  72. for (const char& ch : key) {
  73. {
  74. // 高 4 位
  75. unsigned int h = ((unsigned char)ch >> 4) & 0x0fu;
  76. next = curr->next[h];
  77. if (next == nullptr) {
  78. curr->bitmap |= 0x01u << h;
  79. curr = curr->next[h] = new Node();
  80. }
  81. else {
  82. curr = next;
  83. }
  84. }
  85. {
  86. // 低 4 位
  87. unsigned int l = ch & 0x0fu;
  88. next = curr->next[l];
  89. if (next == nullptr) {
  90. curr->bitmap |= 0x01u << l;
  91. curr = curr->next[l] = new Node();
  92. }
  93. else {
  94. curr = next;
  95. }
  96. }
  97. }
  98. iterator it = _container.append(value);
  99. curr->value = &it;
  100. return it;
  101. }
  102. TEMPLATE(size_t) ADT::erase(const std::string& key) {
  103. Node* curr = _root;
  104. Node* next = nullptr;
  105. for (const char& ch : key) {
  106. {
  107. // 高 4 位
  108. unsigned int h = ((unsigned char)ch >> 4) & 0x0fu;
  109. next = curr->next[h];
  110. if (next == nullptr) {
  111. return 0;
  112. }
  113. curr = next;
  114. }
  115. {
  116. // 低 4 位
  117. unsigned int l = ch & 0x0fu;
  118. next = curr->next[l];
  119. if (next == nullptr) {
  120. return 0;
  121. }
  122. curr = next;
  123. }
  124. }
  125. size_t res = curr->value == nullptr ? 0 : 1;
  126. _container.erase(curr->value);
  127. curr->value = nullptr;
  128. return res;
  129. }
  130. TEMPLATE(typename ITERATOR) ADT::begin() { return _container.begin(); }
  131. TEMPLATE(typename ITERATOR) ADT::end() { return _container.end(); }
  132. TEMPLATE(typename ITERATOR) ADT::rbegin() { return _container.rbegin(); }
  133. TEMPLATE(typename ITERATOR) ADT::rend() { return _container.rend(); }
  134. // #pragma endregion
  135. #undef HOLDER
  136. #undef __TEMPLATE
  137. #undef TEMPLATE
  138. #undef ADT
  139. #undef NODE
  140. #undef ITERATOR