/* hash.c -- type-general hashing */ /* Copyright (c) 2009-2011 Alex Shinn. All rights reserved. */ /* BSD-style license: http://synthcode.com/license.txt */ #include #define HASH_DEPTH 5 #define HASH_BOUND sexp_make_fixnum(SEXP_MAX_FIXNUM) #define FNV_PRIME 16777619 #define FNV_OFFSET_BASIS 2166136261uL #define sexp_hash_table_buckets(x) sexp_slot_ref(x, 0) #define sexp_hash_table_size(x) sexp_slot_ref(x, 1) #define sexp_hash_table_hash_fn(x) sexp_slot_ref(x, 2) #define sexp_hash_table_eq_fn(x) sexp_slot_ref(x, 3) #define sexp_hash_resize_check(n, len) (((n)*3) > ((len)>>2)) static sexp_uint_t string_hash (char *str, sexp_uint_t bound) { sexp_uint_t acc = FNV_OFFSET_BASIS; while (*str) {acc *= FNV_PRIME; acc ^= *str++;} return acc % bound; } sexp sexp_string_hash (sexp ctx, sexp self, sexp_sint_t n, sexp str, sexp bound) { if (! sexp_stringp(str)) return sexp_type_exception(ctx, self, SEXP_STRING, str); else if (! sexp_fixnump(bound)) return sexp_type_exception(ctx, self, SEXP_FIXNUM, bound); return sexp_make_fixnum(string_hash(sexp_string_data(str), sexp_unbox_fixnum(bound))); } static sexp_uint_t string_ci_hash (char *str, sexp_uint_t bound) { sexp_uint_t acc = FNV_OFFSET_BASIS; while (*str) {acc *= FNV_PRIME; acc ^= sexp_tolower(*str++);} return acc % bound; } sexp sexp_string_ci_hash (sexp ctx, sexp self, sexp_sint_t n, sexp str, sexp bound) { if (! sexp_stringp(str)) return sexp_type_exception(ctx, self, SEXP_STRING, str); else if (! sexp_fixnump(bound)) return sexp_type_exception(ctx, self, SEXP_FIXNUM, bound); return sexp_make_fixnum(string_ci_hash(sexp_string_data(str), sexp_unbox_fixnum(bound))); } static sexp_uint_t hash_one (sexp ctx, sexp obj, sexp_uint_t bound, sexp_sint_t depth) { sexp_uint_t acc = FNV_OFFSET_BASIS, size; sexp_sint_t i, len; sexp t, *p; char *p0; loop: if (obj) { #if SEXP_USE_FLONUMS if (sexp_flonump(obj)) acc ^= (sexp_sint_t) sexp_flonum_value(obj); else #endif if (sexp_pointerp(obj)) { if (depth > 0) { t = sexp_object_type(ctx, obj); p = (sexp*) (((char*)obj) + sexp_type_field_base(t)); p0 = ((char*)obj) + offsetof(struct sexp_struct, value); if ((sexp)p == obj) p=(sexp*)p0; /* hash trailing non-object data */ size = sexp_type_size_of_object(t, obj)-offsetof(struct sexp_struct, value); p0 = ((char*)p + sexp_type_num_slots_of_object(t,obj)*sizeof(sexp)); if (((char*)obj + size) > p0) for (i=0; i<(sexp_sint_t)size; i++) {acc *= FNV_PRIME; acc ^= p0[i];} /* hash eq-object slots */ len = sexp_type_num_eq_slots_of_object(t, obj); if (len > 0) { depth--; for (i=0; i= len) { res = SEXP_ZERO; } sexp_gc_release1(ctx); } return res; } static sexp sexp_scan_bucket (sexp ctx, sexp ls, sexp obj, sexp eq_fn) { sexp_gc_var1(res); sexp p; res = SEXP_FALSE; if ((eq_fn == SEXP_ONE) || ((eq_fn == SEXP_TWO) && (sexp_pointerp(obj) ? (sexp_pointer_tag(obj) == SEXP_SYMBOL) : ! sexp_fixnump(obj)))) { for (p=ls; sexp_pairp(p); p=sexp_cdr(p)) { if (sexp_caar(p) == obj) { res = p; break; } } } else if (eq_fn == SEXP_TWO) { for (p=ls; sexp_pairp(p); p=sexp_cdr(p)) { if (sexp_truep(sexp_equalp(ctx, sexp_caar(p), obj))) { res = p; break; } } } else { sexp_gc_preserve1(ctx, res); for (p=ls; sexp_pairp(p); p=sexp_cdr(p)) { res = sexp_list2(ctx, sexp_caar(p), obj); if (sexp_truep(sexp_apply(ctx, eq_fn, res))) { res = p; break; } else { res = SEXP_FALSE; } } sexp_gc_release1(ctx); } return res; } static void sexp_regrow_hash_table (sexp ctx, sexp ht, sexp oldbuckets, sexp hash_fn) { sexp ls, *oldvec, *newvec; int i, j, oldsize=sexp_vector_length(oldbuckets), newsize=oldsize*2; sexp_gc_var1(newbuckets); sexp_gc_preserve1(ctx, newbuckets); newbuckets = sexp_make_vector(ctx, sexp_make_fixnum(newsize), SEXP_NULL); if (newbuckets) { oldvec = sexp_vector_data(oldbuckets); newvec = sexp_vector_data(newbuckets); for (i=0; i