caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Hash over ref
@ 2004-05-20 20:37 Jon Harrop
  2004-05-20 20:48 ` David Monniaux
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jon Harrop @ 2004-05-20 20:37 UTC (permalink / raw)
  To: caml-list


Is it possible to hash a ref itself, rather than the object it references?

Cheers,
Jon.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Hash over ref
  2004-05-20 20:37 [Caml-list] Hash over ref Jon Harrop
@ 2004-05-20 20:48 ` David Monniaux
  2004-05-20 22:52 ` Damien Doligez
  2004-05-20 23:29 ` Alain Frisch
  2 siblings, 0 replies; 5+ messages in thread
From: David Monniaux @ 2004-05-20 20:48 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Thu, 2004-05-20 at 22:37, Jon Harrop wrote:
> Is it possible to hash a ref itself, rather than the object it references?
Yes. References are actually implemented as mutable records with one
field.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Hash over ref
  2004-05-20 20:37 [Caml-list] Hash over ref Jon Harrop
  2004-05-20 20:48 ` David Monniaux
@ 2004-05-20 22:52 ` Damien Doligez
  2004-05-20 23:29 ` Alain Frisch
  2 siblings, 0 replies; 5+ messages in thread
From: Damien Doligez @ 2004-05-20 22:52 UTC (permalink / raw)
  To: caml-list

On May 20, 2004, at 22:37, Jon Harrop wrote:

> Is it possible to hash a ref itself, rather than the object it 
> references?

If I understand the question correctly, you want the hash to remain
constant even when you update the reference.  Then the answer is no.

The normal technique for this case is to replace your reference with
a record of two fields: a mutable field for the contents, and an int
field which you initialize with a random number (or a sequential
counter) when you create the record.  Then your hash code is this
number.

-- Damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Hash over ref
  2004-05-20 20:37 [Caml-list] Hash over ref Jon Harrop
  2004-05-20 20:48 ` David Monniaux
  2004-05-20 22:52 ` Damien Doligez
@ 2004-05-20 23:29 ` Alain Frisch
  2004-05-21 10:59   ` Jon Harrop
  2 siblings, 1 reply; 5+ messages in thread
From: Alain Frisch @ 2004-05-20 23:29 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Thu, 20 May 2004, Jon Harrop wrote:

> Is it possible to hash a ref itself, rather than the object it references?

If you really need to use values of type 'a ref (that you create yourself,
but need to store in existing data structures), you can cheat and
implement Damien suggestion like this:

module HashedRef : sig
  val ref: 'a -> 'a ref
  val hash: 'a ref -> int
end = struct
  class c = object end
  type 'a my_ref = { mutable value: 'a; uid : int }

  let ref x = Obj.magic { value = x; uid = Oo.id (new c) }

  let hash x =
    if Obj.size (Obj.repr x) = 2 then (Obj.magic x).uid
    else failwith "HashedRef.hash: this reference is not hashable"
end

(Don't do this at home, of course.)

-- Alain

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Hash over ref
  2004-05-20 23:29 ` Alain Frisch
@ 2004-05-21 10:59   ` Jon Harrop
  0 siblings, 0 replies; 5+ messages in thread
From: Jon Harrop @ 2004-05-21 10:59 UTC (permalink / raw)
  To: caml-list


Ok, thanks for all the advice from everyone. :-)

Essentially, I wish to hash the pointer value of the reference itself. So, if 
a reference were reassigned, then the hash would be different. Two identical 
copies of an object would have different references and, consequently, would 
give different hashes (i.e. a != b is true where a and b are 'a ref).

The reason I want to do this is to spot duplicate uses of a single object 
(multiple references to it) but without confusing this with different uses of 
identical objects (i.e. where "=" would give true but "==" would not).

I can think of a couple of alternative approaches. I could implement a naff 
kind of set as a list which used "==" to test for equality. Or, I could loop 
through all of the references to the objects, marking unmarked objects with a 
new identifier (an int, say) and then insert them all into a Hashtbl on the 
basis of their identifiers. I just thought hashing refs themselves would be 
simpler.

I think I can do this given Alain's code though, thanks!

Cheers,
Jon.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2004-05-21 11:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-20 20:37 [Caml-list] Hash over ref Jon Harrop
2004-05-20 20:48 ` David Monniaux
2004-05-20 22:52 ` Damien Doligez
2004-05-20 23:29 ` Alain Frisch
2004-05-21 10:59   ` Jon Harrop

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).