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
49 TreeItem( const PAYLOAD & val,
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
69 TreeItem( PAYLOAD val,
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:
87 TreeItem ( const TreeItem<PAYLOAD> & ) {}
88 TreeItem<PAYLOAD> & operator= ( const TreeItem<PAYLOAD> & ) {}
89
90
91public:
92
97 virtual ~TreeItem()
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
142 void setParent( TreeItem<PAYLOAD> * newParent ) { _parent = newParent; }
143
147 void setNext( TreeItem<PAYLOAD> * newNext ) { _next = newNext; }
148
152 void setFirstChild( TreeItem<PAYLOAD> * newFirstChild )
153 { _firstChild = newFirstChild; }
154
155
165 void addChild( TreeItem<PAYLOAD> * newChild )
166 {
167 if ( newChild )
168 {
169 newChild->setNext( firstChild() );
170 setFirstChild( newChild );
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
199 SortedTreeItem( PAYLOAD val,
200 SortedTreeItem<PAYLOAD> * parentItem = 0 )
201 : TreeItem<PAYLOAD> ( val, false, parentItem )
202 {
203 if ( parentItem )
204 {
205 // Hopefully we have a SortedTreeItem parent
206 SortedTreeItem<PAYLOAD> * sortParent =
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() {}
221
222
228 {
229 if ( ! newChild )
230 return;
231
232 if ( ! firstChild() ||
233 newChild->value() < firstChild()->value() )
234 {
235 // Insert as first child
236
237 newChild->setNext( firstChild() );
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
265 { return ( SortedTreeItem<PAYLOAD> * ) TreeItem<PAYLOAD>::_parent; }
266
271 { return ( SortedTreeItem<PAYLOAD> * ) TreeItem<PAYLOAD>::_next; }
272
277 { return ( SortedTreeItem<PAYLOAD> * ) TreeItem<PAYLOAD>::_firstChild; }
278
279
280private:
281
287 SortedTreeItem<PAYLOAD> & operator= ( const SortedTreeItem<PAYLOAD> & ) {}
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 val, SortedTreeItem< PAYLOAD > *parentItem=0)
Definition TreeItem.h:199
virtual ~SortedTreeItem()
Definition TreeItem.h:220
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
virtual ~TreeItem()
Definition TreeItem.h:97
TreeItem(const PAYLOAD &val, TreeItem< PAYLOAD > *parent=0)
Definition TreeItem.h:49
const PAYLOAD & value() const
Definition TreeItem.h:113
TreeItem(PAYLOAD val, bool autoAddChild, TreeItem< PAYLOAD > *parent=0)
Definition TreeItem.h:69
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