AVLTreeMap/TwoKeyAVLTree: More access to nodes

both:
* Add Previous()/Next().
* Add Insert() version that returns a Node* instead of an Iterator.
* Add Remove() version that takes a Node* instead of a key.

TwoKeyAVLTree:
* Add GetIterator() version that takes an additional Node*, i.e.
  initializing an iterator to point to the node.
* Add Iterator::CurrentNode().
This commit is contained in:
Ingo Weinhold 2011-07-07 07:48:09 +02:00
parent e7346707c9
commit 004b3604a0
2 changed files with 134 additions and 1 deletions

View File

@ -65,6 +65,9 @@ public:
Node* RootNode() const;
Node* Previous(Node* node) const;
Node* Next(Node* node) const;
inline Iterator GetIterator();
inline ConstIterator GetIterator() const;
@ -76,7 +79,10 @@ public:
status_t Insert(const Key& key, const Value& value,
Iterator* iterator);
status_t Insert(const Key& key, const Value& value,
Node** _node = NULL);
status_t Remove(const Key& key);
status_t Remove(Node* node);
const NodeStrategy& GetNodeStrategy() const { return fStrategy; }
@ -185,6 +191,13 @@ public:
return NULL;
}
inline Node* CurrentNode()
{
if (AVLTreeNode* node = fTreeIterator.Current())
return fParent->_GetNode(node);
return NULL;
}
inline bool HasNext() const
{
return fTreeIterator.HasNext();
@ -270,6 +283,32 @@ _AVL_TREE_MAP_CLASS_NAME::RootNode() const
}
// Previous
_AVL_TREE_MAP_TEMPLATE_LIST
inline typename _AVL_TREE_MAP_CLASS_NAME::Node*
_AVL_TREE_MAP_CLASS_NAME::Previous(Node* node) const
{
if (node == NULL)
return NULL;
AVLTreeNode* treeNode = fTree.Previous(_GetAVLTreeNode(node));
return treeNode != NULL ? _GetNode(treeNode) : NULL;
}
// Next
_AVL_TREE_MAP_TEMPLATE_LIST
inline typename _AVL_TREE_MAP_CLASS_NAME::Node*
_AVL_TREE_MAP_CLASS_NAME::Next(Node* node) const
{
if (node == NULL)
return NULL;
AVLTreeNode* treeNode = fTree.Next(_GetAVLTreeNode(node));
return treeNode != NULL ? _GetNode(treeNode) : NULL;
}
// GetIterator
_AVL_TREE_MAP_TEMPLATE_LIST
inline typename _AVL_TREE_MAP_CLASS_NAME::Iterator
@ -354,6 +393,32 @@ _AVL_TREE_MAP_CLASS_NAME::Insert(const Key& key, const Value& value,
}
// Insert
_AVL_TREE_MAP_TEMPLATE_LIST
status_t
_AVL_TREE_MAP_CLASS_NAME::Insert(const Key& key, const Value& value,
Node** _node)
{
// allocate a node
Node* userNode = _Allocate(key, value);
if (!userNode)
return B_NO_MEMORY;
// insert node
AVLTreeNode* node = _GetAVLTreeNode(userNode);
status_t error = fTree.Insert(node);
if (error != B_OK) {
_Free(userNode);
return error;
}
if (_node != NULL)
*_node = userNode;
return B_OK;
}
// Remove
_AVL_TREE_MAP_TEMPLATE_LIST
status_t
@ -368,6 +433,19 @@ _AVL_TREE_MAP_CLASS_NAME::Remove(const Key& key)
}
// Remove
_AVL_TREE_MAP_TEMPLATE_LIST
status_t
_AVL_TREE_MAP_CLASS_NAME::Remove(Node* node)
{
if (!fTree.Remove(node))
return B_ENTRY_NOT_FOUND;
_Free(node);
return B_OK;
}
// CompareKeyNode
_AVL_TREE_MAP_TEMPLATE_LIST
int

View File

@ -274,6 +274,9 @@ public:
inline int CountItems() const { return fTreeMap.Count(); }
Node* Previous(Node* node) const;
Node* Next(Node* node) const;
Value* FindFirst(const PrimaryKey& key,
Iterator* iterator = NULL);
Value* FindLast(const PrimaryKey& key,
@ -283,11 +286,15 @@ public:
Iterator* iterator = NULL);
inline void GetIterator(Iterator* iterator);
inline void GetIterator(Node* node, Iterator* iterator);
inline status_t Insert(const Value& value,
Iterator* iterator = NULL);
Iterator* iterator);
inline status_t Insert(const Value& value,
Node** _node = NULL);
inline status_t Remove(const PrimaryKey& primaryKey,
const SecondaryKey& secondaryKey);
inline status_t Remove(Node* node);
private:
TreeMap fTreeMap;
@ -320,6 +327,11 @@ public:
return fIterator.CurrentValuePointer();
}
inline Node* CurrentNode()
{
return fIterator.CurrentNode();
}
inline Value* Next()
{
fIterator.Next();
@ -398,6 +410,22 @@ TWO_KEY_AVL_TREE_CLASS_NAME::~TwoKeyAVLTree()
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
typename TWO_KEY_AVL_TREE_CLASS_NAME::Node*
TWO_KEY_AVL_TREE_CLASS_NAME::Previous(Node* node) const
{
return fTreeMap.Previous(node);
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
typename TWO_KEY_AVL_TREE_CLASS_NAME::Node*
TWO_KEY_AVL_TREE_CLASS_NAME::Next(Node* node) const
{
return fTreeMap.Next(node);
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
Value*
TWO_KEY_AVL_TREE_CLASS_NAME::FindFirst(const PrimaryKey& key,
@ -484,6 +512,14 @@ TWO_KEY_AVL_TREE_CLASS_NAME::GetIterator(Iterator* iterator)
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
void
TWO_KEY_AVL_TREE_CLASS_NAME::GetIterator(Node* node, Iterator* iterator)
{
iterator->_SetTo(fTreeMap.GetIterator(node));
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
status_t
TWO_KEY_AVL_TREE_CLASS_NAME::Insert(const Value& value, Iterator* iterator)
@ -501,6 +537,17 @@ TWO_KEY_AVL_TREE_CLASS_NAME::Insert(const Value& value, Iterator* iterator)
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
status_t
TWO_KEY_AVL_TREE_CLASS_NAME::Insert(const Value& value, Node** _node)
{
NodeStrategy& strategy
= const_cast<NodeStrategy&>(fTreeMap.GetNodeStrategy());
return fTreeMap.Insert(strategy.GetValueKey(value), value, _node);
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
status_t
TWO_KEY_AVL_TREE_CLASS_NAME::Remove(const PrimaryKey& primaryKey,
@ -510,4 +557,12 @@ TWO_KEY_AVL_TREE_CLASS_NAME::Remove(const PrimaryKey& primaryKey,
}
TWO_KEY_AVL_TREE_TEMPLATE_LIST
status_t
TWO_KEY_AVL_TREE_CLASS_NAME::Remove(Node* node)
{
return fTreeMap.Remove(node);
}
#endif // _KERNEL_UTIL_TWO_KEY_AVL_TREE_H