Restandardised formatting.
This commit is contained in:
parent
93d4bd14a0
commit
b0a49fb71d
29 changed files with 1861 additions and 1604 deletions
169
src/ops/equal.c
169
src/ops/equal.c
|
|
@ -20,9 +20,8 @@
|
|||
* Shallow, and thus cheap, equality: true if these two objects are
|
||||
* the same object, else false.
|
||||
*/
|
||||
bool eq(struct cons_pointer a, struct cons_pointer b)
|
||||
{
|
||||
return ((a.page == b.page) && (a.offset == b.offset));
|
||||
bool eq( struct cons_pointer a, struct cons_pointer b ) {
|
||||
return ( ( a.page == b.page ) && ( a.offset == b.offset ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -32,10 +31,9 @@ bool eq(struct cons_pointer a, struct cons_pointer b)
|
|||
* @return true if the objects at these two cons pointers have the same tag,
|
||||
* else false.
|
||||
*/
|
||||
bool same_type(struct cons_pointer a, struct cons_pointer b)
|
||||
{
|
||||
struct cons_space_object *cell_a = &pointer2cell(a);
|
||||
struct cons_space_object *cell_b = &pointer2cell(b);
|
||||
bool same_type( struct cons_pointer a, struct cons_pointer b ) {
|
||||
struct cons_space_object *cell_a = &pointer2cell( a );
|
||||
struct cons_space_object *cell_b = &pointer2cell( b );
|
||||
|
||||
return cell_a->tag.value == cell_b->tag.value;
|
||||
}
|
||||
|
|
@ -45,104 +43,95 @@ bool same_type(struct cons_pointer a, struct cons_pointer b)
|
|||
* @param string the string to test
|
||||
* @return true if it's the end of a string.
|
||||
*/
|
||||
bool end_of_string(struct cons_pointer string)
|
||||
{
|
||||
return nilp(string) ||
|
||||
pointer2cell(string).payload.string.character == '\0';
|
||||
bool end_of_string( struct cons_pointer string ) {
|
||||
return nilp( string ) ||
|
||||
pointer2cell( string ).payload.string.character == '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep, and thus expensive, equality: true if these two objects have
|
||||
* identical structure, else false.
|
||||
*/
|
||||
bool equal(struct cons_pointer a, struct cons_pointer b)
|
||||
{
|
||||
bool result = eq(a, b);
|
||||
bool equal( struct cons_pointer a, struct cons_pointer b ) {
|
||||
bool result = eq( a, b );
|
||||
|
||||
if (!result && same_type(a, b))
|
||||
{
|
||||
struct cons_space_object *cell_a = &pointer2cell(a);
|
||||
struct cons_space_object *cell_b = &pointer2cell(b);
|
||||
if ( !result && same_type( a, b ) ) {
|
||||
struct cons_space_object *cell_a = &pointer2cell( a );
|
||||
struct cons_space_object *cell_b = &pointer2cell( b );
|
||||
|
||||
switch (cell_a->tag.value)
|
||||
{
|
||||
case CONSTV:
|
||||
case LAMBDATV:
|
||||
case NLAMBDATV:
|
||||
/* TODO: it is not OK to do this on the stack since list-like
|
||||
* structures can be of indefinite extent. It *must* be done by
|
||||
* iteration (and even that is problematic) */
|
||||
result =
|
||||
equal(cell_a->payload.cons.car, cell_b->payload.cons.car) && equal(cell_a->payload.cons.cdr,
|
||||
cell_b->payload.cons.cdr);
|
||||
break;
|
||||
case KEYTV:
|
||||
case STRINGTV:
|
||||
case SYMBOLTV:
|
||||
/* slightly complex because a string may or may not have a '\0'
|
||||
* cell at the end, but I'll ignore that for now. I think in
|
||||
* practice only the empty string will.
|
||||
*/
|
||||
/* TODO: it is not OK to do this on the stack since list-like
|
||||
* structures can be of indefinite extent. It *must* be done by
|
||||
* iteration (and even that is problematic) */
|
||||
result =
|
||||
cell_a->payload.string.character ==
|
||||
switch ( cell_a->tag.value ) {
|
||||
case CONSTV:
|
||||
case LAMBDATV:
|
||||
case NLAMBDATV:
|
||||
/* TODO: it is not OK to do this on the stack since list-like
|
||||
* structures can be of indefinite extent. It *must* be done by
|
||||
* iteration (and even that is problematic) */
|
||||
result =
|
||||
equal( cell_a->payload.cons.car, cell_b->payload.cons.car )
|
||||
&& equal( cell_a->payload.cons.cdr,
|
||||
cell_b->payload.cons.cdr );
|
||||
break;
|
||||
case KEYTV:
|
||||
case STRINGTV:
|
||||
case SYMBOLTV:
|
||||
/* slightly complex because a string may or may not have a '\0'
|
||||
* cell at the end, but I'll ignore that for now. I think in
|
||||
* practice only the empty string will.
|
||||
*/
|
||||
/* TODO: it is not OK to do this on the stack since list-like
|
||||
* structures can be of indefinite extent. It *must* be done by
|
||||
* iteration (and even that is problematic) */
|
||||
result =
|
||||
cell_a->payload.string.character ==
|
||||
cell_b->payload.string.character &&
|
||||
(equal(cell_a->payload.string.cdr,
|
||||
cell_b->payload.string.cdr) ||
|
||||
(end_of_string(cell_a->payload.string.cdr) && end_of_string(cell_b->payload.string.cdr)));
|
||||
break;
|
||||
case INTEGERTV:
|
||||
result =
|
||||
(cell_a->payload.integer.value ==
|
||||
cell_b->payload.integer.value) &&
|
||||
equal(cell_a->payload.integer.more,
|
||||
cell_b->payload.integer.more);
|
||||
break;
|
||||
case RATIOTV:
|
||||
result = equal_ratio_ratio(a, b);
|
||||
break;
|
||||
case REALTV:
|
||||
{
|
||||
double num_a = to_long_double(a);
|
||||
double num_b = to_long_double(b);
|
||||
double max =
|
||||
fabs(num_a) >
|
||||
fabs(num_b)
|
||||
? fabs(num_a)
|
||||
: fabs(num_b);
|
||||
( equal( cell_a->payload.string.cdr,
|
||||
cell_b->payload.string.cdr ) ||
|
||||
( end_of_string( cell_a->payload.string.cdr )
|
||||
&& end_of_string( cell_b->payload.string.cdr ) ) );
|
||||
break;
|
||||
case INTEGERTV:
|
||||
result =
|
||||
( cell_a->payload.integer.value ==
|
||||
cell_b->payload.integer.value ) &&
|
||||
equal( cell_a->payload.integer.more,
|
||||
cell_b->payload.integer.more );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result = equal_ratio_ratio( a, b );
|
||||
break;
|
||||
case REALTV:
|
||||
{
|
||||
double num_a = to_long_double( a );
|
||||
double num_b = to_long_double( b );
|
||||
double max = fabs( num_a ) > fabs( num_b )
|
||||
? fabs( num_a )
|
||||
: fabs( num_b );
|
||||
|
||||
/*
|
||||
* not more different than one part in a million - close enough
|
||||
*/
|
||||
result = fabs(num_a - num_b) < (max / 1000000.0);
|
||||
/*
|
||||
* not more different than one part in a million - close enough
|
||||
*/
|
||||
result = fabs( num_a - num_b ) < ( max / 1000000.0 );
|
||||
}
|
||||
break;
|
||||
default:
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (numberp(a) && numberp(b))
|
||||
{
|
||||
if (integerp(a))
|
||||
{
|
||||
result = equal_integer_real(a, b);
|
||||
}
|
||||
else if (integerp(b))
|
||||
{
|
||||
result = equal_integer_real(b, a);
|
||||
} else if ( numberp( a ) && numberp( b ) ) {
|
||||
if ( integerp( a ) ) {
|
||||
result = equal_integer_real( a, b );
|
||||
} else if ( integerp( b ) ) {
|
||||
result = equal_integer_real( b, a );
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* there's only supposed ever to be one T and one NIL cell, so each
|
||||
* should be caught by eq; equality of vector-space objects is a whole
|
||||
* other ball game so we won't deal with it now (and indeed may never).
|
||||
* I'm not certain what equality means for read and write streams, so
|
||||
* I'll ignore them, too, for now.
|
||||
*/
|
||||
* there's only supposed ever to be one T and one NIL cell, so each
|
||||
* should be caught by eq; equality of vector-space objects is a whole
|
||||
* other ball game so we won't deal with it now (and indeed may never).
|
||||
* I'm not certain what equality means for read and write streams, so
|
||||
* I'll ignore them, too, for now.
|
||||
*/
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue