aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/dsa/SplayTree.h
blob: 78b54f6846517624a9d24f18cde622af79289dee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef   SplayTree_h__
#define   SplayTree_h__

#include "utility.h"

namespace meow{
  template<class Key, class Value>
  class SplayTree{
    private:
      struct Node{
        Key    _key;
        Key    _keyOffset;
        Value  _value;
        size_t _size;
        Node*  _parent;
        Node*  _lChild;
        Node*  _rChild;
        //
        Node(Key const& __key, Value const& __value);
      };
      //
      Node* _root;
      //
      void offsetAdd    (Node* __node, Key const& __delta) const;
      void offsetDown   (Node* __node                    ) const;
      void sizeUpdate   (Node* __node                    ) const;
      void connectLChild(Node* __parent, Node* __child   ) const;
      void connectRChild(Node* __parent, Node* __child   ) const;
      //
      Node* clear(Node* __node);
      Node* dup  (Node* __node);
      //
      void rotate(               Node* __parent, Node* __child) const;
      void rotate(Node* __grand, Node* __parent, Node* __child) const;
      Node* splay(Node* __node) const;
      //
      Node* findKey   (Node* __node, Key const& __key) const;
      Node* findMinMax(Node* __node, bool minimum    ) const;
      Node* findOrder (Node* __node, size_t __order  ) const;
      //
      void  split(Node* __root, Node** __left, Node** __right);
      Node* merge(              Node*  __left, Node*  __right);
      //
      void print(Node* __now, int __depth) const;
    public:
      class Element{
        private:
          typedef std::pair<Key const&, Value&> Entry;
          Entry* _entry;
          Node * _node;
          //
          void reset(Node* __node);
        public:
          Element();
          Element(Node* __node);
          Element(Element const& __iterator2);
          ~Element();
          //
          Element& operator=(Element const& __e2);
          //
          Entry* operator->();
          Entry& operator *();
          //
          bool operator==(Element const& __e2) const;
          bool operator!=(Element const& __e2) const;
      };
      //
      SplayTree();
      SplayTree(SplayTree const& __tree2);
      ~SplayTree();
      //
      SplayTree& operator=(SplayTree const& __tree2);
      void moveTo(SplayTree* __tree2);
      //
      Element  lowerBound(Key const& __key) const;
      Element  upperBound(Key const& __key) const;
      Element rLowerBound(Key const& __key) const;
      Element rUpperBound(Key const& __key) const;
      Element find (Key const& __key) const;
      Element order(size_t __order  ) const;
      Element first(                ) const;
      Element last (                ) const;
      Element   end(                ) const;
      //
      size_t  size() const;
      bool   empty() const;
      //
      void   clear();
      void   keyOffset(Key const& __delta);
      bool   insert      (Key const& __key, Value const& __value);
      bool   erase       (Key const& __key);
      Value& operator[](Key const& __key);
      void   splitOut(Key const& __upper_bound, SplayTree* __right);
      bool   mergeAfter(SplayTree* __tree2);
      bool   merge     (SplayTree* __tree2);
      //
      void print() const;
  };
}

#include "SplayTree.hpp"

#endif // SplayTree_h__