NCDStringIndex.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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)
  45. {
  46. ASSERT(str)
  47. NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, str);
  48. ASSERT(ref.link == -1 || ref.link >= 0)
  49. ASSERT(ref.link == -1 || ref.link < o->entries_size)
  50. ASSERT(ref.link == -1 || !strcmp(ref.ptr->str, str))
  51. if (ref.link != -1) {
  52. return ref.link;
  53. }
  54. if (o->entries_size == o->entries_capacity && !Array_DoubleUp(o)) {
  55. BLog(BLOG_ERROR, "Array_DoubleUp failed");
  56. return -1;
  57. }
  58. ASSERT(o->entries_size < o->entries_capacity)
  59. struct NCDStringIndex__entry *entry = &o->entries[o->entries_size];
  60. if (!(entry->str = b_strdup(str))) {
  61. BLog(BLOG_ERROR, "b_strdup failed");
  62. return -1;
  63. }
  64. NCDStringIndex__HashRef newref = {entry, o->entries_size};
  65. int res = NCDStringIndex__Hash_Insert(&o->hash, o->entries, newref, NULL);
  66. ASSERT_EXECUTE(res)
  67. return o->entries_size++;
  68. }
  69. int NCDStringIndex_Init (NCDStringIndex *o)
  70. {
  71. o->entries_size = 0;
  72. if (!Array_Init(o, NCDSTRINGINDEX_INITIAL_CAPACITY)) {
  73. BLog(BLOG_ERROR, "Array_Init failed");
  74. goto fail0;
  75. }
  76. if (!NCDStringIndex__Hash_Init(&o->hash, NCDSTRINGINDEX_HASH_BUCKETS)) {
  77. BLog(BLOG_ERROR, "NCDStringIndex__Hash_Init failed");
  78. goto fail1;
  79. }
  80. if (do_get(o, "") < 0) {
  81. goto fail2;
  82. }
  83. DebugObject_Init(&o->d_obj);
  84. return 1;
  85. fail2:
  86. NCDStringIndex__Hash_Free(&o->hash);
  87. fail1:
  88. Array_Free(o);
  89. fail0:
  90. return 0;
  91. }
  92. void NCDStringIndex_Free (NCDStringIndex *o)
  93. {
  94. DebugObject_Free(&o->d_obj);
  95. for (NCD_string_id_t i = 0; i < o->entries_size; i++) {
  96. free(o->entries[i].str);
  97. }
  98. NCDStringIndex__Hash_Free(&o->hash);
  99. Array_Free(o);
  100. }
  101. NCD_string_id_t NCDStringIndex_Lookup (NCDStringIndex *o, const char *str)
  102. {
  103. DebugObject_Access(&o->d_obj);
  104. ASSERT(str)
  105. NCDStringIndex__HashRef ref = NCDStringIndex__Hash_Lookup(&o->hash, o->entries, str);
  106. ASSERT(ref.link == -1 || ref.link >= 0)
  107. ASSERT(ref.link == -1 || ref.link < o->entries_size)
  108. ASSERT(ref.link == -1 || !strcmp(ref.ptr->str, str))
  109. return ref.link;
  110. }
  111. NCD_string_id_t NCDStringIndex_Get (NCDStringIndex *o, const char *str)
  112. {
  113. DebugObject_Access(&o->d_obj);
  114. ASSERT(str)
  115. return do_get(o, str);
  116. }
  117. const char * NCDStringIndex_Value (NCDStringIndex *o, NCD_string_id_t id)
  118. {
  119. DebugObject_Access(&o->d_obj);
  120. ASSERT(id >= 0)
  121. ASSERT(id < o->entries_size)
  122. ASSERT(o->entries[id].str)
  123. return o->entries[id].str;
  124. }
  125. int NCDStringIndex_GetRequests (NCDStringIndex *o, struct NCD_string_request *requests)
  126. {
  127. DebugObject_Access(&o->d_obj);
  128. ASSERT(requests)
  129. while (requests->str) {
  130. NCD_string_id_t id = NCDStringIndex_Get(o, requests->str);
  131. if (id < 0) {
  132. return 0;
  133. }
  134. requests->id = id;
  135. requests++;
  136. }
  137. return 1;
  138. }