| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /**
- * @file CHash_impl.h
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the author nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include "CHash_header.h"
- static CHashLink CHashNullLink ()
- {
- return CHASH_PARAM_NULL;
- }
- static CHashRef CHashNullRef ()
- {
- CHashRef entry = {NULL, CHASH_PARAM_NULL};
- return entry;
- }
- static int CHash_Init (CHash *o, size_t numBuckets)
- {
- if (numBuckets == 0) {
- numBuckets = 1;
- }
-
- o->numBuckets = numBuckets;
- o->numEntries = 0;
-
- o->buckets = (CHashLink *)BAllocArray(o->numBuckets, sizeof(o->buckets[0]));
- if (!o->buckets) {
- return 0;
- }
-
- for (size_t i = 0; i < o->numBuckets; i++) {
- o->buckets[i] = CHashNullLink();
- }
-
- return 1;
- }
- static void CHash_Free (CHash *o)
- {
- BFree(o->buckets);
- }
- static CHashRef CHash_Deref (CHashArg arg, CHashLink link)
- {
- if (link == CHashNullLink()) {
- return CHashNullRef();
- }
-
- CHashRef entry;
- entry.ptr = CHASH_PARAM_DEREF(arg, link);
- entry.link = link;
-
- ASSERT(entry.ptr)
-
- return entry;
- }
- static int CHash_Insert (CHash *o, CHashArg arg, CHashRef entry, CHashRef *out_existing)
- {
- CHashKey key = entry.ptr->CHASH_PARAM_ENTRY_KEY;
- size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
-
- CHashRef e = CHash_Deref(arg, o->buckets[index]);
- while (e.link != CHashNullLink()) {
- if (CHASH_PARAM_KEYSEQUAL(arg, key, e.ptr->CHASH_PARAM_ENTRY_KEY)) {
- if (out_existing) {
- *out_existing = e;
- }
- return 0;
- }
- e = CHash_Deref(arg, e.ptr->CHASH_PARAM_ENTRY_NEXT);
- }
-
- entry.ptr->CHASH_PARAM_ENTRY_NEXT = o->buckets[index];
- o->buckets[index] = entry.link;
-
- o->numEntries++;
-
- if (out_existing) {
- *out_existing = entry;
- }
- return 1;
- }
- static void CHash_InsertMulti (CHash *o, CHashArg arg, CHashRef entry)
- {
- CHashKey key = entry.ptr->CHASH_PARAM_ENTRY_KEY;
- size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
-
- CHashRef prev = CHashNullRef();
- CHashRef cur = CHash_Deref(arg, o->buckets[index]);
- while (cur.link != CHashNullLink()) {
- if (CHASH_PARAM_KEYSEQUAL(arg, cur.ptr->CHASH_PARAM_ENTRY_KEY, key)) {
- break;
- }
- prev = cur;
- cur = CHash_Deref(arg, cur.ptr->CHASH_PARAM_ENTRY_NEXT);
- }
-
- if (cur.link == CHashNullLink() || prev.link == CHashNullLink()) {
- entry.ptr->CHASH_PARAM_ENTRY_NEXT = o->buckets[index];
- o->buckets[index] = entry.link;
- } else {
- entry.ptr->CHASH_PARAM_ENTRY_NEXT = cur.link;
- prev.ptr->CHASH_PARAM_ENTRY_NEXT = entry.link;
- }
-
- o->numEntries++;
- }
- static void CHash_Remove (CHash *o, CHashArg arg, CHashRef entry)
- {
- CHashKey key = entry.ptr->CHASH_PARAM_ENTRY_KEY;
- size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
-
- CHashRef prev = CHashNullRef();
- CHashRef cur = CHash_Deref(arg, o->buckets[index]);
- while (cur.link != entry.link) {
- prev = cur;
- cur = CHash_Deref(arg, cur.ptr->CHASH_PARAM_ENTRY_NEXT);
- ASSERT(cur.link != CHashNullLink());
- }
-
- if (prev.link == CHashNullLink()) {
- o->buckets[index] = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
- } else {
- prev.ptr->CHASH_PARAM_ENTRY_NEXT = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
- }
-
- o->numEntries--;
- }
- static CHashRef CHash_Lookup (const CHash *o, CHashArg arg, CHashKey key)
- {
- size_t index = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
-
- CHashRef e = CHash_Deref(arg, o->buckets[index]);
- while (e.link != CHashNullLink()) {
- if (CHASH_PARAM_KEYSEQUAL(arg, e.ptr->CHASH_PARAM_ENTRY_KEY, key)) {
- return e;
- }
- e = CHash_Deref(arg, e.ptr->CHASH_PARAM_ENTRY_NEXT);
- }
-
- return CHashNullRef();
- }
- static CHashRef CHash_GetFirst (const CHash *o, CHashArg arg)
- {
- size_t i = 0;
- while (i < o->numBuckets && o->buckets[i] == CHashNullLink()) {
- i++;
- }
-
- if (i == o->numBuckets) {
- return CHashNullRef();
- }
-
- return CHash_Deref(arg, o->buckets[i]);
- }
- static CHashRef CHash_GetNext (const CHash *o, CHashArg arg, CHashRef entry)
- {
- CHashLink next = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
- if (next != CHashNullLink()) {
- return CHash_Deref(arg, next);
- }
-
- CHashKey key = entry.ptr->CHASH_PARAM_ENTRY_KEY;
- size_t i = CHASH_PARAM_HASHFUN(arg, key) % o->numBuckets;
- i++;
-
- while (i < o->numBuckets && o->buckets[i] == CHashNullLink()) {
- i++;
- }
-
- if (i == o->numBuckets) {
- return CHashNullRef();
- }
-
- return CHash_Deref(arg, o->buckets[i]);
- }
- static CHashRef CHash_GetNextEqual (const CHash *o, CHashArg arg, CHashRef entry)
- {
- CHashLink next = entry.ptr->CHASH_PARAM_ENTRY_NEXT;
-
- if (next == CHashNullLink()) {
- return CHashNullRef();
- }
-
- CHashRef next_ref = CHash_Deref(arg, next);
- if (!CHASH_PARAM_KEYSEQUAL(arg, next_ref.ptr->CHASH_PARAM_ENTRY_KEY, entry.ptr->CHASH_PARAM_ENTRY_KEY)) {
- return CHashNullRef();
- }
-
- return next_ref;
- }
- static size_t CHash_NumEntries (const CHash *o)
- {
- return o->numEntries;
- }
- static int CHash_IsEmpty (const CHash *o)
- {
- return o->numEntries == 0;
- }
- #include "CHash_footer.h"
|