Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Tree.h
1 /*
2  * $Id$
3  * inhstruct::Tree class interface
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2007, 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_INHSTRUCT_TREE_H
23 #define ELM_INHSTRUCT_TREE_H
24 
25 #include <elm/PreIterator.h>
26 #include <elm/assert.h>
27 
28 namespace elm { namespace inhstruct {
29 
30 // Tree class
31 class Tree {
32 public:
33 
34  inline Tree(void): _children(0), _sibling(0) { }
35 
36  // Accessors
37  inline Tree *children(void) const { return _children; }
38  inline Tree *sibling(void) const { return _sibling; }
39  bool hasChild(Tree *tree) const { return _children; }
40  inline bool contains(Tree *tree) const { return hasChild(tree); }
41  int count(void) const;
42  inline bool isEmpty(void) const { return !_children; }
43  inline operator bool(void) const { return !isEmpty(); }
44 
45  // Iterator class
46  class Iter {
47  public:
48  inline Iter(const Tree *tree): cur(tree->children()) { }
49  inline Iter(const Iter& iter): cur(iter.cur) { }
50  inline bool ended (void) const { return !cur; }
51  inline Tree *item (void) const { return cur; }
52  inline void next(void) { cur = cur->sibling(); }
53  inline bool equals(Iter ii) const { return cur == ii.cur; }
54 
55  inline operator bool() const { return !ended(); }
56  inline operator Tree *() const { return item(); }
57  inline Tree *operator*() const { return item(); }
58  inline Tree *operator->() const { return item(); }
59  inline Iter& operator++() { next(); return *this; }
60  inline Iter operator++(int) { auto o = *this; next(); return o; }
61  inline bool operator==(Iter i) const { return equals(i); }
62  inline bool operator!=(Iter i) const { return !equals(i); }
63  private:
64  Tree *cur;
65  };
66 
67  // Mutators
68  inline void prependChild(Tree *child) {
69  ASSERTP(child, "null child argument");
70  child->_sibling = _children;
71  _children = child;
72  }
73 
74  void appendChild(Tree *child);
75  inline void addSibling(Tree *newSibling) {
76  Tree *oldSibling = _sibling;
77  newSibling->_sibling = oldSibling;
78  _sibling = newSibling;
79  }
80 
81  inline void add(Tree *child) { prependChild(child); }
82  template <class TT> void addAll(const TT& coll)
83  { for(typename TT::Iterator iter(coll); iter; iter++) add(*iter); }
84  void removeChild(Tree *child);
85  inline void remove(Tree *child) { removeChild(child); }
86  inline void remove(const Iter& iter) { removeChild(iter); }
87  template <class TT> void removeAll(const TT& coll)
88  { for(typename TT::Iterator iter(coll); iter; iter++) remove(*iter); }
89  inline void clear(void) { _children = 0; }
90 
91 private:
92  Tree *_children, *_sibling;
93 };
94 
95 } } // elm::inhstruct
96 
97 #endif // ELM_INHSTRUCT_TREE_H
Definition: Tree.h:46
Tree * operator*() const
Definition: Tree.h:57
Tree * operator->() const
Definition: Tree.h:58
Iter(const Iter &iter)
Definition: Tree.h:49
Iter operator++(int)
Definition: Tree.h:60
bool equals(Iter ii) const
Definition: Tree.h:53
void next(void)
Definition: Tree.h:52
Tree * item(void) const
Definition: Tree.h:51
Iter(const Tree *tree)
Definition: Tree.h:48
bool operator!=(Iter i) const
Definition: Tree.h:62
bool operator==(Iter i) const
Definition: Tree.h:61
bool ended(void) const
Definition: Tree.h:50
Iter & operator++()
Definition: Tree.h:59
Definition: Tree.h:31
void addSibling(Tree *newSibling)
Definition: Tree.h:75
void removeAll(const TT &coll)
Definition: Tree.h:87
void remove(Tree *child)
Definition: Tree.h:85
void prependChild(Tree *child)
Definition: Tree.h:68
void addAll(const TT &coll)
Definition: Tree.h:82
Tree * children(void) const
Definition: Tree.h:37
Tree * sibling(void) const
Definition: Tree.h:38
Tree(void)
Definition: Tree.h:34
void removeChild(Tree *child)
Definition: inhstruct_Tree.cpp:145
int count(void) const
Definition: inhstruct_Tree.cpp:76
void remove(const Iter &iter)
Definition: Tree.h:86
bool hasChild(Tree *tree) const
Definition: Tree.h:39
void add(Tree *child)
Definition: Tree.h:81
bool contains(Tree *tree) const
Definition: Tree.h:40
void appendChild(Tree *child)
Definition: inhstruct_Tree.cpp:115
void clear(void)
Definition: Tree.h:89
bool isEmpty(void) const
Definition: Tree.h:42
Definition: util_WAHVector.cpp:157
void iter(const C &c, const F &f)
Definition: util.h:95
Definition: adapter.h:26