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
Definition: Tree.h:58
Tree * operator*() const
Definition: Tree.h:70
Tree * operator->() const
Definition: Tree.h:71
Iter(const Iter &iter)
Definition: Tree.h:61
Iter operator++(int)
Definition: Tree.h:73
bool equals(Iter i) const
Definition: Tree.h:65
void next(void)
Definition: Tree.h:64
Iter(const Tree *tree)
Definition: Tree.h:60
bool operator!=(Iter i) const
Definition: Tree.h:75
const T & item(void) const
Definition: Tree.h:63
bool operator==(Iter i) const
Definition: Tree.h:74
bool ended(void) const
Definition: Tree.h:62
Iter & operator++()
Definition: Tree.h:72
Definition: Tree.h:31
void addSibling(Tree *newSibling)
Definition: Tree.h:86
void removeAll(const TT &coll)
Definition: Tree.h:98
Tree * sibling(void)
Definition: Tree.h:45
Tree(const T &value)
Definition: Tree.h:33
void remove(Tree *child)
Definition: Tree.h:94
void addAll(const TT &coll)
Definition: Tree.h:90
void removeChild(Tree *child)
Definition: Tree.h:92
const Tree * sibling(void) const
Definition: Tree.h:43
void prependChild(Tree *child)
Definition: Tree.h:82
const Tree * children(void) const
Definition: Tree.h:39
int count(void) const
Definition: Tree.h:50
T & data(void)
Definition: Tree.h:36
Tree * children(void)
Definition: Tree.h:41
void remove(const Iter &iter)
Definition: Tree.h:96
bool hasChild(Tree *tree) const
Definition: Tree.h:47
void add(Tree *child)
Definition: Tree.h:88
const T & data(void) const
Definition: Tree.h:35
bool contains(Tree *tree) const
Definition: Tree.h:49
void appendChild(Tree *child)
Definition: Tree.h:84
void clear(void)
Definition: Tree.h:100
bool isEmpty(void) const
Definition: Tree.h:52
Definition: Tree.h:46
bool equals(Iter ii) const
Definition: Tree.h:53
void next(void)
Definition: Tree.h:52
Tree * item(void) const
Definition: Tree.h:51
bool ended(void) const
Definition: Tree.h:50
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
void removeChild(Tree *child)
Definition: inhstruct_Tree.cpp:145
int count(void) const
Definition: inhstruct_Tree.cpp:76
bool hasChild(Tree *tree) const
Definition: Tree.h:39
void add(Tree *child)
Definition: Tree.h:81
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
Definition: adapter.h:26