libdict 1.0.7
Data Structure C Library
Loading...
Searching...
No Matches
dict.h File Reference
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "hashtable.h"
#include "hashtable2.h"
#include "hb_tree.h"
#include "pr_tree.h"
#include "rb_tree.h"
#include "skiplist.h"
#include "sp_tree.h"
#include "tr_tree.h"
#include "wb_tree.h"
Include dependency graph for dict.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  dict_insert_result
 
struct  dict_remove_result
 
struct  dict_vtable
 The operations of one container type. More...
 
struct  itor_vtable
 
struct  dict
 
struct  dict_itor
 

Macros

#define BEGIN_DECL
 
#define END_DECL
 
#define DICT_VERSION_MAJOR   0
 
#define DICT_VERSION_MINOR   3
 
#define DICT_VERSION_PATCH   0
 
#define dict_private(dct)
 
#define dict_insert(dct, key)
 
#define dict_search(dct, key)
 Return the address of the datum stored for |key|, or NULL if |key| is not present.
 
#define dict_is_sorted(dct)
 
#define dict_search_le(dct, key)
 Near searches on sorted containers: return the address of the datum of the greatest key less than or equal to (le) or less than (lt) |key|, or of the least key greater than or equal to (ge) or greater than (gt) |key|; NULL if there is no such key.
 
#define dict_search_lt(dct, key)
 
#define dict_search_ge(dct, key)
 
#define dict_search_gt(dct, key)
 
#define dict_remove(dct, key)
 
#define dict_clear(dct, func)
 
#define dict_traverse(dct, func, ud)
 
#define dict_select(dct, n, key, d)
 Retrieve the |n|th element in key order (0-based), storing its key in |key| and its datum in |d|, and evaluating to true.
 
#define dict_count(dct)
 
#define dict_verify(dct)
 Check the container's internal invariants and evaluate to true if they hold.
 
#define dict_itor_new(dct)
 
#define dict_itor_private(i)
 
#define dict_itor_valid(i)
 
#define dict_itor_invalidate(i)
 
#define dict_itor_next(i)
 
#define dict_itor_prev(i)
 
#define dict_itor_nextn(i, n)
 
#define dict_itor_prevn(i, n)
 
#define dict_itor_first(i)
 
#define dict_itor_last(i)
 
#define dict_itor_search(i, k)
 Position the iterator on |k| (exact match) and evaluate to true, or leave it unpositioned and evaluate to false if |k| is not present.
 
#define dict_itor_search_le(i, k)
 
#define dict_itor_search_lt(i, k)
 
#define dict_itor_search_ge(i, k)
 
#define dict_itor_search_gt(i, k)
 
#define dict_itor_key(i)
 
#define dict_itor_datum(i)
 
#define dict_itor_compare(i1, i2)
 Compare the positions of two iterators over the same dictionary.
 
#define dict_itor_remove(i)
 

Typedefs

typedef int(* dict_compare_func) (const void *, const void *)
 A pointer to a function that compares two keys.
 
typedef void(* dict_delete_func) (void *, void *)
 A pointer to a function that is called when a key-value pair gets removed from a dictionary.
 
typedef bool(* dict_visit_func) (const void *, void *, void *)
 A pointer to a function used for iterating over dictionary contents.
 
typedef unsigned(* dict_hash_func) (const void *)
 A pointer to a function that returns the hash value of a key.
 
typedef unsigned(* dict_prio_func) (const void *)
 A pointer to a function that returns the priority of a key, used by tr_tree (a treap).
 
typedef struct dict_itor dict_itor
 Forward declaration of dict_itor, whose definition appears below.
 
typedef dict_itor *(* dict_inew_func) (void *obj)
 
typedef size_t(* dict_dfree_func) (void *obj, dict_delete_func delete_func)
 
typedef dict_insert_result(* dict_insert_func) (void *obj, void *key)
 
typedef void **(* dict_search_func) (void *obj, const void *key)
 
typedef dict_remove_result(* dict_remove_func) (void *obj, const void *key)
 
typedef size_t(* dict_clear_func) (void *obj, dict_delete_func delete_func)
 
typedef size_t(* dict_traverse_func) (void *obj, dict_visit_func visit, void *user_data)
 
typedef bool(* dict_select_func) (void *obj, size_t n, const void **key, void **datum)
 
typedef size_t(* dict_count_func) (const void *obj)
 
typedef bool(* dict_verify_func) (const void *obj)
 
typedef void(* dict_ifree_func) (void *itor)
 
typedef bool(* dict_valid_func) (const void *itor)
 
typedef void(* dict_invalidate_func) (void *itor)
 
typedef bool(* dict_next_func) (void *itor)
 
typedef bool(* dict_prev_func) (void *itor)
 
typedef bool(* dict_nextn_func) (void *itor, size_t count)
 
typedef bool(* dict_prevn_func) (void *itor, size_t count)
 
typedef bool(* dict_first_func) (void *itor)
 
typedef bool(* dict_last_func) (void *itor)
 
typedef void *(* dict_key_func) (void *itor)
 
typedef void **(* dict_datum_func) (void *itor)
 
typedef bool(* dict_isearch_func) (void *itor, const void *key)
 
typedef bool(* dict_iremove_func) (void *itor)
 
typedef int(* dict_icompare_func) (void *itor1, void *itor2)
 

Functions

size_t dict_free (dict *dct, dict_delete_func delete_func)
 
void dict_itor_free (dict_itor *itor)
 
int dict_int_cmp (const void *k1, const void *k2)
 Comparison functions for common key types, for use as dict_compare_func.
 
int dict_uint_cmp (const void *k1, const void *k2)
 
int dict_long_cmp (const void *k1, const void *k2)
 
int dict_ulong_cmp (const void *k1, const void *k2)
 
int dict_ptr_cmp (const void *k1, const void *k2)
 
int dict_str_cmp (const void *k1, const void *k2)
 
unsigned dict_str_hash (const void *str)
 FNV-1a hash of a NUL-terminated string, over its bytes in order, so a key hashes to the same value on every platform.
 

Variables

const char *const kDictVersionString
 
void *(* dict_malloc_func )(size_t)
 A pointer to a function that libdict will use to allocate memory.
 
void(* dict_free_func )(void *)
 A pointer to a function that libdict will use to deallocate memory.
 

Macro Definition Documentation

◆ BEGIN_DECL

#define BEGIN_DECL

Definition at line 42 of file dict.h.

◆ dict_clear

#define dict_clear ( dct,
func )
Value:
((dct)->_vtable->clear((dct)->_object, (func)))

Definition at line 185 of file dict.h.

◆ dict_count

#define dict_count ( dct)
Value:
((dct)->_vtable->count((dct)->_object))

Definition at line 193 of file dict.h.

◆ dict_insert

#define dict_insert ( dct,
key )
Value:
((dct)->_vtable->insert((dct)->_object, (key)))

Definition at line 170 of file dict.h.

◆ dict_is_sorted

#define dict_is_sorted ( dct)
Value:
((dct)->_vtable->sorted)

Definition at line 174 of file dict.h.

◆ dict_itor_compare

#define dict_itor_compare ( i1,
i2 )
Value:
((i1)->_vtable->compare ? (i1)->_vtable->compare((i1)->_itor, (i2)->_itor) : 0)

Compare the positions of two iterators over the same dictionary.

Returns a negative value, zero or a positive value as |i1| precedes, is at, or follows |i2|. Evaluates to 0 if the container supplies no comparison.

Definition at line 263 of file dict.h.

◆ dict_itor_datum

#define dict_itor_datum ( i)
Value:
((i)->_vtable->datum((i)->_itor))

Definition at line 259 of file dict.h.

◆ dict_itor_first

#define dict_itor_first ( i)
Value:
((i)->_vtable->first((i)->_itor))

Definition at line 246 of file dict.h.

◆ dict_itor_invalidate

#define dict_itor_invalidate ( i)
Value:
((i)->_vtable->invalid((i)->_itor))

Definition at line 241 of file dict.h.

◆ dict_itor_key

#define dict_itor_key ( i)
Value:
((i)->_vtable->key((i)->_itor))

Definition at line 258 of file dict.h.

◆ dict_itor_last

#define dict_itor_last ( i)
Value:
((i)->_vtable->last((i)->_itor))

Definition at line 247 of file dict.h.

◆ dict_itor_new

#define dict_itor_new ( dct)
Value:
(dct)->_vtable->inew((dct)->_object)

Definition at line 198 of file dict.h.

◆ dict_itor_next

#define dict_itor_next ( i)
Value:
((i)->_vtable->next((i)->_itor))

Definition at line 242 of file dict.h.

◆ dict_itor_nextn

#define dict_itor_nextn ( i,
n )
Value:
((i)->_vtable->nextn((i)->_itor, (n)))

Definition at line 244 of file dict.h.

◆ dict_itor_prev

#define dict_itor_prev ( i)
Value:
((i)->_vtable->prev((i)->_itor))

Definition at line 243 of file dict.h.

◆ dict_itor_prevn

#define dict_itor_prevn ( i,
n )
Value:
((i)->_vtable->prevn((i)->_itor, (n)))

Definition at line 245 of file dict.h.

◆ dict_itor_private

#define dict_itor_private ( i)
Value:
((i)->_itor)

Definition at line 239 of file dict.h.

◆ dict_itor_remove

#define dict_itor_remove ( i)
Value:
((i)->_vtable->remove && (i)->_vtable->remove((i)->_itor))

Definition at line 264 of file dict.h.

◆ dict_itor_search

#define dict_itor_search ( i,
k )
Value:
((i)->_vtable->search((i)->_itor, (k)))

Position the iterator on |k| (exact match) and evaluate to true, or leave it unpositioned and evaluate to false if |k| is not present.

The le, _lt, _ge and _gt variants position it on the same key the corresponding dict_search* finds; on the hash tables they evaluate to false and leave the iterator unchanged.

Definition at line 253 of file dict.h.

◆ dict_itor_search_ge

#define dict_itor_search_ge ( i,
k )
Value:
((i)->_vtable->search_ge && (i)->_vtable->search_ge((i)->_itor, (k)))

Definition at line 256 of file dict.h.

◆ dict_itor_search_gt

#define dict_itor_search_gt ( i,
k )
Value:
((i)->_vtable->search_gt && (i)->_vtable->search_gt((i)->_itor, (k)))

Definition at line 257 of file dict.h.

◆ dict_itor_search_le

#define dict_itor_search_le ( i,
k )
Value:
((i)->_vtable->search_le && (i)->_vtable->search_le((i)->_itor, (k)))

Definition at line 254 of file dict.h.

◆ dict_itor_search_lt

#define dict_itor_search_lt ( i,
k )
Value:
((i)->_vtable->search_lt && (i)->_vtable->search_lt((i)->_itor, (k)))

Definition at line 255 of file dict.h.

◆ dict_itor_valid

#define dict_itor_valid ( i)
Value:
((i)->_vtable->valid((i)->_itor))

Definition at line 240 of file dict.h.

◆ dict_private

#define dict_private ( dct)
Value:
((dct)->_object)

Definition at line 169 of file dict.h.

◆ dict_remove

#define dict_remove ( dct,
key )
Value:
((dct)->_vtable->remove((dct)->_object, (key)))

Definition at line 184 of file dict.h.

◆ dict_search

#define dict_search ( dct,
key )
Value:
((dct)->_vtable->search((dct)->_object, (key)))

Return the address of the datum stored for |key|, or NULL if |key| is not present.

Definition at line 173 of file dict.h.

◆ dict_search_ge

#define dict_search_ge ( dct,
key )
Value:
((dct)->_vtable->search_ge ? (dct)->_vtable->search_ge((dct)->_object, (key)) : NULL)

Definition at line 182 of file dict.h.

◆ dict_search_gt

#define dict_search_gt ( dct,
key )
Value:
((dct)->_vtable->search_gt ? (dct)->_vtable->search_gt((dct)->_object, (key)) : NULL)

Definition at line 183 of file dict.h.

◆ dict_search_le

#define dict_search_le ( dct,
key )
Value:
((dct)->_vtable->search_le ? (dct)->_vtable->search_le((dct)->_object, (key)) : NULL)

Near searches on sorted containers: return the address of the datum of the greatest key less than or equal to (le) or less than (lt) |key|, or of the least key greater than or equal to (ge) or greater than (gt) |key|; NULL if there is no such key.

They evaluate to NULL on the hash tables, which are not sorted.

Definition at line 180 of file dict.h.

◆ dict_search_lt

#define dict_search_lt ( dct,
key )
Value:
((dct)->_vtable->search_lt ? (dct)->_vtable->search_lt((dct)->_object, (key)) : NULL)

Definition at line 181 of file dict.h.

◆ dict_select

#define dict_select ( dct,
n,
key,
d )
Value:
((dct)->_vtable->select && (dct)->_vtable->select((dct)->_object, (n), (key), (d)))

Retrieve the |n|th element in key order (0-based), storing its key in |key| and its datum in |d|, and evaluating to true.

Either out-parameter may be NULL. If |n| is out of range, evaluates to false and stores NULL through any non-NULL out-parameter. Containers that do not support selection (the hash tables and the skip list) evaluate to false and store nothing.

Definition at line 192 of file dict.h.

◆ dict_traverse

#define dict_traverse ( dct,
func,
ud )
Value:
((dct)->_vtable->traverse((dct)->_object, (func), (ud)))

Definition at line 186 of file dict.h.

◆ dict_verify

#define dict_verify ( dct)
Value:
((dct)->_vtable->verify((dct)->_object))

Check the container's internal invariants and evaluate to true if they hold.

On failure the failed check is printed to stderr. Meant for testing and debugging: it visits every element.

Definition at line 197 of file dict.h.

◆ DICT_VERSION_MAJOR

#define DICT_VERSION_MAJOR   0

Definition at line 48 of file dict.h.

◆ DICT_VERSION_MINOR

#define DICT_VERSION_MINOR   3

Definition at line 49 of file dict.h.

◆ DICT_VERSION_PATCH

#define DICT_VERSION_PATCH   0

Definition at line 50 of file dict.h.

◆ END_DECL

#define END_DECL

Definition at line 43 of file dict.h.

Typedef Documentation

◆ dict_clear_func

typedef size_t(* dict_clear_func) (void *obj, dict_delete_func delete_func)

Definition at line 98 of file dict.h.

◆ dict_compare_func

typedef int(* dict_compare_func) (const void *, const void *)

A pointer to a function that compares two keys.

It needs to return a negative value if k1<k2, a positive value if k1>k2, and zero if the keys are equal. The comparison must be a total order, i.e. reflexive (cmp(k,k)==0), antisymmetric (k1>k2 implies k2<k1) and transitive (k1>k2 and k2>k3 implies k1>k3). Keys must compare identically for as long as they are in the dictionary.

Definition at line 59 of file dict.h.

◆ dict_count_func

typedef size_t(* dict_count_func) (const void *obj)

Definition at line 101 of file dict.h.

◆ dict_datum_func

typedef void **(* dict_datum_func) (void *itor)

Definition at line 138 of file dict.h.

◆ dict_delete_func

typedef void(* dict_delete_func) (void *, void *)

A pointer to a function that is called when a key-value pair gets removed from a dictionary.

Definition at line 62 of file dict.h.

◆ dict_dfree_func

typedef size_t(* dict_dfree_func) (void *obj, dict_delete_func delete_func)

Definition at line 92 of file dict.h.

◆ dict_first_func

typedef bool(* dict_first_func) (void *itor)

Definition at line 135 of file dict.h.

◆ dict_hash_func

typedef unsigned(* dict_hash_func) (const void *)

A pointer to a function that returns the hash value of a key.

Definition at line 66 of file dict.h.

◆ dict_icompare_func

typedef int(* dict_icompare_func) (void *itor1, void *itor2)

Definition at line 141 of file dict.h.

◆ dict_ifree_func

typedef void(* dict_ifree_func) (void *itor)

Definition at line 128 of file dict.h.

◆ dict_inew_func

typedef dict_itor *(* dict_inew_func) (void *obj)

Definition at line 91 of file dict.h.

◆ dict_insert_func

typedef dict_insert_result(* dict_insert_func) (void *obj, void *key)

Definition at line 93 of file dict.h.

◆ dict_invalidate_func

typedef void(* dict_invalidate_func) (void *itor)

Definition at line 130 of file dict.h.

◆ dict_iremove_func

typedef bool(* dict_iremove_func) (void *itor)

Definition at line 140 of file dict.h.

◆ dict_isearch_func

typedef bool(* dict_isearch_func) (void *itor, const void *key)

Definition at line 139 of file dict.h.

◆ dict_itor

typedef struct dict_itor dict_itor

Forward declaration of dict_itor, whose definition appears below.

Definition at line 78 of file dict.h.

◆ dict_key_func

typedef void *(* dict_key_func) (void *itor)

Definition at line 137 of file dict.h.

◆ dict_last_func

typedef bool(* dict_last_func) (void *itor)

Definition at line 136 of file dict.h.

◆ dict_next_func

typedef bool(* dict_next_func) (void *itor)

Definition at line 131 of file dict.h.

◆ dict_nextn_func

typedef bool(* dict_nextn_func) (void *itor, size_t count)

Definition at line 133 of file dict.h.

◆ dict_prev_func

typedef bool(* dict_prev_func) (void *itor)

Definition at line 132 of file dict.h.

◆ dict_prevn_func

typedef bool(* dict_prevn_func) (void *itor, size_t count)

Definition at line 134 of file dict.h.

◆ dict_prio_func

typedef unsigned(* dict_prio_func) (const void *)

A pointer to a function that returns the priority of a key, used by tr_tree (a treap).

The treap is a max-heap on priority, so a LARGER value places the key closer to the root. Pass NULL to have priorities chosen at random.

Definition at line 70 of file dict.h.

◆ dict_remove_func

typedef dict_remove_result(* dict_remove_func) (void *obj, const void *key)

Definition at line 96 of file dict.h.

◆ dict_search_func

typedef void **(* dict_search_func) (void *obj, const void *key)

Definition at line 95 of file dict.h.

◆ dict_select_func

typedef bool(* dict_select_func) (void *obj, size_t n, const void **key, void **datum)

Definition at line 100 of file dict.h.

◆ dict_traverse_func

typedef size_t(* dict_traverse_func) (void *obj, dict_visit_func visit, void *user_data)

Definition at line 99 of file dict.h.

◆ dict_valid_func

typedef bool(* dict_valid_func) (const void *itor)

Definition at line 129 of file dict.h.

◆ dict_verify_func

typedef bool(* dict_verify_func) (const void *obj)

Definition at line 102 of file dict.h.

◆ dict_visit_func

typedef bool(* dict_visit_func) (const void *, void *, void *)

A pointer to a function used for iterating over dictionary contents.

Definition at line 64 of file dict.h.

Function Documentation

◆ dict_free()

size_t dict_free ( dict * dct,
dict_delete_func delete_func )

Definition at line 111 of file dict.c.

◆ dict_int_cmp()

int dict_int_cmp ( const void * k1,
const void * k2 )

Comparison functions for common key types, for use as dict_compare_func.

The keys are pointers to values of the type, except for dict_ptr_cmp, which orders the key pointers themselves (by address, as uintptr_t), and dict_str_cmp, which takes NUL-terminated strings. dict_str_cmp orders bytes as unsigned char, exactly as strcmp() does, so string keys sort the same on every platform.

Definition at line 41 of file dict.c.

◆ dict_itor_free()

void dict_itor_free ( dict_itor * itor)

Definition at line 121 of file dict.c.

◆ dict_long_cmp()

int dict_long_cmp ( const void * k1,
const void * k2 )

Definition at line 57 of file dict.c.

◆ dict_ptr_cmp()

int dict_ptr_cmp ( const void * k1,
const void * k2 )

Definition at line 73 of file dict.c.

◆ dict_str_cmp()

int dict_str_cmp ( const void * k1,
const void * k2 )

Definition at line 83 of file dict.c.

◆ dict_str_hash()

unsigned dict_str_hash ( const void * str)

FNV-1a hash of a NUL-terminated string, over its bytes in order, so a key hashes to the same value on every platform.

For use as dict_hash_func.

Definition at line 100 of file dict.c.

◆ dict_uint_cmp()

int dict_uint_cmp ( const void * k1,
const void * k2 )

Definition at line 49 of file dict.c.

◆ dict_ulong_cmp()

int dict_ulong_cmp ( const void * k1,
const void * k2 )

Definition at line 65 of file dict.c.

Variable Documentation

◆ dict_free_func

void(* dict_free_func) (void *) ( void * )
extern

A pointer to a function that libdict will use to deallocate memory.

Definition at line 38 of file dict.c.

◆ dict_malloc_func

void *(* dict_malloc_func) (size_t) ( size_t )
extern

A pointer to a function that libdict will use to allocate memory.

Definition at line 37 of file dict.c.

◆ kDictVersionString

const char* const kDictVersionString
extern

Definition at line 33 of file dict.c.