1.30 <GTree>

Structure: <GTree>

The GTree struct is an opaque data structure representing a balanced binary tree. It should be accessed only by using the following functions.

Procedures

Method: tree:destroy self
Method: destroy self

Removes all keys and values from the <GTree> and decreases its reference count by one. If keys and/or values are dynamically allocated, you should either free them first or create the <GTree> using tree:new-full. In the latter case the destroy functions you supplied will be called on all keys and values before destroying the <GTree>.

self

Passed: a <GTree>.

a <GTree>

Method: tree:foreach self func
Method: foreach self func

Calls the given function for each of the key/value pairs in the <GTree>. The function is passed the key and value of each pair, and the given data 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.

self

Passed: a <GTree>.

a <GTree>

func

Passed: a procedure.

the function to call for each node visited. If this function returns #t, the traversal is stopped.

See GTraverseFunc for how it is called.

user-data

Not passed: guile-gi supplies it.

user data to pass to the function

Method: tree:height self ⇒ %return
Method: height self ⇒ %return

Gets the height of a <GTree>.

If the <GTree> contains no nodes, the height is 0. If the <GTree> contains only one root node the height is 1. If the root node has children the height is 2, etc.

self

Passed: a <GTree>.

a <GTree>

%return

Returned: a 32-bit integer.

the height of self

Method: tree:insert self [key] [value]
Method: insert self [key] [value]

Inserts a key/value pair into a <GTree>.

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.

self

Passed: a <GTree>.

a <GTree>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to insert

value

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the value corresponding to the key

Method: tree:lookup self [key] ⇒ %return
Method: lookup self [key] ⇒ %return

Gets the value corresponding to the given key. Since a <GTree> 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).

self

Passed: a <GTree>.

a <GTree>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to look up

%return

Returned: a foreign pointer, or #f.

the value corresponding to the key, or #f if the key was not found

Method: tree:lookup-extended self [lookup-key] ⇒ %return orig-key value
Method: lookup-extended self [lookup-key] ⇒ %return orig-key value

Looks up a key in the <GTree>, 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 tree:remove?.

self

Passed: a <GTree>.

a <GTree>

lookup-key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to look up

%return

Returned: a boolean.

#t if the key was found in the <GTree>

orig-key

Returned: a foreign pointer, or #f.

returns the original key

value

Returned: a foreign pointer, or #f.

returns the value associated with the key

Procedure: tree:new-full key-compare-func key-destroy-func ⇒ %return

Creates a new <GTree> 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 the <GTree>.

key-compare-func

Passed: a procedure.

qsort()-style comparison function

See GCompareDataFunc for how it is called.

key-destroy-func

Passed: a procedure.

a function to free the memory allocated for the key used when removing the entry from the <GTree> or #f if you don’t want to supply such a function

See GDestroyNotify for how it is called.

%return

Returned: a <GTree>.

a newly allocated <GTree>

key-compare-data

Not passed: guile-gi supplies it.

data to pass to comparison function

value-destroy-func

Not passed: guile-gi supplies it.

a function to free the memory allocated for the value used when removing the entry from the <GTree> or #f if you don’t want to supply such a function

See GDestroyNotify for how it is called.

Method: tree:nnodes self ⇒ %return
Method: nnodes self ⇒ %return

Gets the number of nodes in a <GTree>.

self

Passed: a <GTree>.

a <GTree>

%return

Returned: a 32-bit integer.

the number of nodes in self

The node counter value type is really a guint, but it is returned as a gint due to backward compatibility issues (can be cast back to guint to support its full range of values).

Method: tree:ref self ⇒ %return
Method: ref self ⇒ %return

Increments the reference count of self by one.

It is safe to call this function from any thread.

self

Passed: a <GTree>.

a <GTree>

%return

Returned: a <GTree>.

the passed in <GTree>

Method: tree:remove-all self
Method: remove-all self

Removes all nodes from a <GTree> and destroys their keys and values, then resets the <GTree>’s root to #f.

self

Passed: a <GTree>.

a <GTree>

Method: tree:remove? self [key] ⇒ %return
Method: remove? self [key] ⇒ %return

Removes a key/value pair from a <GTree>.

If the <GTree> was created using 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 the <GTree>, 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)).

self

Passed: a <GTree>.

a <GTree>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to remove

%return

Returned: a boolean.

#t if the key was found (prior to 2.8, this function returned nothing)

Method: tree:replace self [key] [value]
Method: replace self [key] [value]

Inserts a new key and value into a <GTree> as g_tree_replace_node() does, only this function does not return the inserted or set node.

self

Passed: a <GTree>.

a <GTree>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to insert

value

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the value corresponding to the key

Method: tree:search self search-func ⇒ %return

Searches a <GTree> using search-func.

The search-func is called with a pointer to the key of a key/value pair in the tree, and the passed in user-data. If search-func returns 0 for a key/value pair, then the corresponding value is returned as the result of tree:search. If search-func returns -1, searching will proceed among the key/value pairs that have a smaller key; if search-func returns 1, searching will proceed among the key/value pairs that have a larger key.

self

Passed: a <GTree>.

a <GTree>

search-func

Passed: a procedure.

a function used to search the <GTree>

See GCompareFunc for how it is called.

%return

Returned: a foreign pointer, or #f.

the value corresponding to the found key, or #f if the key was not found

user-data

Not passed: guile-gi supplies it.

the data passed as the second argument to search-func

Method: tree:steal? self [key] ⇒ %return
Method: steal? self [key] ⇒ %return

Removes a key and its associated value from a <GTree> without calling the key and value destroy functions.

If the key does not exist in the <GTree>, the function does nothing.

self

Passed: a <GTree>.

a <GTree>

key

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the key to remove

%return

Returned: a boolean.

#t if the key was found (prior to 2.8, this function returned nothing)

Method: tree:traverse self traverse-func traverse-type
Method: traverse self traverse-func traverse-type

Calls the given function for each node in the <GTree>.

self

Passed: a <GTree>.

a <GTree>

traverse-func

Passed: a procedure.

the function to call for each node visited. If this function returns #t, the traversal is stopped.

See GTraverseFunc for how it is called.

traverse-type

Passed: a <%GTraverseType>, or one of its symbols.

the order in which nodes are visited, one of in-order, pre-order and post-order

user-data

Not passed: guile-gi supplies it.

user data to pass to the function

Method: tree:unref self
Method: unref self

Decrements the reference count of self 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 self will be released.

It is safe to call this function from any thread.

self

Passed: a <GTree>.

a <GTree>