source: trunk/examples/extended/geometry/olap/include/tree.hh@ 1215

Last change on this file since 1215 was 807, checked in by garnier, 17 years ago

update

File size: 4.8 KB
Line 
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//
26//
27// $Id: tree.hh,v 1.4 2006/06/29 17:22:48 gunter Exp $
28// GEANT4 tag $Name: $
29//
30//
31// --------------------------------------------------------------
32// Tree
33//
34// Very simple tree data-type to get rid of QT-ListView ...
35// Trees are build once and not to be modified.
36// No memory management for Data!
37//
38// Author: Martin Liendl - Martin.Liendl@cern.ch
39//
40// --------------------------------------------------------------
41//
42#ifndef OlapTree_h
43#define OlapTree_h
44
45#include <set>
46#include <vector>
47#include <list>
48
49#include "G4Types.hh"
50
51template <class Data> class TreeNodeIterator;
52
53template <class Data>
54class TreeNode
55{
56
57 friend class TreeNodeIterator<Data>;
58
59public:
60
61 TreeNode(TreeNode * parent, Data data)
62 : parent_(parent), firstChild_(0),
63 lastChild_(0), nextSibling_(0), data_(data)
64 {
65 if (parent)
66 {
67 if(!parent->firstChild_)
68 {
69 parent->firstChild_=this;
70 parent->lastChild_=this;
71 }
72 else
73 {
74 parent->lastChild_->nextSibling_ = this;
75 parent->lastChild_=this;
76 }
77 }
78 }
79
80 ~TreeNode() // deletes the whole subtree as well!
81 {
82 if (parent_)
83 {
84 TreeNode * i = parent_->firstChild_;
85 while(*i)
86 {
87 TreeNode * temp = i;
88 i = i->nextSibling_;
89 delete temp;
90 }
91 }
92 }
93
94 TreeNode * parent() const { return parent_; }
95 Data data() const { return data_; }
96
97 G4int childCount() const
98 {
99 TreeNode * i = firstChild_;
100 G4int c=0;
101 while (i)
102 {
103 i = i->nextSibling_; c++;
104 }
105 return c;
106 }
107 TreeNode * firstChild() const { return firstChild_; }
108 TreeNode * nextSibling() const { return nextSibling_; }
109 TreeNode * lastChild() const { return lastChild_; }
110
111protected:
112
113 TreeNode * parent_;
114 TreeNode * firstChild_;
115 TreeNode * lastChild_;
116 TreeNode * nextSibling_;
117 Data data_;
118
119private:
120 TreeNode(const TreeNode &);
121 TreeNode & operator=(const TreeNode *);
122};
123
124
125template <class Data>
126class Tree
127{
128public:
129
130 typedef TreeNode<Data> node_t;
131
132 Tree(Data rt)
133 : root_(new node_t(0,rt)) {}
134
135 node_t * root() { return root_; }
136
137protected:
138
139 node_t * root_;
140};
141
142
143template<class Data>
144class TreeNodeIterator
145{
146public:
147 TreeNodeIterator(TreeNode<Data> * np)
148 : myroot_(np), curpos_(np)
149 {
150 //if (np->parent())
151 //nexts_=++(np->parent()->firstChild());
152 }
153
154 ~TreeNodeIterator() { }
155
156 TreeNode<Data> * current() { return curpos_; }
157
158 TreeNode<Data> * next()
159 {
160
161 if (curpos_->firstChild_)
162 {
163 curpos_ = curpos_->firstChild_;
164 return curpos_;
165 }
166
167 if (curpos_->nextSibling_)
168 {
169 curpos_ = curpos_->nextSibling_;
170 return curpos_;
171 }
172 while(up())
173 {
174 if (curpos_->nextSibling_)
175 {
176 curpos_ = curpos_->nextSibling_;
177 return curpos_;
178 }
179 }
180 return 0;
181 }
182
183protected:
184
185 G4bool up()
186 {
187 G4bool result(true);
188 if (curpos_->parent_)
189 curpos_ = curpos_->parent_;
190 else
191 result=false;
192
193 if (curpos_==myroot_)
194 {
195 result=false;
196 }
197 return result;
198 }
199
200 TreeNode<Data> * myroot_;
201 TreeNode<Data> * curpos_;
202 //Tree<Data>::c_iterator nextc_;
203};
204
205#endif
Note: See TracBrowser for help on using the repository browser.