1995-08-09 08:06:35 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Objective Caml */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
1996-04-30 07:53:58 -07:00
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2000-11-13 06:51:01 -08:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2000-02-10 06:04:59 -08:00
|
|
|
#include "custom.h"
|
1995-05-04 03:15:53 -07:00
|
|
|
#include "fail.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
|
2000-11-13 06:51:01 -08:00
|
|
|
/* Structural comparison on trees. */
|
|
|
|
|
|
|
|
struct compare_item { value * v1, * v2; mlsize_t count; };
|
|
|
|
|
|
|
|
#define COMPARE_STACK_INIT_SIZE 256
|
2001-08-13 06:53:51 -07:00
|
|
|
#define COMPARE_STACK_MAX_SIZE (1024*1024)
|
2000-11-13 06:51:01 -08:00
|
|
|
|
|
|
|
static struct compare_item compare_stack_init[COMPARE_STACK_INIT_SIZE];
|
|
|
|
|
|
|
|
static struct compare_item * compare_stack = compare_stack_init;
|
|
|
|
static struct compare_item * compare_stack_limit = compare_stack_init
|
|
|
|
+ COMPARE_STACK_INIT_SIZE;
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLexport int caml_compare_unordered;
|
2003-11-21 07:55:47 -08:00
|
|
|
|
2000-11-13 06:51:01 -08:00
|
|
|
/* Free the compare stack if needed */
|
|
|
|
static void compare_free_stack(void)
|
|
|
|
{
|
|
|
|
if (compare_stack != compare_stack_init) {
|
2003-11-21 07:55:47 -08:00
|
|
|
free(compare_stack);
|
2000-11-13 06:51:01 -08:00
|
|
|
/* Reinitialize the globals for next time around */
|
|
|
|
compare_stack = compare_stack_init;
|
|
|
|
compare_stack_limit = compare_stack + COMPARE_STACK_INIT_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-13 01:29:08 -08:00
|
|
|
/* Same, then raise Out_of_memory */
|
2000-11-13 06:51:01 -08:00
|
|
|
static void compare_stack_overflow(void)
|
|
|
|
{
|
2004-07-13 05:19:15 -07:00
|
|
|
caml_gc_message (0x04, "Stack overflow in structural comparison\n", 0);
|
2000-11-13 06:51:01 -08:00
|
|
|
compare_free_stack();
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_raise_out_of_memory();
|
2000-11-13 06:51:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Grow the compare stack */
|
|
|
|
static struct compare_item * compare_resize_stack(struct compare_item * sp)
|
|
|
|
{
|
|
|
|
asize_t newsize = 2 * (compare_stack_limit - compare_stack);
|
|
|
|
asize_t sp_offset = sp - compare_stack;
|
|
|
|
struct compare_item * newstack;
|
|
|
|
|
|
|
|
if (newsize >= COMPARE_STACK_MAX_SIZE) compare_stack_overflow();
|
|
|
|
if (compare_stack == compare_stack_init) {
|
|
|
|
newstack = malloc(sizeof(struct compare_item) * newsize);
|
|
|
|
if (newstack == NULL) compare_stack_overflow();
|
|
|
|
memcpy(newstack, compare_stack_init,
|
|
|
|
sizeof(struct compare_item) * COMPARE_STACK_INIT_SIZE);
|
|
|
|
} else {
|
|
|
|
newstack =
|
|
|
|
realloc(compare_stack, sizeof(struct compare_item) * newsize);
|
|
|
|
if (newstack == NULL) compare_stack_overflow();
|
|
|
|
}
|
|
|
|
compare_stack = newstack;
|
|
|
|
compare_stack_limit = newstack + newsize;
|
|
|
|
return newstack + sp_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Structural comparison */
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-11-21 07:55:47 -08:00
|
|
|
#define LESS -1
|
|
|
|
#define EQUAL 0
|
|
|
|
#define GREATER 1
|
2006-05-04 05:41:26 -07:00
|
|
|
#define UNORDERED ((intnat)1 << (8 * sizeof(value) - 1))
|
2003-11-21 07:55:47 -08:00
|
|
|
|
|
|
|
/* The return value of compare_val is as follows:
|
|
|
|
> 0 v1 is greater than v2
|
|
|
|
0 v1 is equal to v2
|
|
|
|
< 0 and > UNORDERED v1 is less than v2
|
|
|
|
UNORDERED v1 and v2 cannot be compared */
|
|
|
|
|
2005-09-22 07:21:50 -07:00
|
|
|
static intnat compare_val(value v1, value v2, int total)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2000-11-13 06:51:01 -08:00
|
|
|
struct compare_item * sp;
|
1995-05-04 03:15:53 -07:00
|
|
|
tag_t t1, t2;
|
|
|
|
|
2000-11-13 06:51:01 -08:00
|
|
|
sp = compare_stack;
|
|
|
|
while (1) {
|
2003-11-21 07:55:47 -08:00
|
|
|
if (v1 == v2 && total) goto next_item;
|
2000-11-13 06:51:01 -08:00
|
|
|
if (Is_long(v1)) {
|
2003-11-21 07:55:47 -08:00
|
|
|
if (v1 == v2) goto next_item;
|
2000-11-13 06:51:01 -08:00
|
|
|
if (Is_long(v2))
|
|
|
|
return Long_val(v1) - Long_val(v2);
|
2003-11-21 07:55:47 -08:00
|
|
|
/* Subtraction above cannot overflow and cannot result in UNORDERED */
|
2007-11-06 07:16:56 -08:00
|
|
|
if ((Is_young(v2) || Is_in_heap(v2) || Is_atom(v2)) &&
|
2002-03-07 05:46:07 -08:00
|
|
|
Tag_val(v2) == Forward_tag) {
|
|
|
|
v2 = Forward_val(v2);
|
|
|
|
continue;
|
|
|
|
}
|
2003-11-21 07:55:47 -08:00
|
|
|
return LESS; /* v1 long < v2 block */
|
2002-03-07 05:46:07 -08:00
|
|
|
}
|
|
|
|
if (Is_long(v2)) {
|
2007-11-06 07:16:56 -08:00
|
|
|
if ((Is_young(v1) || Is_in_heap(v1) || Is_atom(v1)) &&
|
2002-03-07 05:46:07 -08:00
|
|
|
Tag_val(v1) == Forward_tag) {
|
|
|
|
v1 = Forward_val(v1);
|
|
|
|
continue;
|
|
|
|
}
|
2003-11-21 07:55:47 -08:00
|
|
|
return GREATER; /* v1 block > v2 long */
|
2000-11-13 06:51:01 -08:00
|
|
|
}
|
|
|
|
/* If one of the objects is outside the heap (but is not an atom),
|
|
|
|
use address comparison. Since both addresses are 2-aligned,
|
|
|
|
shift lsb off to avoid overflow in subtraction. */
|
2007-11-06 07:16:56 -08:00
|
|
|
if ((!Is_young(v1) && !Is_in_heap(v1) && !Is_atom(v1)) ||
|
|
|
|
(!Is_young(v2) && !Is_in_heap(v2) && !Is_atom(v2))) {
|
2003-11-21 07:55:47 -08:00
|
|
|
if (v1 == v2) goto next_item;
|
1996-10-09 04:14:11 -07:00
|
|
|
return (v1 >> 1) - (v2 >> 1);
|
2003-11-21 07:55:47 -08:00
|
|
|
/* Subtraction above cannot result in UNORDERED */
|
|
|
|
}
|
2000-11-13 06:51:01 -08:00
|
|
|
t1 = Tag_val(v1);
|
|
|
|
t2 = Tag_val(v2);
|
2002-03-07 05:46:07 -08:00
|
|
|
if (t1 == Forward_tag) { v1 = Forward_val (v1); continue; }
|
|
|
|
if (t2 == Forward_tag) { v2 = Forward_val (v2); continue; }
|
2005-09-22 07:21:50 -07:00
|
|
|
if (t1 != t2) return (intnat)t1 - (intnat)t2;
|
2000-11-13 06:51:01 -08:00
|
|
|
switch(t1) {
|
|
|
|
case String_tag: {
|
|
|
|
mlsize_t len1, len2, len;
|
|
|
|
unsigned char * p1, * p2;
|
2003-11-21 07:55:47 -08:00
|
|
|
if (v1 == v2) break;
|
2003-12-16 10:09:44 -08:00
|
|
|
len1 = caml_string_length(v1);
|
|
|
|
len2 = caml_string_length(v2);
|
2000-11-13 06:51:01 -08:00
|
|
|
for (len = (len1 <= len2 ? len1 : len2),
|
|
|
|
p1 = (unsigned char *) String_val(v1),
|
|
|
|
p2 = (unsigned char *) String_val(v2);
|
|
|
|
len > 0;
|
|
|
|
len--, p1++, p2++)
|
2005-09-22 07:21:50 -07:00
|
|
|
if (*p1 != *p2) return (intnat)*p1 - (intnat)*p2;
|
2000-11-13 06:51:01 -08:00
|
|
|
if (len1 != len2) return len1 - len2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Double_tag: {
|
|
|
|
double d1 = Double_val(v1);
|
|
|
|
double d2 = Double_val(v2);
|
2003-11-21 07:55:47 -08:00
|
|
|
if (d1 < d2) return LESS;
|
|
|
|
if (d1 > d2) return GREATER;
|
|
|
|
if (d1 != d2) {
|
|
|
|
if (! total) return UNORDERED;
|
|
|
|
/* One or both of d1 and d2 is NaN. Order according to the
|
|
|
|
convention NaN = NaN and NaN < f for all other floats f. */
|
|
|
|
if (d1 == d1) return GREATER; /* d1 is not NaN, d2 is NaN */
|
|
|
|
if (d2 == d2) return LESS; /* d2 is not NaN, d1 is NaN */
|
|
|
|
/* d1 and d2 are both NaN, thus equal: continue comparison */
|
|
|
|
}
|
2000-11-13 06:51:01 -08:00
|
|
|
break;
|
1995-07-27 10:41:09 -07:00
|
|
|
}
|
2000-11-13 06:51:01 -08:00
|
|
|
case Double_array_tag: {
|
|
|
|
mlsize_t sz1 = Wosize_val(v1) / Double_wosize;
|
|
|
|
mlsize_t sz2 = Wosize_val(v2) / Double_wosize;
|
|
|
|
mlsize_t i;
|
|
|
|
if (sz1 != sz2) return sz1 - sz2;
|
|
|
|
for (i = 0; i < sz1; i++) {
|
|
|
|
double d1 = Double_field(v1, i);
|
|
|
|
double d2 = Double_field(v2, i);
|
2003-11-21 07:55:47 -08:00
|
|
|
if (d1 < d2) return LESS;
|
|
|
|
if (d1 > d2) return GREATER;
|
|
|
|
if (d1 != d2) {
|
|
|
|
if (! total) return UNORDERED;
|
|
|
|
/* See comment for Double_tag case */
|
|
|
|
if (d1 == d1) return GREATER;
|
|
|
|
if (d2 == d2) return LESS;
|
|
|
|
}
|
2000-11-13 06:51:01 -08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Abstract_tag:
|
|
|
|
compare_free_stack();
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_invalid_argument("equal: abstract value");
|
2000-11-13 06:51:01 -08:00
|
|
|
case Closure_tag:
|
|
|
|
case Infix_tag:
|
|
|
|
compare_free_stack();
|
2004-01-01 08:42:43 -08:00
|
|
|
caml_invalid_argument("equal: functional value");
|
2000-11-13 06:51:01 -08:00
|
|
|
case Object_tag: {
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat oid1 = Oid_val(v1);
|
|
|
|
intnat oid2 = Oid_val(v2);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (oid1 != oid2) return oid1 - oid2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Custom_tag: {
|
2002-06-07 02:49:45 -07:00
|
|
|
int res;
|
|
|
|
int (*compare)(value v1, value v2) = Custom_ops_val(v1)->compare;
|
2007-02-09 05:31:15 -08:00
|
|
|
if (compare == NULL) {
|
|
|
|
compare_free_stack();
|
|
|
|
caml_invalid_argument("equal: abstract value");
|
|
|
|
}
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_compare_unordered = 0;
|
2002-06-07 02:49:45 -07:00
|
|
|
res = Custom_ops_val(v1)->compare(v1, v2);
|
2003-12-31 06:20:40 -08:00
|
|
|
if (caml_compare_unordered && !total) return UNORDERED;
|
1995-05-04 03:15:53 -07:00
|
|
|
if (res != 0) return res;
|
2000-11-13 06:51:01 -08:00
|
|
|
break;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
2000-11-13 06:51:01 -08:00
|
|
|
default: {
|
|
|
|
mlsize_t sz1 = Wosize_val(v1);
|
|
|
|
mlsize_t sz2 = Wosize_val(v2);
|
|
|
|
/* Compare sizes first for speed */
|
|
|
|
if (sz1 != sz2) return sz1 - sz2;
|
|
|
|
if (sz1 == 0) break;
|
|
|
|
/* Remember that we still have to compare fields 1 ... sz - 1 */
|
|
|
|
if (sz1 > 1) {
|
|
|
|
sp++;
|
|
|
|
if (sp >= compare_stack_limit) sp = compare_resize_stack(sp);
|
|
|
|
sp->v1 = &Field(v1, 1);
|
|
|
|
sp->v2 = &Field(v2, 1);
|
|
|
|
sp->count = sz1 - 1;
|
|
|
|
}
|
|
|
|
/* Continue comparison with first field */
|
|
|
|
v1 = Field(v1, 0);
|
|
|
|
v2 = Field(v2, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next_item:
|
|
|
|
/* Pop one more item to compare, if any */
|
2003-11-21 07:55:47 -08:00
|
|
|
if (sp == compare_stack) return EQUAL; /* we're done */
|
|
|
|
v1 = *((sp->v1)++);
|
|
|
|
v2 = *((sp->v2)++);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (--(sp->count) == 0) sp--;
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_compare(value v1, value v2)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res = compare_val(v1, v2, 1);
|
2000-11-13 06:51:01 -08:00
|
|
|
/* Free stack if needed */
|
|
|
|
if (compare_stack != compare_stack_init) compare_free_stack();
|
2003-11-21 07:55:47 -08:00
|
|
|
if (res < 0)
|
|
|
|
return Val_int(LESS);
|
1996-10-09 04:14:11 -07:00
|
|
|
else if (res > 0)
|
2003-11-21 07:55:47 -08:00
|
|
|
return Val_int(GREATER);
|
1996-10-09 04:14:11 -07:00
|
|
|
else
|
2003-11-21 07:55:47 -08:00
|
|
|
return Val_int(EQUAL);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_equal(value v1, value v2)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res = compare_val(v1, v2, 0);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (compare_stack != compare_stack_init) compare_free_stack();
|
|
|
|
return Val_int(res == 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_notequal(value v1, value v2)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res = compare_val(v1, v2, 0);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (compare_stack != compare_stack_init) compare_free_stack();
|
|
|
|
return Val_int(res != 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_lessthan(value v1, value v2)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res = compare_val(v1, v2, 0);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (compare_stack != compare_stack_init) compare_free_stack();
|
2003-11-21 07:55:47 -08:00
|
|
|
return Val_int(res - 1 < -1);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_lessequal(value v1, value v2)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res = compare_val(v1, v2, 0);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (compare_stack != compare_stack_init) compare_free_stack();
|
2003-11-21 07:55:47 -08:00
|
|
|
return Val_int(res - 1 <= -1);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_greaterthan(value v1, value v2)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res = compare_val(v1, v2, 0);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (compare_stack != compare_stack_init) compare_free_stack();
|
|
|
|
return Val_int(res > 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_greaterequal(value v1, value v2)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
2005-09-22 07:21:50 -07:00
|
|
|
intnat res = compare_val(v1, v2, 0);
|
2000-11-13 06:51:01 -08:00
|
|
|
if (compare_stack != compare_stack_init) compare_free_stack();
|
|
|
|
return Val_int(res >= 0);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|