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