2 * Copyright 2003-2009, Ingo Weinhold <ingo_weinhold@gmx.de>.
3 * Distributed under the terms of the MIT License.
5 #ifndef _KERNEL_UTIL_AVL_TREE_H
6 #define _KERNEL_UTIL_AVL_TREE_H
9 #include <util/AVLTreeBase.h>
13 To be implemented by the definition:
18 AVLTreeNode* GetAVLTreeNode(Value* value) const;
19 Value* GetValue(AVLTreeNode* node) const;
20 int Compare(const Key& a, const Value* b) const;
21 int Compare(const Value* a, const Value* b) const;
26 template<typename Definition
>
27 class AVLTree
: protected AVLTreeCompare
{
29 typedef typename
Definition::Key Key
;
30 typedef typename
Definition::Value Value
;
38 AVLTree(const Definition
& definition
);
41 inline int Count() const { return fTree
.Count(); }
42 inline bool IsEmpty() const { return fTree
.IsEmpty(); }
45 Value
* RootNode() const;
47 Value
* Previous(Value
* value
) const;
48 Value
* Next(Value
* value
) const;
50 Value
* LeftMost(Value
* value
) const;
51 Value
* RightMost(Value
* value
) const;
53 inline Iterator
GetIterator();
54 inline ConstIterator
GetIterator() const;
56 inline Iterator
GetIterator(Value
* value
);
57 inline ConstIterator
GetIterator(Value
* value
) const;
59 Value
* Find(const Key
& key
) const;
60 Value
* FindClosest(const Key
& key
, bool less
) const;
62 status_t
Insert(Value
* value
, Iterator
* iterator
= NULL
);
63 Value
* Remove(const Key
& key
);
64 bool Remove(Value
* key
);
66 void CheckTree() const { fTree
.CheckTree(); }
70 virtual int CompareKeyNode(const void* key
,
71 const AVLTreeNode
* node
);
72 virtual int CompareNodes(const AVLTreeNode
* node1
,
73 const AVLTreeNode
* node2
);
75 // definition shortcuts
76 inline AVLTreeNode
* _GetAVLTreeNode(Value
* value
) const;
77 inline Value
* _GetValue(const AVLTreeNode
* node
) const;
78 inline int _Compare(const Key
& a
, const Value
* b
);
79 inline int _Compare(const Value
* a
, const Value
* b
);
82 friend class Iterator
;
83 friend class ConstIterator
;
86 Definition fDefinition
;
89 // (need to implement it here, otherwise gcc 2.95.3 chokes)
90 class Iterator
: public ConstIterator
{
98 inline Iterator(const Iterator
& other
)
106 if (AVLTreeNode
* node
= ConstIterator::fTreeIterator
.Remove()) {
107 AVLTree
<Definition
>* parent
108 = const_cast<AVLTree
<Definition
>*>(
109 ConstIterator::fParent
);
114 inline Iterator(AVLTree
<Definition
>* parent
,
115 const AVLTreeIterator
& treeIterator
)
116 : ConstIterator(parent
, treeIterator
)
120 friend class AVLTree
<Definition
>;
125 template<typename Definition
>
126 class AVLTree
<Definition
>::ConstIterator
{
128 inline ConstIterator()
135 inline ConstIterator(const ConstIterator
& other
)
137 fParent(other
.fParent
),
138 fTreeIterator(other
.fTreeIterator
)
142 inline bool HasCurrent() const
144 return fTreeIterator
.Current();
147 inline Value
* Current()
149 if (AVLTreeNode
* node
= fTreeIterator
.Current())
150 return fParent
->_GetValue(node
);
154 inline bool HasNext() const
156 return fTreeIterator
.HasNext();
161 if (AVLTreeNode
* node
= fTreeIterator
.Next())
162 return fParent
->_GetValue(node
);
166 inline Value
* Previous()
168 if (AVLTreeNode
* node
= fTreeIterator
.Previous())
169 return fParent
->_GetValue(node
);
173 inline ConstIterator
& operator=(const ConstIterator
& other
)
175 fParent
= other
.fParent
;
176 fTreeIterator
= other
.fTreeIterator
;
181 inline ConstIterator(const AVLTree
<Definition
>* parent
,
182 const AVLTreeIterator
& treeIterator
)
185 fTreeIterator
= treeIterator
;
188 friend class AVLTree
<Definition
>;
190 const AVLTree
<Definition
>* fParent
;
191 AVLTreeIterator fTreeIterator
;
195 template<typename Definition
>
196 AVLTree
<Definition
>::AVLTree()
204 template<typename Definition
>
205 AVLTree
<Definition
>::AVLTree(const Definition
& definition
)
208 fDefinition(definition
)
213 template<typename Definition
>
214 AVLTree
<Definition
>::~AVLTree()
219 template<typename Definition
>
221 AVLTree
<Definition
>::Clear()
227 template<typename Definition
>
228 inline typename AVLTree
<Definition
>::Value
*
229 AVLTree
<Definition
>::RootNode() const
231 if (AVLTreeNode
* root
= fTree
.Root())
232 return _GetValue(root
);
237 template<typename Definition
>
238 inline typename AVLTree
<Definition
>::Value
*
239 AVLTree
<Definition
>::Previous(Value
* value
) const
244 AVLTreeNode
* node
= fTree
.Previous(_GetAVLTreeNode(value
));
245 return node
!= NULL
? _GetValue(node
) : NULL
;
249 template<typename Definition
>
250 inline typename AVLTree
<Definition
>::Value
*
251 AVLTree
<Definition
>::Next(Value
* value
) const
256 AVLTreeNode
* node
= fTree
.Next(_GetAVLTreeNode(value
));
257 return node
!= NULL
? _GetValue(node
) : NULL
;
261 template<typename Definition
>
262 inline typename AVLTree
<Definition
>::Value
*
263 AVLTree
<Definition
>::LeftMost(Value
* value
) const
268 AVLTreeNode
* node
= fTree
.LeftMost(_GetAVLTreeNode(value
));
269 return node
!= NULL
? _GetValue(node
) : NULL
;
273 template<typename Definition
>
274 inline typename AVLTree
<Definition
>::Value
*
275 AVLTree
<Definition
>::RightMost(Value
* value
) const
280 AVLTreeNode
* node
= fTree
.RightMost(_GetAVLTreeNode(value
));
281 return node
!= NULL
? _GetValue(node
) : NULL
;
285 template<typename Definition
>
286 inline typename AVLTree
<Definition
>::Iterator
287 AVLTree
<Definition
>::GetIterator()
289 return Iterator(this, fTree
.GetIterator());
293 template<typename Definition
>
294 inline typename AVLTree
<Definition
>::ConstIterator
295 AVLTree
<Definition
>::GetIterator() const
297 return ConstIterator(this, fTree
.GetIterator());
301 template<typename Definition
>
302 inline typename AVLTree
<Definition
>::Iterator
303 AVLTree
<Definition
>::GetIterator(Value
* value
)
305 return Iterator(this, fTree
.GetIterator(_GetAVLTreeNode(value
)));
309 template<typename Definition
>
310 inline typename AVLTree
<Definition
>::ConstIterator
311 AVLTree
<Definition
>::GetIterator(Value
* value
) const
313 return ConstIterator(this, fTree
.GetIterator(_GetAVLTreeNode(value
)));
317 template<typename Definition
>
318 typename AVLTree
<Definition
>::Value
*
319 AVLTree
<Definition
>::Find(const Key
& key
) const
321 if (AVLTreeNode
* node
= fTree
.Find(&key
))
322 return _GetValue(node
);
327 template<typename Definition
>
328 typename AVLTree
<Definition
>::Value
*
329 AVLTree
<Definition
>::FindClosest(const Key
& key
, bool less
) const
331 if (AVLTreeNode
* node
= fTree
.FindClosest(&key
, less
))
332 return _GetValue(node
);
337 template<typename Definition
>
339 AVLTree
<Definition
>::Insert(Value
* value
, Iterator
* iterator
)
341 AVLTreeNode
* node
= _GetAVLTreeNode(value
);
342 status_t error
= fTree
.Insert(node
);
346 if (iterator
!= NULL
)
347 *iterator
= Iterator(this, fTree
.GetIterator(node
));
353 template<typename Definition
>
354 typename AVLTree
<Definition
>::Value
*
355 AVLTree
<Definition
>::Remove(const Key
& key
)
357 AVLTreeNode
* node
= fTree
.Remove(&key
);
358 return node
!= NULL
? _GetValue(node
) : NULL
;
362 template<typename Definition
>
364 AVLTree
<Definition
>::Remove(Value
* value
)
366 return fTree
.Remove(_GetAVLTreeNode(value
));
370 template<typename Definition
>
372 AVLTree
<Definition
>::CompareKeyNode(const void* key
,
373 const AVLTreeNode
* node
)
375 return _Compare(*(const Key
*)key
, _GetValue(node
));
379 template<typename Definition
>
381 AVLTree
<Definition
>::CompareNodes(const AVLTreeNode
* node1
,
382 const AVLTreeNode
* node2
)
384 return _Compare(_GetValue(node1
), _GetValue(node2
));
388 template<typename Definition
>
390 AVLTree
<Definition
>::_GetAVLTreeNode(Value
* value
) const
392 return fDefinition
.GetAVLTreeNode(value
);
396 template<typename Definition
>
397 inline typename AVLTree
<Definition
>::Value
*
398 AVLTree
<Definition
>::_GetValue(const AVLTreeNode
* node
) const
400 return fDefinition
.GetValue(const_cast<AVLTreeNode
*>(node
));
404 template<typename Definition
>
406 AVLTree
<Definition
>::_Compare(const Key
& a
, const Value
* b
)
408 return fDefinition
.Compare(a
, b
);
412 template<typename Definition
>
414 AVLTree
<Definition
>::_Compare(const Value
* a
, const Value
* b
)
416 return fDefinition
.Compare(a
, b
);
420 #endif // _KERNEL_UTIL_AVL_TREE_H