Added simple string map (hash) utility.
[idzebra-moved-to-github.git] / util / strmap.c
1 /* $Id: strmap.c,v 1.1 2007-12-02 11:30:28 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stddef.h>
24 #include <string.h>
25 #include <zebra_strmap.h>
26 #include <yaz/nmem.h>
27
28 struct strmap_entry {
29     char *name;
30     size_t data_len;
31     void *data_buf;
32     struct strmap_entry *next;
33 };
34     
35 struct zebra_strmap {
36     NMEM nmem_str;
37     NMEM nmem_ent;
38     int hsize;
39     struct strmap_entry **entries;
40     struct strmap_entry *free_entries;
41 };
42
43 zebra_strmap_t zebra_strmap_create(void)
44 {
45     int i;
46     NMEM nmem_ent = nmem_create();
47     zebra_strmap_t st = nmem_malloc(nmem_ent, sizeof(*st));
48     st->nmem_ent = nmem_ent;
49     st->nmem_str = nmem_create();
50     st->hsize = 1001;
51     st->free_entries = 0;
52     st->entries = nmem_malloc(nmem_ent, st->hsize * sizeof(*st->entries));
53     for (i = 0; i < st->hsize; i++)
54         st->entries[i] = 0;
55     return st;
56 }
57
58 void zebra_strmap_destroy(zebra_strmap_t st)
59 {
60     if (st)
61     {
62         nmem_destroy(st->nmem_str);
63         nmem_destroy(st->nmem_ent);
64     }
65 }
66
67 static struct strmap_entry **hash(zebra_strmap_t st, const char *name)
68 {
69     unsigned hash = 0;
70     int i;
71     for (i = 0; name[i]; i++)
72         hash += hash*65519 + name[i];
73     hash = hash % st->hsize;
74     return st->entries + hash;
75 }
76
77 void zebra_strmap_add(zebra_strmap_t st, const char *name,
78                       void *data_buf, size_t data_len)
79 {
80     struct strmap_entry **e = hash(st, name);
81     struct strmap_entry *ne = st->free_entries;
82
83     if (ne)
84         st->free_entries = ne->next;
85     else
86         ne = nmem_malloc(st->nmem_ent, sizeof(*ne));
87     ne->next = *e;
88     *e = ne;
89     ne->name = nmem_strdup(st->nmem_str, name);
90     ne->data_buf = nmem_malloc(st->nmem_str, data_len);
91     memcpy(ne->data_buf, data_buf, data_len);
92     ne->data_len = data_len;
93 }
94
95 void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no,
96                           size_t *data_len)
97 {
98     struct strmap_entry *e = *hash(st, name);
99     int i = 0;
100     for (; e ; e = e->next)
101         if (!strcmp(name, e->name))
102         {
103             if (i == no)
104             {
105                 if (data_len)
106                     *data_len = e->data_len;
107                 return e->data_buf;
108             }
109             i++;
110         }
111     return 0;
112 }
113
114 int zebra_strmap_remove(zebra_strmap_t st, const char *name)
115 {
116     struct strmap_entry **e = hash(st, name);
117     for (; *e ; e = &(*e)->next)
118         if (!strcmp(name, (*e)->name))
119         {
120             struct strmap_entry *tmp = *e;
121             *e = (*e)->next;
122
123             tmp->next = st->free_entries;
124             st->free_entries = tmp;
125             return 1;
126         }
127     return 0;
128 }
129                          
130 /*
131  * Local variables:
132  * c-basic-offset: 4
133  * indent-tabs-mode: nil
134  * End:
135  * vim: shiftwidth=4 tabstop=8 expandtab
136  */
137