Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] some Hashtbl observations
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michel Quercia <michel.quercia@p...>
Subject: Re: [Caml-list] some Hashtbl observations
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<l; i++)                                    /* MQ */
+     accu = accu*65599 + BnGetDigit(Bignum_val(a),i);     /* MQ */
+   return(accu);                                          /* MQ */
+ }                                                        /* MQ */
----------------------  cut here ----------------------------------

Refer to the Numerix doc (file doc/numerix-eng.ps, page 42) for
instructions.

Regards,
-- 
Michel Quercia
57 rue abbé Grégoire, 38000 Grenoble
http://michel.quercia.free.fr (maths)
http://pauillac.inria.fr/~quercia (informatique)
mailto:michel.quercia@prepas.org
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr