2007-08-11 18:57:25 +04:00
|
|
|
/*
|
2009-12-06 18:59:37 +03:00
|
|
|
* Copyright 2003-2009, Ingo Weinhold <ingo_weinhold@gmx.de>.
|
2007-08-11 18:57:25 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2009-12-06 18:59:37 +03:00
|
|
|
#ifndef _KERNEL_UTIL_AVL_TREE_MAP_H
|
|
|
|
#define _KERNEL_UTIL_AVL_TREE_MAP_H
|
2003-06-28 19:49:33 +04:00
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
#include <util/MallocFreeAllocator.h>
|
2009-12-06 18:59:37 +03:00
|
|
|
#include <util/AVLTreeBase.h>
|
2007-08-11 18:57:25 +04:00
|
|
|
|
|
|
|
|
2003-06-28 19:49:33 +04:00
|
|
|
// strategies
|
|
|
|
namespace AVLTreeMapStrategy {
|
|
|
|
// key orders
|
|
|
|
template<typename Value> class Ascending;
|
|
|
|
template<typename Value> class Descending;
|
|
|
|
|
|
|
|
//! Automatic node strategy (works like STL containers do)
|
|
|
|
template <typename Key, typename Value,
|
|
|
|
typename KeyOrder = Ascending<Key>,
|
|
|
|
template <typename> class Allocator = MallocFreeAllocator>
|
|
|
|
class Auto;
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
/*! NodeStrategy must implement this interface:
|
|
|
|
typename Node;
|
|
|
|
inline Node* Allocate(const Key& key, const Value& value)
|
|
|
|
inline void Free(Node* node)
|
|
|
|
inline Key GetKey(Node* node) const
|
|
|
|
inline Value& GetValue(Node* node) const
|
|
|
|
inline AVLTreeNode* GetAVLTreeNode(Node* node) const
|
|
|
|
inline Node* GetNode(AVLTreeNode* node) const
|
|
|
|
inline int CompareKeyNode(const Key& a, const Node* b)
|
|
|
|
inline int CompareNodes(const Node* a, const Node* b)
|
|
|
|
*/
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for convenience
|
|
|
|
#define _AVL_TREE_MAP_TEMPLATE_LIST template<typename Key, typename Value, \
|
2007-08-11 18:57:25 +04:00
|
|
|
typename NodeStrategy>
|
2003-06-28 19:49:33 +04:00
|
|
|
#define _AVL_TREE_MAP_CLASS_NAME AVLTreeMap<Key, Value, NodeStrategy>
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
2003-06-28 19:49:33 +04:00
|
|
|
// AVLTreeMap
|
|
|
|
template<typename Key, typename Value,
|
|
|
|
typename NodeStrategy = AVLTreeMapStrategy::Auto<Key, Value> >
|
2007-08-11 18:57:25 +04:00
|
|
|
class AVLTreeMap : protected AVLTreeCompare {
|
2003-06-28 19:49:33 +04:00
|
|
|
private:
|
|
|
|
typedef typename NodeStrategy::Node Node;
|
|
|
|
typedef _AVL_TREE_MAP_CLASS_NAME Class;
|
|
|
|
|
|
|
|
public:
|
|
|
|
class Iterator;
|
|
|
|
class ConstIterator;
|
|
|
|
|
|
|
|
public:
|
2007-08-11 18:57:25 +04:00
|
|
|
AVLTreeMap(const NodeStrategy& strategy
|
|
|
|
= NodeStrategy());
|
|
|
|
virtual ~AVLTreeMap();
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline int Count() const { return fTree.Count(); }
|
|
|
|
inline bool IsEmpty() const { return fTree.IsEmpty(); }
|
|
|
|
inline void MakeEmpty();
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
Node* RootNode() const;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Iterator GetIterator();
|
|
|
|
inline ConstIterator GetIterator() const;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Iterator GetIterator(Node* node);
|
|
|
|
inline ConstIterator GetIterator(Node* node) const;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
Iterator Find(const Key& key);
|
|
|
|
Iterator FindClose(const Key& key, bool less);
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
status_t Insert(const Key& key, const Value& value,
|
|
|
|
Iterator* iterator);
|
|
|
|
status_t Remove(const Key& key);
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
const NodeStrategy& GetNodeStrategy() const { return fStrategy; }
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
protected:
|
|
|
|
// AVLTreeCompare
|
|
|
|
virtual int CompareKeyNode(const void* key,
|
|
|
|
const AVLTreeNode* node);
|
|
|
|
virtual int CompareNodes(const AVLTreeNode* node1,
|
|
|
|
const AVLTreeNode* node2);
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
void _FreeTree(AVLTreeNode* node);
|
2003-06-28 19:49:33 +04:00
|
|
|
|
|
|
|
// strategy shortcuts
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Node* _Allocate(const Key& key, const Value& value);
|
|
|
|
inline void _Free(Node* node);
|
2009-11-05 20:18:12 +03:00
|
|
|
inline const Key& _GetKey(Node* node) const;
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Value& _GetValue(Node* node) const;
|
|
|
|
inline AVLTreeNode* _GetAVLTreeNode(const Node* node) const;
|
|
|
|
inline Node* _GetNode(const AVLTreeNode* node) const;
|
|
|
|
inline int _CompareKeyNode(const Key& a, const Node* b);
|
|
|
|
inline int _CompareNodes(const Node* a, const Node* b);
|
2003-06-28 19:49:33 +04:00
|
|
|
|
|
|
|
protected:
|
2007-08-11 18:57:25 +04:00
|
|
|
friend class Iterator;
|
|
|
|
friend class ConstIterator;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2009-12-06 18:59:37 +03:00
|
|
|
AVLTreeBase fTree;
|
2007-08-11 18:57:25 +04:00
|
|
|
NodeStrategy fStrategy;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
public:
|
2003-06-28 19:49:33 +04:00
|
|
|
// Iterator
|
2007-08-11 18:57:25 +04:00
|
|
|
// (need to implement it here, otherwise gcc 2.95.3 chokes)
|
|
|
|
class Iterator : public ConstIterator {
|
2003-06-28 19:49:33 +04:00
|
|
|
public:
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Iterator()
|
|
|
|
: ConstIterator()
|
|
|
|
{
|
|
|
|
}
|
2009-11-05 20:18:12 +03:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Iterator(const Iterator& other)
|
|
|
|
: ConstIterator(other)
|
|
|
|
{
|
|
|
|
}
|
2009-11-05 20:18:12 +03:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline void Remove()
|
|
|
|
{
|
|
|
|
if (AVLTreeNode* node = ConstIterator::fTreeIterator.Remove()) {
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME* parent
|
|
|
|
= const_cast<_AVL_TREE_MAP_CLASS_NAME*>(
|
|
|
|
ConstIterator::fParent);
|
|
|
|
parent->_Free(parent->_GetNode(node));
|
|
|
|
}
|
|
|
|
}
|
2009-11-05 20:18:12 +03:00
|
|
|
|
2003-06-28 19:49:33 +04:00
|
|
|
private:
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Iterator(_AVL_TREE_MAP_CLASS_NAME* parent,
|
|
|
|
const AVLTreeIterator& treeIterator)
|
|
|
|
: ConstIterator(parent, treeIterator)
|
|
|
|
{
|
|
|
|
}
|
2009-11-05 20:18:12 +03:00
|
|
|
|
2003-06-28 19:49:33 +04:00
|
|
|
friend class _AVL_TREE_MAP_CLASS_NAME;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// ConstIterator
|
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
|
|
|
class _AVL_TREE_MAP_CLASS_NAME::ConstIterator {
|
2003-06-28 19:49:33 +04:00
|
|
|
public:
|
2007-08-11 18:57:25 +04:00
|
|
|
inline ConstIterator()
|
2003-06-28 19:49:33 +04:00
|
|
|
: fParent(NULL),
|
2007-08-11 18:57:25 +04:00
|
|
|
fTreeIterator()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline ConstIterator(const ConstIterator& other)
|
2003-06-28 19:49:33 +04:00
|
|
|
: fParent(other.fParent),
|
2007-08-11 18:57:25 +04:00
|
|
|
fTreeIterator(other.fTreeIterator)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline bool HasCurrent() const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fTreeIterator.Current();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Key CurrentKey()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (AVLTreeNode* node = fTreeIterator.Current())
|
|
|
|
return fParent->_GetKey(fParent->_GetNode(node));
|
|
|
|
return Key();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Value Current()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (AVLTreeNode* node = fTreeIterator.Current())
|
|
|
|
return fParent->_GetValue(fParent->_GetNode(node));
|
|
|
|
return Value();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Value* CurrentValuePointer()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (AVLTreeNode* node = fTreeIterator.Current())
|
|
|
|
return &fParent->_GetValue(fParent->_GetNode(node));
|
|
|
|
return NULL;
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline bool HasNext() const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fTreeIterator.HasNext();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Value Next()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (AVLTreeNode* node = fTreeIterator.Next())
|
|
|
|
return fParent->_GetValue(fParent->_GetNode(node));
|
|
|
|
return Value();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2009-11-05 20:18:12 +03:00
|
|
|
inline Value* NextValuePointer()
|
|
|
|
{
|
|
|
|
if (AVLTreeNode* node = fTreeIterator.Next())
|
|
|
|
return &fParent->_GetValue(fParent->_GetNode(node));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Value Previous()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (AVLTreeNode* node = fTreeIterator.Previous())
|
|
|
|
return fParent->_GetValue(fParent->_GetNode(node));
|
|
|
|
return Value();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline ConstIterator& operator=(const ConstIterator& other)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
fParent = other.fParent;
|
|
|
|
fTreeIterator = other.fTreeIterator;
|
|
|
|
return *this;
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
protected:
|
|
|
|
inline ConstIterator(const _AVL_TREE_MAP_CLASS_NAME* parent,
|
|
|
|
const AVLTreeIterator& treeIterator)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
fParent = parent;
|
|
|
|
fTreeIterator = treeIterator;
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
friend class _AVL_TREE_MAP_CLASS_NAME;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
const _AVL_TREE_MAP_CLASS_NAME* fParent;
|
|
|
|
AVLTreeIterator fTreeIterator;
|
2003-06-28 19:49:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// constructor
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
_AVL_TREE_MAP_CLASS_NAME::AVLTreeMap(const NodeStrategy& strategy)
|
|
|
|
: fTree(this),
|
|
|
|
fStrategy(strategy)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// destructor
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
_AVL_TREE_MAP_CLASS_NAME::~AVLTreeMap()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// MakeEmpty
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline void
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::MakeEmpty()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
AVLTreeNode* root = fTree.Root();
|
|
|
|
_FreeTree(root);
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// RootNode
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline typename _AVL_TREE_MAP_CLASS_NAME::Node*
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::RootNode() const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (AVLTreeNode* root = fTree.Root())
|
|
|
|
return _GetNode(root);
|
|
|
|
return NULL;
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// GetIterator
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline typename _AVL_TREE_MAP_CLASS_NAME::Iterator
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::GetIterator()
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return Iterator(this, fTree.GetIterator());
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// GetIterator
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline typename _AVL_TREE_MAP_CLASS_NAME::ConstIterator
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::GetIterator() const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return ConstIterator(this, fTree.GetIterator());
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// GetIterator
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline typename _AVL_TREE_MAP_CLASS_NAME::Iterator
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::GetIterator(Node* node)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return Iterator(this, fTree.GetIterator(_GetAVLTreeNode(node)));
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// GetIterator
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline typename _AVL_TREE_MAP_CLASS_NAME::ConstIterator
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::GetIterator(Node* node) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return ConstIterator(this, fTree.GetIterator(_GetAVLTreeNode(node)));
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// Find
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
typename _AVL_TREE_MAP_CLASS_NAME::Iterator
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::Find(const Key& key)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (AVLTreeNode* node = fTree.Find(&key))
|
|
|
|
return Iterator(this, fTree.GetIterator(node));
|
|
|
|
return Iterator();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// FindClose
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
typename _AVL_TREE_MAP_CLASS_NAME::Iterator
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::FindClose(const Key& key, bool less)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2009-12-06 18:59:37 +03:00
|
|
|
if (AVLTreeNode* node = fTree.FindClosest(&key, less))
|
2007-08-11 18:57:25 +04:00
|
|
|
return Iterator(this, fTree.GetIterator(node));
|
|
|
|
return Iterator();
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// Insert
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
status_t
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::Insert(const Key& key, const Value& value,
|
|
|
|
Iterator* iterator)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
// allocate a node
|
|
|
|
Node* userNode = _Allocate(key, value);
|
|
|
|
if (!userNode)
|
|
|
|
return B_NO_MEMORY;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// insert node
|
|
|
|
AVLTreeNode* node = _GetAVLTreeNode(userNode);
|
|
|
|
status_t error = fTree.Insert(node);
|
|
|
|
if (error != B_OK) {
|
|
|
|
_Free(userNode);
|
|
|
|
return error;
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
if (iterator)
|
|
|
|
*iterator = Iterator(this, fTree.GetIterator(node));
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
return B_OK;
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
2003-06-28 19:49:33 +04:00
|
|
|
// Remove
|
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
status_t
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::Remove(const Key& key)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
AVLTreeNode* node = fTree.Remove(&key);
|
|
|
|
if (!node)
|
|
|
|
return B_ENTRY_NOT_FOUND;
|
2003-06-28 19:49:33 +04:00
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
_Free(_GetNode(node));
|
|
|
|
return B_OK;
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// CompareKeyNode
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
int
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::CompareKeyNode(const void* key,
|
|
|
|
const AVLTreeNode* node)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return _CompareKeyNode(*(const Key*)key, _GetNode(node));
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// CompareNodes
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
|
|
|
int
|
2007-08-11 18:57:25 +04:00
|
|
|
_AVL_TREE_MAP_CLASS_NAME::CompareNodes(const AVLTreeNode* node1,
|
|
|
|
const AVLTreeNode* node2)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return _CompareNodes(_GetNode(node1), _GetNode(node2));
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// _Allocate
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 23:53:20 +04:00
|
|
|
inline typename _AVL_TREE_MAP_CLASS_NAME::Node*
|
2007-08-11 18:57:25 +04:00
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_Allocate(const Key& key, const Value& value)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fStrategy.Allocate(key, value);
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// _Free
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline void
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_Free(Node* node)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
fStrategy.Free(node);
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// _GetKey
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2009-11-05 20:18:12 +03:00
|
|
|
inline const Key&
|
2007-08-11 18:57:25 +04:00
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_GetKey(Node* node) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fStrategy.GetKey(node);
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// _GetValue
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Value&
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_GetValue(Node* node) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fStrategy.GetValue(node);
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// _GetAVLTreeNode
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline AVLTreeNode*
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_GetAVLTreeNode(const Node* node) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fStrategy.GetAVLTreeNode(const_cast<Node*>(node));
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
|
|
|
// _GetNode
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 23:53:20 +04:00
|
|
|
inline typename _AVL_TREE_MAP_CLASS_NAME::Node*
|
2007-08-11 18:57:25 +04:00
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_GetNode(const AVLTreeNode* node) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fStrategy.GetNode(const_cast<AVLTreeNode*>(node));
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
|
|
|
// _CompareKeyNode
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline int
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_CompareKeyNode(const Key& a, const Node* b)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fStrategy.CompareKeyNode(a, b);
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
|
|
|
// _CompareNodes
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
inline int
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_CompareNodes(const Node* a, const Node* b)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fStrategy.CompareNodes(a, b);
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
|
|
|
// _FreeTree
|
2003-06-28 19:49:33 +04:00
|
|
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
2007-08-11 18:57:25 +04:00
|
|
|
void
|
|
|
|
_AVL_TREE_MAP_CLASS_NAME::_FreeTree(AVLTreeNode* node)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
if (node) {
|
|
|
|
_FreeTree(node->left);
|
|
|
|
_FreeTree(node->right);
|
|
|
|
_Free(_GetNode(node));
|
|
|
|
}
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
// #pragma mark - strategy parameters
|
2003-06-28 19:49:33 +04:00
|
|
|
|
|
|
|
// Ascending
|
|
|
|
namespace AVLTreeMapStrategy {
|
|
|
|
template<typename Value>
|
|
|
|
class Ascending {
|
2007-08-11 18:57:25 +04:00
|
|
|
public:
|
2003-06-28 19:49:33 +04:00
|
|
|
inline int operator()(const Value &a, const Value &b) const
|
|
|
|
{
|
|
|
|
if (a < b)
|
|
|
|
return -1;
|
|
|
|
else if (a > b)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
2003-06-28 19:49:33 +04:00
|
|
|
// Descending
|
|
|
|
namespace AVLTreeMapStrategy {
|
|
|
|
template<typename Value>
|
|
|
|
class Descending {
|
2007-08-11 18:57:25 +04:00
|
|
|
public:
|
2003-06-28 19:49:33 +04:00
|
|
|
inline int operator()(const Value &a, const Value &b) const
|
|
|
|
{
|
|
|
|
if (a < b)
|
|
|
|
return -1;
|
|
|
|
else if (a > b)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
|
|
|
|
// #pragma mark - strategies
|
|
|
|
|
2003-06-28 19:49:33 +04:00
|
|
|
|
|
|
|
// Auto
|
|
|
|
namespace AVLTreeMapStrategy {
|
|
|
|
template <typename Key, typename Value, typename KeyOrder,
|
|
|
|
template <typename> class Allocator>
|
|
|
|
class Auto {
|
|
|
|
public:
|
2007-08-11 18:57:25 +04:00
|
|
|
struct Node : AVLTreeNode {
|
2003-06-28 19:49:33 +04:00
|
|
|
Node(const Key &key, const Value &value)
|
2007-08-11 18:57:25 +04:00
|
|
|
: AVLTreeNode(),
|
|
|
|
key(key),
|
|
|
|
value(value)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Key key;
|
|
|
|
Value value;
|
|
|
|
};
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Node* Allocate(const Key& key, const Value& value)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
Node* result = fAllocator.Allocate();
|
2003-06-28 19:49:33 +04:00
|
|
|
if (result)
|
|
|
|
fAllocator.Construct(result, key, value);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline void Free(Node* node)
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
fAllocator.Destruct(node);
|
|
|
|
fAllocator.Deallocate(node);
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Key& GetKey(Node* node) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
return node->key;
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline Value& GetValue(Node* node) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
|
|
|
return node->value;
|
|
|
|
}
|
|
|
|
|
2007-08-11 18:57:25 +04:00
|
|
|
inline AVLTreeNode* GetAVLTreeNode(Node* node) const
|
|
|
|
{
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Node* GetNode(AVLTreeNode* node) const
|
|
|
|
{
|
|
|
|
return static_cast<Node*>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int CompareKeyNode(const Key& a, const Node* b) const
|
|
|
|
{
|
|
|
|
return fCompare(a, _GetKey(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int CompareNodes(const Node* a, const Node* b) const
|
2003-06-28 19:49:33 +04:00
|
|
|
{
|
2007-08-11 18:57:25 +04:00
|
|
|
return fCompare(_GetKey(a), _GetKey(b));
|
2003-06-28 19:49:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
KeyOrder fCompare;
|
|
|
|
Allocator<Node> fAllocator;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-12-06 18:59:37 +03:00
|
|
|
#endif // _KERNEL_UTIL_AVL_TREE_MAP_H
|