CStringTrie_impl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @file CStringTrie_impl.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. #include "CStringTrie_header.h"
  30. static int CStringTrie__new_node (CStringTrie *o, int *out_nodeidx)
  31. {
  32. ASSERT(o->count <= o->capacity)
  33. ASSERT(o->capacity > 0)
  34. ASSERT(o->count >= 0)
  35. ASSERT(out_nodeidx)
  36. if (o->count == o->capacity) {
  37. if (o->capacity > INT_MAX / 2 || o->capacity > SIZE_MAX / 2) {
  38. return 0;
  39. }
  40. int newcap = 2 * o->capacity;
  41. struct CStringTrie__node *newarr = BAllocArray(newcap, sizeof(newarr[0]));
  42. if (!newarr) {
  43. return 0;
  44. }
  45. memcpy(newarr, o->arr, o->count * sizeof(newarr[0]));
  46. BFree(o->arr);
  47. o->arr = newarr;
  48. o->capacity = newcap;
  49. }
  50. struct CStringTrie__node *node = &o->arr[o->count];
  51. node->value = CSTRINGTRIE_PARAM_DEFAULT;
  52. for (int i = 0; i < CStringTrie__DEGREE; i++) {
  53. node->links[i] = -1;
  54. }
  55. *out_nodeidx = o->count;
  56. o->count++;
  57. return 1;
  58. }
  59. static int CStringTrie_Init (CStringTrie *o)
  60. {
  61. o->count = 0;
  62. o->capacity = 1;
  63. if (!(o->arr = BAllocArray(o->capacity, sizeof(o->arr[0])))) {
  64. return 0;
  65. }
  66. int idx0;
  67. CStringTrie__new_node(o, &idx0);
  68. return 1;
  69. }
  70. static void CStringTrie_Free (CStringTrie *o)
  71. {
  72. BFree(o->arr);
  73. }
  74. static int CStringTrie_Set (CStringTrie *o, const char *key, CStringTrieValue value)
  75. {
  76. ASSERT(key)
  77. const unsigned char *ukey = (const unsigned char *)key;
  78. int nodeidx = 0;
  79. while (*ukey) {
  80. unsigned char mod = *ukey % CStringTrie__DEGREE;
  81. ASSERT(o->arr[nodeidx].links[mod] >= -1)
  82. int new_nodeidx;
  83. if (o->arr[nodeidx].links[mod] >= 0) {
  84. new_nodeidx = o->arr[nodeidx].links[mod];
  85. } else {
  86. if (!CStringTrie__new_node(o, &new_nodeidx)) {
  87. return 0;
  88. }
  89. o->arr[nodeidx].links[mod] = new_nodeidx;
  90. }
  91. ASSERT(new_nodeidx >= 0)
  92. ASSERT(new_nodeidx < o->count)
  93. nodeidx = new_nodeidx;
  94. ukey++;
  95. }
  96. o->arr[nodeidx].value = value;
  97. return 1;
  98. }
  99. static CStringTrieValue CStringTrie_Get (const CStringTrie *o, const char *key)
  100. {
  101. ASSERT(key)
  102. const unsigned char *ukey = (const unsigned char *)key;
  103. int nodeidx = 0;
  104. while (*ukey) {
  105. unsigned char mod = *ukey % CStringTrie__DEGREE;
  106. nodeidx = o->arr[nodeidx].links[mod];
  107. ASSERT(nodeidx >= -1)
  108. if (nodeidx < 0) {
  109. return CSTRINGTRIE_PARAM_DEFAULT;
  110. }
  111. ASSERT(nodeidx < o->count)
  112. ukey++;
  113. }
  114. return o->arr[nodeidx].value;
  115. }
  116. #include "CStringTrie_footer.h"