From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id IAA29420; Mon, 4 Feb 2002 08:58:10 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id IAA28373 for ; Mon, 4 Feb 2002 08:58:08 +0100 (MET) Received: from medianet-1v.grolier.fr (medianet-1v.grolier.fr [194.158.98.201]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g147w8114993 for ; Mon, 4 Feb 2002 08:58:08 +0100 (MET) Received: from gregoire.dijon.fr ([213.195.0.60]) by medianet-1v.grolier.fr (8.9.3+Sun/8.9.3) with ESMTP id IAA11659; Mon, 4 Feb 2002 08:58:04 +0100 (CET) Received: from gregoire (localhost [127.0.0.1]) by gregoire.dijon.fr (8.11.2/8.11.2/SuSE Linux 8.11.1-0.5) with SMTP id g147vkd06575; Mon, 4 Feb 2002 08:57:46 +0100 Date: Mon, 4 Feb 2002 08:57:45 +0100 From: Michel Quercia To: doug@bagley.org (Doug Bagley) Cc: caml-list@inria.fr Subject: Re: [Caml-list] some Hashtbl observations Message-Id: <20020204085745.41c58120.michel.quercia@prepas.org> In-Reply-To: <15453.36509.586757.825002@ns.bagley.org> References: <15453.36509.586757.825002@ns.bagley.org> X-Mailer: Sylpheed version 0.6.6 (GTK+ 1.2.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Le Sun, 3 Feb 2002 13:25:17 -0600 doug@bagley.org (Doug Bagley) écrivit : > I recently had occasion to create a hash table to remember some Num's > I was generating. Of course, I didn't figure out right away that you > can't directly use the generic interface for Hashtbl with the type > "Num". > ... > Exception: Failure "equal: abstract value". I ran into this problem when developping the Bigint interface for Numerix. You'll find in numerix-0.19 (http://pauillac.inria.fr/~quercia/cdrom/bibs/numerix-0.19a.tar.gz) a work around implementing the missing functions (generic compare and generic hash) for nats. Below is the patch file used in numerix-0.19 : ---------------------- cut here ---------------------------------- *** otherlibs/num/nat_stubs.c.orig Fri Aug 24 15:08:51 2001 --- otherlibs/num/nat_stubs.c Fri Aug 24 15:46:58 2001 *************** *** 11,16 **** --- 11,18 ---- /***********************************************************************/ /* $Id: nat_stubs.c,v 1.10 2000/04/20 16:20:59 xleroy Exp $ */ + /* Modified by M.Quercia on August 24th, 2001 for generic compare and hash */+ /* All modified lines end with the comment MQ */ #define CAML_LIGHT #include "alloc.h" *************** *** 28,39 **** static void serialize_nat(value, unsigned long *, unsigned long *); static unsigned long deserialize_nat(void * dst); static struct custom_operations nat_operations = { "_nat", custom_finalize_default, ! custom_compare_default, ! custom_hash_default, serialize_nat, deserialize_nat }; --- 30,43 ---- static void serialize_nat(value, unsigned long *, unsigned long *); static unsigned long deserialize_nat(void * dst); + static int nat_compare(value a, value b); /* MQ */ + static long nat_hash(value a); /* MQ */ static struct custom_operations nat_operations = { "_nat", custom_finalize_default, ! nat_compare, /* custom_compare_default, MQ */ ! nat_hash, /* custom_hash_default, MQ */ serialize_nat, deserialize_nat }; *************** *** 331,333 **** --- 335,350 ---- return len * 4; } + /* custom comparison MQ */ + static int nat_compare(value a, value b) { /* MQ */ + return (BnnCompare(Bignum_val(a), Wosize_val(a) - 1, /* MQ */ + Bignum_val(b), Wosize_val(b) - 1)); /* MQ */ + } /* MQ */ + + /* hashing, borrowed from ocaml/byterun/hash.c MQ */ + static long nat_hash(value a) { /* MQ */ + unsigned long l = Wosize_val(a) - 1, accu = l, i; /* MQ */ + for (i=0; i