DeSiGNAR  0.5a
Data Structures General Library
tree.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGTREE_H
26 # define DSGTREE_H
27 
28 # include <nodesdef.H>
29 # include <containeralgorithms.H>
30 # include <setalgorithms.H>
31 # include <stack.H>
32 # include <iterator.H>
33 
34 namespace Designar
35 {
36  template <typename Key>
37  class TreapRkNode : public BaseBinTreeNode<Key, TreapRkNode<Key>,
38  BinTreeNodeNullValue::SENTINEL>
39  {
42 
43  nat_t count;
44  rng_seed_t prior;
45 
46  public:
48  : BaseNode()
49  {
50  // empty
51  }
52 
53  TreapRkNode(const Key & k)
54  : BaseNode(k), count(1), prior(0)
55  {
56  // empty
57  }
58 
59  TreapRkNode(Key && k)
60  : BaseNode(std::forward<Key>(k)), count(1), prior(0)
61  {
62  // empty
63  }
64 
66  : BaseNode(ctor), count(0), prior(rng_t::max())
67  {
68  // empty
69  }
70 
72  {
73  return count;
74  }
75 
77  {
78  return prior;
79  }
80 
81  void reset()
82  {
84  count = 1;
85  }
86  };
87 
88  template <class RkNode>
89  inline nat_t & COUNT(RkNode * p)
90  {
91  return p->get_count();
92  }
93 
94  template <class TreapNode>
95  inline rng_seed_t & PRIOR(TreapNode * p)
96  {
97  return p->get_priority();
98  }
99 
100 
101  template <typename Key, class Cmp = std::less<Key>>
102  class RankedTreap : public ContainerAlgorithms<RankedTreap<Key, Cmp>, Key>,
103  public SetAlgorithms<RankedTreap<Key, Cmp>, Key>
104  {
105  public:
107 
108  private:
109  Node head;
110  Node *& root;
111  Cmp & cmp;
112 
113  rng_t rng;
114 
115  static bool verify(Node *, Cmp & cmp);
116 
117  static bool verify_dup(Node *, Cmp & cmp);
118 
119  static Node * copy(Node *);
120 
121  static void destroy(Node *&);
122 
123  static Node * rotate_left(Node *);
124 
125  static Node * rotate_right(Node *);
126 
127  static void split_pos(Node *, nat_t, Node *&, Node *&);
128 
129  static bool split_key(Node *, const Key &, Node *&, Node *&, Cmp &);
130 
131  static void split_key_dup(Node *, const Key &, Node *&, Node *&,
132  Cmp &);
133 
134  static Node * exclusive_join(Node *&, Node *&);
135 
136  static void join_dup(Node *&, Node *&, Cmp &);
137 
138  static Node * insert(Node *&, Node *, Cmp &);
139 
140  static Node * insert_dup(Node *&, Node *, Cmp &);
141 
142  static Node * search(Node *, const Key &, Cmp &);
143 
144  static Node * search(Node *, Key &&, Cmp &);
145 
146  static Node * search_or_insert(Node *&, Node *, Cmp &);
147 
148  static Node * remove_root(Node *& root)
149  {
150  Node * ret_val = root;
151  root = exclusive_join(L(root), R(root));
152  return ret_val;
153  }
154 
155  static Node * remove(Node *&, const Key &, Cmp &);
156 
157  static Node * remove_pos(Node *&, nat_t);
158 
159  static Node * select(Node *, nat_t);
160 
161  static lint_t position(Node *, const Key &, Cmp &);
162 
163  static Node * min(Node *);
164 
165  static Node * max(Node *);
166 
167  template <class Op>
168  static void preorder_rec(Node *, Op &);
169 
170  template <class Op>
171  static void inorder_rec(Node *, Op &);
172 
173  template <class Op>
174  static void postorder_rec(Node *, Op &);
175 
176  Key * insert(Node * p)
177  {
178  PRIOR(p) = rng();
179 
180  if (insert(root, p, cmp) != Node::null)
181  return &KEY(p);
182 
183  delete p;
184  return nullptr;
185  }
186 
187  Key * insert_dup(Node * p)
188  {
189  PRIOR(p) = rng();
190  return &KEY(insert_dup(root, p, cmp));
191  }
192 
193  Key * search_or_insert(Node * p)
194  {
195  PRIOR(p) = rng();
196 
197  Node * result = search_or_insert(root, p, cmp);
198 
199  if (p != result)
200  delete p;
201 
202  return &KEY(result);
203  }
204 
205  public:
206  using ItemType = Key;
207  using KeyType = Key;
208  using DataType = Key;
209  using ValueType = Key;
210  using SizeType = nat_t;
211  using CmpType = Cmp;
212 
213  bool verify() const
214  {
215  return verify(root, cmp);
216  }
217 
218  bool verify_dup() const
219  {
220  return verify_dup(root, cmp);
221  }
222 
223  RankedTreap(rng_seed_t seed, Cmp & _cmp)
224  : head(), root(L(&head)), cmp(_cmp), rng(seed)
225  {
226  // empty
227  }
228 
229  RankedTreap(Cmp & _cmp)
230  : RankedTreap(time(nullptr), _cmp)
231  {
232  // empty
233  }
234 
235  RankedTreap(Cmp && _cmp = Cmp())
236  : RankedTreap(_cmp)
237  {
238  // empty
239  }
240 
241  RankedTreap(rng_seed_t seed, Cmp && _cmp = Cmp())
242  : RankedTreap(seed, _cmp)
243  {
244  // empty
245  }
246 
248  : RankedTreap(t.cmp)
249  {
250  root = copy(t.root);
251  }
252 
254  : RankedTreap()
255  {
256  swap(t);
257  }
258 
259  RankedTreap(const std::initializer_list<Key> &);
260 
262  {
263  clear();
264  }
265 
267  {
268  if (this == &t)
269  return *this;
270 
271  clear();
272  root = copy(t.root);
273  cmp = t.cmp;
274  return *this;
275  }
276 
278  {
279  swap(t);
280  return *this;
281  }
282 
283  void swap(RankedTreap & t)
284  {
285  std::swap(root, t.root);
286  std::swap(cmp, t.cmp);
287  }
288 
289  bool is_empty() const
290  {
291  return root == Node::null;
292  }
293 
294  bool is_sorted() const
295  {
296  return true;
297  }
298 
299  nat_t size() const
300  {
301  return COUNT(root);
302  }
303 
304  void clear()
305  {
306  destroy(root);
307  }
308 
309  Cmp & get_cmp()
310  {
311  return cmp;
312  }
313 
314  const Cmp & get_cmp() const
315  {
316  return cmp;
317  }
318 
319  Key * insert(const Key & k)
320  {
321  Node * p = new Node(k);
322  return insert(p);
323  }
324 
325  Key * insert(Key && k)
326  {
327  Node * p = new Node(std::forward<Key>(k));
328  return insert(p);
329  }
330 
331  Key * insert_dup(const Key & k)
332  {
333  Node * p = new Node(k);
334  return insert_dup(p);
335  }
336 
337  Key * insert_dup(Key && k)
338  {
339  Node * p = new Node(std::forward<Key>(k));
340  return insert_dup(p);
341  }
342 
343  Key * append(const Key & k)
344  {
345  return insert(k);
346  }
347 
348  Key * append(Key && k)
349  {
350  return insert(std::forward<Key>(k));
351  }
352 
353  Key * append_dup(const Key & k)
354  {
355  return insert_dup(k);
356  }
357 
358  Key * append_dup(Key && k)
359  {
360  return insert(std::forward<Key>(k));
361  }
362 
363  Key * search(const Key & k)
364  {
365  Node * result = search(root, k, cmp);
366 
367  if (result == Node::null)
368  return nullptr;
369 
370  return &KEY(result);
371  }
372 
373  const Key * search(const Key & k) const
374  {
375  Node * result = search(root, k, cmp);
376 
377  if (result == Node::null)
378  return nullptr;
379 
380  return &KEY(result);
381  }
382 
383  Key * search_or_insert(const Key & k)
384  {
385  Node * p = new Node(k);
386  return search_or_insert(p);
387  }
388 
389  Key * search_or_insert(Key && k)
390  {
391  Node * p = new Node(std::forward<Key>(k));
392  return search_or_insert(p);
393  }
394 
395  Key & find(const Key & k)
396  {
397  Key * result = search(k);
398 
399  if (result == nullptr)
400  throw std::domain_error("Key not found");
401 
402  return *result;
403  }
404 
405  const Key & find(const Key & k) const
406  {
407  const Key * result = search(k);
408 
409  if (result == nullptr)
410  throw std::domain_error("Key not found");
411 
412  return *result;
413  }
414 
415  bool remove(const Key & k)
416 
417  {
418  Node * result = remove(root, k, cmp);
419 
420  if (result == Node::null)
421  return false;
422 
423  delete result;
424  return true;
425  }
426 
428  {
429  if (i >= size())
430  throw std::out_of_range("Infix position is out of range");
431 
432  Node * result = remove_pos(root, i);
433  Key ret_val = std::move(KEY(result));
434  delete result;
435  return ret_val;
436  }
437 
438  const Key & min() const
439  {
440  if (is_empty())
441  throw std::underflow_error("Tree is empty");
442 
443  return KEY(min(root));
444  }
445 
446  const Key & max() const
447  {
448  if (is_empty())
449  throw std::underflow_error("Tree is empty");
450 
451  return KEY(max(root));
452  }
453 
454  std::tuple<RankedTreap, RankedTreap> split_pos(nat_t i)
455  {
456  if (i >= size())
457  throw std::out_of_range("Infix position is out of range");
458 
459  RankedTreap ts, tg;
460  split_pos(root, i, ts.root, tg.root);
461  root = Node::null;
462  return std::make_tuple(std::move(ts), std::move(tg));
463  }
464 
465  std::tuple<RankedTreap, RankedTreap> split_key(const Key & k)
466  {
467  RankedTreap ts, tg;
468 
469  if (split_key(root, k, ts.root, tg.root, cmp))
470  root = Node::null;
471 
472  return std::make_tuple(std::move(ts), std::move(tg));
473  }
474 
475  std::tuple<RankedTreap, RankedTreap> split_key_dup(const Key & k)
476  {
477  RankedTreap ts, tg;
478  split_key_dup(root, k, ts.root, tg.root, cmp);
479  root = Node::null;
480  return std::make_tuple(std::move(ts), std::move(tg));
481  }
482 
484  {
485  root = exclusive_join(ts.root, tg.root);
486  ts.root = tg.root = Node::null;
487  }
488 
490  {
491  join_dup(ts.root, tg.root, cmp);
492  root = ts.root;
493  ts.root = tg.root = Node::null;
494  }
495 
496  Key & select(nat_t i)
497  {
498  if (i >= size())
499  throw std::out_of_range("Infix position is out of range");
500 
501  return KEY(select(root, i));
502  }
503 
504  const Key & select(nat_t i) const
505  {
506  if (i >= size())
507  throw std::out_of_range("Infix position is out of range");
508 
509  return KEY(select(root, i));
510  }
511 
512  lint_t position(const Key & k) const
513  {
514  return position(root, k, cmp);
515  }
516 
517  Key & operator [] (nat_t i)
518  {
519  return select(i);
520  }
521 
522  const Key & operator [] (nat_t i) const
523  {
524  return select(i);
525  }
526 
527  template <class Op>
528  void for_each_preorder(Op & op)
529  {
530  preorder_rec<Op>(root, op);
531  }
532 
533  template <class Op>
534  void for_each_preorder(Op && op = Op())
535  {
536  for_each_preorder<Op>(op);
537  }
538 
539  template <class Op>
540  void for_each_inorder(Op & op)
541  {
542  inorder_rec<Op>(root, op);
543  }
544 
545  template <class Op>
546  void for_each_inorder(Op && op = Op())
547  {
548  for_each_inorder<Op>(op);
549  }
550 
551  template <class Op>
552  void for_each_postorder(Op & op)
553  {
554  postorder_rec<Op>(root, op);
555  }
556 
557  template <class Op>
558  void for_each_postorder(Op && op = Op())
559  {
560  for_each_postorder<Op>(op);
561  }
562 
564  {
565  friend class RankedTreap;
566 
567  DynStack<Node *> stack;
568  Node * root = Node::null;
569  Node * curr = Node::null;
570  lint_t pos = 0;
571 
572  Node * last(Node *);
573 
574  protected:
576  : root(t.root), curr(Node::null), pos(t.size())
577  {
578  // empty
579  }
580 
581  Node * get_location() const
582  {
583  return curr;
584  }
585 
586  public:
588  : root(t.root), curr(root)
589  {
590  // empty
591  }
592 
594  : stack(it.stack), root(it.root), curr(it.curr), pos(it.pos)
595  {
596  // empty
597  }
598 
600  {
601  swap(it);
602  }
603 
605  {
606  if (this == &it)
607  return *this;
608 
609  stack = it.stack;
610  root = it.root;
611  curr = it.curr;
612  pos = it.pos;
613 
614  return *this;
615  }
616 
618  {
619  swap(it);
620  return *this;
621  }
622 
624  {
625  std::swap(stack, it.stack);
626  std::swap(root, it.root);
627  std::swap(curr, it.curr);
628  std::swap(pos, it.pos);
629  }
630 
631  void reset()
632  {
633  stack.clear();
634  pos = 0;
635  curr = root;
636  }
637 
639  {
640  return pos;
641  }
642 
643  bool has_current() const
644  {
645  return curr != Node::null;
646  }
647 
648  Key & get_current()
649  {
650  if (not has_current())
651  throw std::overflow_error("There is not current element");
652 
653  return KEY(curr);
654  }
655 
656  const Key & get_current() const
657  {
658  if (not has_current())
659  throw std::overflow_error("There is not current element");
660 
661  return KEY(curr);
662  }
663 
664  void next()
665  {
666  if (not has_current())
667  return;
668 
669  stack.push(curr);
670 
671  curr = L(curr);
672 
673  ++pos;
674 
675  if (curr != Node::null)
676  return;
677 
678  while (not stack.is_empty() and curr == Node::null)
679  curr = R(stack.pop());
680  }
681  };
682 
684  {
685  friend class RankedTreap;
686 
687  DynStack<Node *> stack;
688  Node * root = Node::null;
689  Node * curr = Node::null;
690  lint_t pos = 0;
691 
692  Node * search_min(Node *);
693 
694  Node * search_max(Node *);
695 
696  void init()
697  {
698  if (root == Node::null)
699  return;
700 
701  pos = 0;
702  curr = search_min(root);
703  }
704 
705  protected:
706  InorderIterator(const RankedTreap & t, int)
707  : root(t.root), curr(Node::null), pos(t.size())
708  {
709  // empty
710  }
711 
712  Node * get_location() const
713  {
714  return curr;
715  }
716 
717  public:
719  : root(t.root)
720  {
721  init();
722  }
723 
725  : stack(it.stack), root(it.root), curr(it.curr), pos(it.pos)
726  {
727  // empty
728  }
729 
731  {
732  swap(it);
733  }
734 
736  {
737  if (this == &it)
738  return *this;
739 
740  stack = it.stack;
741  root = it.root;
742  curr = it.curr;
743  pos = it.pos;
744 
745  return *this;
746  }
747 
749  {
750  swap(it);
751  return *this;
752  }
753 
755  {
756  std::swap(stack, it.stack);
757  std::swap(root, it.root);
758  std::swap(curr, it.curr);
759  std::swap(pos, it.pos);
760  }
761 
762  void reset()
763  {
764  stack.clear();
765  init();
766  }
767 
768  void reset_last()
769  {
770  pos = -1;
771  stack.clear();
772  curr = search_max(root);
773  }
774 
776  {
777  return pos;
778  }
779 
780  bool has_current() const
781  {
782  return curr != Node::null;
783  }
784 
785  Key & get_current()
786  {
787  if (not has_current())
788  throw std::overflow_error("There is not current element");
789 
790  return KEY(curr);
791  }
792 
793  const Key & get_current() const
794  {
795  if (not has_current())
796  throw std::overflow_error("There is not current element");
797 
798  return KEY(curr);
799  }
800 
801  void next()
802  {
803  if (not has_current())
804  throw std::overflow_error("There is not current element");
805 
806  ++pos;
807  curr = R(curr);
808 
809  if (curr != Node::null)
810  curr = search_min(curr);
811  else
812  if (not stack.is_empty())
813  curr = stack.pop();
814  }
815  };
816 
818  {
819  friend class RankedTreap;
820 
821  DynStack<Node *> stack;
822  Node * root = Node::null;
823  Node * curr = Node::null;
824  lint_t pos = 0;
825 
826  Node * first(Node *);
827 
828  void init()
829  {
830  if (root == Node::null)
831  return;
832 
833  pos = 0;
834  curr = first(root);
835  }
836 
837  protected:
839  : root(t.root), curr(Node::null), pos(t.size())
840  {
841  // empty
842  }
843 
844  Node * get_location() const
845  {
846  return curr;
847  }
848 
849  public:
851  : root(t.root)
852  {
853  init();
854  }
855 
857  : stack(it.stack), root(it.root), curr(it.curr), pos(it.pos)
858  {
859  // empty
860  }
861 
863  {
864  swap(it);
865  }
866 
868  {
869  if (this == &it)
870  return *this;
871 
872  stack = it.stack;
873  root = it.root;
874  curr = it.curr;
875  pos = it.pos;
876 
877  return *this;
878  }
879 
881  {
882  swap(it);
883  return *this;
884  }
885 
887  {
888  std::swap(stack, it.stack);
889  std::swap(root, it.root);
890  std::swap(curr, it.curr);
891  std::swap(pos, it.pos);
892  }
893 
894  void reset()
895  {
896  stack.clear();
897  init();
898  }
899 
900  void reset_last()
901  {
902  pos = -1;
903  stack.clear();
904  curr = root;
905  }
906 
908  {
909  return pos;
910  }
911 
912  bool has_current() const
913  {
914  return curr != Node::null;
915  }
916 
917  Key & get_current()
918  {
919  if (not has_current())
920  throw std::overflow_error("There is not current element");
921 
922  return KEY(curr);
923  }
924 
925  const Key & get_current() const
926  {
927  if (not has_current())
928  throw std::overflow_error("There is not current element");
929 
930  return KEY(curr);
931  }
932 
933  void next()
934  {
935  if (not has_current())
936  throw std::overflow_error("There is not current element");
937 
938  ++pos;
939 
940  if (stack.is_empty())
941  {
942  curr = Node::null;
943  return;
944  }
945 
946  Node *& parent = stack.top();
947 
948  if (curr == R(parent) or R(parent) == Node::null)
949  {
950  curr = stack.pop();
951  return;
952  }
953 
954  curr = first(R(parent));
955  }
956  };
957 
958  class Iterator : public InorderIterator,
959  public ForwardIterator<Iterator, Key>
960  {
961  friend class RankedTreap;
962  friend class BasicIterator<Iterator, Key>;
963  using Base = InorderIterator;
964  using Base::Base;
965  };
966 
968  {
969  return Iterator(*this);
970  }
971 
972  Iterator begin() const
973  {
974  return Iterator(*this);
975  }
976 
978  {
979  return Iterator(*this, 0);
980  }
981 
982  Iterator end() const
983  {
984  return Iterator(*this, 0);
985  }
986  };
987 
988  template <typename Key, class Cmp>
989  RankedTreap<Key, Cmp>::RankedTreap(const std::initializer_list<Key> & l)
990  : RankedTreap()
991  {
992  for (const auto & item : l)
993  append(item);
994  }
995 
996  template <typename Key, class Cmp>
997  bool RankedTreap<Key, Cmp>::verify(Node * r, Cmp & cmp)
998  {
999  if (r == Node::null)
1000  return true;
1001 
1002  Node * lc = L(r);
1003  Node * rc = R(r);
1004 
1005  if (not verify(lc, cmp))
1006  return false;
1007 
1008  if (not verify(rc, cmp))
1009  return false;
1010 
1011  bool test = (COUNT(r) == (COUNT(lc) + COUNT(rc) + 1)) and
1012  (PRIOR(r) <= PRIOR(lc)) and (PRIOR(r) <= PRIOR(rc));
1013 
1014  if (lc != Node::null)
1015  test = test and cmp(KEY(lc), KEY(r));
1016 
1017  if (rc != Node::null)
1018  test = test and cmp(KEY(r), KEY(rc));
1019 
1020  return test;
1021  }
1022 
1023  template <typename Key, class Cmp>
1024  bool RankedTreap<Key, Cmp>::verify_dup(Node * r, Cmp & cmp)
1025  {
1026  if (r == Node::null)
1027  return true;
1028 
1029  Node * lc = L(r);
1030  Node * rc = R(r);
1031 
1032  if (not verify_dup(lc, cmp))
1033  return false;
1034 
1035  if (not verify_dup(rc, cmp))
1036  return false;
1037 
1038  bool test = (COUNT(r) == (COUNT(lc) + COUNT(rc) + 1)) and
1039  (PRIOR(r) <= PRIOR(lc)) and (PRIOR(r) <= PRIOR(rc));
1040 
1041  if (lc != Node::null)
1042  test = test and not cmp(KEY(r), KEY(lc));
1043 
1044  if (rc != Node::null)
1045  test = test and not cmp(KEY(rc), KEY(r));
1046 
1047  return test;
1048  }
1049 
1050  template <typename Key, class Cmp>
1052  {
1053  if (r == Node::null)
1054  return Node::null;
1055 
1056  Node * p = new Node(KEY(r));
1057  COUNT(p) = COUNT(r);
1058  PRIOR(p) = PRIOR(r);
1059  L(p) = copy(L(r));
1060  R(p) = copy(R(r));
1061  return p;
1062  }
1063 
1064  template <typename Key, class Cmp>
1065  void RankedTreap<Key, Cmp>::destroy(Node *& r)
1066  {
1067  if (r == Node::null)
1068  return;
1069 
1070  destroy(L(r));
1071  destroy(R(r));
1072  delete r;
1073  r = Node::null;
1074  }
1075 
1076  template <typename Key, class Cmp>
1077  typename RankedTreap<Key, Cmp>::Node *
1079  {
1080  Node * q = R(r);
1081  R(r) = L(q);
1082  L(q) = r;
1083 
1084  COUNT(r) = COUNT(L(r)) + COUNT(R(r)) + 1;
1085  COUNT(q) = COUNT(L(q)) + COUNT(R(q)) + 1;
1086 
1087  return q;
1088  }
1089 
1090  template <typename Key, class Cmp>
1091  typename RankedTreap<Key, Cmp>::Node *
1093  {
1094  Node * q = L(r);
1095  L(r) = R(q);
1096  R(q) = r;
1097 
1098  COUNT(r) = COUNT(L(r)) + COUNT(R(r)) + 1;
1099  COUNT(q) = COUNT(L(q)) + COUNT(R(q)) + 1;
1100 
1101  return q;
1102  }
1103 
1104  template <typename Key, class Cmp>
1105  void RankedTreap<Key, Cmp>::split_pos(Node * r, nat_t i,
1106  Node *& ts, Node *& tg)
1107  {
1108  if (i == COUNT(L(r)))
1109  {
1110  ts = L(r);
1111  tg = r;
1112  L(tg) = Node::null;
1113  COUNT(tg) -= COUNT(ts);
1114  return;
1115  }
1116 
1117  if (i < COUNT(L(r)))
1118  {
1119  split_pos(L(r), i, ts, L(r));
1120  tg = r;
1121  COUNT(r) -= COUNT(ts);
1122  }
1123  else
1124  {
1125  split_pos(R(r), i - (COUNT(L(r)) + 1), R(r), tg);
1126  ts = r;
1127  COUNT(r) -= COUNT(tg);
1128  }
1129  }
1130 
1131  template <typename Key, class Cmp>
1132  bool RankedTreap<Key, Cmp>::split_key(Node * r, const Key & k,
1133  Node *& ts, Node *& tg, Cmp & cmp)
1134  {
1135  if (r == Node::null)
1136  {
1137  ts = tg = Node::null;
1138  return true;
1139  }
1140 
1141  if (cmp(k, KEY(r)))
1142  {
1143  if (split_key(L(r), k, ts, L(r), cmp))
1144  {
1145  tg = r;
1146  COUNT(tg) -= COUNT(ts);
1147  return true;
1148  }
1149  }
1150  else if (cmp(KEY(r), k))
1151  {
1152  if (split_key(R(r), k, R(r), tg, cmp))
1153  {
1154  ts = r;
1155  COUNT(ts) -= COUNT(tg);
1156  return true;
1157  }
1158  }
1159 
1160  return false;
1161  }
1162 
1163  template <typename Key, class Cmp>
1164  void RankedTreap<Key, Cmp>::split_key_dup(Node * r, const Key & k,
1165  Node *& ts, Node *& tg,
1166  Cmp & cmp)
1167  {
1168  if (r == Node::null)
1169  {
1170  ts = tg = Node::null;
1171  return;
1172  }
1173 
1174  if (cmp(k, KEY(r)))
1175  {
1176  split_key_dup(L(r), k, ts, L(r), cmp);
1177  tg = r;
1178  COUNT(tg) -= COUNT(ts);
1179  }
1180  else
1181  {
1182  split_key_dup(R(r), k, R(r), tg, cmp);
1183  ts = r;
1184  COUNT(ts) -= COUNT(tg);
1185  }
1186  }
1187 
1188  template <typename Key, class Cmp>
1189  typename RankedTreap<Key, Cmp>::Node *
1190  RankedTreap<Key, Cmp>::exclusive_join(Node *& ts, Node *& tg)
1191  {
1192  if (ts == Node::null)
1193  return tg;
1194 
1195  if (tg == Node::null)
1196  return ts;
1197 
1198  if (PRIOR(ts) < PRIOR(tg))
1199  {
1200  COUNT(ts) += COUNT(tg);
1201  R(ts) = exclusive_join(R(ts), tg);
1202  return ts;
1203  }
1204  else
1205  {
1206  COUNT(tg) += COUNT(ts);
1207  L(tg) = exclusive_join(ts, L(tg));
1208  return tg;
1209  }
1210  }
1211 
1212  template <typename Key, class Cmp>
1213  void RankedTreap<Key, Cmp>::join_dup(Node *& t1, Node *& t2, Cmp & cmp)
1214  {
1215  if (t2 == Node::null)
1216  return;
1217 
1218  Node * l = L(t2);
1219  Node * r = R(t2);
1220 
1221  t2->reset();
1222  insert_dup(t1, t2, cmp);
1223  join_dup(t1, l, cmp);
1224  join_dup(t1, r, cmp);
1225  }
1226 
1227  template <typename Key, class Cmp>
1228  typename RankedTreap<Key, Cmp>::Node *
1229  RankedTreap<Key, Cmp>::insert(Node *& r, Node * p, Cmp & cmp)
1230  {
1231  if (r == Node::null)
1232  {
1233  r = p;
1234  return r;
1235  }
1236 
1237  if (cmp(KEY(p), KEY(r)))
1238  {
1239  Node * result = insert(L(r), p, cmp);
1240 
1241  if (result == Node::null)
1242  return Node::null;
1243 
1244  ++COUNT(r);
1245 
1246  if (PRIOR(L(r)) < PRIOR(r))
1247  r = result = rotate_right(r);
1248 
1249  return result;
1250  }
1251  else if (cmp(KEY(r), KEY(p)))
1252  {
1253  Node * result = insert(R(r), p, cmp);
1254 
1255  if (result == Node::null)
1256  return Node::null;
1257 
1258  ++COUNT(r);
1259 
1260  if (PRIOR(R(r)) < PRIOR(r))
1261  r = result = rotate_left(r);
1262 
1263  return result;
1264  }
1265 
1266  return Node::null;
1267  }
1268 
1269  template <typename Key, class Cmp>
1270  typename RankedTreap<Key, Cmp>::Node *
1271  RankedTreap<Key, Cmp>::insert_dup(Node *& r, Node * p, Cmp & cmp)
1272  {
1273  if (r == Node::null)
1274  {
1275  r = p;
1276  return r;
1277  }
1278 
1279  if (cmp(KEY(p), KEY(r)))
1280  {
1281  Node * result = insert_dup(L(r), p, cmp);
1282 
1283  if (result == Node::null)
1284  return Node::null;
1285 
1286  ++COUNT(r);
1287 
1288  if (PRIOR(L(r)) < PRIOR(r))
1289  r = result = rotate_right(r);
1290 
1291  return result;
1292  }
1293 
1294  Node * result = insert_dup(R(r), p, cmp);
1295 
1296  if (result == Node::null)
1297  return Node::null;
1298 
1299  ++COUNT(r);
1300 
1301  if (PRIOR(R(r)) < PRIOR(r))
1302  r = result = rotate_left(r);
1303 
1304  return result;
1305  }
1306 
1307  template <typename Key, class Cmp>
1308  typename RankedTreap<Key, Cmp>::Node *
1309  RankedTreap<Key, Cmp>::search(Node * r, const Key & k, Cmp & cmp)
1310  {
1311  if (r == Node::null)
1312  return Node::null;
1313 
1314  if (cmp(k, KEY(r)))
1315  return search(L(r), k, cmp);
1316  else if (cmp(KEY(r), k))
1317  return search(R(r), k, cmp);
1318 
1319  return r;
1320  }
1321 
1322  template <typename Key, class Cmp>
1323  typename RankedTreap<Key, Cmp>::Node *
1324  RankedTreap<Key, Cmp>::search(Node * r, Key && k, Cmp & cmp)
1325  {
1326  if (r == Node::null)
1327  return Node::null;
1328 
1329  if (cmp(k, KEY(r)))
1330  return search(L(r), std::forward<Key>(k), cmp);
1331  else if (cmp(KEY(r), k))
1332  return search(R(r), std::forward<Key>(k), cmp);
1333 
1334  return r;
1335  }
1336 
1337 
1338  template <typename Key, class Cmp>
1339  typename RankedTreap<Key, Cmp>::Node *
1340  RankedTreap<Key, Cmp>::search_or_insert(Node *& r, Node * p, Cmp & cmp)
1341  {
1342  if (r == Node::null)
1343  {
1344  r = p;
1345  return r;
1346  }
1347 
1348  if (cmp(KEY(p), KEY(r)))
1349  {
1350  Node * result = search_or_insert(L(r), p, cmp);
1351 
1352  if (result == p)
1353  {
1354  ++COUNT(r);
1355 
1356  if (PRIOR(L(r)) < PRIOR(r))
1357  r = result = rotate_right(r);
1358  }
1359 
1360  return result;
1361  }
1362  else if (cmp(KEY(r), KEY(p)))
1363  {
1364  Node * result = search_or_insert(R(r), p, cmp);
1365 
1366  if (result == p)
1367  {
1368  ++COUNT(r);
1369 
1370  if (PRIOR(R(r)) < PRIOR(r))
1371  r = result = rotate_left(r);
1372  }
1373 
1374  return result;
1375  }
1376 
1377  return r;
1378  }
1379 
1380  template <typename Key, class Cmp>
1381  typename RankedTreap<Key, Cmp>::Node *
1382  RankedTreap<Key, Cmp>::remove(Node *& r, const Key & k, Cmp & cmp)
1383  {
1384  if (r == Node::null)
1385  return Node::null;
1386 
1387  if (cmp(k, KEY(r)))
1388  {
1389  Node * result = remove(L(r), k, cmp);
1390 
1391  if (result != Node::null)
1392  --COUNT(r);
1393 
1394  return result;
1395  }
1396  else if (cmp(KEY(r), k))
1397  {
1398  Node * result = remove(R(r), k, cmp);
1399 
1400  if (result != Node::null)
1401  --COUNT(r);
1402 
1403  return result;
1404  }
1405 
1406  return remove_root(r);
1407  }
1408 
1409  template <typename Key, class Cmp>
1410  typename RankedTreap<Key, Cmp>::Node *
1412  {
1413  if (COUNT(L(r)) == i)
1414  return remove_root(r);
1415 
1416  Node * result = Node::null;
1417 
1418  if (i < COUNT(L(r)))
1419  result = remove_pos(L(r), i);
1420  else
1421  result = remove_pos(R(r), i - COUNT(L(r)) - 1);
1422 
1423  --COUNT(r);
1424  return result;
1425  }
1426 
1427  template <typename Key, class Cmp>
1428  typename RankedTreap<Key, Cmp>::Node *
1430  {
1431  if (COUNT(L(r)) == i)
1432  return r;
1433 
1434  if (i < COUNT(L(r)))
1435  return select(L(r), i);
1436 
1437  return select(R(r), i - COUNT(L(r)) - 1);
1438  }
1439 
1440  template <typename Key, class Cmp>
1441  lint_t RankedTreap<Key, Cmp>::position(Node * r, const Key & k, Cmp & cmp)
1442  {
1443  if (r == Node::null)
1444  return -1;
1445 
1446  if (cmp(k, KEY(r)))
1447  return position(L(r), k, cmp);
1448  else if (cmp(KEY(r), k))
1449  {
1450  lint_t p = position(R(r), k, cmp);
1451 
1452  if (p == -1)
1453  return p;
1454 
1455  return p + COUNT(L(r)) + 1;
1456  }
1457 
1458  return COUNT(L(r));
1459  }
1460  template <typename Key, class Cmp>
1462  {
1463  while (L(r) != Node::null)
1464  r = L(r);
1465 
1466  return r;
1467  }
1468 
1469  template <typename Key, class Cmp>
1471  {
1472  while (R(r) != Node::null)
1473  r = R(r);
1474 
1475  return r;
1476  }
1477 
1478  template <typename Key, class Cmp>
1479  template <class Op>
1480  void RankedTreap<Key, Cmp>::preorder_rec(Node * r, Op & op)
1481  {
1482  if (r == Node::null)
1483  return;
1484 
1485  op(KEY(r));
1486  preorder_rec(L(r), op);
1487  preorder_rec(R(r), op);
1488  }
1489 
1490  template <typename Key, class Cmp>
1491  template <class Op>
1492  void RankedTreap<Key, Cmp>::inorder_rec(Node * r, Op & op)
1493  {
1494  if (r == Node::null)
1495  return;
1496 
1497  inorder_rec(L(r), op);
1498  op(KEY(r));
1499  inorder_rec(R(r), op);
1500  }
1501 
1502  template <typename Key, class Cmp>
1503  template <class Op>
1504  void RankedTreap<Key, Cmp>::postorder_rec(Node * r, Op & op)
1505  {
1506  if (r == Node::null)
1507  return;
1508 
1509  postorder_rec(L(r), op);
1510  postorder_rec(R(r), op);
1511  op(KEY(r));
1512  }
1513 
1514  template <typename Key, class Cmp>
1515  typename RankedTreap<Key, Cmp>::Node *
1517  {
1518  while (true)
1519  {
1520  while (R(r) != Node::null)
1521  r = R(r);
1522 
1523  if (L(r) == Node::null)
1524  break;
1525 
1526  r = L(r);
1527  }
1528 
1529  return r;
1530  }
1531 
1532  template <typename Key, class Cmp>
1533  typename RankedTreap<Key, Cmp>::Node *
1535  {
1536  while (L(r) != Node::null)
1537  {
1538  stack.push(r);
1539  r = L(r);
1540  }
1541 
1542  return r;
1543  }
1544 
1545  template <typename Key, class Cmp>
1546  typename RankedTreap<Key, Cmp>::Node *
1548  {
1549  while (R(r) != Node::null)
1550  r = R(r);
1551 
1552  return r;
1553  }
1554 
1555  template <typename Key, class Cmp>
1556  typename RankedTreap<Key, Cmp>::Node *
1558  {
1559  while (true)
1560  {
1561  while (L(r) != Node::null)
1562  {
1563  stack.push(r);
1564  r = L(r);
1565  }
1566 
1567  if (R(r) == Node::null)
1568  break;
1569 
1570  stack.push(r);
1571  r = R(r);
1572  }
1573 
1574  return r;
1575  }
1576 
1577 } // end namespace Designar
1578 
1579 # endif // DSGBINTREE_H
PreorderIterator(const RankedTreap &t, int)
Definition: tree.H:575
bool is_sorted() const
Definition: tree.H:294
static TreapRkNode< Key > *const null
Definition: nodesdef.H:909
~RankedTreap()
Definition: tree.H:261
const Key & select(nat_t i) const
Definition: tree.H:504
Key ItemType
Definition: tree.H:206
Iterator end() const
Definition: tree.H:982
void for_each_postorder(Op &&op=Op())
Definition: tree.H:558
RankedTreap(Cmp &_cmp)
Definition: tree.H:229
void for_each_preorder(Op &op)
Definition: tree.H:528
void next()
Definition: tree.H:933
const Cmp & get_cmp() const
Definition: tree.H:314
Definition: tree.H:958
Key * search_or_insert(Key &&k)
Definition: tree.H:389
Key & select(nat_t i)
Definition: tree.H:496
Definition: iterator.H:92
std::tuple< RankedTreap, RankedTreap > split_pos(nat_t i)
Definition: tree.H:454
rng_seed_t & get_priority()
Definition: tree.H:76
PostorderIterator(const RankedTreap &t, int)
Definition: tree.H:838
Definition: setalgorithms.H:36
TreapRkNode(BinTreeNodeCtor ctor)
Definition: tree.H:65
Iterator begin()
Definition: tree.H:967
bool has_current() const
Definition: tree.H:643
void join_dup(RankedTreap &ts, RankedTreap &tg)
Definition: tree.H:489
InorderIterator(const RankedTreap &t)
Definition: tree.H:718
Definition: stack.H:190
Cmp CmpType
Definition: tree.H:211
Key * append(Key &&k)
Definition: tree.H:348
void swap(PreorderIterator &it)
Definition: tree.H:623
void for_each_inorder(Op &op)
Definition: tree.H:540
nat_t get_position() const
Definition: tree.H:638
Definition: iterator.H:36
Cmp & get_cmp()
Definition: tree.H:309
Key * append_dup(const Key &k)
Definition: tree.H:353
TreapRkNode< Key > Node
Definition: tree.H:106
const Key & max() const
Definition: tree.H:446
const Key & get_current() const
Definition: tree.H:656
PostorderIterator(PostorderIterator &&it)
Definition: tree.H:862
const Key & get_current() const
Definition: tree.H:925
Node * get_location() const
Definition: tree.H:844
void exclusive_join(RankedTreap &ts, RankedTreap &tg)
Definition: tree.H:483
std::mt19937_64 rng_t
Definition: types.H:52
BinTreeNode *& R(BinTreeNode *p)
Definition: nodesdef.H:993
Iterator end()
Definition: tree.H:977
Definition: nodesdef.H:902
void for_each_inorder(Op &&op=Op())
Definition: tree.H:546
Iterator begin() const
Definition: tree.H:972
Node * get_location() const
Definition: tree.H:581
Key & find(const Key &k)
Definition: tree.H:395
RankedTreap(rng_seed_t seed, Cmp &&_cmp=Cmp())
Definition: tree.H:241
bool is_empty() const
Definition: tree.H:289
Key * append_dup(Key &&k)
Definition: tree.H:358
PostorderIterator(const PostorderIterator &it)
Definition: tree.H:856
void swap(PostorderIterator &it)
Definition: tree.H:886
PostorderIterator(const RankedTreap &t)
Definition: tree.H:850
rng_seed_t & PRIOR(TreapNode *p)
Definition: tree.H:95
Key * insert_dup(const Key &k)
Definition: tree.H:331
void swap(InorderIterator &it)
Definition: tree.H:754
void next()
Definition: tree.H:801
RankedTreap(RankedTreap &&t)
Definition: tree.H:253
TreapRkNode()
Definition: tree.H:47
T pop()
Definition: stack.H:299
rng_t::result_type rng_seed_t
Definition: types.H:53
PreorderIterator(const PreorderIterator &it)
Definition: tree.H:593
typename GT::Node Node
Definition: graphutilities.H:78
Key * search_or_insert(const Key &k)
Definition: tree.H:383
void for_each_preorder(Op &&op=Op())
Definition: tree.H:534
void swap(RankedTreap &t)
Definition: tree.H:283
const Key & get_current() const
Definition: tree.H:793
BinTreeNode *& L(BinTreeNode *p)
Definition: nodesdef.H:987
TreapRkNode(Key &&k)
Definition: tree.H:59
void for_each_postorder(Op &op)
Definition: tree.H:552
long long lint_t
Definition: types.H:49
Key DataType
Definition: tree.H:208
RankedTreap(Cmp &&_cmp=Cmp())
Definition: tree.H:235
Key ValueType
Definition: tree.H:209
Key & get_current()
Definition: tree.H:917
bool is_empty() const
Definition: stack.H:240
Definition: containeralgorithms.H:33
Definition: tree.H:102
TreapRkNode(const Key &k)
Definition: tree.H:53
nat_t get_position() const
Definition: tree.H:775
void next()
Definition: tree.H:664
Key * insert(Key &&k)
Definition: tree.H:325
T & top()
Definition: stack.H:267
Key * append(const Key &k)
Definition: tree.H:343
Node * get_location() const
Definition: tree.H:712
Definition: tree.H:37
void reset()
Definition: tree.H:81
const Key * search(const Key &k) const
Definition: tree.H:373
void reset_last()
Definition: tree.H:900
BinTreeNode::KeyType & KEY(BinTreeNode *p)
Definition: nodesdef.H:981
const Key & find(const Key &k) const
Definition: tree.H:405
bool has_current() const
Definition: tree.H:912
BinTreeNodeCtor
Definition: nodesdef.H:897
Key remove_pos(nat_t i)
Definition: tree.H:427
nat_t get_position() const
Definition: tree.H:907
nat_t size() const
Definition: tree.H:299
nat_t SizeType
Definition: tree.H:210
const Key & min() const
Definition: tree.H:438
Definition: array.H:32
unsigned long int nat_t
Definition: types.H:50
PreorderIterator(const RankedTreap &t)
Definition: tree.H:587
InorderIterator(InorderIterator &&it)
Definition: tree.H:730
Key * search(const Key &k)
Definition: tree.H:363
bool verify() const
Definition: tree.H:213
Key * insert_dup(Key &&k)
Definition: tree.H:337
RankedTreap(const RankedTreap &t)
Definition: tree.H:247
std::tuple< RankedTreap, RankedTreap > split_key(const Key &k)
Definition: tree.H:465
Key KeyType
Definition: tree.H:207
void clear()
Definition: stack.H:245
Key & get_current()
Definition: tree.H:785
void clear()
Definition: tree.H:304
std::tuple< RankedTreap, RankedTreap > split_key_dup(const Key &k)
Definition: tree.H:475
lint_t position(const Key &k) const
Definition: tree.H:512
Key & get_current()
Definition: tree.H:648
InorderIterator(const InorderIterator &it)
Definition: tree.H:724
nat_t & COUNT(RkNode *p)
Definition: tree.H:89
nat_t & get_count()
Definition: tree.H:71
bool verify_dup() const
Definition: tree.H:218
void reset()
Definition: tree.H:762
void reset_last()
Definition: tree.H:768
InorderIterator(const RankedTreap &t, int)
Definition: tree.H:706
bool has_current() const
Definition: tree.H:780
RankedTreap(rng_seed_t seed, Cmp &_cmp)
Definition: tree.H:223
PreorderIterator(PreorderIterator &&it)
Definition: tree.H:599
T & push(const T &item)
Definition: stack.H:255
Key * insert(const Key &k)
Definition: tree.H:319
void reset()
Definition: tree.H:894
void reset()
Definition: tree.H:631