Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SortedBinTree.h
1 /*
2  * $Id$
3  * SortedBinTree class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2004-07, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_GENSTRUCT_SORTEDBINTREE_H
23 #define ELM_GENSTRUCT_SORTEDBINTREE_H
24 
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>
32 
33 
34 namespace elm { namespace genstruct {
35 
36 // GenSortedBinTree class implements MutableCollection
37 template <class T, class K = IdAdapter<T>, class C = Comparator<typename K::t> >
39 private:
40  class Node: public inhstruct::BinTree::Node {
41  public:
42  inline Node(const T& value): val(value) { }
43  T val;
44  };
45 
46 public:
47 
48  // Methods
49  inline GenSortedBinTree(void) { }
50  inline ~GenSortedBinTree(void) { clear(); }
51 
52  // Collection concept
53  inline int count(void) const { return root.count(); }
54  inline bool contains(const T& value) const { return find(K::key(value)); }
55  inline bool isEmpty(void) const { return root.isEmpty(); }
56  inline operator bool(void) const { return !isEmpty(); }
57 
58  // Iterator class
59  class Iterator: public PreIterator<Iterator, const T&> {
60  public:
61  inline Iterator(const GenSortedBinTree& tree)
62  { stack.push((Node *)tree.root.root()); }
63  inline Iterator(const Iterator& iter)
64  { stack = iter.stack; }
65  bool ended(void) const { return !stack; }
66  void next(void) {
67  Node *top = stack.pop();
68  if(top->right()) stack.push((Node *)top->right());
69  if(top->left()) stack.push((Node *)top->left());
70  }
71  const T& item(void) const { return stack.top()->val; }
72  private:
73  Vector<Node *> stack;
74  };
75 
76  // MutableCollection concept
77  void clear(void) {
78  if(isEmpty())
79  return;
81  todo.put((Node *)root.root());
82  while(todo) {
83  Node *node = todo.get();
84  if(node->left())
85  todo.put((Node *)node->left());
86  if(node->right())
87  todo.put((Node *)node->right());
88  delete node;
89  }
90  }
91 
92 
93  void add(const T &value) {
94  Node *node = (Node *)root.root();
95  Node *new_node = new Node(value);
96  if(!node)
97  root.setRoot(new_node);
98  else
99  while(node) {
100  int cmp = C::compare(K::key(value), K::key(node->val));
101  if(cmp <= 0) {
102  if(!node->right()) {
103  node->insertRight(new_node);
104  break;
105  }
106  else
107  node = (Node *)node->right();
108  }
109  else {
110  if(!node->left()) {
111  node->insertLeft(new_node);
112  break;
113  }
114  else
115  node = (Node *)node->left();
116  }
117  }
118  }
119 
120  template <template <class _> class S>
121  void addAll (const S<T> &items) {
122  for(typename S<T>::Iterator iter(items); iter; iter++)
123  add(iter);
124  }
125  void remove(const T& value) {
126  Node *node = (Node *)root.root(), *parent = 0;
127  while(true) {
128  ASSERT(node);
129  int cmp = C::compare(K::key(value), K::key(node->val));
130  if(cmp == 0)
131  break;
132  parent = node;
133  if(cmp < 0)
134  node = (Node *)node->right();
135  else
136  node = (Node *)node->left();
137  }
138  Node *left = (Node *)node->left(), *right = (Node *)node->right();
139  delete node;
140  if(!left)
141  replace(parent, node, right);
142  else if(!right)
143  replace(parent, node, left);
144  else {
145  replace(parent, node, left);
146  for(node = left; node->right(); node = (Node *)node->right());
147  node->insertRight(right);
148  }
149  }
150 
151  template <template <class _> class S>
152  void removeAll(const S<T> &items) {
153  for(typename S<T>::Iterator iter(items); iter; iter++)
154  remove(iter);
155  }
156  inline void remove(const Iterator &iter) {
157  remove(*iter);
158  }
159 
160  // MutableLookable<K> implementation
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; }
165 
166 private:
167  inhstruct::BinTree root;
168 
169  Node *find(const typename K::key_t& key) const {
170  Node *node = (Node *)root.root();
171  while(node) {
172  int cmp = C::compare(key, K::key(node->val));
173  if(cmp == 0)
174  break;
175  else if(cmp < 0)
176  node = (Node *)node->right();
177  else
178  node = (Node *)node->left();
179  }
180  return node;
181  }
182 
183  void replace(Node *parent, Node *old, Node *_new) {
184  if(!parent)
185  root.setRoot(_new);
186  else if(parent->left() == old)
187  parent->insertLeft(_new);
188  else
189  parent->insertRight(_new);
190  }
191 };
192 
193 template <class T, class C = Comparator<T> >
194 class SortedBinTree: public GenSortedBinTree<T, IdAdapter<T>, C> {
195 };
196 
197 } } // elm::genstruct
198 
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
Definition: BinTree.h:13
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
Definition: BinTree.h:16
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