BStringTrie.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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)
  38. struct BStringTrie__node {
  39. int value[BSTRINGTRIE_DEGREE];
  40. int link[BSTRINGTRIE_DEGREE];
  41. };
  42. typedef struct {
  43. struct BStringTrie__node *arr;
  44. size_t count;
  45. size_t capacity;
  46. int empty_value;
  47. } BStringTrie;
  48. static int BStringTrie_Init (BStringTrie *o) WARN_UNUSED;
  49. static void BStringTrie_Free (BStringTrie *o);
  50. static int BStringTrie_Set (BStringTrie *o, const char *key, int value) WARN_UNUSED;
  51. static int BStringTrie_Lookup (const BStringTrie *o, const char *key);
  52. // implementation follows
  53. static int BStringTrie__new_node (BStringTrie *o, int *out_nodeidx)
  54. {
  55. ASSERT(o->count <= o->capacity)
  56. ASSERT(o->capacity > 0)
  57. ASSERT(out_nodeidx)
  58. if (o->count == o->capacity) {
  59. if (o->capacity > SIZE_MAX / 2) {
  60. return 0;
  61. }
  62. size_t newcap = 2 * o->capacity;
  63. struct BStringTrie__node *newarr = BAllocArray(newcap, sizeof(newarr[0]));
  64. if (!newarr) {
  65. return 0;
  66. }
  67. memcpy(newarr, o->arr, o->count * sizeof(newarr[0]));
  68. BFree(o->arr);
  69. o->arr = newarr;
  70. o->capacity = newcap;
  71. }
  72. struct BStringTrie__node *node = &o->arr[o->count];
  73. for (size_t i = 0; i < BSTRINGTRIE_DEGREE; i++) {
  74. node->value[i] = BSTRINGTRIE_DEFAULT_VALUE;
  75. node->link[i] = -1;
  76. }
  77. *out_nodeidx = o->count;
  78. o->count++;
  79. return 1;
  80. }
  81. static int BStringTrie_Init (BStringTrie *o)
  82. {
  83. o->count = 0;
  84. o->capacity = 1;
  85. o->empty_value = BSTRINGTRIE_DEFAULT_VALUE;
  86. if (!(o->arr = BAllocArray(o->capacity, sizeof(o->arr[0])))) {
  87. return 0;
  88. }
  89. int idx0;
  90. BStringTrie__new_node(o, &idx0);
  91. return 1;
  92. }
  93. static void BStringTrie_Free (BStringTrie *o)
  94. {
  95. BFree(o->arr);
  96. }
  97. static int BStringTrie_Set (BStringTrie *o, const char *key, int value)
  98. {
  99. ASSERT(key)
  100. if (!*key) {
  101. o->empty_value = value;
  102. return 1;
  103. }
  104. const unsigned char *ukey = (const unsigned char *)key;
  105. int nodeidx = 0;
  106. while (*(ukey + 1)) {
  107. ASSERT(o->arr[nodeidx].link[*ukey] >= -1)
  108. int new_nodeidx;
  109. if (o->arr[nodeidx].link[*ukey] >= 0) {
  110. new_nodeidx = o->arr[nodeidx].link[*ukey];
  111. } else {
  112. if (!BStringTrie__new_node(o, &new_nodeidx)) {
  113. return 0;
  114. }
  115. o->arr[nodeidx].link[*ukey] = new_nodeidx;
  116. }
  117. ASSERT(new_nodeidx >= 0)
  118. ASSERT(new_nodeidx < o->count)
  119. nodeidx = new_nodeidx;
  120. ukey++;
  121. }
  122. o->arr[nodeidx].value[*ukey] = value;
  123. return 1;
  124. }
  125. static int BStringTrie_Lookup (const BStringTrie *o, const char *key)
  126. {
  127. ASSERT(key)
  128. if (!*key) {
  129. return o->empty_value;
  130. }
  131. const unsigned char *ukey = (const unsigned char *)key;
  132. int nodeidx = 0;
  133. while (*(ukey + 1)) {
  134. nodeidx = o->arr[nodeidx].link[*ukey];
  135. ASSERT(nodeidx >= -1)
  136. if (nodeidx < 0) {
  137. return -1;
  138. }
  139. ASSERT(nodeidx < o->count)
  140. ukey++;
  141. }
  142. return o->arr[nodeidx].value[*ukey];
  143. }
  144. #endif