Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tree.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
28 //
29 //
30 // $Id$
31 //
32 //
33 // --------------------------------------------------------------
34 // Tree
35 //
36 // Very simple tree data-type to get rid of QT-ListView ...
37 // Trees are build once and not to be modified.
38 // No memory management for Data!
39 //
40 // Author: Martin Liendl - Martin.Liendl@cern.ch
41 //
42 // --------------------------------------------------------------
43 //
44 #ifndef OlapTree_h
45 #define OlapTree_h
46 
47 #include <set>
48 #include <vector>
49 #include <list>
50 
51 #include "G4Types.hh"
52 
53 template <class Data> class TreeNodeIterator;
54 
55 template <class Data>
56 class TreeNode
57 {
58 
59  friend class TreeNodeIterator<Data>;
60 
61 public:
62 
63  TreeNode(TreeNode * prt, Data dta)
64  : parent_(prt), firstChild_(0),
65  lastChild_(0), nextSibling_(0), data_(dta)
66  {
67  if (prt)
68  {
69  if(!prt->firstChild_)
70  {
71  prt->firstChild_=this;
72  prt->lastChild_=this;
73  }
74  else
75  {
76  prt->lastChild_->nextSibling_ = this;
77  prt->lastChild_=this;
78  }
79  }
80  }
81 
82  ~TreeNode() // deletes the whole subtree as well!
83  {
84  if (parent_)
85  {
87  while(*i)
88  {
89  TreeNode * temp = i;
90  i = i->nextSibling_;
91  delete temp;
92  }
93  }
94  }
95 
96  TreeNode * parent() const { return parent_; }
97  Data data() const { return data_; }
98 
99  G4int childCount() const
100  {
101  TreeNode * i = firstChild_;
102  G4int c=0;
103  while (i)
104  {
105  i = i->nextSibling_; c++;
106  }
107  return c;
108  }
109  TreeNode * firstChild() const { return firstChild_; }
110  TreeNode * nextSibling() const { return nextSibling_; }
111  TreeNode * lastChild() const { return lastChild_; }
112 
113 protected:
114 
119  Data data_;
120 
121 private:
122  TreeNode(const TreeNode &);
123  TreeNode & operator=(const TreeNode *);
124 };
125 
126 
127 template <class Data>
128 class Tree
129 {
130 public:
131 
133 
134  Tree(Data rt)
135  : root_(new node_t(0,rt)) {}
136 
137  node_t * root() { return root_; }
138 
139 protected:
140 
142 };
143 
144 
145 template<class Data>
146 class TreeNodeIterator
147 {
148 public:
150  : myroot_(np), curpos_(np)
151  {
152  //if (np->parent())
153  //nexts_=++(np->parent()->firstChild());
154  }
155 
157 
159 
161  {
162 
163  if (curpos_->firstChild_)
164  {
165  curpos_ = curpos_->firstChild_;
166  return curpos_;
167  }
168 
169  if (curpos_->nextSibling_)
170  {
171  curpos_ = curpos_->nextSibling_;
172  return curpos_;
173  }
174  while(up())
175  {
176  if (curpos_->nextSibling_)
177  {
178  curpos_ = curpos_->nextSibling_;
179  return curpos_;
180  }
181  }
182  return 0;
183  }
184 
185 protected:
186 
188  {
189  G4bool result(true);
190  if (curpos_->parent_)
191  curpos_ = curpos_->parent_;
192  else
193  result=false;
194 
195  if (curpos_==myroot_)
196  {
197  result=false;
198  }
199  return result;
200  }
201 
204  //Tree<Data>::c_iterator nextc_;
205 };
206 
207 #endif