22 #ifndef ELM_GENSTRUCT_SORTEDBINTREE_H
23 #define ELM_GENSTRUCT_SORTEDBINTREE_H
25 #include <elm/utility.h>
26 #include <elm/PreIterator.h>
27 #include <elm/genstruct/Vector.h>
28 #include <elm/genstruct/VectorQueue.h>
29 #include <elm/inhstruct/BinTree.h>
30 #include <elm/compare.h>
31 #include <elm/adapter.h>
34 namespace elm {
namespace genstruct {
37 template <
class T,
class K = IdAdapter<T>,
class C = Comparator<
typename K::t> >
42 inline Node(
const T&
value): val(value) { }
53 inline int count(
void)
const {
return root.count(); }
54 inline bool contains(
const T&
value)
const {
return find(K::key(value)); }
56 inline operator bool(
void)
const {
return !
isEmpty(); }
62 { stack.
push((Node *)tree.root.
root()); }
64 { stack = iter.stack; }
65 bool ended(
void)
const {
return !stack; }
67 Node *top = stack.
pop();
68 if(top->right()) stack.
push((Node *)top->right());
69 if(top->left()) stack.
push((Node *)top->left());
71 const T&
item(
void)
const {
return stack.
top()->val; }
83 Node *node = todo.
get();
85 todo.
put((Node *)node->left());
87 todo.
put((Node *)node->right());
94 Node *node = (Node *)root.
root();
95 Node *new_node =
new Node(value);
100 int cmp = C::compare(K::key(value), K::key(node->val));
103 node->insertRight(new_node);
107 node = (Node *)node->right();
111 node->insertLeft(new_node);
115 node = (Node *)node->left();
120 template <
template <
class _>
class S>
122 for(
typename S<T>::Iterator iter(items); iter; iter++)
126 Node *node = (Node *)root.
root(), *parent = 0;
129 int cmp = C::compare(K::key(
value), K::key(node->val));
134 node = (Node *)node->right();
136 node = (Node *)node->left();
138 Node *
left = (Node *)node->left(), *
right = (Node *)node->right();
141 replace(parent, node,
right);
143 replace(parent, node, left);
145 replace(parent, node, left);
146 for(node = left; node->right(); node = (Node *)node->right());
147 node->insertRight(
right);
151 template <
template <
class _>
class S>
153 for(
typename S<T>::Iterator iter(items); iter; iter++)
156 inline void remove(
const Iterator &iter) {
161 inline const T *
look(
const typename K::key_t& key)
const
162 { Node * node = find(key);
return node ? &node->val : 0; }
163 inline T *
look(
const typename K::key_t& key)
164 { Node * node = find(key);
return node ? (T *)&node->val : 0; }
169 Node *find(
const typename K::key_t& key)
const {
170 Node *node = (Node *)root.
root();
172 int cmp = C::compare(key, K::key(node->val));
176 node = (Node *)node->right();
178 node = (Node *)node->left();
183 void replace(Node *parent, Node *old, Node *_new) {
186 else if(parent->left() == old)
187 parent->insertLeft(_new);
189 parent->insertRight(_new);
193 template <
class T,
class C = Comparator<T> >
199 #endif // ELM_GENSTRUCT_SORTEDBINTREE_H
void add(const T &value)
Definition: SortedBinTree.h:93
Definition: SortedBinTree.h:38
void clear(void)
Definition: SortedBinTree.h:77
Definition: PreIterator.h:29
void setRoot(Node *node)
Definition: BinTree.h:93
GenSortedBinTree(void)
Definition: SortedBinTree.h:49
const T & top(void) const
Definition: Vector.h:85
bool isEmpty(void) const
Definition: BinTree.h:81
const T pop(void)
Definition: Vector.h:109
T * look(const typename K::key_t &key)
Definition: SortedBinTree.h:163
IntFormat right(IntFormat fmt)
Definition: Output.h:250
Node * root(void) const
Definition: BinTree.h:90
const T & item(void) const
Definition: SortedBinTree.h:71
void removeAll(const S< T > &items)
Definition: SortedBinTree.h:152
value_t value(CString name, int value)
Definition: rtti.h:40
void next(void)
Definition: SortedBinTree.h:66
Iterator(const Iterator &iter)
Definition: SortedBinTree.h:63
void put(const T &value)
Definition: VectorQueue.h:105
bool isEmpty(void) const
Definition: SortedBinTree.h:55
void push(const T &value)
Definition: Vector.h:107
const T * look(const typename K::key_t &key) const
Definition: SortedBinTree.h:161
Definition: SortedBinTree.h:194
IntFormat left(IntFormat fmt)
Definition: Output.h:249
bool ended(void) const
Definition: SortedBinTree.h:65
Iterator(const GenSortedBinTree &tree)
Definition: SortedBinTree.h:61
int count(void) const
Definition: SortedBinTree.h:53
void addAll(const S< T > &items)
Definition: SortedBinTree.h:121
bool contains(const T &value) const
Definition: SortedBinTree.h:54
const T & get(void)
Definition: VectorQueue.h:115
~GenSortedBinTree(void)
Definition: SortedBinTree.h:50
Definition: SortedBinTree.h:59
Definition: VectorQueue.h:32