CHash_impl.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @file CHash_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 "CHash_header.h"
  30. static CHashLink CHashNullLink ()
  31. {
  32. return CHASH_PARAM_NULL;
  33. }
  34. static CHashRef CHashNullRef ()
  35. {
  36. CHashRef entry = {NULL, CHASH_PARAM_NULL};
  37. return entry;
  38. }
  39. static int CHash_Init (CHash *o, size_t numBuckets)
  40. {
  41. if (numBuckets == 0) {
  42. numBuckets = 1;
  43. }
  44. o->numBuckets = numBuckets;
  45. o->numEntries = 0;
  46. o->buckets = (CHashLink *)BAllocArray(o->numBuckets, sizeof(o->buckets[0]));
  47. if (!o->buckets) {
  48. return 0;
  49. }
  50. for (size_t i = 0; i < o->numBuckets; i++) {
  51. o->buckets[i] = CHashNullLink();
  52. }
  53. return 1;
  54. }
  55. static void CHash_Free (CHash *o)
  56. {
  57. BFree(o->buckets);
  58. }
  59. static CHashRef CHash_Deref (CHashArg arg, CHashLink link)
  60. {
  61. if (link == CHashNullLink()) {
  62. return CHashNullRef();
  63. }
  64. CHashRef entry;
  65. entry.ptr = CHASH_PARAM_DEREF(arg, link);
  66. entry.link = link;
  67. ASSERT(entry.ptr)
  68. return entry;
  69. }
  70. static int CHash_Insert (CHash *o, CHashArg arg, CHashRef entry, CHashRef *out_existing)
  71. {
  72. CHashKey key = CHASH_PARAM_GETKEY(arg, entry);
  73. size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
  74. CHashRef e = CHash_Deref(arg, o->buckets[index]);
  75. while (e.link != CHashNullLink()) {
  76. if (CHASH_PARAM_KEYSEQUAL(arg, key, CHASH_PARAM_GETKEY(arg, e))) {
  77. if (out_existing) {
  78. *out_existing = e;
  79. }
  80. return 0;
  81. }
  82. e = CHash_Deref(arg, e.ptr->CHASH_PARAM_ENTRY_NEXT);
  83. }
  84. entry.ptr->CHASH_PARAM_ENTRY_NEXT = o->buckets[index];
  85. o->buckets[index] = entry.link;
  86. o->numEntries++;
  87. if (out_existing) {
  88. *out_existing = entry;
  89. }
  90. return 1;
  91. }
  92. static void CHash_InsertMulti (CHash *o, CHashArg arg, CHashRef entry)
  93. {
  94. CHashKey key = CHASH_PARAM_GETKEY(arg, entry);
  95. size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
  96. CHashRef prev = CHashNullRef();
  97. CHashRef cur = CHash_Deref(arg, o->buckets[index]);
  98. while (cur.link != CHashNullLink()) {
  99. if (CHASH_PARAM_KEYSEQUAL(arg, CHASH_PARAM_GETKEY(arg, cur), key)) {
  100. break;
  101. }
  102. prev = cur;
  103. cur = CHash_Deref(arg, cur.ptr->CHASH_PARAM_ENTRY_NEXT);
  104. }
  105. if (cur.link == CHashNullLink() || prev.link == CHashNullLink()) {
  106. entry.ptr->CHASH_PARAM_ENTRY_NEXT = o->buckets[index];
  107. o->buckets[index] = entry.link;
  108. } else {
  109. entry.ptr->CHASH_PARAM_ENTRY_NEXT = cur.link;
  110. prev.ptr->CHASH_PARAM_ENTRY_NEXT = entry.link;
  111. }
  112. o->numEntries++;
  113. }
  114. static void CHash_Remove (CHash *o, CHashArg arg, CHashRef entry)
  115. {
  116. CHashKey key = CHASH_PARAM_GETKEY(arg, entry);
  117. size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
  118. CHashRef prev = CHashNullRef();
  119. CHashRef cur = CHash_Deref(arg, o->buckets[index]);
  120. while (cur.link != entry.link) {
  121. prev = cur;
  122. cur = CHash_Deref(arg, cur.ptr->CHASH_PARAM_ENTRY_NEXT);
  123. ASSERT(cur.link != CHashNullLink());
  124. }
  125. if (prev.link == CHashNullLink()) {
  126. o->buckets[index] = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
  127. } else {
  128. prev.ptr->CHASH_PARAM_ENTRY_NEXT = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
  129. }
  130. o->numEntries--;
  131. }
  132. static CHashRef CHash_Lookup (const CHash *o, CHashArg arg, CHashKey key)
  133. {
  134. size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
  135. CHashRef e = CHash_Deref(arg, o->buckets[index]);
  136. while (e.link != CHashNullLink()) {
  137. if (CHASH_PARAM_KEYSEQUAL(arg, CHASH_PARAM_GETKEY(arg, e), key)) {
  138. return e;
  139. }
  140. e = CHash_Deref(arg, e.ptr->CHASH_PARAM_ENTRY_NEXT);
  141. }
  142. return CHashNullRef();
  143. }
  144. static CHashRef CHash_GetFirst (const CHash *o, CHashArg arg)
  145. {
  146. size_t i = 0;
  147. while (i < o->numBuckets && o->buckets[i] == CHashNullLink()) {
  148. i++;
  149. }
  150. if (i == o->numBuckets) {
  151. return CHashNullRef();
  152. }
  153. return CHash_Deref(arg, o->buckets[i]);
  154. }
  155. static CHashRef CHash_GetNext (const CHash *o, CHashArg arg, CHashRef entry)
  156. {
  157. CHashLink next = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
  158. if (next != CHashNullLink()) {
  159. return CHash_Deref(arg, next);
  160. }
  161. CHashKey key = CHASH_PARAM_GETKEY(arg, entry);
  162. size_t i = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
  163. i++;
  164. while (i < o->numBuckets && o->buckets[i] == CHashNullLink()) {
  165. i++;
  166. }
  167. if (i == o->numBuckets) {
  168. return CHashNullRef();
  169. }
  170. return CHash_Deref(arg, o->buckets[i]);
  171. }
  172. static CHashRef CHash_GetNextEqual (const CHash *o, CHashArg arg, CHashRef entry)
  173. {
  174. CHashLink next = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
  175. if (next == CHashNullLink()) {
  176. return CHashNullRef();
  177. }
  178. CHashRef next_ref = CHash_Deref(arg, next);
  179. if (!CHASH_PARAM_KEYSEQUAL(arg, CHASH_PARAM_GETKEY(arg, next_ref), CHASH_PARAM_GETKEY(arg, entry))) {
  180. return CHashNullRef();
  181. }
  182. return next_ref;
  183. }
  184. static size_t CHash_NumEntries (const CHash *o)
  185. {
  186. return o->numEntries;
  187. }
  188. static int CHash_IsEmpty (const CHash *o)
  189. {
  190. return o->numEntries == 0;
  191. }
  192. #include "CHash_footer.h"