[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. |
---|
| 5 | * O(n) on searches, although it should switch to a binary search for O(log n) |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include <stdlib.h> |
---|
| 9 | #include <string.h> |
---|
| 10 | #include <unistd.h> |
---|
[1aee7d9] | 11 | #include "owl.h" |
---|
| 12 | |
---|
| 13 | static const char fileIdent[] = "$Id$"; |
---|
| 14 | |
---|
[7d4fbcd] | 15 | |
---|
| 16 | #define INITSIZE 30 |
---|
| 17 | #define GROWAT 2 |
---|
| 18 | #define GROWBY 1.5 |
---|
| 19 | |
---|
| 20 | int owl_dict_create(owl_dict *d) { |
---|
| 21 | d->size=0; |
---|
| 22 | d->els=(owl_dict_el *)owl_malloc(INITSIZE*sizeof(owl_dict_el)); |
---|
| 23 | d->avail=INITSIZE; |
---|
| 24 | if (d->els==NULL) return(-1); |
---|
| 25 | return(0); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | int owl_dict_get_size(owl_dict *d) { |
---|
| 29 | return(d->size); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | /* Finds the position of an element with key k, or of the element where |
---|
| 33 | * this element would logically go, and stores the index in pos. |
---|
| 34 | * TODO: optimize to do a binary search. |
---|
| 35 | * Returns 1 if found, else 0. */ |
---|
| 36 | int _owl_dict_find_pos(owl_dict *d, char *k, int *pos) { |
---|
| 37 | int i; |
---|
| 38 | for (i=0; (i<d->size) && strcmp(k,d->els[i].k)>0; i++); |
---|
| 39 | *pos = i; |
---|
| 40 | if (i>=d->size || strcmp(k,d->els[i].k)) { |
---|
| 41 | return(0); |
---|
| 42 | } else { |
---|
| 43 | return(1); |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /* returns the value corresponding to key k */ |
---|
| 48 | void *owl_dict_find_element(owl_dict *d, char *k) { |
---|
| 49 | int found, pos; |
---|
| 50 | found = _owl_dict_find_pos(d, k, &pos); |
---|
| 51 | if (!found) { |
---|
| 52 | return(NULL); |
---|
| 53 | } |
---|
| 54 | return(d->els[pos].v); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /* creates a list and fills it in with keys. duplicates the keys, |
---|
| 58 | * so they will need to be freed by the caller. */ |
---|
| 59 | int owl_dict_get_keys(owl_dict *d, owl_list *l) { |
---|
| 60 | int i; |
---|
| 61 | char *dupk; |
---|
| 62 | if (owl_list_create(l)) return(-1); |
---|
| 63 | for (i=0; i<d->size; i++) { |
---|
| 64 | if ((dupk = owl_strdup(d->els[i].k)) == NULL) return(-1); |
---|
| 65 | owl_list_append_element(l, (void*)dupk); |
---|
| 66 | } |
---|
| 67 | return(0); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | void owl_dict_noop_free(void *x) { |
---|
| 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 free_on_replace!=NULL, |
---|
| 77 | and will run free_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, char *k, void *v, void (*free_on_replace)(void *old)) { |
---|
| 81 | int pos, found; |
---|
| 82 | char *dupk; |
---|
| 83 | found = _owl_dict_find_pos(d, k, &pos); |
---|
| 84 | if (found && free_on_replace) { |
---|
| 85 | free_on_replace(d->els[pos].v); |
---|
| 86 | d->els[pos].v = v; |
---|
| 87 | return(0); |
---|
| 88 | } else if (found && !free_on_replace) { |
---|
| 89 | return(-2); |
---|
| 90 | } else { |
---|
| 91 | if ((d->size+1) > (d->avail/GROWAT)) { |
---|
| 92 | d->els=owl_realloc(d->els, d->avail*GROWBY*sizeof(void *)); |
---|
| 93 | d->avail=d->avail*GROWBY; |
---|
| 94 | if (d->els==NULL) return(-1); |
---|
| 95 | } |
---|
| 96 | if ((dupk = owl_strdup(k)) == NULL) return(-1); |
---|
| 97 | if (pos!=d->size) { |
---|
| 98 | /* shift forward to leave us a slot */ |
---|
| 99 | memmove((void*)(d->els+pos+1), (void*)(d->els+pos), |
---|
| 100 | sizeof(owl_dict_el)*(d->size-pos)); |
---|
| 101 | } |
---|
| 102 | d->size++; |
---|
| 103 | d->els[pos].k = dupk; |
---|
| 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 | void *owl_dict_remove_element(owl_dict *d, char *k) { |
---|
| 112 | int i; |
---|
| 113 | int pos, found; |
---|
| 114 | void *v; |
---|
| 115 | found = _owl_dict_find_pos(d, k, &pos); |
---|
| 116 | if (!found) return(NULL); |
---|
| 117 | owl_free(d->els[pos].k); |
---|
| 118 | v = d->els[pos].v; |
---|
| 119 | for (i=pos; i<d->size-1; i++) { |
---|
| 120 | d->els[i]=d->els[i+1]; |
---|
| 121 | } |
---|
| 122 | d->size--; |
---|
| 123 | return(v); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /* elefree should free the value as well */ |
---|
| 127 | void owl_dict_free_all(owl_dict *d, void (*elefree)(void *)) { |
---|
| 128 | int i; |
---|
| 129 | |
---|
| 130 | for (i=0; i<d->size; i++) { |
---|
| 131 | owl_free(d->els[i].k); |
---|
| 132 | if (elefree) (elefree)(d->els[i].v); |
---|
| 133 | } |
---|
| 134 | if (d->els) owl_free(d->els); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void owl_dict_free_simple(owl_dict *d) { |
---|
| 138 | owl_dict_free_all(d, NULL); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | /************* REGRESSION TESTS **************/ |
---|
| 144 | #ifdef OWL_INCLUDE_REG_TESTS |
---|
| 145 | |
---|
[0138478] | 146 | #include "test.h" |
---|
[7d4fbcd] | 147 | |
---|
| 148 | int owl_dict_regtest(void) { |
---|
| 149 | owl_dict d; |
---|
| 150 | owl_list l; |
---|
| 151 | int numfailed=0; |
---|
| 152 | char *av="aval", *bv="bval", *cv="cval", *dv="dval"; |
---|
| 153 | |
---|
[1cf3f8d3] | 154 | printf("# BEGIN testing owl_dict\n"); |
---|
[7d4fbcd] | 155 | FAIL_UNLESS("create", 0==owl_dict_create(&d)); |
---|
| 156 | FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", (void*)bv, owl_dict_noop_free)); |
---|
| 157 | FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", (void*)dv, owl_dict_noop_free)); |
---|
| 158 | FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", (void*)av, owl_dict_noop_free)); |
---|
| 159 | FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", (void*)cv, owl_dict_noop_free)); |
---|
| 160 | FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", (void*)dv, 0)); |
---|
| 161 | FAIL_UNLESS("find a", (void*)av==owl_dict_find_element(&d, "a")); |
---|
| 162 | FAIL_UNLESS("find b", (void*)bv==owl_dict_find_element(&d, "b")); |
---|
| 163 | FAIL_UNLESS("find c", (void*)cv==owl_dict_find_element(&d, "c")); |
---|
| 164 | FAIL_UNLESS("find d", (void*)dv==owl_dict_find_element(&d, "d")); |
---|
| 165 | FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e")); |
---|
| 166 | FAIL_UNLESS("remove d", (void*)dv==owl_dict_remove_element(&d, "d")); |
---|
| 167 | FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d")); |
---|
| 168 | |
---|
| 169 | FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d)); |
---|
| 170 | FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l)); |
---|
| 171 | FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l)); |
---|
| 172 | |
---|
| 173 | /* these assume the returned keys are sorted */ |
---|
| 174 | FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0))); |
---|
| 175 | FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1))); |
---|
| 176 | FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2))); |
---|
| 177 | |
---|
| 178 | owl_list_free_all(&l, owl_free); |
---|
| 179 | owl_dict_free_all(&d, NULL); |
---|
| 180 | |
---|
[1cf3f8d3] | 181 | // if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); |
---|
| 182 | printf("# END testing owl_dict (%d failures)\n", numfailed); |
---|
[7d4fbcd] | 183 | return(numfailed); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | #endif /* OWL_INCLUDE_REG_TESTS */ |
---|