Calico
|
Associative arrays implemented using B-trees. More...
Classes | |
struct | btree |
An associative array data structure backed by a B-tree. More... | |
struct | btree_entry |
Refers to a specific location in a B-tree. More... | |
Functions | |
static int | btree_contains_key (const btree *m, const KeyType *k) |
Returns whether the the given key exists in the btree . More... | |
static ValueType * | btree_entry_get (const btree_entry *ent) |
Gets a pointer to the value at an occupied entry. More... | |
static int | btree_entry_insert (btree_entry *entry, const KeyType *key, const ValueType *value) |
Inserts an element into the btree at a vacant entry. More... | |
static KeyType * | btree_entry_key (const btree_entry *ent) |
Gets a pointer to the key at an occupied entry. More... | |
static int | btree_entry_next (btree_entry *ent) |
Modifies an occupied entry to refer to its next element. More... | |
static int | btree_entry_occupied (const btree_entry *ent) |
Returns whether a entry refers to an existing element. More... | |
static int | btree_entry_prev (btree_entry *ent) |
Modifies an occupied entry to refer to its previous element. More... | |
static void | btree_entry_remove (btree_entry *ent) |
Removes the element at an occupied entry. More... | |
static void | btree_entry_xinsert (btree_entry *entry, const KeyType *key, const ValueType *value) |
Similar to btree_entry_insert but aborts on error. More... | |
static void | btree_find (btree *m, const KeyType *key, btree_entry *entry_out) |
Searches for the given key . More... | |
static void | btree_find_first (btree *m, btree_entry *entry_out) |
Locates the first (least) entry. More... | |
static void | btree_find_last (btree *m, btree_entry *entry_out) |
Locates the last (greatest) entry. More... | |
static ValueType * | btree_get (btree *m, const KeyType *k) |
Retrieves the value with the given key. More... | |
static const ValueType * | btree_get_const (const btree *m, const KeyType *k) |
Retrieves the value with the given key. More... | |
static void | btree_init (btree *m) |
Initializes an empty btree . More... | |
static int | btree_insert (btree *m, const KeyType *key, const ValueType *value, ValueType *value_out) |
Inserts an element into the btree . More... | |
static size_t | btree_len (const btree *m) |
Gets the number of elements. More... | |
static int | btree_remove (btree *m, const KeyType *key, KeyType *key_out, ValueType *value_out) |
Removes the element at the given key and stores the results in *key_out and *value_out respectively, returning 1 . More... | |
static void | btree_reset (btree *m) |
Removes all elements from a btree and frees all memory consumed by the B-tree nodes. More... | |
static int | btree_xinsert (btree *m, const KeyType *key, const ValueType *value, ValueType *value_out) |
Similar to btree_insert but aborts if an error occurs. More... | |
Associative arrays implemented using B-trees.
This is a template header. All identifiers in this are prefixed with an arbitrary prefix provided by the user. Before including this header, <calico/btree_head.h>
must be included as well.
The main parameters are:
KeyType
: key typeValueType
: value typeCompareFunction
: function used to compare keys, with a type that, through implicit conversions, can be called as if its type is int (*)(btree *, const KeyType *, const KeyType *)
(default: cal_pcmp
).Some other parameters useful for tuning performance are:
MinArity
: number of children per node (default: 8, minimum: 2)SearchFunction
: function used search for a key in a node (default: linear_sorted_search
)ChildIndexType
: type used to store indices of child nodes (default: unsigned short
)HeightType
: type used to store the height of the tree (default: unsigned char
)Here's an example:
|
inlinestatic |
Returns whether the the given key exists in the btree
.
|
inlinestatic |
Gets a pointer to the value at an occupied entry.
If the entry is not occupied, the behavior is undefined.
For sets, this returns a non-null pointer if the entry exists. Otherwise, it returns NULL
.
|
inlinestatic |
Inserts an element into the btree
at a vacant entry.
If the entry is not vacant, the behavior is undefined.
If the insert is successful, the entry is invalidated and 0
is returned. On error, a positive integer is returned.
key
must match the original key used to obtain the vacant entry, or the behavior is undefined.
|
inlinestatic |
Gets a pointer to the key at an occupied entry.
If the entry is not occupied, the behavior is undefined.
btree_find
btree_get
btree_get_const
btree_insert
btree_remove
btree_xinsert
|
inlinestatic |
Modifies an occupied entry to refer to its next element.
If there are no more elements, the next entry is vacant. Otherwise, it is occupied. The argument ent
must point to an occupied entry, or the behavior is undefined. Returns 1
if the new entry is occupied, 0
if the new entry is vacant.
|
inlinestatic |
Returns whether a entry refers to an existing element.
|
inlinestatic |
Modifies an occupied entry to refer to its previous element.
If there are no more elements, the previous entry is vacant. Otherwise, it is occupied. The argument ent
must point to an occupied entry, or the behavior is undefined.
|
inlinestatic |
Removes the element at an occupied entry.
The entry is invalidated afterwards. If the entry is not occupied, the behavior is undefined.
|
inlinestatic |
Similar to btree_entry_insert
but aborts on error.
|
inlinestatic |
Searches for the given key
.
The entry returned can be either occupied or vacant, which indicates whether the key
was found.
|
inlinestatic |
Locates the first (least) entry.
If the B-tree is empty, a vacant entry is returned.
|
inlinestatic |
Locates the last (greatest) entry.
If the B-tree is empty, a vacant entry is returned.
|
inlinestatic |
Retrieves the value with the given key.
Returns NULL
if it is not found.
|
inlinestatic |
Retrieves the value with the given key.
Returns NULL
if it is not found.
|
inlinestatic |
Initializes an empty btree
.
|
inlinestatic |
Inserts an element into the btree
.
If an element with the same key
already exists, its value is replaced (the key remains unchanged, however) and the old value is stored at value_out
.
value
and value_out
are permitted to alias each other.
If the element was added without replacing an existing element, 0
is returned. If an existing element was replaced, 1
is returned. If an error occurs, a negative value is returned.
value_out
as well as the key
if an existing value was replaced. For example,
|
inlinestatic |
Gets the number of elements.
This is an O(1) operation.
|
inlinestatic |
Removes the element at the given key and stores the results in *key_out
and *value_out
respectively, returning 1
.
If the element does not exist, does nothing and returns 0
.
If the key and/or value are associated with resources, it may be necessary to release them if btree_remove
returns 1
, like this:
key
and key_out
are permitted to alias.
|
inlinestatic |
Removes all elements from a btree
and frees all memory consumed by the B-tree nodes.
This operation does not read any of the keys.
btree_reset
does not release them! In this situation, one must manually iterate over each element and release them:
|
inlinestatic |
Similar to btree_insert
but aborts if an error occurs.