multi_avl_tree.cxx 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. #include "libbinom/include/binom_impl/multi_avl_tree.hxx"
  2. using namespace binom;
  3. using namespace binom::priv;
  4. // ======================================================== MultiAVLTree::AVLKeyNode
  5. void MultiAVLTree::AVLKeyNode::swapPosition(AVLKeyNode& other, MultiAVLTree& avl_tree) {
  6. auto this_position = getPosition();
  7. auto other_position = other.getPosition();
  8. if(parent != &other && other.parent != this) {
  9. std::swap(parent, other.parent);
  10. std::swap(left, other.left);
  11. std::swap(right, other.right);
  12. std::swap(depth, other.depth);
  13. } elif (parent == &other) {
  14. parent = other.parent;
  15. other.parent = this;
  16. if(other.left == this) {
  17. other.left = left;
  18. left = &other;
  19. std::swap(right, other.right);
  20. } else {
  21. other.right = right;
  22. right = &other;
  23. std::swap(left, other.left);
  24. }
  25. std::swap(depth, other.depth);
  26. } else {
  27. other.parent = parent;
  28. parent = &other;
  29. if(left == &other) {
  30. left = other.left;
  31. other.left = this;
  32. std::swap(right, other.right);
  33. } else {
  34. right = other.right;
  35. other.right = this;
  36. std::swap(left, other.left);
  37. }
  38. }
  39. if(hasLeft()) left->parent = this;
  40. if(hasRight()) right->parent = this;
  41. if(other.hasLeft()) other.left->parent = &other;
  42. if(other.hasRight()) other.right->parent = &other;
  43. switch (other_position) {
  44. case NodePosition::left: parent->left = this; break;
  45. case NodePosition::right: parent->right = this; break;
  46. case NodePosition::root: avl_tree.root = this; break;
  47. }
  48. switch (this_position) {
  49. case NodePosition::left: other.parent->left = &other; break;
  50. case NodePosition::right: other.parent->right = &other; break;
  51. case NodePosition::root: avl_tree.root = &other; break;
  52. }
  53. }
  54. void MultiAVLTree::AVLKeyNode::unpin(MultiAVLTree& avl_tree) {
  55. if(isLeft()) parent->left = nullptr;
  56. elif(isRight()) parent->right = nullptr;
  57. else avl_tree.root = nullptr;
  58. parent = nullptr;
  59. }
  60. MultiAVLTree::AVLKeyNode::AVLKeyNode(KeyValue key, AVLKeyNode* parent)
  61. : parent(parent), key(std::move(key)) {}
  62. MultiAVLTree::AVLKeyNode::AVLKeyNode(AVLKeyNode& other)
  63. : depth(other.depth),
  64. left(other.left),
  65. right(other.right),
  66. parent(other.parent),
  67. key(other.key) {}
  68. MultiAVLTree::AVLKeyNode::AVLKeyNode(AVLKeyNode&& other)
  69. : depth(other.depth),
  70. left(other.left),
  71. right(other.right),
  72. parent(other.parent),
  73. key(std::move(other.key)) {}
  74. MultiAVLTree::AVLKeyNode& MultiAVLTree::AVLKeyNode::operator=(AVLKeyNode other) { this->~AVLKeyNode(); return *new(this) AVLKeyNode(std::move(other)); }
  75. void MultiAVLTree::AVLKeyNode::pushBack(AVLNode* node) {
  76. node->key_node = this;
  77. if(!first) {
  78. first = last = node;
  79. node->next = node->prev = nullptr;
  80. ++size;
  81. return;
  82. }
  83. node->prev = last;
  84. last = last->next = node;
  85. ++size;
  86. }
  87. void MultiAVLTree::AVLKeyNode::pushFront(AVLNode* node) {
  88. node->key_node = this;
  89. if(!first) {
  90. first = last = node;
  91. node->next = node->prev = nullptr;
  92. ++size;
  93. return;
  94. }
  95. node->next = first;
  96. first = first->prev = node;
  97. ++size;
  98. }
  99. MultiAVLTree::AVLNode& MultiAVLTree::AVLKeyNode::extract(Iterator it) {
  100. AVLNode& node = *it;
  101. if(node.next) {node.next->prev = node.prev;}
  102. else if(&node == last) {
  103. last = node.prev;
  104. }
  105. if(node.prev) {node.prev->next = node.next;}
  106. else if(&node == first) {
  107. first = node.next;
  108. }
  109. node.key_node = nullptr;
  110. node.next = nullptr;
  111. node.prev = nullptr;
  112. --size;
  113. return node;
  114. }
  115. size_t MultiAVLTree::AVLKeyNode::getElementCount() const noexcept {return size;}
  116. bool MultiAVLTree::AVLKeyNode::isEmpty() const noexcept {return !size;}
  117. bool MultiAVLTree::AVLKeyNode::isRoot() const noexcept {return !parent;}
  118. bool MultiAVLTree::AVLKeyNode::isLeft() const noexcept {return isRoot() ? false : parent->left == this;}
  119. bool MultiAVLTree::AVLKeyNode::isRight() const noexcept {return isRoot() ? false : parent->right == this;}
  120. bool MultiAVLTree::AVLKeyNode::hasLeft() const noexcept {return left;}
  121. bool MultiAVLTree::AVLKeyNode::hasRight() const noexcept {return right;}
  122. bool MultiAVLTree::AVLKeyNode::hasChild() const noexcept {return left || right;}
  123. KeyValue& MultiAVLTree::AVLKeyNode::getKey() {return key;}
  124. const KeyValue& MultiAVLTree::AVLKeyNode::getKey() const {return key;}
  125. MultiAVLTree::AVLKeyNode::NodePosition MultiAVLTree::AVLKeyNode::getPosition() const {
  126. if(isLeft()) return NodePosition::left;
  127. elif(isRight()) return NodePosition::right;
  128. else return NodePosition::root;
  129. }
  130. MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::AVLKeyNode::begin() noexcept {return first;}
  131. MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::AVLKeyNode::end() noexcept {return nullptr;}
  132. MultiAVLTree::AVLKeyNode::ConstIterator MultiAVLTree::AVLKeyNode::begin() const noexcept {return first;}
  133. MultiAVLTree::AVLKeyNode::ConstIterator MultiAVLTree::AVLKeyNode::end() const noexcept {return nullptr;}
  134. MultiAVLTree::AVLKeyNode::ConstIterator MultiAVLTree::AVLKeyNode::cbegin() const noexcept {return first;}
  135. MultiAVLTree::AVLKeyNode::ConstIterator MultiAVLTree::AVLKeyNode::cend() const noexcept {return nullptr;}
  136. MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::AVLKeyNode::rbegin() noexcept {return last;}
  137. MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::AVLKeyNode::rend() noexcept {return nullptr;}
  138. MultiAVLTree::AVLKeyNode::ConstReverseIterator MultiAVLTree::AVLKeyNode::rbegin() const noexcept {return last;}
  139. MultiAVLTree::AVLKeyNode::ConstReverseIterator MultiAVLTree::AVLKeyNode::rend() const noexcept {return nullptr;}
  140. MultiAVLTree::AVLKeyNode::ConstReverseIterator MultiAVLTree::AVLKeyNode::crbegin() const noexcept {return last;}
  141. MultiAVLTree::AVLKeyNode::ConstReverseIterator MultiAVLTree::AVLKeyNode::crend() const noexcept {return nullptr;}
  142. // ======================================================== MultiAVLTree::AVLKeyNode::Iterator
  143. MultiAVLTree::AVLKeyNode::Iterator::Iterator(AVLNode* node) noexcept : node(node) {}
  144. MultiAVLTree::AVLKeyNode::Iterator::Iterator(const Iterator& other) noexcept : node(other.node) {}
  145. MultiAVLTree::AVLKeyNode::Iterator::Iterator(Iterator&& other) noexcept : node(other.node) {}
  146. MultiAVLTree::AVLKeyNode::Iterator& MultiAVLTree::AVLKeyNode::Iterator::operator=(Iterator other) {node = other.node; return self;}
  147. MultiAVLTree::AVLKeyNode::Iterator& MultiAVLTree::AVLKeyNode::Iterator::operator++() {if(node) node = node->next; return self;}
  148. MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::AVLKeyNode::Iterator::operator++(int) {Iterator tmp(self); ++self; return tmp;}
  149. MultiAVLTree::AVLKeyNode::Iterator& MultiAVLTree::AVLKeyNode::Iterator::operator--() {if(node) node = node->prev; return self;}
  150. MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::AVLKeyNode::Iterator::operator--(int) {Iterator tmp(self); --self; return tmp;}
  151. const MultiAVLTree::AVLKeyNode::Iterator& MultiAVLTree::AVLKeyNode::Iterator::operator++() const {if(node) node = node->next; return self;}
  152. const MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::AVLKeyNode::Iterator::operator++(int) const {Iterator tmp(self); ++self; return tmp;}
  153. const MultiAVLTree::AVLKeyNode::Iterator& MultiAVLTree::AVLKeyNode::Iterator::operator--() const {if(node) node = node->prev; return self;}
  154. const MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::AVLKeyNode::Iterator::operator--(int) const {Iterator tmp(self); --self; return tmp;}
  155. bool MultiAVLTree::AVLKeyNode::Iterator::operator==(Iterator other) const noexcept {return node == other.node;}
  156. bool MultiAVLTree::AVLKeyNode::Iterator::operator!=(Iterator other) const noexcept {return node != other.node;}
  157. MultiAVLTree::AVLNode& MultiAVLTree::AVLKeyNode::Iterator::operator*() {return *node;}
  158. MultiAVLTree::AVLNode* MultiAVLTree::AVLKeyNode::Iterator::operator->() noexcept {return node;}
  159. const MultiAVLTree::AVLNode& MultiAVLTree::AVLKeyNode::Iterator::operator*() const {return *node;}
  160. const MultiAVLTree::AVLNode* MultiAVLTree::AVLKeyNode::Iterator::operator->() const noexcept {return node;}
  161. MultiAVLTree::AVLKeyNode::Iterator::operator ReverseIterator() noexcept {return node;}
  162. MultiAVLTree::AVLKeyNode::Iterator::operator const ReverseIterator() const noexcept {return node;}
  163. // ======================================================== MultiAVLTree::AVLKeyNode::ReverseIterator
  164. MultiAVLTree::AVLKeyNode::ReverseIterator::ReverseIterator(AVLNode* node) noexcept : node(node) {}
  165. MultiAVLTree::AVLKeyNode::ReverseIterator::ReverseIterator(const ReverseIterator& other) noexcept : node(other.node) {}
  166. MultiAVLTree::AVLKeyNode::ReverseIterator::ReverseIterator(ReverseIterator&& other) noexcept : node(other.node) {}
  167. MultiAVLTree::AVLKeyNode::ReverseIterator& MultiAVLTree::AVLKeyNode::ReverseIterator::operator=(ReverseIterator other) {node = other.node; return self;}
  168. MultiAVLTree::AVLKeyNode::ReverseIterator& MultiAVLTree::AVLKeyNode::ReverseIterator::operator++() {if(node) node = node->prev; return self;}
  169. MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::AVLKeyNode::ReverseIterator::operator++(int) {ReverseIterator tmp(self); ++self; return tmp;}
  170. MultiAVLTree::AVLKeyNode::ReverseIterator& MultiAVLTree::AVLKeyNode::ReverseIterator::operator--() {if(node) node = node->next; return self;}
  171. MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::AVLKeyNode::ReverseIterator::operator--(int) {ReverseIterator tmp(self); --self; return tmp;}
  172. const MultiAVLTree::AVLKeyNode::ReverseIterator& MultiAVLTree::AVLKeyNode::ReverseIterator::operator++() const {if(node) node = node->prev; return self;}
  173. const MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::AVLKeyNode::ReverseIterator::operator++(int) const {ReverseIterator tmp(self); ++self; return tmp;}
  174. const MultiAVLTree::AVLKeyNode::ReverseIterator& MultiAVLTree::AVLKeyNode::ReverseIterator::operator--() const {if(node) node = node->next; return self;}
  175. const MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::AVLKeyNode::ReverseIterator::operator--(int) const {ReverseIterator tmp(self); --self; return tmp;}
  176. bool MultiAVLTree::AVLKeyNode::ReverseIterator::operator==(ReverseIterator other) const noexcept {return node == other.node;}
  177. bool MultiAVLTree::AVLKeyNode::ReverseIterator::operator!=(ReverseIterator other) const noexcept {return node != other.node;}
  178. MultiAVLTree::AVLNode& MultiAVLTree::AVLKeyNode::ReverseIterator::operator*() {return *node;}
  179. MultiAVLTree::AVLNode* MultiAVLTree::AVLKeyNode::ReverseIterator::operator->() noexcept {return node;}
  180. const MultiAVLTree::AVLNode& MultiAVLTree::AVLKeyNode::ReverseIterator::operator*() const {return *node;}
  181. const MultiAVLTree::AVLNode* MultiAVLTree::AVLKeyNode::ReverseIterator::operator->() const noexcept {return node;}
  182. MultiAVLTree::AVLKeyNode::ReverseIterator::operator Iterator() noexcept {return node;}
  183. MultiAVLTree::AVLKeyNode::ReverseIterator::operator const Iterator() const noexcept {return node;}
  184. // ======================================================== MultiAVLTree
  185. i64 MultiAVLTree::max(i64 a, i64 b) noexcept {return (a > b) ? a : b;}
  186. i64 MultiAVLTree::depth(AVLKeyNode* node) noexcept {return node ? node->depth : 0;}
  187. MultiAVLTree::AVLKeyNode* MultiAVLTree::rotateRight(AVLKeyNode* y) {
  188. auto y_position = y->getPosition();
  189. AVLKeyNode* x = y->left;
  190. AVLKeyNode* T2 = x->right;
  191. x->right = y;
  192. y->left = T2;
  193. y->depth = max(depth(y->left), depth(y->right)) + 1;
  194. x->depth = max(depth(x->left), depth(x->right)) + 1;
  195. // Update parent poionters
  196. x->parent = y->parent;
  197. y->parent = x;
  198. if(T2) T2->parent = y;
  199. // Update parent's child poionter
  200. if(y_position == AVLKeyNode::NodePosition::left) x->parent->left = x;
  201. elif(y_position == AVLKeyNode::NodePosition::right) x->parent->right = x;
  202. else root = x;
  203. return x;
  204. }
  205. MultiAVLTree::AVLKeyNode* MultiAVLTree::rotateLeft(AVLKeyNode* x) {
  206. auto x_position = x->getPosition();
  207. AVLKeyNode* y = x->right;
  208. AVLKeyNode* T2 = y->left;
  209. y->left = x;
  210. x->right = T2;
  211. y->depth = max(depth(y->left), depth(y->right)) + 1;
  212. x->depth = max(depth(x->left), depth(x->right)) + 1;
  213. // Update parent poionters
  214. y->parent = x->parent;
  215. x->parent = y;
  216. if(T2) T2->parent = x;
  217. // Update parent's child poionter
  218. if(x_position == AVLKeyNode::NodePosition::left) y->parent->left = y;
  219. elif(x_position == AVLKeyNode::NodePosition::right) y->parent->right = y;
  220. else root = y;
  221. return y;
  222. }
  223. i64 MultiAVLTree::getBalance(AVLKeyNode* node) { return node ? depth(node->left) - depth(node->right) : 0; }
  224. MultiAVLTree::AVLKeyNode* MultiAVLTree::minKeyNode(AVLKeyNode* node) noexcept {
  225. if(!node) return nullptr;
  226. while(node->left) node = node->left;
  227. return node;
  228. }
  229. MultiAVLTree::AVLKeyNode* MultiAVLTree::minKeyNode() const noexcept {return minKeyNode(root);}
  230. MultiAVLTree::AVLKeyNode* MultiAVLTree::maxKeyNode(AVLKeyNode* node) noexcept {
  231. if(!node) return nullptr;
  232. while (node->right) node = node->right;
  233. return node;
  234. }
  235. MultiAVLTree::AVLKeyNode* MultiAVLTree::maxKeyNode() const noexcept {return maxKeyNode(root);}
  236. bool MultiAVLTree::isEmpty() const noexcept {return !root;}
  237. MultiAVLTree::NodePair MultiAVLTree::insert(KeyValue key, AVLNode* new_node, NewNodePosition position) {
  238. AVLKeyNode* node = root;
  239. NodePair result{.node = new_node};
  240. if(!node) {
  241. root = new AVLKeyNode(std::move(key));
  242. root->pushBack(new_node);
  243. return {root, new_node};
  244. }
  245. forever {
  246. auto cmp = key.getCompare(node->key);
  247. if(cmp == KeyValue::lower) {
  248. if(node->left) { node = node->left; continue; }
  249. else {
  250. node->left = new AVLKeyNode(std::move(key), node);
  251. node->left->pushBack(new_node);
  252. result.key_node = node->left;
  253. break;
  254. }
  255. } elif(cmp == KeyValue::highter) {
  256. if(node->right) { node = node->right; continue; }
  257. else {
  258. node->right = new AVLKeyNode(std::move(key), node);
  259. node->right->pushBack(new_node);
  260. result.key_node = node->right;
  261. break;
  262. }
  263. } else { // KeyValue::equal
  264. switch (position) {
  265. case binom::priv::MultiAVLTree::NewNodePosition::front:
  266. node->pushFront(new_node);
  267. break;
  268. case binom::priv::MultiAVLTree::NewNodePosition::back:
  269. node->pushBack(new_node);
  270. break;
  271. }
  272. return {node, new_node};
  273. }
  274. }
  275. while(node) { // Balancing
  276. node->depth = 1 + max(depth(node->left), depth(node->right));
  277. i64 balance = getBalance(node);
  278. if (balance > 1 && getBalance(root->left) >= 0) {
  279. rotateRight(node);
  280. } elif (balance > 1 && getBalance(root->left) < 0) {
  281. rotateLeft(node->left);
  282. rotateRight(node);
  283. } elif (balance < -1 && getBalance(node->right) <= 0) {
  284. rotateLeft(node);
  285. } elif (balance < -1 && getBalance(node->right) > 0) {
  286. rotateRight(node->right);
  287. rotateLeft(node);
  288. }
  289. node = node->parent;
  290. }
  291. return result;
  292. }
  293. MultiAVLTree::AVLNode* MultiAVLTree::extract(Iterator it) {
  294. auto& key_node = it.getKeyNode();
  295. auto iterator = it.getKeyNodeIterator();
  296. auto& result = key_node.extract(iterator);
  297. if(key_node.getElementCount()) return &result;
  298. AVLKeyNode* node = &key_node;
  299. forever if(!node->left || !node->right) {
  300. AVLKeyNode* tmp = node->left ? node->left : node->right;
  301. if(!tmp) { // If node hasn't child
  302. tmp = node;
  303. node = nullptr;
  304. tmp->unpin(self);
  305. } else { // If node has 1 child
  306. node->swapPosition(*tmp, self);
  307. node->unpin(self);
  308. node = tmp; // For balancing
  309. }
  310. break;
  311. } else { // If node has 2 childs (Test it!!!)
  312. // Change the position of the node
  313. // to be deleted whith the position
  314. // of the leftmost node in th right branch
  315. AVLKeyNode* tmp = minKeyNode(node->right);
  316. node->swapPosition(*tmp, self);
  317. continue;
  318. }
  319. while(node) { // Balancing
  320. node->depth = 1 + max(depth(node->left), depth(node->right));
  321. i64 balance = getBalance(node);
  322. if (balance > 1 && getBalance(root->left) >= 0) {
  323. rotateRight(node);
  324. } elif (balance > 1 && getBalance(root->left) < 0) {
  325. rotateLeft(node->left);
  326. rotateRight(node);
  327. } elif (balance < -1 && getBalance(node->right) <= 0) {
  328. rotateLeft(node);
  329. } elif (balance < -1 && getBalance(node->right) > 0) {
  330. rotateRight(node->right);
  331. rotateLeft(node);
  332. }
  333. node = node->parent;
  334. }
  335. delete &key_node;
  336. return &result;
  337. }
  338. MultiAVLTree::AVLNode* MultiAVLTree::extract(ReverseIterator r_it) {return extract(Iterator(r_it));}
  339. bool MultiAVLTree::removeKey(KeyValue key, std::function<void (AVLNode*)> destructor) {
  340. AVLKeyNode* node = root;
  341. // Find key node by key
  342. forever {
  343. if(!node) return false;
  344. auto cmp = key.getCompare(node->key);
  345. if(cmp == KeyValue::lower) {node = node->left; continue;}
  346. elif(cmp == KeyValue::highter) {node = node->left; continue;}
  347. else break;
  348. }
  349. AVLKeyNode* deletable_node = node;
  350. // Delete value-nodes
  351. for(auto it = node->begin(), end = node->end(); it != end; destructor(&*it++));
  352. // Extract key-node
  353. forever if(!node->left || !node->right) {
  354. AVLKeyNode* tmp = node->left ? node->left : node->right;
  355. if(!tmp) { // If node hasn't child
  356. tmp = node;
  357. node = nullptr;
  358. tmp->unpin(self);
  359. } else { // If node has 1 child
  360. node->swapPosition(*tmp, self);
  361. node->unpin(self);
  362. node = tmp; // For balancing
  363. }
  364. break;
  365. } else { // If node has 2 childs (Test it!!!)
  366. // Change the position of the node
  367. // to be deleted whith the position
  368. // of the leftmost node in th right branch
  369. AVLKeyNode* tmp = minKeyNode(node->right);
  370. node->swapPosition(*tmp, self);
  371. continue;
  372. }
  373. while(node) { // Balancing
  374. node->depth = 1 + max(depth(node->left), depth(node->right));
  375. i64 balance = getBalance(node);
  376. if (balance > 1 && getBalance(root->left) >= 0) {
  377. rotateRight(node);
  378. } elif (balance > 1 && getBalance(root->left) < 0) {
  379. rotateLeft(node->left);
  380. rotateRight(node);
  381. } elif (balance < -1 && getBalance(node->right) <= 0) {
  382. rotateLeft(node);
  383. } elif (balance < -1 && getBalance(node->right) > 0) {
  384. rotateRight(node->right);
  385. rotateLeft(node);
  386. }
  387. node = node->parent;
  388. }
  389. // Delete key-node
  390. delete deletable_node;
  391. return true;
  392. }
  393. MultiAVLTree::Iterator MultiAVLTree::find(KeyValue key) const {
  394. AVLKeyNode* key_node = root;
  395. forever {
  396. if(!key_node) return nullptr;
  397. auto cmp = key.getCompare(key_node->key);
  398. if(cmp == KeyValue::lower) {key_node = key_node->left; continue;}
  399. elif(cmp == KeyValue::highter) {key_node = key_node->left; continue;}
  400. else return Iterator(key_node); // Node is finded
  401. }
  402. }
  403. MultiAVLTree::Iterator MultiAVLTree::findLast(KeyValue key) const {
  404. AVLKeyNode* key_node = root;
  405. forever {
  406. if(!key_node) return nullptr;
  407. auto cmp = key.getCompare(key_node->key);
  408. if(cmp == KeyValue::lower) {key_node = key_node->left; continue;}
  409. elif(cmp == KeyValue::highter) {key_node = key_node->left; continue;}
  410. else return Iterator(key_node->rbegin()); // Node is finded
  411. }
  412. }
  413. MultiAVLTree::ReverseIterator MultiAVLTree::rfind(KeyValue key) const {
  414. AVLKeyNode* key_node = root;
  415. forever {
  416. if(!key_node) return nullptr;
  417. auto cmp = key.getCompare(key_node->key);
  418. if(cmp == KeyValue::lower) {key_node = key_node->left; continue;}
  419. elif(cmp == KeyValue::highter) {key_node = key_node->left; continue;}
  420. else return Iterator(key_node); // Node is finded
  421. }
  422. }
  423. MultiAVLTree::ReverseIterator MultiAVLTree::rfindLast(KeyValue key) const {
  424. AVLKeyNode* key_node = root;
  425. forever {
  426. if(!key_node) return nullptr;
  427. auto cmp = key.getCompare(key_node->key);
  428. if(cmp == KeyValue::lower) {key_node = key_node->left; continue;}
  429. elif(cmp == KeyValue::highter) {key_node = key_node->left; continue;}
  430. else return Iterator(key_node->rbegin()); // Node is finded
  431. }
  432. }
  433. MultiAVLTree::Iterator MultiAVLTree::begin() noexcept {return minKeyNode();}
  434. MultiAVLTree::Iterator MultiAVLTree::end() noexcept {return nullptr;}
  435. MultiAVLTree::ReverseIterator MultiAVLTree::rbegin() noexcept {return maxKeyNode();}
  436. MultiAVLTree::ReverseIterator MultiAVLTree::rend() noexcept {return nullptr;}
  437. MultiAVLTree::ConstIterator MultiAVLTree::begin() const noexcept {return cbegin();}
  438. MultiAVLTree::ConstIterator MultiAVLTree::end() const noexcept {return cend();}
  439. MultiAVLTree::ConstReverseIterator MultiAVLTree::rbegin() const noexcept {return crbegin();}
  440. MultiAVLTree::ConstReverseIterator MultiAVLTree::rend() const noexcept {return crend();}
  441. MultiAVLTree::ConstIterator MultiAVLTree::cbegin() const noexcept {return minKeyNode();}
  442. MultiAVLTree::ConstIterator MultiAVLTree::cend() const noexcept {return nullptr;}
  443. MultiAVLTree::ConstReverseIterator MultiAVLTree::crbegin() const noexcept {return maxKeyNode();}
  444. MultiAVLTree::ConstReverseIterator MultiAVLTree::crend() const noexcept {return nullptr;}
  445. void MultiAVLTree::clear(std::function<void (AVLNode*)> destructor) {
  446. if(!root) return;
  447. AVLKeyNode* key_node = minKeyNode();
  448. if(key_node->hasRight()) key_node = key_node->right;
  449. while(key_node) {
  450. AVLKeyNode* last = key_node;
  451. if(!!key_node->parent) {
  452. if(key_node->isLeft() && key_node->parent->hasRight()) {
  453. key_node = minKeyNode(key_node->parent->right);
  454. if(key_node->hasRight()) key_node = key_node->right;
  455. } elif(key_node->isLeft() || key_node->isRight())
  456. key_node = key_node->parent;
  457. } else key_node = nullptr;
  458. for(auto it = last->begin(), end = last->end();
  459. it != end;) {
  460. AVLNode* node = &*it;
  461. ++it;
  462. destructor(node);
  463. }
  464. delete last;
  465. }
  466. root = nullptr;
  467. }
  468. // ======================================================== MultiAVLTree::Iterator
  469. MultiAVLTree::Iterator::Iterator(AVLKeyNode* key_node)
  470. : iterator(key_node ? key_node->begin() : nullptr) {}
  471. MultiAVLTree::Iterator::Iterator(AVLKeyNode::Iterator iterator)
  472. : iterator(iterator) {}
  473. MultiAVLTree::Iterator::Iterator(const Iterator& other)
  474. : iterator(other.iterator) {}
  475. MultiAVLTree::Iterator::Iterator(Iterator&& other)
  476. : iterator(other.iterator) {}
  477. MultiAVLTree::Iterator& MultiAVLTree::Iterator::goToNextKey() {
  478. if(!iterator) return self;
  479. AVLKeyNode* key_node = iterator->key_node;
  480. if(!key_node) {
  481. iterator = nullptr;
  482. return self;
  483. }
  484. elif(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  485. elif(key_node->isLeft()) key_node = key_node->parent;
  486. elif(key_node->isRight()) {
  487. AVLKeyNode* tmp = key_node->parent;
  488. key_node = key_node->parent->parent;
  489. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  490. } elif(key_node->isRoot()) {
  491. iterator = nullptr;
  492. return self;
  493. }
  494. if(key_node) iterator = key_node->begin();
  495. else iterator = nullptr;
  496. return self;
  497. }
  498. MultiAVLTree::Iterator& MultiAVLTree::Iterator::goToPrevKey() {
  499. if(!iterator) return self;
  500. AVLKeyNode* key_node = iterator->key_node;
  501. if(!key_node) {
  502. iterator = nullptr;
  503. return self;
  504. }
  505. elif(key_node->hasLeft()) key_node = MultiAVLTree::maxKeyNode(key_node->left);
  506. elif(key_node->isRight()) key_node = key_node->parent;
  507. elif(key_node->isLeft()) {
  508. AVLKeyNode* tmp = key_node->parent;
  509. key_node = key_node->parent->parent;
  510. while (tmp->isLeft() && key_node) {tmp = key_node; key_node = key_node->parent;}
  511. } elif(key_node->isRoot()) {
  512. iterator = nullptr;
  513. return self;
  514. }
  515. if(key_node) iterator = key_node->rbegin();
  516. else iterator = nullptr;
  517. return *this;
  518. }
  519. const MultiAVLTree::Iterator& MultiAVLTree::Iterator::goToNextKey() const {
  520. if(!iterator) return self;
  521. AVLKeyNode* key_node = iterator->key_node;
  522. if(!key_node) {
  523. iterator = nullptr;
  524. return self;
  525. }
  526. elif(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  527. elif(key_node->isLeft()) key_node = key_node->parent;
  528. elif(key_node->isRight()) {
  529. AVLKeyNode* tmp = key_node->parent;
  530. key_node = key_node->parent->parent;
  531. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  532. } elif(key_node->isRoot()) {
  533. iterator = nullptr;
  534. return self;
  535. }
  536. if(key_node) iterator = key_node->begin();
  537. else iterator = nullptr;
  538. return self;
  539. }
  540. const MultiAVLTree::Iterator& MultiAVLTree::Iterator::goToPrevKey() const {
  541. if(!iterator) return self;
  542. AVLKeyNode* key_node = iterator->key_node;
  543. if(!key_node) {
  544. iterator = nullptr;
  545. return self;
  546. }
  547. elif(key_node->hasLeft()) key_node = MultiAVLTree::maxKeyNode(key_node->left);
  548. elif(key_node->isRight()) key_node = key_node->parent;
  549. elif(key_node->isLeft()) {
  550. AVLKeyNode* tmp = key_node->parent;
  551. key_node = key_node->parent->parent;
  552. while (tmp->isLeft() && key_node) {tmp = key_node; key_node = key_node->parent;}
  553. } elif(key_node->isRoot()) {
  554. iterator = nullptr;
  555. return self;
  556. }
  557. if(key_node) iterator = key_node->rbegin();
  558. else iterator = nullptr;
  559. return *this;
  560. }
  561. MultiAVLTree::Iterator& MultiAVLTree::Iterator::operator=(Iterator other) {iterator = other.iterator; return self;}
  562. MultiAVLTree::Iterator& MultiAVLTree::Iterator::operator++() {
  563. if(!iterator) return self;
  564. AVLKeyNode* key_node = iterator->key_node;
  565. ++iterator;
  566. if(iterator != nullptr) return self;
  567. elif(!key_node) {
  568. iterator = nullptr;
  569. return self;
  570. }
  571. elif(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  572. elif(key_node->isLeft()) key_node = key_node->parent;
  573. elif(key_node->isRight()) {
  574. AVLKeyNode* tmp = key_node->parent;
  575. key_node = key_node->parent->parent;
  576. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  577. } elif(key_node->isRoot()) {
  578. iterator = nullptr;
  579. return self;
  580. }
  581. if(key_node) iterator = key_node->begin();
  582. else iterator = nullptr;
  583. return self;
  584. }
  585. MultiAVLTree::Iterator MultiAVLTree::Iterator::operator++(int) {Iterator tmp(self); ++self; return tmp;}
  586. MultiAVLTree::Iterator& MultiAVLTree::Iterator::operator--() {
  587. if(!iterator) return self;
  588. AVLKeyNode* key_node = iterator->key_node;
  589. --iterator;
  590. if(iterator != nullptr) return self;
  591. elif(!key_node) {
  592. iterator = nullptr;
  593. return self;
  594. }
  595. elif(key_node->hasLeft()) key_node = MultiAVLTree::maxKeyNode(key_node->left);
  596. elif(key_node->isRight()) key_node = key_node->parent;
  597. elif(key_node->isLeft()) {
  598. AVLKeyNode* tmp = key_node->parent;
  599. key_node = key_node->parent->parent;
  600. while (tmp->isLeft() && key_node) {tmp = key_node; key_node = key_node->parent;}
  601. } elif(key_node->isRoot()) {
  602. iterator = nullptr;
  603. return self;
  604. }
  605. if(key_node) iterator = key_node->rbegin();
  606. else iterator = nullptr;
  607. return *this;
  608. }
  609. size_t MultiAVLTree::Iterator::getElementCount() const noexcept {
  610. if(iterator->key_node) return iterator->key_node->getElementCount();
  611. else return 0;
  612. }
  613. MultiAVLTree::Iterator MultiAVLTree::Iterator::operator--(int) {Iterator tmp(self); --self; return tmp;}
  614. const MultiAVLTree::Iterator& MultiAVLTree::Iterator::operator++() const {
  615. if(!iterator) return self;
  616. AVLKeyNode* key_node = iterator->key_node;
  617. ++iterator;
  618. if(iterator != nullptr) return self;
  619. elif(!key_node) {
  620. iterator = nullptr;
  621. return self;
  622. }
  623. if(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  624. elif(key_node->isLeft()) key_node = key_node->parent;
  625. elif(key_node->isRight()) {
  626. AVLKeyNode* tmp = key_node->parent;
  627. key_node = key_node->parent->parent;
  628. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  629. } elif(key_node->isRoot()) {
  630. iterator = nullptr;
  631. return self;
  632. }
  633. if(key_node) iterator = key_node->begin();
  634. else iterator = nullptr;
  635. return self;
  636. }
  637. const MultiAVLTree::Iterator MultiAVLTree::Iterator::operator++(int) const {Iterator tmp(self); ++self; return tmp;}
  638. const MultiAVLTree::Iterator& MultiAVLTree::Iterator::operator--() const {
  639. if(!iterator) return self;
  640. AVLKeyNode* key_node = iterator->key_node;
  641. --iterator;
  642. if(iterator != nullptr) return self;
  643. elif(!key_node) {
  644. iterator = nullptr;
  645. return self;
  646. }
  647. elif(key_node->hasLeft()) key_node = MultiAVLTree::maxKeyNode(key_node->left);
  648. elif(key_node->isRight()) key_node = key_node->parent;
  649. elif(key_node->isLeft()) {
  650. AVLKeyNode* tmp = key_node->parent;
  651. key_node = key_node->parent->parent;
  652. while (tmp->isLeft() && key_node) {tmp = key_node; key_node = key_node->parent;}
  653. } elif(key_node->isRoot()) {
  654. iterator = nullptr;
  655. return self;
  656. }
  657. if(key_node) iterator = key_node->rbegin();
  658. else iterator = nullptr;
  659. return *this;
  660. }
  661. MultiAVLTree::AVLKeyNode& MultiAVLTree::Iterator::getKeyNode() {return *iterator->key_node;}
  662. const MultiAVLTree::AVLKeyNode& MultiAVLTree::Iterator::getKeyNode() const {return *iterator->key_node;}
  663. MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::Iterator::getKeyNodeIterator() {return iterator;}
  664. const MultiAVLTree::AVLKeyNode::Iterator MultiAVLTree::Iterator::getKeyNodeIterator() const {return iterator;}
  665. KeyValue& MultiAVLTree::Iterator::getKey() {return iterator->key_node->getKey();}
  666. const KeyValue& MultiAVLTree::Iterator::getKey() const {return iterator->key_node->getKey();}
  667. const MultiAVLTree::Iterator MultiAVLTree::Iterator::operator--(int) const {Iterator tmp(self); --self; return tmp;}
  668. bool MultiAVLTree::Iterator::operator==(Iterator other) const noexcept {return iterator == other.iterator;}
  669. bool MultiAVLTree::Iterator::operator!=(Iterator other) const noexcept {return iterator != other.iterator;}
  670. MultiAVLTree::AVLNode& MultiAVLTree::Iterator::operator*() {return *iterator;}
  671. MultiAVLTree::AVLNode* MultiAVLTree::Iterator::operator->() noexcept {return iterator.operator->();}
  672. const MultiAVLTree::AVLNode& MultiAVLTree::Iterator::operator*() const {return *iterator;}
  673. const MultiAVLTree::AVLNode* MultiAVLTree::Iterator::operator->() const noexcept {return iterator.operator->();}
  674. MultiAVLTree::Iterator::operator ReverseIterator() {return ReverseIterator(iterator);}
  675. MultiAVLTree::Iterator::operator const ReverseIterator() const {return ReverseIterator(iterator);}
  676. MultiAVLTree::Iterator MultiAVLTree::Iterator::begin() noexcept {return (iterator)? iterator->key_node->begin() : nullptr;}
  677. MultiAVLTree::Iterator MultiAVLTree::Iterator::end() noexcept {return Iterator(self).goToNextKey();}
  678. MultiAVLTree::ReverseIterator MultiAVLTree::Iterator::rbegin() noexcept {return (iterator)? iterator->key_node->rbegin() : nullptr;}
  679. MultiAVLTree::ReverseIterator MultiAVLTree::Iterator::rend() noexcept {return ReverseIterator(self).goToNextKey();}
  680. const MultiAVLTree::Iterator MultiAVLTree::Iterator::begin() const noexcept {return (iterator)? iterator->key_node->begin() : nullptr;}
  681. const MultiAVLTree::Iterator MultiAVLTree::Iterator::end() const noexcept {return Iterator(self).goToNextKey();}
  682. const MultiAVLTree::ReverseIterator MultiAVLTree::Iterator::rbegin() const noexcept {return (iterator)? iterator->key_node->rbegin() : nullptr;}
  683. const MultiAVLTree::ReverseIterator MultiAVLTree::Iterator::rend() const noexcept {return ReverseIterator(self).goToNextKey();}
  684. const MultiAVLTree::Iterator MultiAVLTree::Iterator::cbegin() const noexcept {return (iterator)? iterator->key_node->begin() : nullptr;}
  685. const MultiAVLTree::Iterator MultiAVLTree::Iterator::cend() const noexcept {return Iterator(self).goToNextKey();}
  686. const MultiAVLTree::ReverseIterator MultiAVLTree::Iterator::crbegin() const noexcept {return (iterator)? iterator->key_node->rbegin() : nullptr;}
  687. const MultiAVLTree::ReverseIterator MultiAVLTree::Iterator::crend() const noexcept {return ReverseIterator(self).goToNextKey();}
  688. // ======================================================== MultiAVLTree::ReverseIterator
  689. MultiAVLTree::ReverseIterator::ReverseIterator(AVLKeyNode* key_node)
  690. : reverse_iterator(key_node ? key_node->rbegin() : nullptr) {}
  691. MultiAVLTree::ReverseIterator::ReverseIterator(AVLKeyNode::ReverseIterator reverse_iterator)
  692. : reverse_iterator(reverse_iterator) {}
  693. MultiAVLTree::ReverseIterator::ReverseIterator(const ReverseIterator& other)
  694. : reverse_iterator(other.reverse_iterator) {}
  695. MultiAVLTree::ReverseIterator::ReverseIterator(ReverseIterator&& other)
  696. : reverse_iterator(other.reverse_iterator) {}
  697. MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::goToNextKey() {
  698. if(!reverse_iterator) return self;
  699. AVLKeyNode* key_node = reverse_iterator->key_node;
  700. if(!key_node) {
  701. reverse_iterator = nullptr;
  702. return self;
  703. }
  704. elif(key_node->hasLeft()) key_node = MultiAVLTree::maxKeyNode(key_node->left);
  705. elif(key_node->isRight()) key_node = key_node->parent;
  706. elif(key_node->isLeft()) {
  707. AVLKeyNode* tmp = key_node->parent;
  708. key_node = key_node->parent->parent;
  709. while (tmp->isLeft() && key_node) {tmp = key_node; key_node = key_node->parent;}
  710. } elif(key_node->isRoot()) {
  711. reverse_iterator = nullptr;
  712. return self;
  713. }
  714. if(key_node) reverse_iterator = key_node->rbegin();
  715. else reverse_iterator = nullptr;
  716. return self;
  717. }
  718. MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::goToPrevKey() {
  719. if(!reverse_iterator) return self;
  720. AVLKeyNode* key_node = reverse_iterator->key_node;
  721. if(!key_node) {
  722. reverse_iterator = nullptr;
  723. return self;
  724. }
  725. elif(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  726. elif(key_node->isLeft()) key_node = key_node->parent;
  727. elif(key_node->isRight()) {
  728. AVLKeyNode* tmp = key_node->parent;
  729. key_node = key_node->parent->parent;
  730. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  731. } elif(key_node->isRoot()) {
  732. reverse_iterator = nullptr;
  733. return self;
  734. }
  735. if(key_node) reverse_iterator = key_node->begin();
  736. else reverse_iterator = nullptr;
  737. return *this;
  738. }
  739. const MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::goToNextKey() const {
  740. if(!reverse_iterator) return self;
  741. AVLKeyNode* key_node = reverse_iterator->key_node;
  742. if(!key_node) {
  743. reverse_iterator = nullptr;
  744. return self;
  745. }
  746. elif(key_node->hasLeft()) key_node = MultiAVLTree::maxKeyNode(key_node->left);
  747. elif(key_node->isRight()) key_node = key_node->parent;
  748. elif(key_node->isLeft()) {
  749. AVLKeyNode* tmp = key_node->parent;
  750. key_node = key_node->parent->parent;
  751. while (tmp->isLeft() && key_node) {tmp = key_node; key_node = key_node->parent;}
  752. } elif(key_node->isRoot()) {
  753. reverse_iterator = nullptr;
  754. return self;
  755. }
  756. if(key_node) reverse_iterator = key_node->rbegin();
  757. else reverse_iterator = nullptr;
  758. return self;
  759. }
  760. const MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::goToPrevKey() const {
  761. if(!reverse_iterator) return self;
  762. AVLKeyNode* key_node = reverse_iterator->key_node;
  763. if(!key_node) {
  764. reverse_iterator = nullptr;
  765. return self;
  766. }
  767. elif(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  768. elif(key_node->isLeft()) key_node = key_node->parent;
  769. elif(key_node->isRight()) {
  770. AVLKeyNode* tmp = key_node->parent;
  771. key_node = key_node->parent->parent;
  772. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  773. } elif(key_node->isRoot()) {
  774. reverse_iterator = nullptr;
  775. return self;
  776. }
  777. if(key_node) reverse_iterator = key_node->begin();
  778. else reverse_iterator = nullptr;
  779. return *this;
  780. }
  781. MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::operator=(ReverseIterator other) {reverse_iterator = other.reverse_iterator; return self;}
  782. MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::operator++() {
  783. if(!reverse_iterator) return self;
  784. AVLKeyNode* key_node = reverse_iterator->key_node;
  785. ++reverse_iterator;
  786. if(reverse_iterator != nullptr) return self;
  787. elif(!key_node) {
  788. reverse_iterator = nullptr;
  789. return self;
  790. }
  791. elif(key_node->hasLeft()) key_node = MultiAVLTree::maxKeyNode(key_node->left);
  792. elif(key_node->isRight()) key_node = key_node->parent;
  793. elif(key_node->isLeft()) {
  794. AVLKeyNode* tmp = key_node->parent;
  795. key_node = key_node->parent->parent;
  796. while (tmp->isLeft() && key_node) {tmp = key_node; key_node = key_node->parent;}
  797. } elif(key_node->isRoot()) {
  798. reverse_iterator = nullptr;
  799. return self;
  800. }
  801. if(key_node) reverse_iterator = key_node->rbegin();
  802. else reverse_iterator = nullptr;
  803. return self;
  804. }
  805. MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::operator++(int) {ReverseIterator tmp(self); ++self; return tmp;}
  806. MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::operator--() {
  807. if(!reverse_iterator) return self;
  808. AVLKeyNode* key_node = reverse_iterator->key_node;
  809. --reverse_iterator;
  810. if(reverse_iterator != nullptr) return self;
  811. elif(!key_node) {
  812. reverse_iterator = nullptr;
  813. return self;
  814. }
  815. elif(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  816. elif(key_node->isLeft()) key_node = key_node->parent;
  817. elif(key_node->isRight()) {
  818. AVLKeyNode* tmp = key_node->parent;
  819. key_node = key_node->parent->parent;
  820. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  821. } elif(key_node->isRoot()) {
  822. reverse_iterator = nullptr;
  823. return self;
  824. }
  825. if(key_node) reverse_iterator = key_node->begin();
  826. else reverse_iterator = nullptr;
  827. return *this;
  828. }
  829. size_t MultiAVLTree::ReverseIterator::getElementCount() const noexcept {
  830. if(reverse_iterator->key_node) return reverse_iterator->key_node->getElementCount();
  831. else return 0;
  832. }
  833. MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::operator--(int) {ReverseIterator tmp(self); --self; return tmp;}
  834. const MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::operator++() const {
  835. if(!reverse_iterator) return self;
  836. AVLKeyNode* key_node = reverse_iterator->key_node;
  837. ++reverse_iterator;
  838. if(reverse_iterator != nullptr) return self;
  839. elif(!key_node) {
  840. reverse_iterator = nullptr;
  841. return self;
  842. }
  843. elif(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  844. elif(key_node->isLeft()) key_node = key_node->parent;
  845. elif(key_node->isRight()) {
  846. AVLKeyNode* tmp = key_node->parent;
  847. key_node = key_node->parent->parent;
  848. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  849. } elif(key_node->isRoot()) {
  850. reverse_iterator = nullptr;
  851. return self;
  852. }
  853. if(key_node) reverse_iterator = key_node->begin();
  854. else reverse_iterator = nullptr;
  855. return self;
  856. }
  857. const MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::operator++(int) const {ReverseIterator tmp(self); ++self; return tmp;}
  858. const MultiAVLTree::ReverseIterator& MultiAVLTree::ReverseIterator::operator--() const {
  859. if(!reverse_iterator) return self;
  860. AVLKeyNode* key_node = reverse_iterator->key_node;
  861. --reverse_iterator;
  862. if(reverse_iterator != nullptr) return self;
  863. elif(!key_node) {
  864. reverse_iterator = nullptr;
  865. return self;
  866. }
  867. if(key_node->hasRight()) key_node = MultiAVLTree::minKeyNode(key_node->right);
  868. elif(key_node->isLeft()) key_node = key_node->parent;
  869. elif(key_node->isRight()) {
  870. AVLKeyNode* tmp = key_node->parent;
  871. key_node = key_node->parent->parent;
  872. while (tmp->isRight() && key_node) {tmp = key_node; key_node = key_node->parent;}
  873. } elif(key_node->isRoot()) {
  874. reverse_iterator = nullptr;
  875. return self;
  876. }
  877. if(key_node) reverse_iterator = key_node->begin();
  878. else reverse_iterator = nullptr;
  879. return *this;
  880. }
  881. MultiAVLTree::AVLKeyNode& MultiAVLTree::ReverseIterator::getKeyNode() {return *reverse_iterator->key_node;}
  882. const MultiAVLTree::AVLKeyNode& MultiAVLTree::ReverseIterator::getKeyNode() const {return *reverse_iterator->key_node;}
  883. MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::ReverseIterator::getKeyNodeIterator() {return reverse_iterator;}
  884. const MultiAVLTree::AVLKeyNode::ReverseIterator MultiAVLTree::ReverseIterator::getKeyNodeIterator() const {return reverse_iterator;}
  885. KeyValue& MultiAVLTree::ReverseIterator::getKey() {return reverse_iterator->key_node->getKey();}
  886. const KeyValue& MultiAVLTree::ReverseIterator::getKey() const {return reverse_iterator->key_node->getKey();}
  887. const MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::operator--(int) const {ReverseIterator tmp(self); --self; return tmp;}
  888. bool MultiAVLTree::ReverseIterator::operator==(ReverseIterator other) const noexcept {return reverse_iterator == other.reverse_iterator;}
  889. bool MultiAVLTree::ReverseIterator::operator!=(ReverseIterator other) const noexcept {return reverse_iterator != other.reverse_iterator;}
  890. MultiAVLTree::AVLNode& MultiAVLTree::ReverseIterator::operator*() {return *reverse_iterator;}
  891. MultiAVLTree::AVLNode* MultiAVLTree::ReverseIterator::operator->() noexcept {return reverse_iterator.operator->();}
  892. const MultiAVLTree::AVLNode& MultiAVLTree::ReverseIterator::operator*() const {return *reverse_iterator;}
  893. const MultiAVLTree::AVLNode* MultiAVLTree::ReverseIterator::operator->() const noexcept {return reverse_iterator.operator->();}
  894. MultiAVLTree::ReverseIterator::operator Iterator() {return Iterator(reverse_iterator);}
  895. MultiAVLTree::ReverseIterator::operator const Iterator() const {return Iterator(reverse_iterator);}
  896. MultiAVLTree::Iterator MultiAVLTree::ReverseIterator::rbegin() noexcept {return (reverse_iterator)? reverse_iterator->key_node->begin() : nullptr;}
  897. MultiAVLTree::Iterator MultiAVLTree::ReverseIterator::rend() noexcept {return Iterator(self).goToNextKey();}
  898. MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::begin() noexcept {return (reverse_iterator)? reverse_iterator->key_node->rbegin() : nullptr;}
  899. MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::end() noexcept {return ReverseIterator(self).goToNextKey();}
  900. const MultiAVLTree::Iterator MultiAVLTree::ReverseIterator::rbegin() const noexcept {return (reverse_iterator)? reverse_iterator->key_node->begin() : nullptr;}
  901. const MultiAVLTree::Iterator MultiAVLTree::ReverseIterator::rend() const noexcept {return Iterator(self).goToNextKey();}
  902. const MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::begin() const noexcept {return (reverse_iterator)? reverse_iterator->key_node->rbegin() : nullptr;}
  903. const MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::end() const noexcept {return ReverseIterator(self).goToNextKey();}
  904. const MultiAVLTree::Iterator MultiAVLTree::ReverseIterator::crbegin() const noexcept {return (reverse_iterator)? reverse_iterator->key_node->begin() : nullptr;}
  905. const MultiAVLTree::Iterator MultiAVLTree::ReverseIterator::crend() const noexcept {return Iterator(self).goToNextKey();}
  906. const MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::cbegin() const noexcept {return (reverse_iterator)? reverse_iterator->key_node->rbegin() : nullptr;}
  907. const MultiAVLTree::ReverseIterator MultiAVLTree::ReverseIterator::cend() const noexcept {return ReverseIterator(self).goToNextKey();}