]> git.ozlabs.org Git - ccan/blob - ccan/stringmap/stringmap.h
licenses: clarify which BSD license it is.
[ccan] / ccan / stringmap / stringmap.h
1 /*
2  * Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
3  * Copyright (c) 2009 Joseph Adams (joeyadams3.14159@gmail.com).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef CCAN_STRINGMAP_H
30 #define CCAN_STRINGMAP_H
31
32 #include <ccan/block_pool/block_pool.h>
33 #include <stdint.h>
34
35 #define stringmap(theType) struct {struct stringmap t; struct {char *str; size_t len; theType value;} *last;}
36         //the 'last' pointer here is used as a hacky typeof() alternative
37
38 #define stringmap_new(ctx) {{0,0,(struct block_pool*)(ctx)},0}
39 #define stringmap_init(sm, ctx) do { \
40                 (sm).t.root = 0; \
41                 (sm).t.count = 0; \
42                 (sm).t.bp = (struct block_pool*)(ctx); \
43                 (sm).last = 0; \
44         } while(0)
45 #define stringmap_free(sm) do { \
46                 if ((sm).t.root) \
47                         block_pool_free((sm).t.bp); \
48         } while(0)
49
50 #define stringmap_lookup(sm, key) stringmap_le(sm, key, 0)
51 #define stringmap_enter(sm, key) stringmap_le(sm, key, 1)
52
53 /* Variants of lookup and enter that let you specify a length.  Note that byte
54    strings may have null characters in them, and it won't affect the
55         algorithm.  Many lives were lost to make this possible. */
56 #define stringmap_lookup_n(sm, key, len) stringmap_le_n(sm, key, len, 0)
57 #define stringmap_enter_n(sm, key, len) stringmap_le_n(sm, key, len, 1)
58
59 #define stringmap_le(sm, key, enterf) stringmap_le_n(sm, key, (size_t)-1, enterf)
60
61 //this macro sets sm.last so it can exploit its type
62 #define stringmap_le_n(sm, key, len, enterf) ((((sm).last) = stringmap_lookup_real(&(sm).t, key, len, enterf, sizeof(*(sm).last))) ? &(sm).last->value : NULL)
63
64
65 struct stringmap_node;
66
67 struct stringmap {
68         struct stringmap_node *root;
69         size_t count;
70         struct block_pool *bp;
71         //hack: 'bp' holds talloc ctx when 'root' is NULL
72 };
73
74 void *stringmap_lookup_real(struct stringmap *t, const char *key, size_t len, int enterf, size_t T_size);
75
76 #endif