2008-06-30 21:54:22 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include "llt.h"
|
|
|
|
#include "flisp.h"
|
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
#define BOUNDED_COMPARE_BOUND 2048
|
|
|
|
#define BOUNDED_HASH_BOUND 4096
|
|
|
|
|
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
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
static value_t eq_class(htable_t *table, value_t key)
|
2008-06-30 21:54:22 -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);
|
|
|
|
}
|
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
static void eq_union(htable_t *table, value_t a, value_t b,
|
2008-06-30 21:54:22 -04:00
|
|
|
value_t c, value_t cb)
|
|
|
|
{
|
|
|
|
value_t ca = (c==NIL ? a : c);
|
|
|
|
if (cb != NIL)
|
|
|
|
ptrhash_put(table, (void*)cb, (void*)ca);
|
|
|
|
ptrhash_put(table, (void*)a, (void*)ca);
|
|
|
|
ptrhash_put(table, (void*)b, (void*)ca);
|
|
|
|
}
|
|
|
|
|
2009-01-02 18:00:21 -05:00
|
|
|
// a is a fixnum, b is a cprim
|
|
|
|
static value_t compare_num_cprim(value_t a, value_t b, int eq)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
2009-01-02 18:00:21 -05:00
|
|
|
cprim_t *bcp = (cprim_t*)ptr(b);
|
|
|
|
numerictype_t bt = cp_numtype(bcp);
|
|
|
|
fixnum_t ia = numval(a);
|
|
|
|
void *bptr = cp_data(bcp);
|
|
|
|
if (cmp_eq(&ia, T_FIXNUM, bptr, bt))
|
|
|
|
return fixnum(0);
|
|
|
|
if (eq) return fixnum(1);
|
|
|
|
if (cmp_lt(&ia, T_FIXNUM, bptr, bt))
|
2008-08-30 18:18:20 -04:00
|
|
|
return fixnum(-1);
|
|
|
|
return fixnum(1);
|
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);
|
2008-11-23 02:12:37 -05:00
|
|
|
static value_t cyc_compare(value_t a, value_t b, htable_t *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;
|
2008-11-05 23:04:04 -05: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++) {
|
2008-11-05 23:04:04 -05:00
|
|
|
value_t d = bounded_compare(vector_elt(a,i), vector_elt(b,i),
|
|
|
|
bound-1, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (d==NIL || numval(d)!=0) return d;
|
|
|
|
}
|
|
|
|
if (la < lb) return fixnum(-1);
|
|
|
|
if (la > lb) return fixnum(1);
|
|
|
|
return fixnum(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// strange comparisons are resolved arbitrarily but consistently.
|
2009-01-02 18:00:21 -05:00
|
|
|
// ordering: number < cprim < builtin < cvalue < vector < 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;
|
|
|
|
|
|
|
|
compare_top:
|
|
|
|
if (a == b) return fixnum(0);
|
|
|
|
if (bound <= 0)
|
|
|
|
return NIL;
|
2008-08-04 21:43:12 -04:00
|
|
|
int taga = tag(a);
|
|
|
|
int tagb = cmptag(b);
|
|
|
|
switch (taga) {
|
|
|
|
case TAG_NUM :
|
|
|
|
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)) {
|
|
|
|
return compare_num_cprim(a, b, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
return fixnum(-1);
|
|
|
|
case TAG_SYM:
|
2008-11-05 23:04:04 -05:00
|
|
|
if (eq) return fixnum(1);
|
2008-08-04 21:43:12 -04:00
|
|
|
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:
|
|
|
|
if (iscprim(b)) {
|
|
|
|
cprim_t *acp=(cprim_t*)ptr(a), *bcp=(cprim_t*)ptr(b);
|
|
|
|
numerictype_t at=cp_numtype(acp), bt=cp_numtype(bcp);
|
|
|
|
void *aptr=cp_data(acp), *bptr=cp_data(bcp);
|
|
|
|
if (cmp_eq(aptr, at, bptr, bt))
|
|
|
|
return fixnum(0);
|
|
|
|
if (eq) return fixnum(1);
|
|
|
|
if (cmp_lt(aptr, at, bptr, bt))
|
|
|
|
return fixnum(-1);
|
|
|
|
return fixnum(1);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
else if (isfixnum(b)) {
|
2009-01-02 18:00:21 -05:00
|
|
|
return fixnum(-numval(compare_num_cprim(b, a, eq)));
|
2008-08-04 21:43:12 -04:00
|
|
|
}
|
|
|
|
break;
|
2009-01-02 18:00:21 -05:00
|
|
|
case TAG_CVALUE:
|
|
|
|
if (iscvalue(b))
|
|
|
|
return cvalue_compare(a, b);
|
|
|
|
break;
|
2008-08-04 21:43:12 -04:00
|
|
|
case TAG_BUILTIN:
|
|
|
|
if (tagb == TAG_BUILTIN) {
|
|
|
|
return (uintval(a) < uintval(b)) ? fixnum(-1) : fixnum(1);
|
|
|
|
}
|
|
|
|
break;
|
2008-06-30 21:54:22 -04:00
|
|
|
case TAG_CONS:
|
2008-08-04 21:43:12 -04:00
|
|
|
if (tagb < TAG_CONS) return fixnum(1);
|
2008-11-05 23:04:04 -05:00
|
|
|
d = bounded_compare(car_(a), car_(b), bound-1, eq);
|
2008-08-30 18:18:20 -04:00
|
|
|
if (d==NIL || numval(d) != 0) return d;
|
2008-06-30 21:54:22 -04:00
|
|
|
a = cdr_(a); b = cdr_(b);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
static value_t cyc_vector_compare(value_t a, value_t b, htable_t *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
|
2008-11-05 23:04:04 -05: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++) {
|
|
|
|
xa = vector_elt(a,i);
|
|
|
|
xb = vector_elt(b,i);
|
|
|
|
if (leafp(xa) || leafp(xb)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
d = bounded_compare(xa, xb, 1, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (numval(d)!=0) return d;
|
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
else if (cmptag(xa) < cmptag(xb)) {
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(-1);
|
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
else if (cmptag(xa) > cmptag(xb)) {
|
2008-06-30 21:54:22 -04:00
|
|
|
return fixnum(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ca = eq_class(table, a);
|
|
|
|
cb = eq_class(table, b);
|
|
|
|
if (ca!=NIL && ca==cb)
|
|
|
|
return fixnum(0);
|
|
|
|
|
|
|
|
eq_union(table, a, b, ca, cb);
|
|
|
|
|
|
|
|
for (i = 0; i < m; i++) {
|
|
|
|
xa = vector_elt(a,i);
|
|
|
|
xb = vector_elt(b,i);
|
|
|
|
if (!leafp(xa) && !leafp(xb)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
d = cyc_compare(xa, xb, table, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (numval(d)!=0)
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (la < lb) return fixnum(-1);
|
|
|
|
if (la > lb) return fixnum(1);
|
|
|
|
return fixnum(0);
|
|
|
|
}
|
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
static value_t cyc_compare(value_t a, value_t b, htable_t *table, int eq)
|
2008-06-30 21:54:22 -04:00
|
|
|
{
|
|
|
|
if (a==b)
|
|
|
|
return fixnum(0);
|
|
|
|
if (iscons(a)) {
|
|
|
|
if (iscons(b)) {
|
|
|
|
value_t aa = car_(a); value_t da = cdr_(a);
|
|
|
|
value_t ab = car_(b); value_t db = cdr_(b);
|
2008-08-04 21:43:12 -04:00
|
|
|
int tagaa = cmptag(aa); int tagda = cmptag(da);
|
|
|
|
int tagab = cmptag(ab); int tagdb = cmptag(db);
|
2008-06-30 21:54:22 -04:00
|
|
|
value_t d, ca, cb;
|
|
|
|
if (leafp(aa) || leafp(ab)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
d = bounded_compare(aa, ab, 1, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (numval(d)!=0) return d;
|
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
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);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (numval(d)!=0) return d;
|
|
|
|
}
|
2008-08-04 21:43:12 -04:00
|
|
|
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);
|
|
|
|
if (ca!=NIL && ca==cb)
|
|
|
|
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);
|
2008-06-30 21:54:22 -04:00
|
|
|
if (numval(d)!=0) return d;
|
2008-11-05 23:04:04 -05:00
|
|
|
return cyc_compare(da, db, table, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return fixnum(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isvector(a) && isvector(b)) {
|
2008-11-05 23:04:04 -05:00
|
|
|
return cyc_vector_compare(a, b, table, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
2008-11-05 23:04:04 -05:00
|
|
|
return bounded_compare(a, b, 1, eq);
|
2008-06-30 21:54:22 -04:00
|
|
|
}
|
|
|
|
|
2008-11-23 02:12:37 -05:00
|
|
|
static htable_t equal_eq_hashtable;
|
2008-08-30 18:18:20 -04:00
|
|
|
void comparehash_init()
|
|
|
|
{
|
2008-11-23 02:12:37 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-11-05 23:04:04 -05:00
|
|
|
value_t compare(value_t a, value_t b)
|
|
|
|
{
|
|
|
|
return compare_(a, b, 0);
|
|
|
|
}
|
|
|
|
|
2008-08-30 18:18:20 -04:00
|
|
|
value_t equal(value_t a, value_t b)
|
|
|
|
{
|
2008-09-06 18:19:51 -04:00
|
|
|
if (eq_comparable(a, b))
|
|
|
|
return (a == b) ? T : NIL;
|
2008-11-05 23:04:04 -05:00
|
|
|
return (numval(compare_(a,b,1))==0 ? T : NIL);
|
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
|
|
|
|
#define MIX(a, b) int64to32hash(((int64_t)(a))<<32 | ((int64_t)(b)))
|
|
|
|
#define doublehash(a) int64to32hash(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static uptrint_t bounded_hash(value_t a, int bound)
|
|
|
|
{
|
|
|
|
double d;
|
|
|
|
numerictype_t nt;
|
|
|
|
size_t i, len;
|
|
|
|
cvalue_t *cv;
|
2009-01-02 18:00:21 -05:00
|
|
|
cprim_t *cp;
|
2008-11-23 02:12:37 -05:00
|
|
|
void *data;
|
|
|
|
if (bound <= 0) return 0;
|
|
|
|
uptrint_t h = 0;
|
|
|
|
int bb, tg = tag(a);
|
|
|
|
switch(tg) {
|
|
|
|
case TAG_NUM :
|
|
|
|
case TAG_NUM1:
|
|
|
|
d = numval(a);
|
|
|
|
return doublehash(*(int64_t*)&d);
|
|
|
|
case TAG_BUILTIN:
|
|
|
|
return inthash(a);
|
|
|
|
case TAG_SYM:
|
|
|
|
return ((symbol_t*)ptr(a))->hash;
|
2009-01-02 18:00:21 -05:00
|
|
|
case TAG_CPRIM:
|
|
|
|
cp = (cprim_t*)ptr(a);
|
|
|
|
data = cp_data(cp);
|
|
|
|
nt = cp_numtype(cp);
|
|
|
|
d = conv_to_double(data, nt);
|
|
|
|
if (d==0) d = 0.0; // normalize -0
|
|
|
|
return doublehash(*(int64_t*)&d);
|
2008-11-23 02:12:37 -05:00
|
|
|
case TAG_CVALUE:
|
|
|
|
cv = (cvalue_t*)ptr(a);
|
|
|
|
data = cv_data(cv);
|
2009-01-02 18:00:21 -05:00
|
|
|
return memhash(data, cv_len(cv));
|
2008-11-23 02:12:37 -05:00
|
|
|
case TAG_VECTOR:
|
|
|
|
len = vector_size(a);
|
|
|
|
for(i=0; i < len; i++) {
|
|
|
|
h = MIX(h, bounded_hash(vector_elt(a,i), bound-1));
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
case TAG_CONS:
|
|
|
|
bb = BOUNDED_HASH_BOUND;
|
|
|
|
do {
|
|
|
|
h = MIX(h, bounded_hash(car_(a), bound-1)+1);
|
|
|
|
bb--;
|
|
|
|
if (bb <= 0) return h;
|
|
|
|
a = cdr_(a);
|
|
|
|
} while (iscons(a));
|
|
|
|
return MIX(h, bounded_hash(a, bound-1)+1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-28 16:44:59 -05:00
|
|
|
int equal_lispvalue(value_t a, value_t b)
|
|
|
|
{
|
|
|
|
if (eq_comparable(a, b))
|
|
|
|
return (a==b);
|
|
|
|
return (numval(compare_(a,b,1))==0);
|
|
|
|
}
|
|
|
|
|
|
|
|
uptrint_t hash_lispvalue(value_t a)
|
2008-11-23 02:12:37 -05:00
|
|
|
{
|
|
|
|
return bounded_hash(a, BOUNDED_HASH_BOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
value_t fl_hash(value_t *args, u_int32_t nargs)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|