glibmm 2.66.5
|
Balanced Binary Trees — a sorted collection of key/value pairs optimized for searching and traversing in order. More...
#include <glibmm/balancedtree.h>
Public Types | |
using | TraverseFunc = sigc::slot< bool, const K &, const V & > |
using | CompareFunc = sigc::slot< int, const K &, const K & > |
Public Member Functions | |
~BalancedTree () | |
GTree * | gobj () |
Provides access to the underlying C GObject. More... | |
const GTree * | gobj () const |
Provides access to the underlying C GObject. More... | |
void | reference () |
Increments the reference count of tree by one. More... | |
void | unreference () |
Decrements the reference count of tree by one. More... | |
void | insert (const K & key, const V & value) |
Inserts a key/value pair into a BalancedTree. More... | |
bool | remove (const K & key) |
Removes a key/value pair from a BalancedTree. More... | |
V * | lookup (const K & key) |
Gets the value corresponding to the given key. More... | |
const V * | lookup (const K & key) const |
gint | height () const |
Gets the height of a BalancedTree. More... | |
gint | nnodes () const |
Gets the number of nodes in a BalancedTree. More... | |
void | foreach (const TraverseFunc & func) const |
Calls the given function for each of the key/value pairs in the BalancedTree. More... | |
V * | search (const CompareFunc & search_func, const K & key) |
Searches a BalancedTree using search_func. More... | |
const V * | search (const CompareFunc & search_func, const K & key) const |
Searches a BalancedTree using search_func. More... | |
Static Public Member Functions | |
static Glib::RefPtr< BalancedTree< K, V > > | create () |
static Glib::RefPtr< BalancedTree< K, V > > | create (const CompareFunc & key_compare_slot) |
Protected Member Functions | |
BalancedTree () | |
BalancedTree (const CompareFunc & key_compare_slot_) | |
Balanced Binary Trees — a sorted collection of key/value pairs optimized for searching and traversing in order.
The BalancedTree structure and its associated functions provide a sorted collection of key/value pairs optimized for searching and traversing in order.
To insert a key/value pair into a BalancedTree use insert().
To lookup the value corresponding to a given key, use lookup().
To find out the number of nodes in a BalancedTree, use nnodes(). To get the height of a BalancedTree, use height().
To traverse a BalancedTree, calling a function for each node visited in the traversal, use foreach().
To remove a key/value pair use remove().
Any type to be used with this template must implement copy constructor. Compiler-generated implementations are OK, provided they do the right thing for the type. Both keys and values are stored in the balanced binary tree as copies, created by copy constructors.
Type of key to be used with this template must implement:
greater than operator
using Glib::BalancedTree< K, V >::CompareFunc = sigc::slot<int, const K&, const K&> |
using Glib::BalancedTree< K, V >::TraverseFunc = sigc::slot<bool, const K&, const V&> |
|
inlineprotected |
|
inlineprotected |
|
inline |
|
inlinestatic |
|
inlinestatic |
|
inline |
Calls the given function for each of the key/value pairs in the BalancedTree.
The function is passed the key and value of each pair. 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 TraverseFunc as you walk over the tree, then walk the list and remove each item.
func | The function to call for each node visited. If this function returns true, the traversal is stopped. |
|
inline |
Provides access to the underlying C GObject.
|
inline |
Provides access to the underlying C GObject.
|
inline |
Gets the height of a BalancedTree.
If the BalancedTree contains no nodes, the height is 0. If the BalancedTree contains only one root node the height is 1. If the root node has children the height is 2, etc.
|
inline |
Inserts a key/value pair into a BalancedTree.
If the given key already exists in the BalancedTree its corresponding value is set to the new value.
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.
key | The key to insert. |
value | The value corresponding to the key. |
|
inline |
Gets the value corresponding to the given key.
Since a BalancedTree is automatically balanced as key/value pairs are added, key lookup is very fast.
key | The key to look up. |
nullptr
if the key was not found.
|
inline |
|
inline |
Gets the number of nodes in a BalancedTree.
|
inline |
Increments the reference count of tree by one.
It is safe to call this function from any thread.
|
inline |
Removes a key/value pair from a BalancedTree.
If the key does not exist in the BalancedTree, the function does nothing.
key | The key to remove. |
|
inline |
Searches a BalancedTree using search_func.
The search_func is called with a reference to the key of a key/value pair in the tree. If search_func returns 0 for a key/value pair, then search() will return the value of that pair. 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.
search_func | A function used to search the BalancedTree. |
key | The key to search for. |
nullptr
if the key was not found.
|
inline |
Searches a BalancedTree using search_func.
The search_func is called with a reference to the key of a key/value pair in the tree. If search_func returns 0 for a key/value pair, then search() will return the value of that pair. 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.
search_func | A function used to search the BalancedTree. |
key | The key to search for. |
nullptr
if the key was not found.
|
inline |
Decrements the reference count of tree by one.
If the reference count drops to 0, all keys and values will be destroyed and all memory allocated by tree will be released.
It is safe to call this function from any thread.