BStringTrie.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * @file BStringTrie.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions 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. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_BSTRINGTRIE_H
  30. #define BADVPN_BSTRINGTRIE_H
  31. #include <stddef.h>
  32. #include <limits.h>
  33. #include <string.h>
  34. #include <misc/debug.h>
  35. #include <misc/balloc.h>
  36. #define BSTRINGTRIE_DEFAULT_VALUE ((int)-1)
  37. #define BSTRINGTRIE_DEGREE ((((size_t)1) << CHAR_BIT) - 1)
  38. struct BStringTrie__node {
  39. int value_links[1 + BSTRINGTRIE_DEGREE];
  40. };
  41. typedef struct {
  42. struct BStringTrie__node *arr;
  43. size_t count;
  44. size_t capacity;
  45. } BStringTrie;
  46. static int BStringTrie_Init (BStringTrie *o) WARN_UNUSED;
  47. static void BStringTrie_Free (BStringTrie *o);
  48. static int BStringTrie_Set (BStringTrie *o, const char *key, int value) WARN_UNUSED;
  49. static int BStringTrie_Lookup (const BStringTrie *o, const char *key);
  50. // implementation follows
  51. static int BStringTrie__new_node (BStringTrie *o, int *out_nodeidx)
  52. {
  53. ASSERT(o->count <= o->capacity)
  54. ASSERT(o->capacity > 0)
  55. ASSERT(out_nodeidx)
  56. if (o->count == o->capacity) {
  57. if (o->capacity > SIZE_MAX / 2) {
  58. return 0;
  59. }
  60. size_t newcap = 2 * o->capacity;
  61. struct BStringTrie__node *newarr = BAllocArray(newcap, sizeof(newarr[0]));
  62. if (!newarr) {
  63. return 0;
  64. }
  65. memcpy(newarr, o->arr, o->count * sizeof(newarr[0]));
  66. BFree(o->arr);
  67. o->arr = newarr;
  68. o->capacity = newcap;
  69. }
  70. struct BStringTrie__node *node = &o->arr[o->count];
  71. node->value_links[0] = BSTRINGTRIE_DEFAULT_VALUE;
  72. for (size_t i = 0; i < BSTRINGTRIE_DEGREE; i++) {
  73. node->value_links[1 + i] = -1;
  74. }
  75. *out_nodeidx = o->count;
  76. o->count++;
  77. return 1;
  78. }
  79. static int BStringTrie_Init (BStringTrie *o)
  80. {
  81. o->count = 0;
  82. o->capacity = 1;
  83. if (!(o->arr = BAllocArray(o->capacity, sizeof(o->arr[0])))) {
  84. return 0;
  85. }
  86. int idx0;
  87. BStringTrie__new_node(o, &idx0);
  88. return 1;
  89. }
  90. static void BStringTrie_Free (BStringTrie *o)
  91. {
  92. BFree(o->arr);
  93. }
  94. static int BStringTrie_Set (BStringTrie *o, const char *key, int value)
  95. {
  96. ASSERT(key)
  97. const unsigned char *ukey = (const unsigned char *)key;
  98. int nodeidx = 0;
  99. while (*ukey) {
  100. ASSERT(o->arr[nodeidx].value_links[*ukey] >= -1)
  101. int new_nodeidx;
  102. if (o->arr[nodeidx].value_links[*ukey] >= 0) {
  103. new_nodeidx = o->arr[nodeidx].value_links[*ukey];
  104. } else {
  105. if (!BStringTrie__new_node(o, &new_nodeidx)) {
  106. return 0;
  107. }
  108. o->arr[nodeidx].value_links[*ukey] = new_nodeidx;
  109. }
  110. ASSERT(new_nodeidx >= 0)
  111. ASSERT(new_nodeidx < o->count)
  112. nodeidx = new_nodeidx;
  113. ukey++;
  114. }
  115. o->arr[nodeidx].value_links[0] = value;
  116. return 1;
  117. }
  118. static int BStringTrie_Lookup (const BStringTrie *o, const char *key)
  119. {
  120. ASSERT(key)
  121. const unsigned char *ukey = (const unsigned char *)key;
  122. int nodeidx = 0;
  123. while (*ukey) {
  124. nodeidx = o->arr[nodeidx].value_links[*ukey];
  125. ASSERT(nodeidx >= -1)
  126. if (nodeidx < 0) {
  127. return -1;
  128. }
  129. ASSERT(nodeidx < o->count)
  130. ukey++;
  131. }
  132. return o->arr[nodeidx].value_links[0];
  133. }
  134. #endif