Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Hashtbls with physical equality?
@ 2004-11-14 22:29 Wheeler Ruml
  2004-11-15  0:33 ` [Caml-list] " Christian Szegedy
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Wheeler Ruml @ 2004-11-14 22:29 UTC (permalink / raw)
  To: caml-list

Is it possible in OCaml to have a hash table that can insert and retrieve
values without walking over their structure?  I tried to hack something
together using Obj.magic (working from Hashtbl.ml and the size.ml example
by Filliatre) but it doesn't work for me and I'm concerned that the garbage
collector might be making the magic values obsolete.  I have a hash table
of strings and I'd like to avoid traversing their length on every lookup.
Do I have to explicitly use integers instead?

And hints or advice would be warmly welcome!  Please respond directly to me
as well as to the list, as I can't keep up with the volume and am not a
regular subscriber.  Thanks very much,

Wheeler
-- 
Wheeler Ruml, Palo Alto Research Center, Rm 1522, 650-812-4329 voice
ruml@parc.com, http://www.parc.com/ruml           650-812-4334 fax
------------ excerpt of my attempt --------------------

let hash x =
  (* want both integers and pointers (presumably word-aligned) to have
     well-spread lower bits *)
  let y = ((Obj.magic x) : int) in
    y lxor (y lsr 16)

    
let add h key info =
  let i = (hash key) mod (Array.length h.data) in
  let bucket = Cons(key, info, h.data.(i)) in
  h.data.(i) <- bucket;
  h.size <- succ h.size;
  if h.size > Array.length h.data lsl 1 then resize h

    
let rec find_rec key = function
    Empty ->
      raise Not_found
  | Cons(k, d, rest) ->
      if key == k then d else find_rec key rest

	
let find h key =
  match h.data.((hash key) mod (Array.length h.data)) with
    Empty -> raise Not_found
  | Cons(k1, d1, rest1) ->
      if key == k1 then d1 else find_rec key rest1

------------------------------------------------------------


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-11-15 23:42 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-14 22:29 Hashtbls with physical equality? Wheeler Ruml
2004-11-15  0:33 ` [Caml-list] " Christian Szegedy
     [not found] ` <20041115012212.GA6561@artisan.com>
2004-11-15  2:20   ` Wheeler Ruml
2004-11-15 10:24     ` Alex Baretta
2004-11-15 16:45       ` Wheeler Ruml
2004-11-15 20:34         ` Marcin 'Qrczak' Kowalczyk
2004-11-15 20:37 ` Brian Hurt
2004-11-15 21:03   ` Wheeler Ruml
2004-11-15 21:30   ` Marcin 'Qrczak' Kowalczyk
2004-11-15 23:42   ` Stefan Monnier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox