comparison mupdf-source/docs/reference/c/fitz/data-structures.md @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 # Data Structures
2
3 MuPDF has implementations of many generally useful data structures and algorithms
4 such as hash tables and binary trees.
5
6 ## Hash Table
7
8 We have a generic hash table structure with fixed length keys.
9
10 The keys and values are not reference counted by the hash table. Callers are responsible for manually taking care of reference counting when inserting and removing values from the table, should that be desired.
11
12 `fz_hash_table *fz_new_hash_table(fz_context *ctx, int initial_size, int key_length, int lock, void (*drop_value)(fz_context *ctx, void *value));`
13 : The lock parameter should be zero, any other value will result in
14 unpredictable behavior. The `drop_value` callback function to the
15 constructor is only used to release values when the hash table is
16 destroyed.
17
18 `void fz_drop_hash_table(fz_context *ctx, fz_hash_table *table);`
19 : Free the hash table and call the `drop_value` function on all the
20 values in the table.
21
22 `void *fz_hash_find(fz_context *ctx, fz_hash_table *table, const void *key);`
23 : Find the value associated with the key. Returns `NULL` if not found.
24
25 `void *fz_hash_insert(fz_context *ctx, fz_hash_table *table, const void *key, void *value);`
26 : Insert the value into the hash table. Inserting a duplicate entry will
27 **not** overwrite the old value, it will return the old value instead.
28 Return `NULL` if the value was inserted for the first time. Does not
29 reference count the value!
30
31 `void fz_hash_remove(fz_context *ctx, fz_hash_table *table, const void *key);`
32 : Remove the associated value from the hash table. This will not
33 reference count the value!
34
35 `void fz_hash_for_each(fz_context *ctx, fz_hash_table *table, void *state, void (*callback)(fz_context *ctx, void *state, void *key, int key_length, void *value);`
36 : Iterate and call a function for each key-value pair in the table.
37
38 ## Binary Tree
39
40 The `fz_tree` structure is a self-balancing binary tree that maps text strings to values.
41
42 `void *fz_tree_lookup(fz_context *ctx, fz_tree *node, const char *key);`
43 : Look up an entry in the tree. Returns `NULL` if not found.
44
45 `fz_tree *fz_tree_insert(fz_context *ctx, fz_tree *root, const char *key, void *value);`
46 : Insert a new entry into the tree. Do not insert duplicate entries.
47 Returns the new root object.
48
49 `void fz_drop_tree(fz_context *ctx, fz_tree *node, void (*dropfunc)(fz_context *ctx, void *value));`
50 : Free the tree and all the values in it.
51
52 There is no constructor for this structure, since there is no containing root
53 structure. Instead, the insert function returns the new root node. Use `NULL`
54 for the initial empty tree.
55
56 fz_tree *tree = NULL;
57 tree = fz_tree_insert(ctx, tree, "A", my_a_obj);
58 tree = fz_tree_insert(ctx, tree, "B", my_b_obj);
59 tree = fz_tree_insert(ctx, tree, "C", my_c_obj);
60 assert(fz_tree_lookup(ctx, tree, "B") == my_b_obj);