Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
SplayTree.h
Go to the documentation of this file.
1 #ifndef dsa_SplayTree_h__
2 #define dsa_SplayTree_h__
3 
4 #include <cstdlib>
5 #include <utility>
6 
7 #include "../math/utility.h"
8 
9 namespace meow {
10 
36 template<class Key, class Value>
37 class SplayTree {
38 private:
39  struct Node {
40  Key key_;
41  Key keyOffset_;
42  Value value_;
43  size_t size_;
44  Node* parent_;
45  Node* child_[2];
46 
47  Node(Key const& key, Value const& value):
48  key_(key), keyOffset_(0), value_(value) {
49  size_ = 1;
50  parent_ = NULL;
51  child_[0] = NULL;
52  child_[1] = NULL;
53  }
54  //
55  void keyOffset(Key const& delta) {
56  key_ = key_ + delta;
57  keyOffset_ = keyOffset_ + delta;
58  }
59  void syncDown() const {
60  for (size_t i = 0; i < 2; i++) {
61  if (child_[i] == NULL) continue;
62  child_[i]->keyOffset(keyOffset_);
63  }
64  ((Node*)this)->keyOffset_ = Key(0);
65  }
66  void syncUp() const {
67  ((Node*)this)->size_ = 1;
68  for (size_t i = 0; i < 2; i++) {
69  if (child_[i] == NULL) continue;
70  ((Node*)this)->size_ += child_[i]->size_;
71  }
72  }
73  };
74 
75  Node* root_;
76 
78  void connect(Node const* parent, size_t left_right, Node const* child) const {
79  Node* p = (Node*)parent;
80  Node* c = (Node*)child;
81  if (p != NULL) p->child_[left_right] = c;
82  if (c != NULL) c->parent_ = p;
83  }
84 
86  Node const* splay(Node const* node) const {
87  if (node != NULL && node->parent_ != NULL) {
88  for (const Node *g_grand, *grand, *parent, *child = node; ; ) {
89  g_grand = (grand = parent = child->parent_)->parent_;
90  size_t pc = (parent->child_[0] == child ? 0 : 1);
91  connect(parent, pc, child->child_[!pc]);
92  connect(child , !pc, parent);
93  if (g_grand != NULL) {
94  g_grand = (grand = g_grand)->parent_;
95  size_t gp = (grand->child_[0] == parent ? 0 : 1);
96  Node const* who = (pc == gp ? parent : child);
97  connect(grand, gp, who->child_[!gp]);
98  connect(who , !gp, grand);
99  grand->syncUp();
100  }
101  parent->syncUp();
102  child ->syncUp();
103  if (g_grand == NULL) {
104  connect(NULL, 0, child);
105  break;
106  }
107  connect(g_grand, (g_grand->child_[0] == grand ? 0 : 1), child);
108  }
109  }
110  return (((SplayTree*)this)->root_ = (Node*)node);
111  }
112 
113  void clear(Node* node) {
114  if (node == NULL) return ;
115  clear(node->child_[0]);
116  clear(node->child_[1]);
117  delete node;
118  }
119 
120  Node* dup(Node* node2) {
121  if (node2 == NULL) return NULL;
122  node2->syncDown();
123  Node* node = new Node(node2->key_, node2->value_);
124  connect(node, 0, dup(node2->child_[0]));
125  connect(node, 1, dup(node2->child_[1]));
126  node->syncUp();
127  return node;
128  }
129 
130  Node const* findKey(Node const* node, Key const& key) const {
131  Node const* ret = node;
132  while (node != NULL) {
133  node->syncDown();
134  ret = node;
135  if (!(key < node->key_)) {
136  if (!(node->key_< key)) break;
137  node = node->child_[1];
138  }
139  else {
140  node = node->child_[0];
141  }
142  }
143  return ret;
144  }
145  Node const* findMinMax(Node const* node, bool minimum) const {
146  Node const* ret = node;
147  for (int i = minimum ? 0 : 1; node != NULL; node = node->child_[i]) {
148  node->syncDown();
149  ret = node;
150  }
151  return ret;
152  }
153  Node const* findOrder(Node const* node, size_t order) const {
154  Node const* ret = node;
155  while (node != NULL) {
156  node->syncDown();
157  ret = node;
158  size_t ord = 1 + (node->child_[0] == NULL ? 0 : node->child_[0]->size_);
159  if (ord == order) return ret;
160  else if(ord < order){ node = node->child_[1]; order -= ord; }
161  else { node = node->child_[0]; }
162  }
163  return ret;
164  }
165 
166  void split(Node* root, Node** left, Node** right) {
167  if (root == NULL) { *left = NULL; *right = NULL; return ; }
168  root->syncDown();
169  *left = root;
170  *right = root->child_[1];
171  if (*right != NULL) {
172  (*left )->child_[1] = NULL;
173  (*right)->parent_ = NULL;
174  (*left )->syncUp();
175  }
176  }
177  Node* merge(Node* left, Node* right) {
178  if (left == NULL) return right;
179  if (right == NULL) return left ;
180  left->syncDown();
181  connect(left, 1, right);
182  left->syncUp();
183  return left;
184  }
185 public:
191  class Element{
192  private:
193  typedef std::pair<Key const&, Value&> Entry;
194  Entry* entry_;
195  Node * node_;
196  //
197  void reset(Node* node) {
198  node_ = node;
199  delete entry_;
200  entry_ = (node == NULL ? NULL : new Entry(node->key_, node->value_));
201  }
202  public:
203  Element(): entry_(NULL), node_(NULL) {
204  }
205  Element(Node* node): entry_(NULL), node_(NULL) {
206  reset(node);
207  }
208  Element(Element const& element2): entry_(NULL), node_(NULL) {
209  reset(element2.node_);
210  }
212  delete entry_;
213  }
214 
216  Element& copyFrom(Element const& e) {
217  reset(e.node_);
218  return *this;
219  }
220 
222  bool same(Element const& e2) const {
223  return (node_ == e2.node_);
224  }
225 
227  Element& operator=(Element const& e2) {
228  return copyFrom(e2);
229  }
230 
232  Entry* operator->() {
233  return entry_;
234  }
235 
237  Entry& operator*() {
238  return *entry_;
239  }
240 
242  bool operator==(Element const& e2) const{
243  return same(e2);
244  }
245 
247  bool operator!=(Element const& e2) const{
248  return !same(e2);
249  }
250  };
251 
253  SplayTree(): root_(NULL) {
254  }
255 
257  SplayTree(SplayTree const& tree2):
258  root_(dup((Node*)(tree2.root_))) {
259  }
260 
263  clear(root_);
264  }
265 
269  SplayTree& copyFrom(SplayTree const& tree2) {
270  clear(root_);
271  root_ = dup((Node*)(tree2.root_));
272  return *this;
273  }
274 
278  void moveTo(SplayTree* tree2) {
279  tree2->clear();
280  tree2->root_ = root_;
281  root_ = NULL;
282  }
283 
289  Element lowerBound(Key const& key) const {
290  splay(findKey(root_, key));
291  if (root_ == NULL || !(root_->key_ < key)) return Element(root_);
292  if (root_->child_[1] == NULL) return Element(NULL);
293  splay(findMinMax(root_->child_[1], true));
294  return Element(root_);
295  }
296 
302  Element upperBound(Key const& key) const {
303  splay(findKey(root_, key));
304  if (root_ == NULL || key < root_->key_) return Element(root_);
305  if (root_->child_[1] == NULL) return Element(NULL);
306  splay(findMinMax(root_->child_[1], true));
307  return Element(root_);
308  }
309 
315  Element rLowerBound(Key const& key) const {
316  splay(findKey(root_, key));
317  if (root_ == NULL || !(key < root_->key_)) return Element(root_);
318  if (root_->child_[0] == NULL) return Element(NULL);
319  splay(findMinMax(root_->child_[0], false));
320  return Element(root_);
321  }
322 
328  Element rUpperBound(Key const& key) const {
329  splay(findKey(root_, key));
330  if (root_ == NULL || root_->key_ < key) return Element(root_);
331  if (root_->child_[0] == NULL) return Element(NULL);
332  splay(findMinMax(root_->child_[0], false));
333  return Element(root_);
334  }
335 
339  Element find(Key const& key) const {
340  splay(findKey(root_, key));
341  if (root_ != NULL && !(key < root_->key_) && !(root_->key_ < key)) {
342  return Element(root_);
343  }
344  return Element(NULL);
345  }
346 
352  Element order(size_t order) const {
353  if (root_ == NULL || order >= root_->size_) return Element(NULL);
354  splay(findOrder(root_, order + 1));
355  return Element(root_);
356  }
357 
361  Element first() const {
362  splay(findMinMax(root_, true));
363  return Element(root_);
364  }
365 
369  Element last() const {
370  splay(findMinMax(root_, false));
371  return Element(root_);
372  }
373 
379  Element end() const {
380  return Element(NULL);
381  }
382 
386  size_t size() const {
387  return (root_ == NULL ? 0 : root_->size_);
388  }
389 
393  bool empty() const{
394  return (size() == 0);
395  }
396 
400  void clear() {
401  clear(root_);
402  root_ = NULL;
403  }
404 
411  bool insert(Key const& key, Value const& value) {
412  if (root_ == NULL) {
413  root_ = new Node(key, value);
414  }
415  else {
416  Node* parent = (Node*)findKey(root_, key);
417  if (!(parent->key_ < key) && !(key < parent->key_)) {
418  splay(parent);
419  return false;
420  }
421  Node* new_node = new Node(key, value);
422  connect(parent, (parent->key_ < key ? 1 : 0), new_node);
423  parent->syncUp();
424  splay(new_node);
425  }
426  return true;
427  }
428 
435  bool erase(Key const& key) {
436  if (root_ == NULL) return false;
437  Node* body = (Node*)findKey(root_, key);
438  if (body->key_ < key || key < body->key_) {
439  splay(body);
440  return false;
441  }
442  Node* ghost;
443  if (body->child_[1] == NULL) {
444  ghost = body->child_[0];
445  if (ghost != NULL) ghost->syncDown();
446  }
447  else {
448  ghost = (Node*)findMinMax(body->child_[1], true);
449  connect(ghost, 0, body->child_[0]);
450  if (ghost != body->child_[1]) {
451  connect(ghost->parent_, 0, ghost->child_[1]);
452  connect(ghost, 1, body->child_[1]);
453  for (Node* a = ghost->parent_; a != ghost; a = a->parent_)
454  a->syncUp();
455  }
456  ghost->syncUp();
457  }
458  Node* parent = body->parent_;
459  connect(parent, parent != NULL && parent->child_[0] == body ? 0 : 1, ghost);
460  delete body;
461  splay(ghost != NULL ? ghost : parent);
462  return true;
463  }
464 
468  void keyOffset(Key const& delta) {
469  if (root_ != NULL) {
470  root_->keyOffset(delta);
471  }
472  }
473 
477  void splitOut(Key const& upper_bound, SplayTree* right) {
478  right->clear();
479  if (rLowerBound(upper_bound) != end()) {
480  split(root_, &root_, &(right->root_));
481  }
482  else {
483  right->root_ = root_;
484  root_ = NULL;
485  }
486  }
487 
494  bool mergeAfter(SplayTree* tree2) {
495  if (root_ == NULL || tree2->root_ == NULL ||
496  last()->first < tree2->first()->first) {
497  root_ = merge(root_, tree2->root_);
498  tree2->root_ = NULL;
499  return true;
500  }
501  return false;
502  }
503 
511  bool merge(SplayTree* tree2) {
512  if (root_ == NULL || tree2->root_ == NULL ||
513  last()->first < tree2->first()->first) {
514  root_ = merge(root_, tree2->root_);
515  }
516  else if(tree2->last()->first < first()->first) {
517  root_ = merge(tree2->root_, root_);
518  }
519  else {
520  return false;
521  }
522  tree2->root_ = NULL;
523  return true;
524  }
525 
532  Value& operator[](Key const& key) {
533  if (find(key) == end()) insert(key, Value());
534  return root_->value_;
535  }
536 
538  SplayTree& operator=(SplayTree const& tree2) {
539  return copyFrom(tree2);
540  }
541 };
542 
568 template<class Key, class Value>
570 private:
571  struct Node {
572  Value valueOffset_;
573  Value range_;
574  Key key_;
575  Key keyOffset_;
576  Value value_;
577  bool same_;
578  size_t size_;
579  Node* parent_;
580  Node* child_[2];
581 
582  Node(Key const& key, Value const& value):
583  valueOffset_(0), range_(value),
584  key_(key), keyOffset_(0), value_(value) {
585  same_ = false;
586  size_ = 1;
587  parent_ = NULL;
588  child_[0] = NULL;
589  child_[1] = NULL;
590  }
591  //
592  void keyOffset(Key const& delta) {
593  key_ = key_ + delta;
594  keyOffset_ = keyOffset_ + delta;
595  }
596  void valueUpdate(Value const& delta, bool over) {
597  if(over) {
598  value_ = delta * size_;
599  valueOffset_ = delta;
600  range_ = delta * size_;
601  same_ = true;
602  }
603  else {
604  value_ = value_ + delta * size_;
605  valueOffset_ = valueOffset_ + delta;
606  range_ = range_ + delta * size_;
607  }
608  }
609  void syncDown() const {
610  for (size_t i = 0; i < 2; i++) {
611  if (child_[i] == NULL) continue;
612  child_[i]->keyOffset(keyOffset_);
613  child_[i]->valueUpdate(valueOffset_, same_);
614  }
615  ((Node*)this)->keyOffset_ = Key(0);
616  ((Node*)this)->valueOffset_ = Value(0);
617  ((Node*)this)->same_ = false;
618  }
619  void syncUp() const {
620  ((Node*)this)->size_ = 1;
621  Value* v[3] = {&(((Node*)this)->value_), NULL, NULL};
622  size_t vct = 1;
623  for (size_t i = 0; i < 2; i++) {
624  if (child_[i] == NULL) continue;
625  ((Node*)this)->size_ += child_[i]->size_;
626  v[vct++] = &(child_[i]->range_);
627  }
628  if (vct == 1) ((Node*)this)->range_ = (*v[0]);
629  else if(vct == 2) ((Node*)this)->range_ = (*v[0]) | (*v[1]);
630  else ((Node*)this)->range_ = (*v[0]) | (*v[1]) | (*v[2]);
631  }
632  };
633 
634  Node* root_;
635 
637  void connect(Node const* parent, size_t left_right, Node const* child) const {
638  Node* p = (Node*)parent;
639  Node* c = (Node*)child;
640  if (p != NULL) p->child_[left_right] = c;
641  if (c != NULL) c->parent_ = p;
642  }
643 
645  Node const* splay(Node const* node) const {
646  if (node != NULL && node->parent_ != NULL) {
647  for (const Node *g_grand, *grand, *parent, *child = node; ; ) {
648  g_grand = (grand = parent = child->parent_)->parent_;
649  size_t pc = (parent->child_[0] == child ? 0 : 1);
650  connect(parent, pc, child->child_[!pc]);
651  connect(child , !pc, parent);
652  if (g_grand != NULL) {
653  g_grand = (grand = g_grand)->parent_;
654  size_t gp = (grand->child_[0] == parent ? 0 : 1);
655  Node const* who = (pc == gp ? parent : child);
656  connect(grand, gp, who->child_[!gp]);
657  connect(who , !gp, grand);
658  grand->syncUp();
659  }
660  parent->syncUp();
661  child ->syncUp();
662  if (g_grand == NULL) {
663  connect(NULL, 0, child);
664  break;
665  }
666  connect(g_grand, (g_grand->child_[0] == grand ? 0 : 1), child);
667  }
668  }
669  return (((SplayTree_Range*)this)->root_ = (Node*)node);
670  }
671 
672  void clear(Node* node) {
673  if (node == NULL) return ;
674  clear(node->child_[0]);
675  clear(node->child_[1]);
676  delete node;
677  }
678 
679  Node* dup(Node* node2) {
680  if (node2 == NULL) return NULL;
681  node2->syncDown();
682  Node* node = new Node(node2->key_, node2->value_);
683  connect(node, 0, dup(node2->child_[0]));
684  connect(node, 1, dup(node2->child_[1]));
685  node->syncUp();
686  return node;
687  }
688 
689  Node const* findKey(Node const* node, Key const& key) const {
690  Node const* ret = node;
691  while (node != NULL) {
692  node->syncDown();
693  ret = node;
694  if (!(key < node->key_)) {
695  if (!(node->key_< key)) break;
696  node = node->child_[1];
697  }
698  else {
699  node = node->child_[0];
700  }
701  }
702  return ret;
703  }
704  Node const* findMinMax(Node const* node, bool minimum) const {
705  Node const* ret = node;
706  for (int i = minimum ? 0 : 1; node != NULL; node = node->child_[i]) {
707  node->syncDown();
708  ret = node;
709  }
710  return ret;
711  }
712  Node const* findOrder(Node const* node, size_t order) const {
713  Node const* ret = node;
714  while (node != NULL) {
715  node->syncDown();
716  ret = node;
717  size_t ord = 1 + (node->child_[0] == NULL ? 0 : node->child_[0]->size_);
718  if (ord == order) return ret;
719  else if(ord < order){ node = node->child_[1]; order -= ord; }
720  else { node = node->child_[0]; }
721  }
722  return ret;
723  }
724 
725  void split(Node* root, Node** left, Node** right) {
726  if (root == NULL) { *left = NULL; *right = NULL; return ; }
727  root->syncDown();
728  *left = root;
729  *right = root->child_[1];
730  if (*right != NULL) {
731  (*left )->child_[1] = NULL;
732  (*right)->parent_ = NULL;
733  (*left )->syncUp();
734  }
735  }
736  Node* merge(Node* left, Node* right) {
737  if (left == NULL) return right;
738  if (right == NULL) return left ;
739  left->syncDown();
740  connect(left, 1, right);
741  left->syncUp();
742  return left;
743  }
744 public:
750  class Element{
751  private:
752  typedef std::pair<Key const&, Value&> Entry;
753  Entry* entry_;
754  Node * node_;
755  //
756  void reset(Node* node) {
757  node_ = node;
758  delete entry_;
759  entry_ = (node == NULL ? NULL : new Entry(node->key_, node->value_));
760  }
761  public:
762  Element(): entry_(NULL), node_(NULL) {
763  }
764  Element(Node* node): entry_(NULL), node_(NULL) {
765  reset(node);
766  }
767  Element(Element const& element2): entry_(NULL), node_(NULL) {
768  reset(element2.node_);
769  }
771  delete entry_;
772  }
773 
775  Element& copyFrom(Element const& e) {
776  reset(e.node_);
777  return *this;
778  }
779 
781  bool same(Element const& e2) const {
782  return (node_ == e2.node_);
783  }
784 
786  Element& operator=(Element const& e2) {
787  return copyFrom(e2);
788  }
789 
791  Entry* operator->() {
792  return entry_;
793  }
794 
796  Entry& operator*() {
797  return *entry_;
798  }
799 
801  bool operator==(Element const& e2) const{
802  return same(e2);
803  }
804 
806  bool operator!=(Element const& e2) const{
807  return !same(e2);
808  }
809  };
810 
812  SplayTree_Range(): root_(NULL) {
813  }
814 
817  root_(dup((Node*)(tree2.root_))) {
818  }
819 
822  clear(root_);
823  }
824 
829  clear(root_);
830  root_ = dup((Node*)(tree2.root_));
831  return *this;
832  }
833 
837  void moveTo(SplayTree_Range* tree2) {
838  tree2->clear();
839  tree2->root_ = root_;
840  root_ = NULL;
841  }
842 
848  Element lowerBound(Key const& key) const {
849  splay(findKey(root_, key));
850  if (root_ == NULL || !(root_->key_ < key)) return Element(root_);
851  if (root_->child_[1] == NULL) return Element(NULL);
852  splay(findMinMax(root_->child_[1], true));
853  return Element(root_);
854  }
855 
861  Element upperBound(Key const& key) const {
862  splay(findKey(root_, key));
863  if (root_ == NULL || key < root_->key_) return Element(root_);
864  if (root_->child_[1] == NULL) return Element(NULL);
865  splay(findMinMax(root_->child_[1], true));
866  return Element(root_);
867  }
868 
874  Element rLowerBound(Key const& key) const {
875  splay(findKey(root_, key));
876  if (root_ == NULL || !(key < root_->key_)) return Element(root_);
877  if (root_->child_[0] == NULL) return Element(NULL);
878  splay(findMinMax(root_->child_[0], false));
879  return Element(root_);
880  }
881 
887  Element rUpperBound(Key const& key) const {
888  splay(findKey(root_, key));
889  if (root_ == NULL || root_->key_ < key) return Element(root_);
890  if (root_->child_[0] == NULL) return Element(NULL);
891  splay(findMinMax(root_->child_[0], false));
892  return Element(root_);
893  }
894 
898  Element find(Key const& key) const {
899  splay(findKey(root_, key));
900  if (root_ != NULL && !(key < root_->key_) && !(root_->key_ < key)) {
901  return Element(root_);
902  }
903  return Element(NULL);
904  }
905 
911  Element order(size_t order) const {
912  if (root_ == NULL || order >= root_->size_) return Element(NULL);
913  splay(findOrder(root_, order + 1));
914  return Element(root_);
915  }
916 
920  Element first() const {
921  splay(findMinMax(root_, true));
922  return Element(root_);
923  }
924 
928  Element last() const {
929  splay(findMinMax(root_, false));
930  return Element(root_);
931  }
932 
938  Element end() const {
939  return Element(NULL);
940  }
941 
945  size_t size() const {
946  return (root_ == NULL ? 0 : root_->size_);
947  }
948 
952  bool empty() const{
953  return (size() == 0);
954  }
955 
961  Value query() const {
962  if (root_ == NULL) return Value(0);
963  return root_->range_;
964  }
965 
971  Value query(Key const& first, Key const& last) const {
972  SplayTree_Range* self = (SplayTree_Range*)this;
973  Node* tmp;
974  rUpperBound(first);
975  self->split(self->root_, &tmp, &(self->root_));
976  upperBound(last);
977  Value ret(0);
978  if (root_ != NULL && root_->child_[0] != NULL) {
979  ret = root_->child_[0]->range_;
980  }
981  self->root_ = self->merge(tmp, self->root_);
982  return ret;
983  }
984 
988  void clear() {
989  clear(root_);
990  root_ = NULL;
991  }
992 
999  bool insert(Key const& key, Value const& value) {
1000  if (root_ == NULL) {
1001  root_ = new Node(key, value);
1002  }
1003  else {
1004  Node* parent = (Node*)findKey(root_, key);
1005  if (!(parent->key_ < key) && !(key < parent->key_)) {
1006  splay(parent);
1007  return false;
1008  }
1009  Node* new_node = new Node(key, value);
1010  connect(parent, (parent->key_ < key ? 1 : 0), new_node);
1011  parent->syncUp();
1012  splay(new_node);
1013  }
1014  return true;
1015  }
1016 
1023  bool erase(Key const& key) {
1024  if (root_ == NULL) return false;
1025  Node* body = (Node*)findKey(root_, key);
1026  if (body->key_ < key || key < body->key_) {
1027  splay(body);
1028  return false;
1029  }
1030  Node* ghost;
1031  if (body->child_[1] == NULL) {
1032  ghost = body->child_[0];
1033  if (ghost != NULL) ghost->syncDown();
1034  }
1035  else {
1036  ghost = (Node*)findMinMax(body->child_[1], true);
1037  connect(ghost, 0, body->child_[0]);
1038  if (ghost != body->child_[1]) {
1039  connect(ghost->parent_, 0, ghost->child_[1]);
1040  connect(ghost, 1, body->child_[1]);
1041  for (Node* a = ghost->parent_; a != ghost; a = a->parent_)
1042  a->syncUp();
1043  }
1044  ghost->syncUp();
1045  }
1046  Node* parent = body->parent_;
1047  connect(parent, parent != NULL && parent->child_[0] == body ? 0 : 1, ghost);
1048  delete body;
1049  splay(ghost != NULL ? ghost : parent);
1050  return true;
1051  }
1052 
1056  void keyOffset(Key const& delta) {
1057  if (root_ != NULL) {
1058  root_->keyOffset(delta);
1059  }
1060  }
1061 
1065  void valueOffset(Value const& delta){
1066  if (root_ != NULL) {
1067  root_->valueUpdate(delta, false);
1068  }
1069  }
1070 
1074  void valueOverride(Value const& value){
1075  if(root_ != NULL){
1076  root_->valueUpdate(value, true);
1077  }
1078  }
1079 
1083  void splitOut(Key const& upper_bound, SplayTree_Range* right) {
1084  right->clear();
1085  if (rLowerBound(upper_bound) != end()) {
1086  split(root_, &root_, &(right->root_));
1087  }
1088  else {
1089  right->root_ = root_;
1090  root_ = NULL;
1091  }
1092  }
1093 
1101  if (root_ == NULL || tree2->root_ == NULL ||
1102  last()->first < tree2->first()->first) {
1103  root_ = merge(root_, tree2->root_);
1104  tree2->root_ = NULL;
1105  return true;
1106  }
1107  return false;
1108  }
1109 
1117  bool merge(SplayTree_Range* tree2) {
1118  if (root_ == NULL || tree2->root_ == NULL ||
1119  last()->first < tree2->first()->first) {
1120  root_ = merge(root_, tree2->root_);
1121  }
1122  else if(tree2->last()->first < first()->first) {
1123  root_ = merge(tree2->root_, root_);
1124  }
1125  else {
1126  return false;
1127  }
1128  tree2->root_ = NULL;
1129  return true;
1130  }
1131 
1138  Value& operator[](Key const& key) {
1139  if (find(key) == end()) insert(key, Value());
1140  return root_->value_;
1141  }
1142 
1145  return copyFrom(tree2);
1146  }
1147 };
1148 
1149 }
1150 
1151 #endif // dsa_SplayTree_h__
bool merge(SplayTree *tree2)
合併
Definition: SplayTree.h:511
bool mergeAfter(SplayTree *tree2)
合併
Definition: SplayTree.h:494
Element upperBound(Key const &key) const
找出第一個(最小的) Element且 k < 它的 Key, 並且回傳之.
Definition: SplayTree.h:861
bool operator==(Element const &e2) const
same as same(e2)
Definition: SplayTree.h:801
Element & operator=(Element const &e2)
same as copyFrom
Definition: SplayTree.h:227
Element order(size_t order) const
將Elements依照Key由小到大排序, 回傳第 ord 個Element (由0算起).
Definition: SplayTree.h:911
Element lowerBound(Key const &key) const
找出第一個(最小的) Element且 k <= 它的 Key, 並且回傳之.
Definition: SplayTree.h:289
Entry & operator*()
重導至std::pair<Key const&,Value&>&
Definition: SplayTree.h:237
~SplayTree()
destructor
Definition: SplayTree.h:262
size_t size() const
回傳資料個數
Definition: SplayTree.h:945
Entry * operator->()
重導至std::pair<Key const&,Value&>*
Definition: SplayTree.h:791
Element lowerBound(Key const &key) const
找出第一個(最小的) Element且 k <= 它的 Key, 並且回傳之.
Definition: SplayTree.h:848
bool insert(Key const &key, Value const &value)
插入一組(Key —> Value)
Definition: SplayTree.h:999
SplayTree & copyFrom(SplayTree const &tree2)
複製資料
Definition: SplayTree.h:269
Element order(size_t order) const
將Elements依照Key由小到大排序, 回傳第 ord 個Element (由0算起).
Definition: SplayTree.h:352
void clear()
清空
Definition: SplayTree.h:988
bool same(Element const &e2) const
比對兩者是否為指向同一個Entry
Definition: SplayTree.h:222
Element find(Key const &key) const
找出 Key= k 的Elemenet 並回傳. 找不到的話回傳 this->end()
Definition: SplayTree.h:898
bool same(Element const &e2) const
比對兩者是否為指向同一個Entry
Definition: SplayTree.h:781
Value query(Key const &first, Key const &last) const
查找
Definition: SplayTree.h:971
Element first() const
回傳Key最小的Element, 如果SplayTree為空, 則回傳 this->end()
Definition: SplayTree.h:920
void splitOut(Key const &upper_bound, SplayTree *right)
將tree2 清空, 再將所有Key > upper_bound 的Element都丟過去
Definition: SplayTree.h:477
Element(Element const &element2)
Definition: SplayTree.h:208
Element find(Key const &key) const
找出 Key= k 的Elemenet 並回傳. 找不到的話回傳 this->end()
Definition: SplayTree.h:339
void clear()
清空
Definition: SplayTree.h:400
bool operator!=(Element const &e2) const
same as !same(e2)
Definition: SplayTree.h:806
bool merge(SplayTree_Range *tree2)
合併
Definition: SplayTree.h:1117
Entry * operator->()
重導至std::pair<Key const&,Value&>*
Definition: SplayTree.h:232
類似 stl 的 iterator ,不過這邊叫做Element
Definition: SplayTree.h:191
基本上跟SplayTree一樣, 不過這邊結合線段樹, 多了區間操作 (線段樹相關operator定義請見 SegmentTree )
Definition: SplayTree.h:569
SplayTree_Range()
constructor
Definition: SplayTree.h:812
bool empty() const
回傳是否為空
Definition: SplayTree.h:393
SplayTree_Range & operator=(SplayTree_Range const &tree2)
same as copyFrom(tree2)
Definition: SplayTree.h:1144
Value & operator[](Key const &key)
就像stl::map::operator[]
Definition: SplayTree.h:1138
bool mergeAfter(SplayTree_Range *tree2)
合併
Definition: SplayTree.h:1100
Element upperBound(Key const &key) const
找出第一個(最小的) Element且 k < 它的 Key, 並且回傳之.
Definition: SplayTree.h:302
Element end() const
回傳一個指向NULL的Element,
Definition: SplayTree.h:938
void splitOut(Key const &upper_bound, SplayTree_Range *right)
將tree2 清空, 再將所有Key > upper_bound 的Element都丟過去
Definition: SplayTree.h:1083
bool empty() const
回傳是否為空
Definition: SplayTree.h:952
Element last() const
回傳Key最大的Element, 如果SplayTree為空, 則回傳 this->end()
Definition: SplayTree.h:928
SplayTree_Range & copyFrom(SplayTree_Range const &tree2)
複製資料
Definition: SplayTree.h:828
是一種神乎其技的資料結構, 維護一堆 Key->Value . 並且支援 一些 std::map 難以快速實踐的操作, 如 split , merge , keyOffset ...
Definition: SplayTree.h:37
bool erase(Key const &key)
刪除一組資料
Definition: SplayTree.h:435
bool operator==(Element const &e2) const
same as same(e2)
Definition: SplayTree.h:242
Element & operator=(Element const &e2)
same as copyFrom
Definition: SplayTree.h:786
bool operator!=(Element const &e2) const
same as !same(e2)
Definition: SplayTree.h:247
void moveTo(SplayTree *tree2)
將資料都丟到 tree2 身上, 並且清空自己
Definition: SplayTree.h:278
size_t size() const
回傳資料個數
Definition: SplayTree.h:386
類似 stl 的 iterator ,不過這邊叫做Element
Definition: SplayTree.h:750
SplayTree_Range(SplayTree_Range const &tree2)
constructor, 複製資料
Definition: SplayTree.h:816
Entry & operator*()
重導至std::pair<Key const&,Value&>&
Definition: SplayTree.h:796
void valueOffset(Value const &delta)
將所有Element的Value同加上 delta
Definition: SplayTree.h:1065
SplayTree(SplayTree const &tree2)
constructor, 複製資料
Definition: SplayTree.h:257
SplayTree()
constructor
Definition: SplayTree.h:253
Value & operator[](Key const &key)
就像stl::map::operator[]
Definition: SplayTree.h:532
void keyOffset(Key const &delta)
將所有Element的Key同加上 delta
Definition: SplayTree.h:468
void keyOffset(Key const &delta)
將所有Element的Key同加上 delta
Definition: SplayTree.h:1056
Element(Element const &element2)
Definition: SplayTree.h:767
void valueOverride(Value const &value)
將所有Element的Value全部設定成value
Definition: SplayTree.h:1074
Element first() const
回傳Key最小的Element, 如果SplayTree為空, 則回傳 this->end()
Definition: SplayTree.h:361
Element end() const
回傳一個指向NULL的Element,
Definition: SplayTree.h:379
bool insert(Key const &key, Value const &value)
插入一組(Key —> Value)
Definition: SplayTree.h:411
Element rLowerBound(Key const &key) const
找出第一個(最小的) Element且 k >= 它的 Key, 並且回傳之.
Definition: SplayTree.h:874
~SplayTree_Range()
destructor
Definition: SplayTree.h:821
Element last() const
回傳Key最大的Element, 如果SplayTree為空, 則回傳 this->end()
Definition: SplayTree.h:369
bool erase(Key const &key)
刪除一組資料
Definition: SplayTree.h:1023
Element rUpperBound(Key const &key) const
找出第一個(最小的) Element且 k > 它的 Key, 並且回傳之.
Definition: SplayTree.h:328
void moveTo(SplayTree_Range *tree2)
將資料都丟到 tree2 身上, 並且清空自己
Definition: SplayTree.h:837
SplayTree & operator=(SplayTree const &tree2)
same as copyFrom(tree2)
Definition: SplayTree.h:538
Element rUpperBound(Key const &key) const
找出第一個(最小的) Element且 k > 它的 Key, 並且回傳之.
Definition: SplayTree.h:887
Element & copyFrom(Element const &e)
複製資料
Definition: SplayTree.h:775
Value query() const
查找
Definition: SplayTree.h:961
Element rLowerBound(Key const &key) const
找出第一個(最小的) Element且 k >= 它的 Key, 並且回傳之.
Definition: SplayTree.h:315
Element & copyFrom(Element const &e)
複製資料
Definition: SplayTree.h:216