Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Tree.h
1 /*
2  * Tree class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2008, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_DATA_TREE_H
22 #define ELM_DATA_TREE_H
23 
24 #include <elm/PreIterator.h>
25 #include <elm/inhstruct/Tree.h>
26 
27 namespace elm {
28 
29 // Tree class
30 template <class T>
31 class Tree: private inhstruct::Tree {
32 public:
33  inline Tree(const T& value): _value(value) { }
34 
35  inline const T& data(void) const { return _value; }
36  inline T& data(void) { return _value; }
37 
38  // Accessors
39  inline const Tree *children(void) const
40  { return static_cast<Tree *>(inhstruct::Tree::children()); }
41  inline Tree *children(void)
42  { return static_cast<Tree *>(inhstruct::Tree::children()); }
43  inline const Tree *sibling(void) const
44  { return static_cast<Tree *>(inhstruct::Tree::sibling()); }
45  inline Tree *sibling(void)
46  { return static_cast<Tree *>(inhstruct::Tree::sibling()); }
47  bool hasChild(Tree *tree) const
48  { return inhstruct::Tree::hasChild(tree); }
49  inline bool contains(Tree *tree) const { return hasChild(tree); }
50  inline int count(void) const
51  { return inhstruct::Tree::count(); }
52  inline bool isEmpty(void) const
53  { return inhstruct::Tree::isEmpty(); }
54  inline operator bool(void) const
55  { return inhstruct::Tree::isEmpty(); }
56 
57  // Iterator class
58  class Iter {
59  public:
60  inline Iter(const Tree *tree): it(tree) { }
61  inline Iter(const Iter& iter): it(iter.it) { }
62  inline bool ended(void) const { return it.ended(); }
63  inline const T& item(void) const { return static_cast<Tree *>(it.item())->_value; }
64  inline void next(void) { it.next(); }
65  inline bool equals(Iter i) const { return it.equals(i.it); }
66 
67  inline operator bool() const { return !ended(); }
68  inline operator T() const { return item(); }
69  inline operator Tree *() const { return item(); }
70  inline Tree *operator*() const { return item(); }
71  inline Tree *operator->() const { return item(); }
72  inline Iter& operator++() { next(); return *this; }
73  inline Iter operator++(int) { auto o = *this; next(); return o; }
74  inline bool operator==(Iter i) const { return equals(i); }
75  inline bool operator!=(Iter i) const { return !equals(i); }
76 
77  private:
79  };
80 
81  // Mutators
82  inline void prependChild(Tree *child)
84  inline void appendChild(Tree *child)
86  inline void addSibling(Tree *newSibling)
87  { inhstruct::Tree::addSibling(newSibling); }
88  inline void add(Tree *child)
89  { inhstruct::Tree::add(child); }
90  template <class TT> void addAll(const TT& coll)
91  { inhstruct::Tree::addAll(coll); }
92  inline void removeChild(Tree *child)
94  inline void remove(Tree *child)
95  { inhstruct::Tree::remove(child); }
96  inline void remove(const Iter& iter)
97  { removeChild(iter); }
98  template <class TT> void removeAll(const TT& coll)
99  { inhstruct::Tree::removeAll(coll); }
100  inline void clear(void)
102 
103 private:
104  T _value;
105 };
106 
107 }
108 
109 #endif // ELM_DATA_TREE_H
elm::Tree::children
const Tree * children(void) const
Definition: Tree.h:39
elm::Tree::count
int count(void) const
Definition: Tree.h:50
elm::Tree::Iter::operator->
Tree * operator->() const
Definition: Tree.h:71
elm::iter
Definition: util_WAHVector.cpp:157
elm::Tree
Definition: Tree.h:31
elm::inhstruct::Tree::removeAll
void removeAll(const TT &coll)
Definition: Tree.h:87
elm::inhstruct::Tree::removeChild
void removeChild(Tree *child)
Definition: inhstruct_Tree.cpp:145
elm::Tree::data
T & data(void)
Definition: Tree.h:36
elm::Tree::sibling
const Tree * sibling(void) const
Definition: Tree.h:43
elm::Tree::Iter::item
const T & item(void) const
Definition: Tree.h:63
elm::Tree::hasChild
bool hasChild(Tree *tree) const
Definition: Tree.h:47
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::Tree::Iter::operator++
Iter & operator++()
Definition: Tree.h:72
elm::Tree::removeChild
void removeChild(Tree *child)
Definition: Tree.h:92
elm::Tree::contains
bool contains(Tree *tree) const
Definition: Tree.h:49
elm::inhstruct::Tree::addSibling
void addSibling(Tree *newSibling)
Definition: Tree.h:75
elm::Tree::remove
void remove(Tree *child)
Definition: Tree.h:94
value
elm::Tree::Iter
Definition: Tree.h:58
elm::inhstruct::Tree::Iter
Definition: Tree.h:46
bool
elm::Tree::add
void add(Tree *child)
Definition: Tree.h:88
elm::Tree::clear
void clear(void)
Definition: Tree.h:100
elm::inhstruct::Tree::Iter::next
void next(void)
Definition: Tree.h:52
elm::Tree::removeAll
void removeAll(const TT &coll)
Definition: Tree.h:98
elm::inhstruct::Tree::prependChild
void prependChild(Tree *child)
Definition: Tree.h:68
elm::Tree::data
const T & data(void) const
Definition: Tree.h:35
elm::inhstruct::Tree::remove
void remove(Tree *child)
Definition: Tree.h:85
elm::Tree::addAll
void addAll(const TT &coll)
Definition: Tree.h:90
elm::Tree::children
Tree * children(void)
Definition: Tree.h:41
elm::Tree::remove
void remove(const Iter &iter)
Definition: Tree.h:96
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::Tree::Iter::operator++
Iter operator++(int)
Definition: Tree.h:73
elm
Definition: adapter.h:26
elm::inhstruct::Tree::Iter::ended
bool ended(void) const
Definition: Tree.h:50
elm::Tree::Iter::equals
bool equals(Iter i) const
Definition: Tree.h:65
elm::Tree::isEmpty
bool isEmpty(void) const
Definition: Tree.h:52
elm::Tree::Iter::operator*
Tree * operator*() const
Definition: Tree.h:70
elm::Tree::appendChild
void appendChild(Tree *child)
Definition: Tree.h:84
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::Tree::sibling
Tree * sibling(void)
Definition: Tree.h:45
elm::inhstruct::Tree
Definition: Tree.h:31
elm::inhstruct::Tree::sibling
Tree * sibling(void) const
Definition: Tree.h:38
elm::Tree::Tree
Tree(const T &value)
Definition: Tree.h:33
elm::Tree::prependChild
void prependChild(Tree *child)
Definition: Tree.h:82
elm::Tree::Iter::operator==
bool operator==(Iter i) const
Definition: Tree.h:74
elm::inhstruct::Tree::Iter::equals
bool equals(Iter ii) const
Definition: Tree.h:53
elm::Tree::Iter::operator!=
bool operator!=(Iter i) const
Definition: Tree.h:75
elm::inhstruct::Tree::children
Tree * children(void) const
Definition: Tree.h:37
elm::Tree::addSibling
void addSibling(Tree *newSibling)
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
elm::Tree::Iter::next
void next(void)
Definition: Tree.h:64
elm::Tree::Iter::Iter
Iter(const Iter &iter)
Definition: Tree.h:61
elm::Tree::Iter::ended
bool ended(void) const
Definition: Tree.h:62
elm::Tree::Iter::Iter
Iter(const Tree *tree)
Definition: Tree.h:60