1 #ifndef MUPDF_FITZ_HASH_H
2 #define MUPDF_FITZ_HASH_H
3 
4 #include "mupdf/fitz/system.h"
5 #include "mupdf/fitz/context.h"
6 #include "mupdf/fitz/output.h"
7 
8 /**
9 	Generic hash-table with fixed-length keys.
10 
11 	The keys and values are NOT reference counted by the hash table.
12 	Callers are responsible for taking care the reference counts are
13 	correct. Inserting a duplicate entry will NOT overwrite the old
14 	value, and will return the old value.
15 
16 	The drop_val callback function is only used to release values
17 	when the hash table is destroyed.
18 */
19 
20 typedef struct fz_hash_table fz_hash_table;
21 
22 /**
23 	Function type called when a hash table entry is dropped.
24 
25 	Only used when the entire hash table is dropped.
26 */
27 typedef void (fz_hash_table_drop_fn)(fz_context *ctx, void *val);
28 
29 /**
30 	Create a new hash table.
31 
32 	initialsize: The initial size of the hashtable. The hashtable
33 	may grow (double in size) if it starts to get crowded (80%
34 	full).
35 
36 	keylen: byte length for each key.
37 
38 	lock: -1 for no lock, otherwise the FZ_LOCK to use to protect
39 	this table.
40 
41 	drop_val: Function to use to destroy values on table drop.
42 */
43 fz_hash_table *fz_new_hash_table(fz_context *ctx, int initialsize, int keylen, int lock, fz_hash_table_drop_fn *drop_val);
44 
45 /**
46 	Destroy the hash table.
47 
48 	Values are dropped using the drop function.
49 */
50 void fz_drop_hash_table(fz_context *ctx, fz_hash_table *table);
51 
52 /**
53 	Search for a matching hash within the table, and return the
54 	associated value.
55 */
56 void *fz_hash_find(fz_context *ctx, fz_hash_table *table, const void *key);
57 
58 /**
59 	Insert a new key/value pair into the hash table.
60 
61 	If an existing entry with the same key is found, no change is
62 	made to the hash table, and a pointer to the existing value is
63 	returned.
64 
65 	If no existing entry with the same key is found, ownership of
66 	val passes in, key is copied, and NULL is returned.
67 */
68 void *fz_hash_insert(fz_context *ctx, fz_hash_table *table, const void *key, void *val);
69 
70 /**
71 	Remove the entry for a given key.
72 
73 	The value is NOT freed, so the caller is expected to take care
74 	of this.
75 */
76 void fz_hash_remove(fz_context *ctx, fz_hash_table *table, const void *key);
77 
78 /**
79 	Callback function called on each key/value pair in the hash
80 	table, when fz_hash_for_each is run.
81 */
82 typedef void (fz_hash_table_for_each_fn)(fz_context *ctx, void *state, void *key, int keylen, void *val);
83 
84 /**
85 	Iterate over the entries in a hash table.
86 */
87 void fz_hash_for_each(fz_context *ctx, fz_hash_table *table, void *state, fz_hash_table_for_each_fn *callback);
88 
89 #endif
90