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:
@@ -65,6 +65,9 @@ public:
|
|||||||
|
|
||||||
Node* RootNode() const;
|
Node* RootNode() const;
|
||||||
|
|
||||||
|
Node* Previous(Node* node) const;
|
||||||
|
Node* Next(Node* node) const;
|
||||||
|
|
||||||
inline Iterator GetIterator();
|
inline Iterator GetIterator();
|
||||||
inline ConstIterator GetIterator() const;
|
inline ConstIterator GetIterator() const;
|
||||||
|
|
||||||
@@ -76,7 +79,10 @@ public:
|
|||||||
|
|
||||||
status_t Insert(const Key& key, const Value& value,
|
status_t Insert(const Key& key, const Value& value,
|
||||||
Iterator* iterator);
|
Iterator* iterator);
|
||||||
|
status_t Insert(const Key& key, const Value& value,
|
||||||
|
Node** _node = NULL);
|
||||||
status_t Remove(const Key& key);
|
status_t Remove(const Key& key);
|
||||||
|
status_t Remove(Node* node);
|
||||||
|
|
||||||
const NodeStrategy& GetNodeStrategy() const { return fStrategy; }
|
const NodeStrategy& GetNodeStrategy() const { return fStrategy; }
|
||||||
|
|
||||||
@@ -185,6 +191,13 @@ public:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Node* CurrentNode()
|
||||||
|
{
|
||||||
|
if (AVLTreeNode* node = fTreeIterator.Current())
|
||||||
|
return fParent->_GetNode(node);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
inline bool HasNext() const
|
inline bool HasNext() const
|
||||||
{
|
{
|
||||||
return fTreeIterator.HasNext();
|
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
|
// GetIterator
|
||||||
_AVL_TREE_MAP_TEMPLATE_LIST
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
||||||
inline typename _AVL_TREE_MAP_CLASS_NAME::Iterator
|
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
|
// Remove
|
||||||
_AVL_TREE_MAP_TEMPLATE_LIST
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
||||||
status_t
|
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
|
// CompareKeyNode
|
||||||
_AVL_TREE_MAP_TEMPLATE_LIST
|
_AVL_TREE_MAP_TEMPLATE_LIST
|
||||||
int
|
int
|
||||||
|
|||||||
@@ -274,6 +274,9 @@ public:
|
|||||||
|
|
||||||
inline int CountItems() const { return fTreeMap.Count(); }
|
inline int CountItems() const { return fTreeMap.Count(); }
|
||||||
|
|
||||||
|
Node* Previous(Node* node) const;
|
||||||
|
Node* Next(Node* node) const;
|
||||||
|
|
||||||
Value* FindFirst(const PrimaryKey& key,
|
Value* FindFirst(const PrimaryKey& key,
|
||||||
Iterator* iterator = NULL);
|
Iterator* iterator = NULL);
|
||||||
Value* FindLast(const PrimaryKey& key,
|
Value* FindLast(const PrimaryKey& key,
|
||||||
@@ -283,11 +286,15 @@ public:
|
|||||||
Iterator* iterator = NULL);
|
Iterator* iterator = NULL);
|
||||||
|
|
||||||
inline void GetIterator(Iterator* iterator);
|
inline void GetIterator(Iterator* iterator);
|
||||||
|
inline void GetIterator(Node* node, Iterator* iterator);
|
||||||
|
|
||||||
inline status_t Insert(const Value& value,
|
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,
|
inline status_t Remove(const PrimaryKey& primaryKey,
|
||||||
const SecondaryKey& secondaryKey);
|
const SecondaryKey& secondaryKey);
|
||||||
|
inline status_t Remove(Node* node);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TreeMap fTreeMap;
|
TreeMap fTreeMap;
|
||||||
@@ -320,6 +327,11 @@ public:
|
|||||||
return fIterator.CurrentValuePointer();
|
return fIterator.CurrentValuePointer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Node* CurrentNode()
|
||||||
|
{
|
||||||
|
return fIterator.CurrentNode();
|
||||||
|
}
|
||||||
|
|
||||||
inline Value* Next()
|
inline Value* Next()
|
||||||
{
|
{
|
||||||
fIterator.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
|
TWO_KEY_AVL_TREE_TEMPLATE_LIST
|
||||||
Value*
|
Value*
|
||||||
TWO_KEY_AVL_TREE_CLASS_NAME::FindFirst(const PrimaryKey& key,
|
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
|
TWO_KEY_AVL_TREE_TEMPLATE_LIST
|
||||||
status_t
|
status_t
|
||||||
TWO_KEY_AVL_TREE_CLASS_NAME::Insert(const Value& value, Iterator* iterator)
|
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
|
TWO_KEY_AVL_TREE_TEMPLATE_LIST
|
||||||
status_t
|
status_t
|
||||||
TWO_KEY_AVL_TREE_CLASS_NAME::Remove(const PrimaryKey& primaryKey,
|
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
|
#endif // _KERNEL_UTIL_TWO_KEY_AVL_TREE_H
|
||||||
|
|||||||
Reference in New Issue
Block a user