stringmap.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef CCAN_STRINGMAP_H
  29. #define CCAN_STRINGMAP_H
  30. #include <ccan/block_pool/block_pool.h>
  31. #include <stdint.h>
  32. #define stringmap(theType) struct {struct stringmap t; struct {char *str; size_t len; theType value;} *last;}
  33. //the 'last' pointer here is used as a hacky typeof() alternative
  34. #define stringmap_new(ctx) {{0,0,(struct block_pool*)(ctx)},0}
  35. #define stringmap_init(sm, ctx) do { \
  36. (sm).t.root = 0; \
  37. (sm).t.count = 0; \
  38. (sm).t.bp = (struct block_pool*)(ctx); \
  39. (sm).last = 0; \
  40. } while(0)
  41. #define stringmap_free(sm) do { \
  42. if ((sm).t.root) \
  43. block_pool_free((sm).t.bp); \
  44. } while(0)
  45. #define stringmap_lookup(sm, key) stringmap_le(sm, key, 0)
  46. #define stringmap_enter(sm, key) stringmap_le(sm, key, 1)
  47. /* Variants of lookup and enter that let you specify a length. Note that byte
  48. strings may have null characters in them, and it won't affect the
  49. algorithm. Many lives were lost to make this possible. */
  50. #define stringmap_lookup_n(sm, key, len) stringmap_le_n(sm, key, len, 0)
  51. #define stringmap_enter_n(sm, key, len) stringmap_le_n(sm, key, len, 1)
  52. #define stringmap_le(sm, key, enterf) stringmap_le_n(sm, key, (size_t)-1, enterf)
  53. //this macro sets sm.last so it can exploit its type
  54. #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)
  55. struct stringmap_node;
  56. struct stringmap {
  57. struct stringmap_node *root;
  58. size_t count;
  59. struct block_pool *bp;
  60. //hack: 'bp' holds talloc ctx when 'root' is NULL
  61. };
  62. void *stringmap_lookup_real(struct stringmap *t, const char *key, size_t len, int enterf, size_t T_size);
  63. #endif