source: dict.c @ c4efb46

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since c4efb46 was 4d86e06, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Get rid of a whole bunch of useless casts. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 5.4 KB
Line 
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>
11#include "owl.h"
12
13
14#define INITSIZE 30
15#define GROWAT 2
16#define GROWBY 1.5
17
18int owl_dict_create(owl_dict *d) {
19  d->size=0;
20  d->els=owl_malloc(INITSIZE*sizeof(owl_dict_el));
21  d->avail=INITSIZE;
22  if (d->els==NULL) return(-1);
23  return(0);
24}
25
26int owl_dict_get_size(owl_dict *d) {
27  return(d->size);
28}
29
30/* Finds the position of an element with key k, or of the element where
31 * this element would logically go, and stores the index in pos.
32 * TODO: optimize to do a binary search.
33 * Returns 1 if found, else 0. */
34int _owl_dict_find_pos(owl_dict *d, char *k, int *pos) {
35  int i;
36  for (i=0; (i<d->size) && strcmp(k,d->els[i].k)>0; i++);
37  *pos = i;
38  if (i>=d->size || strcmp(k,d->els[i].k)) {
39    return(0);
40  } else {
41    return(1);
42  }
43}
44
45/* returns the value corresponding to key k */
46void *owl_dict_find_element(owl_dict *d, char *k) {
47  int found, pos;
48  found = _owl_dict_find_pos(d, k, &pos);
49  if (!found) {
50    return(NULL);
51  }
52  return(d->els[pos].v);
53}
54
55/* creates a list and fills it in with keys.  duplicates the keys,
56 * so they will need to be freed by the caller. */
57int owl_dict_get_keys(owl_dict *d, owl_list *l) {
58  int i;
59  char *dupk;
60  if (owl_list_create(l)) return(-1);
61  for (i=0; i<d->size; i++) {
62    if ((dupk = owl_strdup(d->els[i].k)) == NULL) return(-1);
63    owl_list_append_element(l, dupk);
64  }
65  return(0);
66}
67
68void owl_dict_noop_free(void *x) {
69  return;
70}
71
72/* Returns 0 on success.  Will copy the key but make
73   a reference to the value.  Will clobber an existing
74   entry with the same key iff free_on_replace!=NULL,
75   and will run free_on_replace on the old element.
76   Will return -2 if replace=NULL and match was found.
77*/
78int owl_dict_insert_element(owl_dict *d, char *k, void *v, void (*free_on_replace)(void *old)) {
79  int pos, found;
80  char *dupk;
81  found = _owl_dict_find_pos(d, k, &pos);
82  if (found && free_on_replace) {
83    free_on_replace(d->els[pos].v);
84    d->els[pos].v = v;
85    return(0);
86  } else if (found && !free_on_replace) {
87    return(-2);
88  } else {
89    if ((d->size+1) > (d->avail/GROWAT)) {
90      d->els=owl_realloc(d->els, d->avail*GROWBY*sizeof(void *));
91      d->avail=d->avail*GROWBY;
92      if (d->els==NULL) return(-1);
93    }
94    if ((dupk = owl_strdup(k)) == NULL) return(-1);
95    if (pos!=d->size) {
96      /* shift forward to leave us a slot */
97      memmove(d->els+pos+1, d->els+pos, 
98              sizeof(owl_dict_el)*(d->size-pos));
99    }
100    d->size++;
101    d->els[pos].k = dupk;
102    d->els[pos].v = v;   
103    return(0);
104  }
105}
106
107/* Doesn't free the value of the element, but does
108 * return it so the caller can free it. */
109void *owl_dict_remove_element(owl_dict *d, char *k) {
110  int i;
111  int pos, found;
112  void *v;
113  found = _owl_dict_find_pos(d, k, &pos);
114  if (!found) return(NULL);
115  owl_free(d->els[pos].k);
116  v = d->els[pos].v;
117  for (i=pos; i<d->size-1; i++) {
118    d->els[i]=d->els[i+1];
119  }
120  d->size--;
121  return(v);
122}
123
124/* elefree should free the value as well */
125void owl_dict_free_all(owl_dict *d, void (*elefree)(void *)) {
126  int i;
127
128  for (i=0; i<d->size; i++) {
129    owl_free(d->els[i].k);
130    if (elefree) (elefree)(d->els[i].v);
131  }
132  if (d->els) owl_free(d->els);
133}
134
135void owl_dict_free_simple(owl_dict *d) {
136  owl_dict_free_all(d, NULL);
137}
138
139
140
141/************* REGRESSION TESTS **************/
142#ifdef OWL_INCLUDE_REG_TESTS
143
144#include "test.h"
145
146int owl_dict_regtest(void) {
147  owl_dict d;
148  owl_list l;
149  int numfailed=0;
150  char *av="aval", *bv="bval", *cv="cval", *dv="dval";
151
152  printf("# BEGIN testing owl_dict\n");
153  FAIL_UNLESS("create", 0==owl_dict_create(&d));
154  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_free));
155  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_free));
156  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_free));
157  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_free));
158  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
159  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
160  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
161  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
162  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
163  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
164  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
165  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
166
167  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
168  FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
169  FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
170 
171  /* these assume the returned keys are sorted */
172  FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
173  FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
174  FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
175
176  owl_list_free_all(&l, owl_free);
177  owl_dict_free_all(&d, NULL);
178
179  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
180  printf("# END testing owl_dict (%d failures)\n", numfailed);
181  return(numfailed);
182}
183
184#endif /* OWL_INCLUDE_REG_TESTS */
Note: See TracBrowser for help on using the repository browser.