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
elm::inhstruct::Tree::Iter::operator->
Tree * operator->() const
Definition: Tree.h:58
elm::inhstruct::Tree::Iter::operator*
Tree * operator*() const
Definition: Tree.h:57
elm::iter
Definition: util_WAHVector.cpp:157
elm::inhstruct::Tree::Iter::operator!=
bool operator!=(Iter i) const
Definition: Tree.h:62
elm::inhstruct::Tree::removeAll
void removeAll(const TT &coll)
Definition: Tree.h:87
elm::iter
void iter(const C &c, const F &f)
Definition: util.h:95
elm::inhstruct::Tree::removeChild
void removeChild(Tree *child)
Definition: inhstruct_Tree.cpp:145
elm::inhstruct::Tree::hasChild
bool hasChild(Tree *tree) const
Definition: Tree.h:39
elm::inhstruct::Tree::Iter::item
Tree * item(void) const
Definition: Tree.h:51
elm::inhstruct::Tree::addSibling
void addSibling(Tree *newSibling)
Definition: Tree.h:75
elm::inhstruct::Tree::Iter
Definition: Tree.h:46
bool
elm::inhstruct::Tree::Iter::next
void next(void)
Definition: Tree.h:52
elm::inhstruct::Tree::prependChild
void prependChild(Tree *child)
Definition: Tree.h:68
elm::inhstruct::Tree::remove
void remove(Tree *child)
Definition: Tree.h:85
elm::inhstruct::Tree::appendChild
void appendChild(Tree *child)
Definition: inhstruct_Tree.cpp:115
elm::inhstruct::Tree::isEmpty
bool isEmpty(void) const
Definition: Tree.h:42
elm
Definition: adapter.h:26
elm::inhstruct::Tree::Iter::operator==
bool operator==(Iter i) const
Definition: Tree.h:61
elm::inhstruct::Tree::Iter::ended
bool ended(void) const
Definition: Tree.h:50
elm::inhstruct::Tree::Iter::operator++
Iter & operator++()
Definition: Tree.h:59
elm::inhstruct::Tree::count
int count(void) const
Definition: inhstruct_Tree.cpp:76
elm::inhstruct::Tree::addAll
void addAll(const TT &coll)
Definition: Tree.h:82
elm::inhstruct::Tree
Definition: Tree.h:31
elm::inhstruct::Tree::sibling
Tree * sibling(void) const
Definition: Tree.h:38
elm::inhstruct::Tree::Iter::Iter
Iter(const Tree *tree)
Definition: Tree.h:48
elm::inhstruct::Tree::contains
bool contains(Tree *tree) const
Definition: Tree.h:40
elm::inhstruct::Tree::Tree
Tree(void)
Definition: Tree.h:34
elm::inhstruct::Tree::Iter::equals
bool equals(Iter ii) const
Definition: Tree.h:53
elm::inhstruct::Tree::children
Tree * children(void) const
Definition: Tree.h:37
elm::inhstruct::Tree::Iter::operator++
Iter operator++(int)
Definition: Tree.h:60
elm::inhstruct::Tree::Iter::Iter
Iter(const Iter &iter)
Definition: Tree.h:49
elm::inhstruct::Tree::remove
void remove(const Iter &iter)
Definition: Tree.h:86
elm::inhstruct::Tree::add
void add(Tree *child)
Definition: Tree.h:81
elm::inhstruct::Tree::clear
void clear(void)
Definition: Tree.h:89