Class Tree
- All Implemented Interfaces:
Proxy
-
Constructor Summary
ConstructorsConstructorDescriptionTree(MemorySegment address) Create a Tree proxy instance for the provided memory address.Tree(@Nullable CompareFunc keyCompareFunc) Creates a newGTree. -
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()Removes all keys and values from theGTreeand decreases its reference count by one.voidforeach(@Nullable TraverseFunc func) Calls the given function for each of the key/value pairs in theGTree.voidforeachNode(@Nullable TraverseNodeFunc func) Calls the given function for each of the nodes in theGTree.static Treefull(@Nullable CompareDataFunc keyCompareFunc) Creates a newGTreelike g_tree_new() and allows to specify functions to free the memory allocated for the key and value that get called when removing the entry from theGTree.static @Nullable TypegetType()Get the GType of the Tree classintheight()Gets the height of aGTree.voidinsert(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a key/value pair into aGTree.@Nullable TreeNodeinsertNode(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a key/value pair into aGTree.@Nullable MemorySegmentlookup(@Nullable MemorySegment key) Gets the value corresponding to the given key.booleanlookupExtended(@Nullable MemorySegment lookupKey, @Nullable Out<MemorySegment> origKey, @Nullable Out<MemorySegment> value) Looks up a key in theGTree, returning the original key and the associated value.@Nullable TreeNodelookupNode(@Nullable MemorySegment key) Gets the tree node corresponding to the given key.@Nullable TreeNodelowerBound(@Nullable MemorySegment key) Gets the lower bound node corresponding to the given key, ornullif the tree is empty or all the nodes in the tree have keys that are strictly lower than the searched key.intnnodes()Gets the number of nodes in aGTree.@Nullable TreeNodeReturns the first in-order node of the tree, ornullfor an empty tree.@Nullable TreeNodenodeLast()Returns the last in-order node of the tree, ornullfor an empty tree.ref()Increments the reference count of this Tree by one.booleanremove(@Nullable MemorySegment key) Removes a key/value pair from aGTree.voidRemoves all nodes from aGTreeand destroys their keys and values, then resets theGTree’s root tonull.voidreplace(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a new key and value into aGTreeas g_tree_replace_node() does, only this function does not return the inserted or set node.@Nullable TreeNodereplaceNode(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a new key and value into aGTreesimilar to g_tree_insert_node().@Nullable MemorySegmentsearch(@Nullable CompareFunc searchFunc) Searches aGTreeusingsearchFunc.@Nullable TreeNodesearchNode(@Nullable CompareFunc searchFunc) Searches aGTreeusingsearchFunc.booleansteal(@Nullable MemorySegment key) Removes a key and its associated value from aGTreewithout calling the key and value destroy functions.voidtraverse(@Nullable TraverseFunc traverseFunc, TraverseType traverseType) Deprecated.The order of a balanced tree is somewhat arbitrary.voidunref()Decrements the reference count of this Tree by one.@Nullable TreeNodeupperBound(@Nullable MemorySegment key) Gets the upper bound node corresponding to the given key, ornullif the tree is empty or all the nodes in the tree have keys that are lower than or equal to the searched key.static TreewithData(@Nullable CompareDataFunc keyCompareFunc) Creates a newGTreewith a comparison function that accepts user data.Methods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Tree
Create a Tree proxy instance for the provided memory address.- Parameters:
address- the memory address of the native object
-
Tree
Creates a newGTree.- Parameters:
keyCompareFunc- the function used to order the nodes in theGTree. It should return values similar to the standard strcmp() function - 0 if the two arguments are equal, a negative value if the first argument comes before the second, or a positive value if the first argument comes after the second.
-
-
Method Details
-
getType
-
full
Creates a newGTreelike g_tree_new() and allows to specify functions to free the memory allocated for the key and value that get called when removing the entry from theGTree.- Parameters:
keyCompareFunc- qsort()-style comparison function- Returns:
- a newly allocated
GTree
-
withData
Creates a newGTreewith a comparison function that accepts user data. See g_tree_new() for more details.- Parameters:
keyCompareFunc- qsort()-style comparison function- Returns:
- a newly allocated
GTree
-
destroy
public void destroy()Removes all keys and values from theGTreeand decreases its reference count by one. If keys and/or values are dynamically allocated, you should either free them first or create theGTreeusing g_tree_new_full(). In the latter case the destroy functions you supplied will be called on all keys and values before destroying theGTree. -
foreach
Calls the given function for each of the key/value pairs in theGTree. The function is passed the key and value of each pair, and the givendataparameter. The tree is traversed in sorted order.The tree may not be modified while iterating over it (you can't add/remove items). To remove all items matching a predicate, you need to add each item to a list in your
GTraverseFuncas you walk over the tree, then walk the list and remove each item.- Parameters:
func- the function to call for each node visited. If this function returnstrue, the traversal is stopped.
-
foreachNode
Calls the given function for each of the nodes in theGTree. The function is passed the pointer to the particular node, and the givendataparameter. The tree traversal happens in-order.The tree may not be modified while iterating over it (you can't add/remove items). To remove all items matching a predicate, you need to add each item to a list in your
GTraverseFuncas you walk over the tree, then walk the list and remove each item.- Parameters:
func- the function to call for each node visited. If this function returnstrue, the traversal is stopped.- Since:
- 2.68
-
height
public int height()Gets the height of aGTree.If the
GTreecontains no nodes, the height is 0. If theGTreecontains only one root node the height is 1. If the root node has children the height is 2, etc.- Returns:
- the height of this Tree
-
insert
Inserts a key/value pair into aGTree.Inserts a new key and value into a
GTreeas g_tree_insert_node() does, only this function does not return the inserted or set node.- Parameters:
key- the key to insertvalue- the value corresponding to the key
-
insertNode
Inserts a key/value pair into aGTree.If the given key already exists in the
GTreeits corresponding value is set to the new value. If you supplied avalueDestroyFuncwhen creating theGTree, the old value is freed using that function. If you supplied akeyDestroyFuncwhen creating theGTree, the passed key is freed using that function.The tree is automatically 'balanced' as new key/value pairs are added, so that the distance from the root to every leaf is as small as possible. The cost of maintaining a balanced tree while inserting new key/value result in a O(n log(n)) operation where most of the other operations are O(log(n)).
- Parameters:
key- the key to insertvalue- the value corresponding to the key- Returns:
- the inserted (or set) node or
nullif insertion would overflow the tree node counter. - Since:
- 2.68
-
lookup
Gets the value corresponding to the given key. Since aGTreeis automatically balanced as key/value pairs are added, key lookup is O(log n) (where n is the number of key/value pairs in the tree).- Parameters:
key- the key to look up- Returns:
- the value corresponding to the key, or
nullif the key was not found
-
lookupExtended
public boolean lookupExtended(@Nullable MemorySegment lookupKey, @Nullable Out<MemorySegment> origKey, @Nullable Out<MemorySegment> value) Looks up a key in theGTree, returning the original key and the associated value. This is useful if you need to free the memory allocated for the original key, for example before calling g_tree_remove().- Parameters:
lookupKey- the key to look uporigKey- returns the original keyvalue- returns the value associated with the key- Returns:
trueif the key was found in theGTree
-
lookupNode
Gets the tree node corresponding to the given key. Since aGTreeis automatically balanced as key/value pairs are added, key lookup is O(log n) (where n is the number of key/value pairs in the tree).- Parameters:
key- the key to look up- Returns:
- the tree node corresponding to
the key, or
nullif the key was not found - Since:
- 2.68
-
lowerBound
Gets the lower bound node corresponding to the given key, ornullif the tree is empty or all the nodes in the tree have keys that are strictly lower than the searched key.The lower bound is the first node that has its key greater than or equal to the searched key.
- Parameters:
key- the key to calculate the lower bound for- Returns:
- the tree node corresponding to
the lower bound, or
nullif the tree is empty or has only keys strictly lower than the searched key. - Since:
- 2.68
-
nnodes
public int nnodes()Gets the number of nodes in aGTree.- Returns:
- the number of nodes in this Tree
The node counter value type is really a
guint, but it is returned as agintdue to backward compatibility issues (can be cast back toguintto support its full range of values).
-
nodeFirst
Returns the first in-order node of the tree, ornullfor an empty tree.- Returns:
- the first node in the tree
- Since:
- 2.68
-
nodeLast
Returns the last in-order node of the tree, ornullfor an empty tree.- Returns:
- the last node in the tree
- Since:
- 2.68
-
ref
Increments the reference count of this Tree by one.It is safe to call this function from any thread.
- Returns:
- the passed in
GTree - Since:
- 2.22
-
remove
Removes a key/value pair from aGTree.If the
GTreewas created using g_tree_new_full(), the key and value are freed using the supplied destroy functions, otherwise you have to make sure that any dynamically allocated values are freed yourself. If the key does not exist in theGTree, the function does nothing.The cost of maintaining a balanced tree while removing a key/value result in a O(n log(n)) operation where most of the other operations are O(log(n)).
- Parameters:
key- the key to remove- Returns:
trueif the key was found (prior to 2.8, this function returned nothing)
-
removeAll
public void removeAll()Removes all nodes from aGTreeand destroys their keys and values, then resets theGTree’s root tonull.- Since:
- 2.70
-
replace
Inserts a new key and value into aGTreeas g_tree_replace_node() does, only this function does not return the inserted or set node.- Parameters:
key- the key to insertvalue- the value corresponding to the key
-
replaceNode
Inserts a new key and value into aGTreesimilar to g_tree_insert_node(). The difference is that if the key already exists in theGTree, it gets replaced by the new key. If you supplied avalueDestroyFuncwhen creating theGTree, the old value is freed using that function. If you supplied akeyDestroyFuncwhen creating theGTree, the old key is freed using that function.The tree is automatically 'balanced' as new key/value pairs are added, so that the distance from the root to every leaf is as small as possible.
- Parameters:
key- the key to insertvalue- the value corresponding to the key- Returns:
- the inserted (or set) node or
nullif insertion would overflow the tree node counter. - Since:
- 2.68
-
search
Searches aGTreeusingsearchFunc.The
searchFuncis called with a pointer to the key of a key/value pair in the tree, and the passed inuserData.IfsearchFuncreturns 0 for a key/value pair, then the corresponding value is returned as the result of g_tree_search(). IfsearchFuncreturns -1, searching will proceed among the key/value pairs that have a smaller key; ifsearchFuncreturns 1, searching will proceed among the key/value pairs that have a larger key.- Parameters:
searchFunc- a function used to search theGTree- Returns:
- the value corresponding to the found key, or
nullif the key was not found
-
searchNode
Searches aGTreeusingsearchFunc.The
searchFuncis called with a pointer to the key of a key/value pair in the tree, and the passed inuserData.IfsearchFuncreturns 0 for a key/value pair, then the corresponding node is returned as the result of g_tree_search(). IfsearchFuncreturns -1, searching will proceed among the key/value pairs that have a smaller key; ifsearchFuncreturns 1, searching will proceed among the key/value pairs that have a larger key.- Parameters:
searchFunc- a function used to search theGTree- Returns:
- the node corresponding to the
found key, or
nullif the key was not found - Since:
- 2.68
-
steal
Removes a key and its associated value from aGTreewithout calling the key and value destroy functions.If the key does not exist in the
GTree, the function does nothing.- Parameters:
key- the key to remove- Returns:
trueif the key was found (prior to 2.8, this function returned nothing)
-
traverse
Deprecated.The order of a balanced tree is somewhat arbitrary. If you just want to visit all nodes in sorted order, use g_tree_foreach() instead. If you really need to visit nodes in a different order, consider using an n-ary tree.Calls the given function for each node in theGTree.- Parameters:
traverseFunc- the function to call for each node visited. If this function returnstrue, the traversal is stopped.traverseType- the order in which nodes are visited, one ofTraverseType.IN_ORDER,TraverseType.PRE_ORDERandTraverseType.POST_ORDER
-
unref
public void unref()Decrements the reference count of this Tree by one. If the reference count drops to 0, all keys and values will be destroyed (if destroy functions were specified) and all memory allocated by this Tree will be released.It is safe to call this function from any thread.
- Since:
- 2.22
-
upperBound
Gets the upper bound node corresponding to the given key, ornullif the tree is empty or all the nodes in the tree have keys that are lower than or equal to the searched key.The upper bound is the first node that has its key strictly greater than the searched key.
- Parameters:
key- the key to calculate the upper bound for- Returns:
- the tree node corresponding to the
upper bound, or
nullif the tree is empty or has only keys lower than or equal to the searched key. - Since:
- 2.68
-