1 | /* Dictionary data abstraction. |
---|
2 | * Maps from strings to pointers. |
---|
3 | * Stores as a sorted list of key/value pairs. |
---|
4 | * O(n) on inserts and deletes. |
---|
5 | * O(log n) on searches |
---|
6 | */ |
---|
7 | |
---|
8 | #include <stdlib.h> |
---|
9 | #include <string.h> |
---|
10 | #include <unistd.h> |
---|
11 | #include "owl.h" |
---|
12 | |
---|
13 | |
---|
14 | #define INITSIZE 30 |
---|
15 | #define GROWBY 3 / 2 |
---|
16 | |
---|
17 | void owl_dict_create(owl_dict *d) { |
---|
18 | d->size=0; |
---|
19 | d->els=g_new(owl_dict_el, INITSIZE); |
---|
20 | d->avail=INITSIZE; |
---|
21 | } |
---|
22 | |
---|
23 | int owl_dict_get_size(const owl_dict *d) { |
---|
24 | return(d->size); |
---|
25 | } |
---|
26 | |
---|
27 | /* Finds the position of an element with key k, or of the element where |
---|
28 | * this element would logically go, and stores the index in pos. |
---|
29 | * Returns 1 if found, else 0. */ |
---|
30 | int _owl_dict_find_pos(const owl_dict *d, const char *k, int *pos) { |
---|
31 | int lo = 0, hi = d->size; |
---|
32 | while (lo < hi) { |
---|
33 | int mid = (lo + hi)/2; /* lo goes up and we can't hit hi, so no +1 */ |
---|
34 | int cmp = strcmp(k, d->els[mid].k); |
---|
35 | if (cmp < 0) { |
---|
36 | hi = mid; |
---|
37 | } else if (cmp > 0) { |
---|
38 | lo = mid+1; |
---|
39 | } else { |
---|
40 | *pos = mid; |
---|
41 | return 1; |
---|
42 | } |
---|
43 | } |
---|
44 | *pos = lo; |
---|
45 | return 0; |
---|
46 | } |
---|
47 | |
---|
48 | /* returns the value corresponding to key k */ |
---|
49 | void *owl_dict_find_element(const owl_dict *d, const char *k) { |
---|
50 | int found, pos; |
---|
51 | found = _owl_dict_find_pos(d, k, &pos); |
---|
52 | if (!found) { |
---|
53 | return(NULL); |
---|
54 | } |
---|
55 | return(d->els[pos].v); |
---|
56 | } |
---|
57 | |
---|
58 | /* Returns a GPtrArray of dictionary keys. Duplicates the keys, so |
---|
59 | * they will need to be freed by the caller with g_free. */ |
---|
60 | CALLER_OWN GPtrArray *owl_dict_get_keys(const owl_dict *d) { |
---|
61 | GPtrArray *keys = g_ptr_array_sized_new(d->size); |
---|
62 | int i; |
---|
63 | for (i = 0; i < d->size; i++) { |
---|
64 | g_ptr_array_add(keys, g_strdup(d->els[i].k)); |
---|
65 | } |
---|
66 | return keys; |
---|
67 | } |
---|
68 | |
---|
69 | void owl_dict_noop_delete(void *x) |
---|
70 | { |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | /* Returns 0 on success. Will copy the key but make |
---|
75 | a reference to the value. Will clobber an existing |
---|
76 | entry with the same key iff delete_on_replace!=NULL, |
---|
77 | and will run delete_on_replace on the old element. |
---|
78 | Will return -2 if replace=NULL and match was found. |
---|
79 | */ |
---|
80 | int owl_dict_insert_element(owl_dict *d, const char *k, void *v, void (*delete_on_replace)(void *old)) |
---|
81 | { |
---|
82 | int pos, found; |
---|
83 | found = _owl_dict_find_pos(d, k, &pos); |
---|
84 | if (found && delete_on_replace) { |
---|
85 | delete_on_replace(d->els[pos].v); |
---|
86 | d->els[pos].v = v; |
---|
87 | return(0); |
---|
88 | } else if (found && !delete_on_replace) { |
---|
89 | return(-2); |
---|
90 | } else { |
---|
91 | if (d->size + 1 > d->avail) { |
---|
92 | int avail = MAX(d->avail * GROWBY, d->size + 1); |
---|
93 | d->els = g_renew(owl_dict_el, d->els, avail); |
---|
94 | d->avail = avail; |
---|
95 | if (d->els==NULL) return(-1); |
---|
96 | } |
---|
97 | if (pos!=d->size) { |
---|
98 | /* shift forward to leave us a slot */ |
---|
99 | memmove(d->els+pos+1, d->els+pos, |
---|
100 | sizeof(owl_dict_el)*(d->size-pos)); |
---|
101 | } |
---|
102 | d->size++; |
---|
103 | d->els[pos].k = g_strdup(k); |
---|
104 | d->els[pos].v = v; |
---|
105 | return(0); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /* Doesn't free the value of the element, but does |
---|
110 | * return it so the caller can free it. */ |
---|
111 | CALLER_OWN void *owl_dict_remove_element(owl_dict *d, const char *k) |
---|
112 | { |
---|
113 | int i; |
---|
114 | int pos, found; |
---|
115 | void *v; |
---|
116 | found = _owl_dict_find_pos(d, k, &pos); |
---|
117 | if (!found) return(NULL); |
---|
118 | g_free(d->els[pos].k); |
---|
119 | v = d->els[pos].v; |
---|
120 | for (i=pos; i<d->size-1; i++) { |
---|
121 | d->els[i]=d->els[i+1]; |
---|
122 | } |
---|
123 | d->size--; |
---|
124 | return(v); |
---|
125 | } |
---|
126 | |
---|
127 | /* elefree should free the value as well */ |
---|
128 | void owl_dict_cleanup(owl_dict *d, void (*elefree)(void *)) |
---|
129 | { |
---|
130 | int i; |
---|
131 | |
---|
132 | for (i=0; i<d->size; i++) { |
---|
133 | g_free(d->els[i].k); |
---|
134 | if (elefree) (elefree)(d->els[i].v); |
---|
135 | } |
---|
136 | if (d->els) g_free(d->els); |
---|
137 | } |
---|
138 | |
---|