From 3c09800abda31a3f5da8157b0ef2863850f6b662 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 19 Nov 2015 20:36:21 -0800 Subject: New equality substitution. If the equal method is defined for structs, its return value is used in their place for hashing and comparison. * eval.h (eq_s, eql_s, equal_s): Declared. * hash.c (equal_hash): If a COBJ defines an equalsub function, we call it. If it returns non-nil, we take the object in its place and recurse. * lib.c (equal): Refactored to support equality substitution. (less): Support equality substitution. * lib.h (cobj_ops): New function pointer member, equalsub. Only struct instances define this, currently. (cobj_ops_init): Add null entry to initializer for equalsub. (cobj_ops_init_ex): New initialiation macro for situations when the equalsub member must be provided. * struct.c (struct struct_type): new member eqmslot. (make_struct_type): Initialize emslot to zero. (static_slot_set, static_slot_ensure): If eqmslot is -1, indicating positive knowledge that there is no equal method static slot, we must invalidate that with a zero: it is no longer known whether there is or isn't such a slot. (get_equal_method, struct_inst_equalsub): New static functions. (struct_inst_ops): Initialize the equalsub member using new cobj_ops_init_ex macro. * txr.1: Document equality substitution. --- lib.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib.h') diff --git a/lib.h b/lib.h index 3b10ce6d..7b33c784 100644 --- a/lib.h +++ b/lib.h @@ -219,10 +219,15 @@ struct cobj_ops { void (*destroy)(val self); void (*mark)(val self); cnum (*hash)(val self); + val (*equalsub)(val self); }; #define cobj_ops_init(equal, print, destroy, mark, hash) \ - { equal, print, destroy, mark, hash } + { equal, print, destroy, mark, hash, 0 } + +#define cobj_ops_init_ex(equal, print, destroy, mark, hash, \ + equalsub) \ + { equal, print, destroy, mark, hash, equalsub } /* Default operations for above structure. * Default equal is eq -- cgit v1.2.3