* Re: Caml-list] Sets and home-made ordered types [not found] <20090917030607.927BCBCA9@yquem.inria.fr> @ 2009-09-17 6:21 ` CUOQ Pascal 2009-09-17 8:45 ` [Caml-list] " Matthias Puech 0 siblings, 1 reply; 3+ messages in thread From: CUOQ Pascal @ 2009-09-17 6:21 UTC (permalink / raw) To: caml-list [-- Attachment #1: Type: text/plain, Size: 1091 bytes --] David Allsopp wrote : > Is it not possible to model your requirement using Map.Make instead - where > the keys represent the equivalence classes and the values whatever data > you're associating with them? Matthias Puech wrote: >Yes, that's exactly the workaround I ended up using, although I'm not >very happy with it because, among other things, these keys/class >disciminant get duplicated (once inside the key, once inside the >element). I'm getting more concrete below. Since you already have the "compare" function between objects of type t, why don't you make your map associate values of type t to identical values of type t instead of trying to have different type for keys and elements? You can even do it generically, and obtain with little effort an implementation of sets that supports find. module Set_With_Find(X:Set.OrderedType) = struct module M = Map.Make(X) type t = X.t M.t (* with invariant that value v is always associated to v *) let find = M.find let add v s = M.add v v s ....... end Pascal [-- Attachment #2: winmail.dat --] [-- Type: application/ms-tnef, Size: 3196 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Caml-list] Sets and home-made ordered types 2009-09-17 6:21 ` Caml-list] Sets and home-made ordered types CUOQ Pascal @ 2009-09-17 8:45 ` Matthias Puech 2009-09-17 9:07 ` CUOQ Pascal 0 siblings, 1 reply; 3+ messages in thread From: Matthias Puech @ 2009-09-17 8:45 UTC (permalink / raw) To: CUOQ Pascal; +Cc: caml-list Hello and thanks to all for your answers, If I understand correctly, you're all (David, Elnatan, Vincent, Tiphaine, Pascal) suggesting more or less the same solution (the one below). Do you have an idea of its memory overhead compared to just using Sets? I guess the value is not copied twice but shared between keys and elements right? So what, one pointer more for each association in the Map? That would be rather acceptable (but still not ideal, sorry I'm very demanding). Thanks again, -- Matthias CUOQ Pascal a écrit : > Since you already have the "compare" function between objects of > type t, why don't you make your map associate values of type t to > identical values of type t instead of trying to have different type > for keys and elements? > > You can even do it generically, and obtain with little effort an > implementation of sets that supports find. > > module Set_With_Find(X:Set.OrderedType) = > struct > module M = Map.Make(X) > type t = X.t M.t (* with invariant that value v is always associated to v *) > let find = M.find > let add v s = M.add v v s > ....... > end > > Pascal > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Sets and home-made ordered types 2009-09-17 8:45 ` [Caml-list] " Matthias Puech @ 2009-09-17 9:07 ` CUOQ Pascal 0 siblings, 0 replies; 3+ messages in thread From: CUOQ Pascal @ 2009-09-17 9:07 UTC (permalink / raw) To: Matthias Puech; +Cc: caml-list > So what, one pointer more for each association > in the Map? That would be rather acceptable (but still not ideal, sorry > I'm very demanding). You were already paying the price of "one pointer more" many times over and were not even thinking about it. One more will not make any difference. You have to realize that each node already carries a height, two pointers to subtrees, and take into account the one-word overhead for the block header. We're not doubling the size of each tree node here, we're increasing it from 5 to 6 words. Pascal PS: You make up for the overhead by having dynamic structures that are just the right size, and by taking advantage of sharing, of course. ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-09-17 9:08 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- [not found] <20090917030607.927BCBCA9@yquem.inria.fr> 2009-09-17 6:21 ` Caml-list] Sets and home-made ordered types CUOQ Pascal 2009-09-17 8:45 ` [Caml-list] " Matthias Puech 2009-09-17 9:07 ` CUOQ Pascal
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox