2017-08-19 12:38:17 -04:00
|
|
|
#define BOUNDED_COMPARE_BOUND 128
|
2019-08-09 07:02:02 -04:00
|
|
|
#define BOUNDED_HASH_BOUND 16384
|
2008-11-23 02:12:37 -05:00
|
|
|
|
2008-08-04 21:43:12 -04:00
|
|
|
// comparable tag
|
|
|
|
#define cmptag(v) (isfixnum(v) ? TAG_NUM : tag(v))
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2019-08-09 12:26:09 -04:00
|
|
|
static value_t eq_class(struct htable *table, value_t key)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t c = (value_t)ptrhash_get(table, (void *)key);
|
2008-11-23 02:12:37 -05:00
|
|
|
if (c == (value_t)HT_NOTFOUND)
|
2008-06-30 21:54:22 -04:00
|
|
|
return NIL;
|
|
|
|
if (c == key)
|
|
|
|
return c;
|
|
|
|
return eq_class(table, c);
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:09 -04:00
|
|
|
static void eq_union(struct htable *table, value_t a, value_t b, value_t c,
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t cb)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t ca = (c == NIL ? a : c);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (cb != NIL)
|
2019-08-09 07:02:02 -04:00
|
|
|
ptrhash_put(table, (void *)cb, (void *)ca);
|
|
|
|
ptrhash_put(table, (void *)a, (void *)ca);
|
|
|
|
ptrhash_put(table, (void *)b, (void *)ca);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2008-11-05 23:04:04 -05:00
|
|
|
static value_t bounded_compare(value_t a, value_t b, int bound, int eq);
|
2019-08-09 12:26:09 -04:00
|
|
|
static value_t cyc_compare(value_t a, value_t b, struct htable *table,
|
|
|
|
int eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
|
2008-11-05 23:04:04 -05:00
|
|
|
static value_t bounded_vector_compare(value_t a, value_t b, int bound, int eq)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
size_t la = vector_size(a);
|
|
|
|
size_t lb = vector_size(b);
|
|
|
|
size_t m, i;
|
2019-08-09 07:02:02 -04:00
|
|
|
if (eq && (la != lb))
|
|
|
|
return fixnum(1);
|
2008-06-30 21:54:22 -04:00
|
|
|
m = la < lb ? la : lb;
|
|
|
|
for (i = 0; i < m; i++) {
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t d =
|
|
|
|
bounded_compare(vector_elt(a, i), vector_elt(b, i), bound - 1, eq);
|
|
|
|
if (d == NIL || numval(d) != 0)
|
|
|
|
return d;
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
if (la < lb)
|
|
|
|
return fixnum(-1);
|
|
|
|
if (la > lb)
|
|
|
|
return fixnum(1);
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// strange comparisons are resolved arbitrarily but consistently.
|
2009-07-10 00:17:39 -04:00
|
|
|
// ordering: number < cprim < function < vector < cvalue < symbol < cons
|
2008-11-05 23:04:04 -05:00
|
|
|
static value_t bounded_compare(value_t a, value_t b, int bound, int eq)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
value_t d;
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
compare_top:
|
|
|
|
if (a == b)
|
|
|
|
return fixnum(0);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (bound <= 0)
|
|
|
|
return NIL;
|
2008-08-04 21:43:12 -04:00
|
|
|
int taga = tag(a);
|
|
|
|
int tagb = cmptag(b);
|
2009-04-15 23:05:38 -04:00
|
|
|
int c;
|
2008-08-04 21:43:12 -04:00
|
|
|
switch (taga) {
|
2019-08-09 07:02:02 -04:00
|
|
|
case TAG_NUM:
|
2008-08-04 21:43:12 -04:00
|
|
|
case TAG_NUM1:
|
2008-06-30 21:54:22 -04:00
|
|
|
if (isfixnum(b)) {
|
|
|
|
return (numval(a) < numval(b)) ? fixnum(-1) : fixnum(1);
|
|
|
|
}
|
2009-01-02 18:00:21 -05:00
|
|
|
if (iscprim(b)) {
|
2019-08-09 12:25:43 -04:00
|
|
|
if (cp_class((struct cprim *)ptr(b)) == wchartype)
|
2009-11-18 12:38:16 -05:00
|
|
|
return fixnum(1);
|
2009-04-15 23:05:38 -04:00
|
|
|
return fixnum(numeric_compare(a, b, eq, 1, NULL));
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
return fixnum(-1);
|
|
|
|
case TAG_SYM:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (eq)
|
|
|
|
return fixnum(1);
|
|
|
|
if (tagb < TAG_SYM)
|
|
|
|
return fixnum(1);
|
|
|
|
if (tagb > TAG_SYM)
|
|
|
|
return fixnum(-1);
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(strcmp(symbol_name(a), symbol_name(b)));
|
2008-08-04 21:43:12 -04:00
|
|
|
case TAG_VECTOR:
|
|
|
|
if (isvector(b))
|
2008-11-05 23:04:04 -05:00
|
|
|
return bounded_vector_compare(a, b, bound, eq);
|
2008-08-04 21:43:12 -04:00
|
|
|
break;
|
2009-01-02 18:00:21 -05:00
|
|
|
case TAG_CPRIM:
|
2019-08-09 12:25:43 -04:00
|
|
|
if (cp_class((struct cprim *)ptr(a)) == wchartype) {
|
|
|
|
if (!iscprim(b) || cp_class((struct cprim *)ptr(b)) != wchartype)
|
2011-12-14 01:57:25 -05:00
|
|
|
return fixnum(-1);
|
2019-08-09 12:25:43 -04:00
|
|
|
} else if (iscprim(b) &&
|
|
|
|
cp_class((struct cprim *)ptr(b)) == wchartype) {
|
2011-12-14 01:57:25 -05:00
|
|
|
return fixnum(1);
|
|
|
|
}
|
2009-04-15 23:05:38 -04:00
|
|
|
c = numeric_compare(a, b, eq, 1, NULL);
|
|
|
|
if (c != 2)
|
|
|
|
return fixnum(c);
|
2008-08-04 21:43:12 -04:00
|
|
|
break;
|
2009-01-02 18:00:21 -05:00
|
|
|
case TAG_CVALUE:
|
2009-04-28 00:10:18 -04:00
|
|
|
if (iscvalue(b)) {
|
2019-08-09 12:36:20 -04:00
|
|
|
if (cv_isPOD((struct cvalue *)ptr(a)) &&
|
|
|
|
cv_isPOD((struct cvalue *)ptr(b)))
|
2009-04-28 00:10:18 -04:00
|
|
|
return cvalue_compare(a, b);
|
|
|
|
return fixnum(1);
|
|
|
|
}
|
2009-01-02 18:00:21 -05:00
|
|
|
break;
|
2009-04-28 00:10:18 -04:00
|
|
|
case TAG_FUNCTION:
|
|
|
|
if (tagb == TAG_FUNCTION) {
|
2009-07-16 21:30:26 -04:00
|
|
|
if (uintval(a) > N_BUILTINS && uintval(b) > N_BUILTINS) {
|
2019-08-09 12:23:30 -04:00
|
|
|
struct function *fa = (struct function *)ptr(a);
|
|
|
|
struct function *fb = (struct function *)ptr(b);
|
2019-08-09 07:02:02 -04:00
|
|
|
d = bounded_compare(fa->bcode, fb->bcode, bound - 1, eq);
|
|
|
|
if (d == NIL || numval(d) != 0)
|
|
|
|
return d;
|
|
|
|
d = bounded_compare(fa->vals, fb->vals, bound - 1, eq);
|
|
|
|
if (d == NIL || numval(d) != 0)
|
|
|
|
return d;
|
|
|
|
d = bounded_compare(fa->env, fb->env, bound - 1, eq);
|
|
|
|
if (d == NIL || numval(d) != 0)
|
|
|
|
return d;
|
2009-07-16 21:30:26 -04:00
|
|
|
return fixnum(0);
|
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
return (uintval(a) < uintval(b)) ? fixnum(-1) : fixnum(1);
|
|
|
|
}
|
|
|
|
break;
|
2008-06-30 21:54:22 -04:00
|
|
|
case TAG_CONS:
|
2019-08-09 07:02:02 -04:00
|
|
|
if (tagb < TAG_CONS)
|
|
|
|
return fixnum(1);
|
|
|
|
d = bounded_compare(car_(a), car_(b), bound - 1, eq);
|
|
|
|
if (d == NIL || numval(d) != 0)
|
|
|
|
return d;
|
|
|
|
a = cdr_(a);
|
|
|
|
b = cdr_(b);
|
2008-06-30 21:54:22 -04:00
|
|
|
bound--;
|
|
|
|
goto compare_top;
|
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
return (taga < tagb) ? fixnum(-1) : fixnum(1);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:09 -04:00
|
|
|
static value_t cyc_vector_compare(value_t a, value_t b, struct htable *table,
|
2008-11-05 23:04:04 -05:00
|
|
|
int eq)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
size_t la = vector_size(a);
|
|
|
|
size_t lb = vector_size(b);
|
|
|
|
size_t m, i;
|
|
|
|
value_t d, xa, xb, ca, cb;
|
|
|
|
|
|
|
|
// first try to prove them different with no recursion
|
2019-08-09 07:02:02 -04:00
|
|
|
if (eq && (la != lb))
|
|
|
|
return fixnum(1);
|
2008-06-30 21:54:22 -04:00
|
|
|
m = la < lb ? la : lb;
|
|
|
|
for (i = 0; i < m; i++) {
|
2019-08-09 07:02:02 -04:00
|
|
|
xa = vector_elt(a, i);
|
|
|
|
xb = vector_elt(b, i);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (leafp(xa) || leafp(xb)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
d = bounded_compare(xa, xb, 1, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (d != NIL && numval(d) != 0)
|
|
|
|
return d;
|
|
|
|
} else if (tag(xa) < tag(xb)) {
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(-1);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (tag(xa) > tag(xb)) {
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ca = eq_class(table, a);
|
|
|
|
cb = eq_class(table, b);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (ca != NIL && ca == cb)
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(0);
|
|
|
|
|
|
|
|
eq_union(table, a, b, ca, cb);
|
|
|
|
|
|
|
|
for (i = 0; i < m; i++) {
|
2019-08-09 07:02:02 -04:00
|
|
|
xa = vector_elt(a, i);
|
|
|
|
xb = vector_elt(b, i);
|
|
|
|
if (!leafp(xa) || tag(xa) == TAG_FUNCTION) {
|
2008-11-05 23:04:04 -05:00
|
|
|
d = cyc_compare(xa, xb, table, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (numval(d) != 0)
|
2008-06-30 21:54:22 -04:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
if (la < lb)
|
|
|
|
return fixnum(-1);
|
|
|
|
if (la > lb)
|
|
|
|
return fixnum(1);
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(0);
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:09 -04:00
|
|
|
static value_t cyc_compare(value_t a, value_t b, struct htable *table, int eq)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2009-07-16 21:30:26 -04:00
|
|
|
value_t d, ca, cb;
|
2019-08-09 07:02:02 -04:00
|
|
|
cyc_compare_top:
|
|
|
|
if (a == b)
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(0);
|
|
|
|
if (iscons(a)) {
|
|
|
|
if (iscons(b)) {
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t aa = car_(a);
|
|
|
|
value_t da = cdr_(a);
|
|
|
|
value_t ab = car_(b);
|
|
|
|
value_t db = cdr_(b);
|
|
|
|
int tagaa = tag(aa);
|
|
|
|
int tagda = tag(da);
|
|
|
|
int tagab = tag(ab);
|
|
|
|
int tagdb = tag(db);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (leafp(aa) || leafp(ab)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
d = bounded_compare(aa, ab, 1, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (d != NIL && numval(d) != 0)
|
|
|
|
return d;
|
|
|
|
} else if (tagaa < tagab)
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(-1);
|
2008-08-04 21:43:12 -04:00
|
|
|
else if (tagaa > tagab)
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(1);
|
|
|
|
if (leafp(da) || leafp(db)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
d = bounded_compare(da, db, 1, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (d != NIL && numval(d) != 0)
|
|
|
|
return d;
|
|
|
|
} else if (tagda < tagdb)
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(-1);
|
2008-08-04 21:43:12 -04:00
|
|
|
else if (tagda > tagdb)
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(1);
|
|
|
|
|
|
|
|
ca = eq_class(table, a);
|
|
|
|
cb = eq_class(table, b);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (ca != NIL && ca == cb)
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(0);
|
|
|
|
|
|
|
|
eq_union(table, a, b, ca, cb);
|
2008-11-05 23:04:04 -05:00
|
|
|
d = cyc_compare(aa, ab, table, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (numval(d) != 0)
|
|
|
|
return d;
|
2009-05-19 23:39:20 -04:00
|
|
|
a = da;
|
|
|
|
b = db;
|
|
|
|
goto cyc_compare_top;
|
2019-08-09 07:02:02 -04:00
|
|
|
} else {
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(1);
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (isvector(a) && isvector(b)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
return cyc_vector_compare(a, b, table, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
} else if (isclosure(a) && isclosure(b)) {
|
2019-08-09 12:23:30 -04:00
|
|
|
struct function *fa = (struct function *)ptr(a);
|
|
|
|
struct function *fb = (struct function *)ptr(b);
|
2009-07-16 21:30:26 -04:00
|
|
|
d = bounded_compare(fa->bcode, fb->bcode, 1, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (numval(d) != 0)
|
|
|
|
return d;
|
|
|
|
|
2009-07-16 21:30:26 -04:00
|
|
|
ca = eq_class(table, a);
|
|
|
|
cb = eq_class(table, b);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (ca != NIL && ca == cb)
|
2009-07-16 21:30:26 -04:00
|
|
|
return fixnum(0);
|
2019-08-09 07:02:02 -04:00
|
|
|
|
2009-07-16 21:30:26 -04:00
|
|
|
eq_union(table, a, b, ca, cb);
|
|
|
|
d = cyc_compare(fa->vals, fb->vals, table, eq);
|
2019-08-09 07:02:02 -04:00
|
|
|
if (numval(d) != 0)
|
|
|
|
return d;
|
2009-07-16 21:30:26 -04:00
|
|
|
a = fa->env;
|
|
|
|
b = fb->env;
|
|
|
|
goto cyc_compare_top;
|
|
|
|
}
|
2008-11-05 23:04:04 -05:00
|
|
|
return bounded_compare(a, b, 1, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2019-08-09 12:26:09 -04:00
|
|
|
static struct htable equal_eq_hashtable;
|
2019-08-09 07:02:02 -04:00
|
|
|
void comparehash_init(void) { htable_new(&equal_eq_hashtable, 512); }
|
2008-08-30 18:18:20 -04:00
|
|
|
|
2008-11-05 23:04:04 -05:00
|
|
|
// 'eq' means unordered comparison is sufficient
|
|
|
|
static value_t compare_(value_t a, value_t b, int eq)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2008-11-23 02:12:37 -05:00
|
|
|
value_t guess = bounded_compare(a, b, BOUNDED_COMPARE_BOUND, eq);
|
2008-08-30 18:18:20 -04:00
|
|
|
if (guess == NIL) {
|
2008-11-05 23:04:04 -05:00
|
|
|
guess = cyc_compare(a, b, &equal_eq_hashtable, eq);
|
2008-11-23 02:12:37 -05:00
|
|
|
htable_reset(&equal_eq_hashtable, 512);
|
2008-08-30 18:18:20 -04:00
|
|
|
}
|
2008-06-30 21:54:22 -04:00
|
|
|
return guess;
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:02:02 -04:00
|
|
|
value_t fl_compare(value_t a, value_t b) { return compare_(a, b, 0); }
|
2008-11-05 23:04:04 -05:00
|
|
|
|
2010-04-29 14:01:26 -04:00
|
|
|
value_t fl_equal(value_t a, value_t b)
|
2008-08-30 18:18:20 -04:00
|
|
|
{
|
2008-09-06 18:19:51 -04:00
|
|
|
if (eq_comparable(a, b))
|
switching to scheme #t, #f, and () values
porting code to sort out which NILs are false and which are
empty lists
switching to scheme-style special forms. however you feel about
scheme names vs. CL names, using both is silly.
mostly switching to scheme predicate names, with compatibility
aliases for now. adding set-constant! to make this efficient.
adding null?, eqv?, assq, assv, assoc, memq, memv, member
adding 2-argument form of if
allowing else as final cond condition
looking for init file in same directory as executable, so flisp
can be started from anywhere
renaming T to FL_T, since exporting a 1-character symbol is
not very nice
adding opaque type boilerplate example file
adding correctness checking for the pattern-lambda benchmark
bugfix in int2str
2009-01-28 20:04:23 -05:00
|
|
|
return (a == b) ? FL_T : FL_F;
|
2019-08-09 07:02:02 -04:00
|
|
|
return (numval(compare_(a, b, 1)) == 0 ? FL_T : FL_F);
|
2008-08-30 18:18:20 -04:00
|
|
|
}
|
|
|
|
|
2008-06-30 21:54:22 -04:00
|
|
|
/*
|
|
|
|
optimizations:
|
|
|
|
- use hash updates instead of calling lookup then insert. i.e. get the
|
|
|
|
bp once and use it twice.
|
2008-08-30 18:18:20 -04:00
|
|
|
* preallocate hash table and call reset() instead of new/free
|
2008-08-04 21:43:12 -04:00
|
|
|
* less redundant tag checking, 3-bit tags
|
2008-06-30 21:54:22 -04:00
|
|
|
*/
|
2008-11-23 02:12:37 -05:00
|
|
|
|
|
|
|
#ifdef BITS64
|
|
|
|
#define MIX(a, b) int64hash((int64_t)(a) ^ (int64_t)(b));
|
|
|
|
#define doublehash(a) int64hash(a)
|
|
|
|
#else
|
2019-08-09 07:02:02 -04:00
|
|
|
#define MIX(a, b) int64to32hash(((int64_t)(a)) << 32 | ((int64_t)(b)))
|
2008-11-23 02:12:37 -05:00
|
|
|
#define doublehash(a) int64to32hash(a)
|
|
|
|
#endif
|
|
|
|
|
2009-05-20 00:30:00 -04:00
|
|
|
// *oob: output argument, means we hit the limit specified by 'bound'
|
2019-08-09 14:07:16 -04:00
|
|
|
static uintptr_t bounded_hash(value_t a, int bound, int *oob)
|
2008-11-23 02:12:37 -05:00
|
|
|
{
|
2009-05-20 00:30:00 -04:00
|
|
|
*oob = 0;
|
2009-07-28 22:32:23 -04:00
|
|
|
union {
|
|
|
|
double d;
|
|
|
|
int64_t i64;
|
|
|
|
} u;
|
2008-11-23 02:12:37 -05:00
|
|
|
numerictype_t nt;
|
|
|
|
size_t i, len;
|
2019-08-09 12:36:20 -04:00
|
|
|
struct cvalue *cv;
|
2019-08-09 12:25:43 -04:00
|
|
|
struct cprim *cp;
|
2008-11-23 02:12:37 -05:00
|
|
|
void *data;
|
2019-08-09 14:07:16 -04:00
|
|
|
uintptr_t h = 0;
|
2009-05-20 00:30:00 -04:00
|
|
|
int oob2, tg = tag(a);
|
2019-08-09 07:02:02 -04:00
|
|
|
switch (tg) {
|
|
|
|
case TAG_NUM:
|
2008-11-23 02:12:37 -05:00
|
|
|
case TAG_NUM1:
|
2009-07-28 22:32:23 -04:00
|
|
|
u.d = (double)numval(a);
|
|
|
|
return doublehash(u.i64);
|
2009-04-28 00:10:18 -04:00
|
|
|
case TAG_FUNCTION:
|
|
|
|
if (uintval(a) > N_BUILTINS)
|
2019-08-09 12:23:30 -04:00
|
|
|
return bounded_hash(((struct function *)ptr(a))->bcode, bound,
|
|
|
|
oob);
|
2008-11-23 02:12:37 -05:00
|
|
|
return inthash(a);
|
|
|
|
case TAG_SYM:
|
2019-08-09 12:30:15 -04:00
|
|
|
return ((struct symbol *)ptr(a))->hash;
|
2009-01-02 18:00:21 -05:00
|
|
|
case TAG_CPRIM:
|
2019-08-09 12:25:43 -04:00
|
|
|
cp = (struct cprim *)ptr(a);
|
2009-01-02 18:00:21 -05:00
|
|
|
data = cp_data(cp);
|
2009-11-18 12:38:16 -05:00
|
|
|
if (cp_class(cp) == wchartype)
|
2019-08-09 07:02:02 -04:00
|
|
|
return inthash(*(int32_t *)data);
|
2009-01-02 18:00:21 -05:00
|
|
|
nt = cp_numtype(cp);
|
2009-07-28 22:32:23 -04:00
|
|
|
u.d = conv_to_double(data, nt);
|
|
|
|
return doublehash(u.i64);
|
2008-11-23 02:12:37 -05:00
|
|
|
case TAG_CVALUE:
|
2019-08-09 12:36:20 -04:00
|
|
|
cv = (struct cvalue *)ptr(a);
|
2008-11-23 02:12:37 -05:00
|
|
|
data = cv_data(cv);
|
2009-01-02 18:00:21 -05:00
|
|
|
return memhash(data, cv_len(cv));
|
2009-05-20 00:30:00 -04:00
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
case TAG_VECTOR:
|
2009-05-19 23:39:20 -04:00
|
|
|
if (bound <= 0) {
|
2009-05-20 00:30:00 -04:00
|
|
|
*oob = 1;
|
|
|
|
return 1;
|
2009-05-19 23:39:20 -04:00
|
|
|
}
|
2008-11-23 02:12:37 -05:00
|
|
|
len = vector_size(a);
|
2019-08-09 07:02:02 -04:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
h = MIX(h, bounded_hash(vector_elt(a, i), bound / 2, &oob2) ^ 1);
|
2009-05-20 00:30:00 -04:00
|
|
|
if (oob2)
|
2019-08-09 07:02:02 -04:00
|
|
|
bound /= 2;
|
2009-05-20 00:30:00 -04:00
|
|
|
*oob = *oob || oob2;
|
2008-11-23 02:12:37 -05:00
|
|
|
}
|
|
|
|
return h;
|
2009-05-20 00:30:00 -04:00
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
case TAG_CONS:
|
2009-05-20 14:52:09 -04:00
|
|
|
do {
|
|
|
|
if (bound <= 0) {
|
|
|
|
*oob = 1;
|
|
|
|
return h;
|
|
|
|
}
|
2019-08-09 07:02:02 -04:00
|
|
|
h = MIX(h, bounded_hash(car_(a), bound / 2, &oob2));
|
2009-05-20 14:52:09 -04:00
|
|
|
// bounds balancing: try to share the bounds efficiently
|
|
|
|
// so we can hash better when a list is cdr-deep (a common case)
|
|
|
|
if (oob2)
|
2019-08-09 07:02:02 -04:00
|
|
|
bound /= 2;
|
2009-05-20 14:52:09 -04:00
|
|
|
else
|
|
|
|
bound--;
|
|
|
|
// recursive OOB propagation. otherwise this case is slow:
|
|
|
|
// (hash '#2=((#0=(#1=(#1#) . #0#)) . #2#))
|
|
|
|
*oob = *oob || oob2;
|
|
|
|
a = cdr_(a);
|
|
|
|
} while (iscons(a));
|
2019-08-09 07:02:02 -04:00
|
|
|
h = MIX(h, bounded_hash(a, bound - 1, &oob2) ^ 2);
|
2009-05-20 00:30:00 -04:00
|
|
|
*oob = *oob || oob2;
|
|
|
|
return h;
|
2008-11-23 02:12:37 -05:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-28 16:44:59 -05:00
|
|
|
int equal_lispvalue(value_t a, value_t b)
|
|
|
|
{
|
|
|
|
if (eq_comparable(a, b))
|
2019-08-09 07:02:02 -04:00
|
|
|
return (a == b);
|
|
|
|
return (numval(compare_(a, b, 1)) == 0);
|
2008-11-28 16:44:59 -05:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:07:16 -04:00
|
|
|
uintptr_t hash_lispvalue(value_t a)
|
2008-11-23 02:12:37 -05:00
|
|
|
{
|
2019-08-09 07:02:02 -04:00
|
|
|
int oob = 0;
|
2019-08-09 14:07:16 -04:00
|
|
|
uintptr_t n = bounded_hash(a, BOUNDED_HASH_BOUND, &oob);
|
2009-05-13 00:03:13 -04:00
|
|
|
return n;
|
2008-11-23 02:12:37 -05:00
|
|
|
}
|
|
|
|
|
2019-08-09 14:00:03 -04:00
|
|
|
value_t fl_hash(value_t *args, uint32_t nargs)
|
2008-11-23 02:12:37 -05:00
|
|
|
{
|
|
|
|
argcount("hash", nargs, 1);
|
2008-11-28 16:44:59 -05:00
|
|
|
return fixnum(hash_lispvalue(args[0]));
|
2008-11-23 02:12:37 -05:00
|
|
|
}
|