21 #ifndef ELM_AVL_GENTREE_H
22 #define ELM_AVL_GENTREE_H
24 #include <elm/utility.h>
25 #include <elm/PreIterator.h>
26 #include <elm/adapter.h>
27 #include <elm/util/array.h>
28 #include <elm/genstruct/Vector.h>
30 namespace elm {
namespace avl {
45 void remove(
Node *pa[],
unsigned char da[],
int k,
Node *p);
46 int count(
void)
const;
53 template <
class T,
class K = IdAdapter<T>,
class C = elm::Comparator<
typename K::key_t> >
55 static const int MAX_HEIGHT = 32;
67 for(
int i = 0; i < t; i++) out <<
" ";
75 Node *
find(
const typename K::key_t& key)
const {
77 int cmp = C::compare(key, K::key(p->data));
97 inline T *
get(
const typename K::key_t& key)
98 {
Node *node =
find(key);
if(!node)
return 0;
else return &node->data; }
99 inline const T *
get(
const typename K::key_t& key)
const
100 {
const Node *node =
find(key);
if(!node)
return 0;
else return &node->data; }
104 inline bool contains(
const typename K::key_t& item)
const {
return find(item) != 0; }
106 inline operator bool(
void)
const {
return !
isEmpty(); }
108 template <
template <
class _>
class Co>
110 {
for(
typename Co<T>::Iterator iter(coll); iter; iter++)
111 if(!
contains(iter))
return false;
return true; }
119 {
array::copy(stack, iter.stack, iter.sp - iter.stack); }
120 inline bool ended(
void)
const {
return sp == stack; }
125 if(last == top()->
left()) { push(top());
break; }
126 else if(last == top() && top()->
right()) { visit(top()->
right());
break; }
129 inline const T&
item(
void)
const {
return top()->
data; }
135 inline void push(
Node *node) { *sp++ = node; }
136 inline Node *pop(
void) {
return *--sp; }
137 inline Node *top(
void)
const {
return sp[-1]; }
138 void visit(Node *node) {
141 while(top()->
left()) push(top()->
left());
144 Node *stack[MAX_HEIGHT + 1], **sp;
149 Node *stack[MAX_HEIGHT + 1];
155 if(node->left()) *sp++ = node->left();
156 if(node->right()) *sp++ = node->right();
180 unsigned char da[MAX_HEIGHT];
186 unsigned char dir = 0;
187 for(q = z, p = y; p != 0; q = p, p = p->succ(dir)) {
188 int cmp = C::compare(K::key(item), K::key(p->data));
189 if(p->balance != 0) {
204 unsigned char da[MAX_HEIGHT];
210 unsigned char dir = 0;
211 for(q = z, p = y; p != 0; q = p, p = p->succ(dir)) {
212 int cmp = C::compare(K::key(item), K::key(p->data));
217 if(p->balance != 0) {
231 void remove(
const typename K::key_t& item) {
233 unsigned char da[MAX_HEIGHT];
239 for(
int cmp = C::compare(item, K::key(p->data));
241 cmp = C::compare(item, K::key(p->data))) {
246 ASSERTP(!p,
"removed item not in the tree");
255 inline void remove(
const Iterator& iter) {
remove(iter.item()); }
256 template <
class CC>
inline void addAll(
const CC& coll)
257 {
for(
typename CC::Iterator iter(coll); iter; iter++)
add(iter); }
258 template <
class CC>
inline void removeAll(
const CC& coll)
259 {
for(
typename CC::Iterator iter(coll); iter; iter++)
remove(iter); }
263 for(; ai && bi; ai++, bi++)
264 if(!(C::compare(*ai, *bi) == 0))
274 #endif // ELM_AVL_GENTREE_H
Iterator(const Iterator &iter)
Definition: GenTree.h:118
void copy(T *target, const T *source, int size)
Definition: array.h:59
void clear(void)
Definition: GenTree.h:148
Definition: PreIterator.h:29
Node(const Node *node)
Definition: GenTree.h:60
Node * left(void) const
Definition: GenTree.h:62
Node * root
Definition: GenTree.h:48
int count(void) const
Definition: GenTree.h:103
int cnt
Definition: GenTree.h:49
Pair< T1, T2 > pair(const T1 &v1, const T2 &v2)
Definition: Pair.h:41
void next(void)
Definition: GenTree.h:121
void insert(unsigned char da[], int dir, Node *node, Node *q, Node *y, Node *z)
Definition: avl_GenTree.cpp:31
T data
Definition: GenTree.h:61
~GenTree(void)
Definition: GenTree.h:92
IntFormat right(IntFormat fmt)
Definition: Output.h:250
bool ended(void) const
Definition: GenTree.h:120
void copy(const GenTree< T, K, C > &tree)
Definition: GenTree.h:163
void remove(Node *pa[], unsigned char da[], int k, Node *p)
Definition: avl_GenTree.cpp:98
AbstractTree(void)
Definition: GenTree.h:35
GenTree(const GenTree< T > &tree)
Definition: GenTree.h:91
const T & item(void) const
Definition: GenTree.h:129
T1 fst
Definition: Pair.h:18
static void dump(Tree::Node *node, int tab=0)
Definition: avl_Tree.cpp:64
signed char balance
Definition: GenTree.h:41
void removeAll(const CC &coll)
Definition: GenTree.h:258
bool operator==(const GenTree< T, K, C > &tree) const
Definition: GenTree.h:268
Node * right(void) const
Definition: GenTree.h:63
Node * find(const typename K::key_t &key) const
Definition: GenTree.h:75
Node(void)
Definition: GenTree.h:39
sys::SystemOutStream & out
Definition: system_SystemIO.cpp:101
GenTree(void)
Definition: GenTree.h:90
GenTree< T, K, C > & operator=(const GenTree< T, K, C > &tree)
Definition: GenTree.h:177
bool isEmpty(void) const
Definition: GenTree.h:105
T2 snd
Definition: Pair.h:19
void set(const T &item)
Definition: GenTree.h:203
bool equals(const GenTree< T, K, C > &tree) const
Definition: GenTree.h:261
T & data(void)
Definition: GenTree.h:132
IntFormat left(IntFormat fmt)
Definition: Output.h:249
int count(void) const
Definition: avl_GenTree.cpp:230
bool operator!=(const GenTree< T, K, C > &tree) const
Definition: GenTree.h:269
Node * links[2]
Definition: GenTree.h:40
Node(const T &item)
Definition: GenTree.h:59
Node * succ(int dir) const
Definition: GenTree.h:64
Iterator(const GenTree< T, K, C > &tree)
Definition: GenTree.h:116
void add(const T &item)
Definition: GenTree.h:179
Definition: GenTree.h:114
bool containsAll(const Co< T > &coll) const
Definition: GenTree.h:109
void addAll(const CC &coll)
Definition: GenTree.h:256
bool contains(const typename K::key_t &item) const
Definition: GenTree.h:104