Class Tree
- All Implemented Interfaces:
Proxy
-
Constructor Summary
ConstructorDescriptionTree
(MemorySegment address) Create a Tree proxy instance for the provided memory address.Tree
(CompareFunc keyCompareFunc) Creates a newGTree
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
destroy()
Removes all keys and values from theGTree
and decreases its reference count by one.void
foreach
(TraverseFunc func) Calls the given function for each of the key/value pairs in theGTree
.void
foreachNode
(TraverseNodeFunc func) Calls the given function for each of the nodes in theGTree
.static Tree
full
(CompareDataFunc keyCompareFunc) Creates a newGTree
like 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 Type
getType()
Get the GType of the Tree classint
height()
Gets the height of aGTree
.void
insert
(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a key/value pair into aGTree
.insertNode
(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a key/value pair into aGTree
.lookup
(@Nullable MemorySegment key) Gets the value corresponding to the given key.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.lookupNode
(@Nullable MemorySegment key) Gets the tree node corresponding to the given key.lowerBound
(@Nullable MemorySegment key) Gets the lower bound node corresponding to the given key, ornull
if the tree is empty or all the nodes in the tree have keys that are strictly lower than the searched key.int
nnodes()
Gets the number of nodes in aGTree
.Returns the first in-order node of the tree, ornull
for an empty tree.nodeLast()
Returns the last in-order node of the tree, ornull
for an empty tree.ref()
Increments the reference count of this Tree by one.boolean
remove
(@Nullable MemorySegment key) Removes a key/value pair from aGTree
.void
Removes all nodes from aGTree
and destroys their keys and values, then resets theGTree
’s root tonull
.void
replace
(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a new key and value into aGTree
as g_tree_replace_node() does, only this function does not return the inserted or set node.replaceNode
(@Nullable MemorySegment key, @Nullable MemorySegment value) Inserts a new key and value into aGTree
similar to g_tree_insert_node().search
(CompareFunc searchFunc) Searches aGTree
usingsearchFunc
.searchNode
(CompareFunc searchFunc) Searches aGTree
usingsearchFunc
.boolean
steal
(@Nullable MemorySegment key) Removes a key and its associated value from aGTree
without calling the key and value destroy functions.void
traverse
(TraverseFunc traverseFunc, TraverseType traverseType) Deprecated.The order of a balanced tree is somewhat arbitrary.void
unref()
Decrements the reference count of this Tree by one.upperBound
(@Nullable MemorySegment key) Gets the upper bound node corresponding to the given key, ornull
if the tree is empty or all the nodes in the tree have keys that are lower than or equal to the searched key.static Tree
withData
(CompareDataFunc keyCompareFunc) Creates a newGTree
with a comparison function that accepts user data.Methods inherited from class io.github.jwharm.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 newGTree
like 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 newGTree
with 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 theGTree
and decreases its reference count by one. If keys and/or values are dynamically allocated, you should either free them first or create theGTree
using 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 givendata
parameter. 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
GTraverseFunc
as 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 givendata
parameter. 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
GTraverseFunc
as 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.
-
height
public int height()Gets the height of aGTree
.If the
GTree
contains no nodes, the height is 0. If theGTree
contains 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
GTree
as 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
public TreeNode insertNode(@Nullable @Nullable MemorySegment key, @Nullable @Nullable MemorySegment value) Inserts a key/value pair into aGTree
.If the given key already exists in the
GTree
its corresponding value is set to the new value. If you supplied avalueDestroyFunc
when creating theGTree
, the old value is freed using that function. If you supplied akeyDestroyFunc
when 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
null
if insertion would overflow the tree node counter.
-
lookup
Gets the value corresponding to the given key. Since aGTree
is 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
null
if the key was not found
-
lookupExtended
public boolean lookupExtended(@Nullable @Nullable MemorySegment lookupKey, @Nullable @Nullable Out<MemorySegment> origKey, @Nullable @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:
true
if the key was found in theGTree
-
lookupNode
Gets the tree node corresponding to the given key. Since aGTree
is 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
null
if the key was not found
-
lowerBound
Gets the lower bound node corresponding to the given key, ornull
if 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
null
if the tree is empty or has only keys strictly lower than the searched key.
-
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 agint
due to backward compatibility issues (can be cast back toguint
to support its full range of values).
-
nodeFirst
Returns the first in-order node of the tree, ornull
for an empty tree.- Returns:
- the first node in the tree
-
nodeLast
Returns the last in-order node of the tree, ornull
for an empty tree.- Returns:
- the last node in the tree
-
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
-
remove
Removes a key/value pair from aGTree
.If the
GTree
was 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:
true
if the key was found (prior to 2.8, this function returned nothing)
-
removeAll
public void removeAll()Removes all nodes from aGTree
and destroys their keys and values, then resets theGTree
’s root tonull
. -
replace
Inserts a new key and value into aGTree
as 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
public TreeNode replaceNode(@Nullable @Nullable MemorySegment key, @Nullable @Nullable MemorySegment value) Inserts a new key and value into aGTree
similar 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 avalueDestroyFunc
when creating theGTree
, the old value is freed using that function. If you supplied akeyDestroyFunc
when 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
null
if insertion would overflow the tree node counter.
-
search
Searches aGTree
usingsearchFunc
.The
searchFunc
is called with a pointer to the key of a key/value pair in the tree, and the passed inuserData
. IfsearchFunc
returns 0 for a key/value pair, then the corresponding value is returned as the result of g_tree_search(). IfsearchFunc
returns -1, searching will proceed among the key/value pairs that have a smaller key; ifsearchFunc
returns 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
null
if the key was not found
-
searchNode
Searches aGTree
usingsearchFunc
.The
searchFunc
is called with a pointer to the key of a key/value pair in the tree, and the passed inuserData
. IfsearchFunc
returns 0 for a key/value pair, then the corresponding node is returned as the result of g_tree_search(). IfsearchFunc
returns -1, searching will proceed among the key/value pairs that have a smaller key; ifsearchFunc
returns 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
null
if the key was not found
-
steal
Removes a key and its associated value from aGTree
without 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:
true
if 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][glib-N-ary-Trees].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_ORDER
andTraverseType.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.
-
upperBound
Gets the upper bound node corresponding to the given key, ornull
if 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
null
if the tree is empty or has only keys lower than or equal to the searched key.
-