caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean-Christophe Filliatre <Jean-Christophe.Filliatre@lri.fr>
To: Christophe Raffalli <raffalli@univ-savoie.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Weak hash table
Date: Wed, 6 Mar 2002 09:33:54 +0100 (MET)	[thread overview]
Message-ID: <15493.54386.780519.557241@pc803.lri.fr> (raw)
In-Reply-To: <3C84F214.C42D6891@univ-savoie.fr>

[-- Attachment #1: Type: text/plain, Size: 436 bytes --]


Christophe Raffalli writes:
 > 
 > did anyone implemented weak hash table using weak pointers in OCaml ?
 > 
 > I need that, and if I can save work ...

I did  it once,  but a very  naive implementation with  only functions
"create",  "add" and  "find" (I  finally ended  using some  other data
structure,  so I  didn't pursue).  Anyhow,  I attach  these two  small
files.

-- 
Jean-Christophe Filliâtre (http://www.lri.fr/~filliatr)


[-- Attachment #2: whash.mi --]
[-- Type: application/octet-stream, Size: 124 bytes --]


type ('a,'b) t

val create : int -> ('a,'b) t

val add : ('a,'b) t -> 'a -> 'b -> unit

val find : ('a,'b) t -> 'a -> 'b



[-- Attachment #3: whash.ml --]
[-- Type: application/octet-stream, Size: 426 bytes --]


type ('a,'b) t = ('a * 'b) list Weak.t

let create = Weak.create

let add t x y =
  let n = Weak.length t in
  let i = (Hashtbl.hash x) mod n in
  let old  = match Weak.get t i with
    | None -> []
    | Some l -> l
  in
  Weak.set t i (Some ((x,y) :: old))

let find t x =
  let n = Weak.length t in
  let i = (Hashtbl.hash x) mod n in
  match Weak.get t i with
    | None -> raise Not_found
    | Some l -> List.assoc x l

  parent reply	other threads:[~2002-03-06  8:33 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-15  1:48 [Caml-list] Another q about many types Ryan Tarpine
2002-04-15  8:06 ` Christian Gillot
2002-03-05 16:28   ` [Caml-list] Weak hash table Christophe Raffalli
2002-03-05 21:40     ` Charles Martin
2002-03-05 22:24       ` Dave Mason
2002-03-06 10:09         ` Christophe Raffalli
2002-03-06 10:39         ` Christophe Raffalli
2002-03-06  8:33     ` Jean-Christophe Filliatre [this message]
2002-03-06 12:50 Damien Doligez
2002-03-06 13:40 ` Remi VANICAT
2002-03-14 14:09 Damien Doligez
2002-03-15  1:49 ` Brian Rogoff
2002-03-14 14:16 Damien Doligez
2002-03-22 14:15 Damien Doligez
2002-03-22 18:35 [Caml-list] Weak Hash table Remi VANICAT

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15493.54386.780519.557241@pc803.lri.fr \
    --to=jean-christophe.filliatre@lri.fr \
    --cc=caml-list@inria.fr \
    --cc=raffalli@univ-savoie.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).