-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict.h
76 lines (56 loc) · 2.01 KB
/
dict.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Ken Sheedlo
* kmdata Data Structures Library
* dict implementation based on hash tables. */
#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>
#include "error_handling.h"
#include "list.h"
#define MAX(a,b) ((a)>(b) ? a : b)
#define MOD(x,m) (((x)%(m) + (m))%(m))
/* Constants for internal use. */
#define DICTHT_MINIMUM_SIZE 2039
#define DICTHT_GROW_FACTOR 2
#define DICTHT_MAX_LOAD 0.75
#define DICTHT_MIN_LOAD 0.25
/* Options for manipulating hash tables. */
#define DICTHT_FREE_KEYS 1
#define DICTHT_FREE_VALUES 2
/*Options for hash table state. */
#define DICTHT_DEFAULTS 0
#define DICTHT_NO_GROW 4
#define DICTHT_NO_SHRINK 8
typedef struct _ht_bucket {
void *data;
void *key;
struct _ht_bucket *next;
} bucket_t;
typedef struct {
bucket_t **table;
int32_t (*hash)(const void *);
int32_t (*eq)(const void *, const void *);
size_t length;
int32_t load;
int32_t flags;
} dict_t;
/* PRIVATE FUNCTIONS */
void _dict_resize(dict_t *dict, size_t new_size);
/* PUBLIC API */
/* Adds the value with the specified key to the dict. If the key is already
* in the dictionary, return it's old K-V pair in result. If an error occurs,
* set result->key to NULL.
*/
int32_t dict_add(dict_t *dict, void *key, void *data, bucket_t *result);
/* Retrieves the data value associated with the given key from the dictionary.*/
void *dict_get(dict_t *dict, void *key);
/* Clear memory belonging to this dictionary. */
void dict_clear(dict_t *dict, int32_t options);
void *dict_remove(dict_t *dict, void *key, bucket_t *result);
/* Initialize a new dictionary given a valid pointer and some other important
* stuff. */
void dict_init(dict_t *dict, size_t size, int32_t (*hash)(const void *),
int32_t (*eq)(const void *, const void *), int32_t options);
void dict_print(FILE *output, dict_t *dict, void (*disp_key)(FILE *, const void *),
void (*disp_value)(FILE *, const void *));
void dict_key_set(list_t *rop, dict_t *dict);
void dict_value_set(list_t *rop, dict_t *dict);