BStringTrie.h 4.8 KB

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