English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: [Caml-list] Question about string ref and Hashtbl.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-03-20 (03:13)
From: Remi Vanicat <remi.vanicat@l...>
Subject: Re: [Caml-list] Question about string ref and Hashtbl.
Bob Bailey <bobbaileyjr@yahoo.com> writes:

> That's an interesting idea.  SO the key string and the value string
> will really point to the same location in memory.
> So if I did (string,string ref) Hashtbl.t, then the string ref would
> be a pointer to the key string. 

It is a strange thing that the key and value of a location of the
Hashtbl are really the same thing. 

Mmm, i believe that I've understood what you want to do, and if you
don't want to modify  the value associated with a key, then you don't
need to use the type ref. the type ref have one and only one interest:
you can modify it.

> Would that allow me to compare two
> string ref variables together?  Would the comparason of the locations
> of the strings mean I wouldn't have to do a full string compare?
> so in my module type index = string ref type dict = (string,index)
> Hashtbl.t
> So in the case of let a = Hashtbl.find dict "a" let b = Hashtbl.find
> dict "a"
> would a==b be the same as !a == !b?  

Well if 

let a = Hashtbl.find dict "a"


let b = Hashtbl.find dict "a"

and if the Hashtbl have not been modified between the two call, then
a==b. and if a==b then !a == !b. but beware :

# let st = "test";;
val st : string = "test"
# let a = ref st;;
val a : string ref = {contents = "test"}
# let b = ref st;;
val b : string ref = {contents = "test"}
# a == b;;
- : bool = false
# !a == !b;;
- : bool = true

> Also, would a==b be a faster comparason in this case?

No, == test the equality of the pointer to the value, so it take the
same time for all type. By the way :

# "test" == "test";;
- : bool = false

May be you want to use =, but with (=) then (a = b) is equal to 
(!a = !b) for all reference a and b, and the evaluation time are more
or less the same (may be (!a = !b) is faster, but I won't bet on it).
Rémi Vanicat

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