NCDStringIndex.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @file NCDStringIndex.c
  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 <string.h>
  30. #include <stdlib.h>
  31. #include <misc/hashfun.h>
  32. #include <misc/strdup.h>
  33. #include <base/BLog.h>
  34. #include "NCDStringIndex.h"
  35. #include "NCDStringIndex_hash.h"
  36. #include <structure/CHash_impl.h>
  37. #define GROWARRAY_NAME Array
  38. #define GROWARRAY_OBJECT_TYPE NCDStringIndex
  39. #define GROWARRAY_ARRAY_MEMBER entries
  40. #define GROWARRAY_CAPACITY_MEMBER entries_capacity
  41. #define GROWARRAY_MAX_CAPACITY NCD_STRING_ID_MAX
  42. #include <misc/grow_array.h>
  43. #include <generated/blog_channel_ncd.h>
  44. static NCD_string_id_t do_get (NCDStringIndex *o, const char *str, size_t str_len)
  45. {
  46. ASSERT(str)
  47. ASSERT(!memchr(str, '\0', str_len))
  48. NCDStringIndex_hash_key key = {str, str_len};
  49. NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, key);
  50. ASSERT(ref.link == -1 || ref.link >= 0)
  51. ASSERT(ref.link == -1 || ref.link < o->entries_size)
  52. ASSERT(ref.link == -1 || (ref.ptr->str_len == str_len && !memcmp(ref.ptr->str, str, str_len)))
  53. if (ref.link != -1) {
  54. return ref.link;
  55. }
  56. if (o->entries_size == o->entries_capacity && !Array_DoubleUp(o)) {
  57. BLog(BLOG_ERROR, "Array_DoubleUp failed");
  58. return -1;
  59. }
  60. ASSERT(o->entries_size < o->entries_capacity)
  61. struct NCDStringIndex__entry *entry = &o->entries[o->entries_size];
  62. if (!(entry->str = b_strdup_bin(str, str_len))) {
  63. BLog(BLOG_ERROR, "b_strdup_bin failed");
  64. return -1;
  65. }
  66. entry->str_len = str_len;
  67. NCDStringIndex__HashRef newref = {entry, o->entries_size};
  68. int res = NCDStringIndex__Hash_Insert(&o->hash, o->entries, newref, NULL);
  69. ASSERT_EXECUTE(res)
  70. return o->entries_size++;
  71. }
  72. int NCDStringIndex_Init (NCDStringIndex *o)
  73. {
  74. o->entries_size = 0;
  75. if (!Array_Init(o, NCDSTRINGINDEX_INITIAL_CAPACITY)) {
  76. BLog(BLOG_ERROR, "Array_Init failed");
  77. goto fail0;
  78. }
  79. if (!NCDStringIndex__Hash_Init(&o->hash, NCDSTRINGINDEX_HASH_BUCKETS)) {
  80. BLog(BLOG_ERROR, "NCDStringIndex__Hash_Init failed");
  81. goto fail1;
  82. }
  83. if (do_get(o, "", 0) < 0) {
  84. goto fail2;
  85. }
  86. DebugObject_Init(&o->d_obj);
  87. return 1;
  88. fail2:
  89. NCDStringIndex__Hash_Free(&o->hash);
  90. fail1:
  91. Array_Free(o);
  92. fail0:
  93. return 0;
  94. }
  95. void NCDStringIndex_Free (NCDStringIndex *o)
  96. {
  97. DebugObject_Free(&o->d_obj);
  98. for (NCD_string_id_t i = 0; i < o->entries_size; i++) {
  99. free(o->entries[i].str);
  100. }
  101. NCDStringIndex__Hash_Free(&o->hash);
  102. Array_Free(o);
  103. }
  104. NCD_string_id_t NCDStringIndex_Lookup (NCDStringIndex *o, const char *str)
  105. {
  106. DebugObject_Access(&o->d_obj);
  107. ASSERT(str)
  108. return NCDStringIndex_LookupBin(o, str, strlen(str));
  109. }
  110. NCD_string_id_t NCDStringIndex_LookupBin (NCDStringIndex *o, const char *str, size_t str_len)
  111. {
  112. DebugObject_Access(&o->d_obj);
  113. ASSERT(str)
  114. ASSERT(!memchr(str, '\0', str_len))
  115. NCDStringIndex_hash_key key = {str, str_len};
  116. NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, key);
  117. ASSERT(ref.link == -1 || ref.link >= 0)
  118. ASSERT(ref.link == -1 || ref.link < o->entries_size)
  119. ASSERT(ref.link == -1 || (ref.ptr->str_len == str_len && !memcmp(ref.ptr->str, str, str_len)))
  120. return ref.link;
  121. }
  122. NCD_string_id_t NCDStringIndex_Get (NCDStringIndex *o, const char *str)
  123. {
  124. DebugObject_Access(&o->d_obj);
  125. ASSERT(str)
  126. return NCDStringIndex_GetBin(o, str, strlen(str));
  127. }
  128. NCD_string_id_t NCDStringIndex_GetBin (NCDStringIndex *o, const char *str, size_t str_len)
  129. {
  130. DebugObject_Access(&o->d_obj);
  131. ASSERT(str)
  132. ASSERT(!memchr(str, '\0', str_len))
  133. return do_get(o, str, str_len);
  134. }
  135. const char * NCDStringIndex_Value (NCDStringIndex *o, NCD_string_id_t id)
  136. {
  137. DebugObject_Access(&o->d_obj);
  138. ASSERT(id >= 0)
  139. ASSERT(id < o->entries_size)
  140. ASSERT(o->entries[id].str)
  141. return o->entries[id].str;
  142. }
  143. int NCDStringIndex_GetRequests (NCDStringIndex *o, struct NCD_string_request *requests)
  144. {
  145. DebugObject_Access(&o->d_obj);
  146. ASSERT(requests)
  147. while (requests->str) {
  148. NCD_string_id_t id = NCDStringIndex_Get(o, requests->str);
  149. if (id < 0) {
  150. return 0;
  151. }
  152. requests->id = id;
  153. requests++;
  154. }
  155. return 1;
  156. }