reprap: Added c3, a small scene graph lib
[simavr] / examples / board_reprap / src / c3 / c_utils.c
1 /*
2         c_utils.c
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "c_utils.h"
24
25 void
26 str_hash_init(str_hash_p h)
27 {
28         memset(h, 0, sizeof(*h));
29 }
30
31 void
32 str_hash_add(
33         str_hash_p h,
34         str_p k,
35         void * v)
36 {
37         uint16_t hv = str_hash(k);
38         hashval_array_p bin = &h->bin[hv & (STR_HASH_SIZE-1)];
39         int inserti = bin->count;
40
41         for (int i = 0; i < bin->count; i++)
42                 if (bin->e[i].key->hash >= hv) {
43                         inserti = i;
44                         break;
45                 }
46         str_hashval_t n = { .key = str_dup(k), .val = v };
47         hashval_array_insert(bin, inserti, &n, 1);
48         return;
49 }
50
51 void *
52 str_hash_lookup(
53         str_hash_p h,
54         str_p k )
55 {
56         uint16_t hv = str_hash(k);
57         hashval_array_p bin = &h->bin[hv & (STR_HASH_SIZE-1)];
58
59         for (int i = 0; i < bin->count; i++) {
60                 uint16_t h = bin->e[i].key->hash;
61                 if (h == hv && !str_cmp(k, bin->e[i].key))
62                         return bin->e[i].val;
63                 else if (h > hv)
64                         break;
65         }
66         return NULL;
67 }