libyui
Loading...
Searching...
No Matches
TreeItem.h
1/*
2 Copyright (C) 2000-2012 Novell, Inc
3 This library is free software; you can redistribute it and/or modify
4 it under the terms of the GNU Lesser General Public License as
5 published by the Free Software Foundation; either version 2.1 of the
6 License, or (at your option) version 3.0 of the License. This library
7 is distributed in the hope that it will be useful, but WITHOUT ANY
8 WARRANTY; without even the implied warranty of MERCHANTABILITY or
9 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10 License for more details. You should have received a copy of the GNU
11 Lesser General Public License along with this library; if not, write
12 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13 Floor, Boston, MA 02110-1301 USA
14*/
15
16
17/*-/
18
19 File: TreeItem.h
20
21 Author: Stefan Hundhammer <shundhammer@suse.de>
22
23/-*/
24
25#ifndef TreeItem_h
26#define TreeItem_h
27
28#include <string>
29
30
31
32
40template<class PAYLOAD> class TreeItem
41{
42public:
43
51 : _value( val )
52 , _parent( parent )
53 , _next(0)
54 , _firstChild(0)
55 {
56 if ( _parent )
57 _parent->addChild( this );
58 }
59
60
61protected:
62
70 bool autoAddChild,
72 : _value( val )
73 , _parent( parent )
74 , _next(0)
75 , _firstChild(0)
76 {
77 if ( _parent && autoAddChild )
78 _parent->addChild( this );
79 }
80
81
82private:
89
90
91public:
92
97 virtual ~TreeItem<PAYLOAD> ()
98 {
99 TreeItem<PAYLOAD> * child = firstChild();
100
101 while ( child )
102 {
103 TreeItem<PAYLOAD> * lastChild = child;
104 child = child->next();
105 delete lastChild;
106 }
107 }
108
109
113 const PAYLOAD & value() const { return _value; }
114
122 void setValue( PAYLOAD newValue ) { _value = newValue; }
123
127 TreeItem<PAYLOAD> * parent() const { return _parent; }
128
132 TreeItem<PAYLOAD> * next() const { return _next; }
133
137 TreeItem<PAYLOAD> * firstChild() const { return _firstChild; }
138
143
148
154
155
166 {
167 if ( newChild )
168 {
171 }
172 }
173
174
175protected:
176
177 PAYLOAD _value;
178 TreeItem<PAYLOAD> * _parent;
179 TreeItem<PAYLOAD> * _next;
180 TreeItem<PAYLOAD> * _firstChild;
181};
182
183
184
191template<class PAYLOAD> class SortedTreeItem: public TreeItem<PAYLOAD>
192{
193public:
194
201 : TreeItem<PAYLOAD> ( val, false, parentItem )
202 {
203 if ( parentItem )
204 {
205 // Hopefully we have a SortedTreeItem parent
207 dynamic_cast<SortedTreeItem<PAYLOAD> *> ( parentItem );
208
209 if ( sortParent )
210 sortParent->insertChildSorted( this );
211 else // no SortedTreeItem parent - add unsorted
212 parentItem->addChild( this );
213 }
214 }
215
216
220 virtual ~SortedTreeItem<PAYLOAD> () {}
221
222
228 {
229 if ( ! newChild )
230 return;
231
232 if ( ! firstChild() ||
233 newChild->value() < firstChild()->value() )
234 {
235 // Insert as first child
236
238 this->setFirstChild( newChild );
239 }
240 else
241 {
242 // Search correct place to insert
243
244 TreeItem<PAYLOAD> * child = firstChild();
245
246 while ( child->next() &&
247 child->next()->value() < newChild->value() )
248 {
249 child = child->next();
250 }
251
252
253 // Insert after 'child'
254
255 newChild->setNext( child->next() );
256 child->setNext( newChild );
257 }
258 }
259
260
266
272
278
279
280private:
281
288};
289
290
291
296template<class ITEM, class PAYLOAD> inline
297ITEM *
298findDirectChild( ITEM * item, PAYLOAD searchVal )
299{
300 TreeItem<PAYLOAD> * child = item->firstChild();
301
302 while ( child )
303 {
304 if ( child->value() == searchVal )
305 return dynamic_cast<ITEM *> ( child );
306
307 child = child->next();
308 }
309
310 return 0;
311}
312
313
314
315#endif // TreeItem_h
Definition TreeItem.h:192
SortedTreeItem< PAYLOAD > * next() const
Definition TreeItem.h:270
SortedTreeItem< PAYLOAD > * parent() const
Definition TreeItem.h:264
SortedTreeItem< PAYLOAD > * firstChild() const
Definition TreeItem.h:276
void insertChildSorted(SortedTreeItem< PAYLOAD > *newChild)
Definition TreeItem.h:227
Definition TreeItem.h:41
TreeItem(const PAYLOAD &val, TreeItem< PAYLOAD > *parent=0)
Definition TreeItem.h:49
const PAYLOAD & value() const
Definition TreeItem.h:113
void setNext(TreeItem< PAYLOAD > *newNext)
Definition TreeItem.h:147
void setParent(TreeItem< PAYLOAD > *newParent)
Definition TreeItem.h:142
void setValue(PAYLOAD newValue)
Definition TreeItem.h:122
void addChild(TreeItem< PAYLOAD > *newChild)
Definition TreeItem.h:165
TreeItem< PAYLOAD > * firstChild() const
Definition TreeItem.h:137
void setFirstChild(TreeItem< PAYLOAD > *newFirstChild)
Definition TreeItem.h:152
TreeItem< PAYLOAD > * next() const
Definition TreeItem.h:132
TreeItem< PAYLOAD > * parent() const
Definition TreeItem.h:127