caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Fw: [Caml-list] Missing a function
@ 2005-01-30  9:53 Frédéric Gava
  2005-01-30 12:14 ` Olivier Andrieu
  0 siblings, 1 reply; 8+ messages in thread
From: Frédéric Gava @ 2005-01-30  9:53 UTC (permalink / raw)
  To: caml-list

Hi,
>
> > Another option is to build on top of the stdlib Map functor another
> > functor that keeps track of the cardinal of a map :
> >
> > module CMap = functor (Ord : Map.OrderedType) ->
> >   (struct
> >     module M = Map.Make (Ord)
> >
> >     type key = M.key
> >     type 'a t = int * 'a M.t
> >
> >     let cardinal = fst
> >
> >     let empty = 0, M.empty
> >
> >     let add k v (n, m) = (n + 1, M.add k v m)
> >
> >     let find k (_, m) = M.find k m
> >
> > ....
> >    : sig
> >        include Map.S
> >        val cardinal : 'a t -> int
> >      end)
> >
Hum. To remove an element, you have to test is its exists or not before
decrease n. It could be to much cost expansive...no ?

Regards,
Frédéric Gava



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

* Re: [Caml-list] Missing a function
  2005-01-30  9:53 Fw: [Caml-list] Missing a function Frédéric Gava
@ 2005-01-30 12:14 ` Olivier Andrieu
  0 siblings, 0 replies; 8+ messages in thread
From: Olivier Andrieu @ 2005-01-30 12:14 UTC (permalink / raw)
  To: frederic.gava; +Cc: caml-list

 > Frédéric Gava [Sun, 30 Jan 2005]:
 > Hi,
 > >
 > > > Another option is to build on top of the stdlib Map functor another
 > > > functor that keeps track of the cardinal of a map :
 > > >
 > > > module CMap = functor (Ord : Map.OrderedType) ->
 > > >   (struct
 > > >     module M = Map.Make (Ord)
 > > >
 > > >     type key = M.key
 > > >     type 'a t = int * 'a M.t
 > > >
 > > >     let cardinal = fst
 > > >
 > > >     let empty = 0, M.empty
 > > >
 > > >     let add k v (n, m) = (n + 1, M.add k v m)
 > > >
 > > >     let find k (_, m) = M.find k m
 > > >
 > > > ....
 > > >    : sig
 > > >        include Map.S
 > > >        val cardinal : 'a t -> int
 > > >      end)
 > > >
 > Hum. To remove an element, you have to test is its exists or not
 > before decrease n. It could be to much cost expansive...no ?

Oh right. And a test is needed for `add' too since the cardinal will
not change if the key is already bound in the map. So, all in all it's
not a very good idea :)

-- 
   Olivier


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

* Re: [Caml-list] Missing a function
  2005-01-29 19:34             ` Radu Grigore
  2005-01-29 19:55               ` Radu Grigore
  2005-01-29 21:05               ` Olivier Andrieu
@ 2005-02-04 20:18               ` Radu Grigore
  2 siblings, 0 replies; 8+ messages in thread
From: Radu Grigore @ 2005-02-04 20:18 UTC (permalink / raw)
  To: caml-list

On Sat, 29 Jan 2005 21:34:28 +0200, Radu Grigore <radugrigore@gmail.com> wrote:

> I do not have the ocaml sources handy right now but... Set and Map are
> probably RB-trees.

Map is not a RB-tree but a height balanced tree: HB(2). (haven't looked at Set)

BTW, what does the license say about reusing the code in map.ml?
Please use simple terms for people that don't like to deal with
administrative stuff. The ideal response would be: do it / don't do it
:)

(Now that I have read the code I would probably write it very
similarly even if I would not look at it any more...)

-- 
regards,
 radu
http://rgrig.idilis.ro/


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

* Re: [Caml-list] Missing a function
  2005-01-29 19:34             ` Radu Grigore
  2005-01-29 19:55               ` Radu Grigore
@ 2005-01-29 21:05               ` Olivier Andrieu
  2005-02-04 20:18               ` Radu Grigore
  2 siblings, 0 replies; 8+ messages in thread
From: Olivier Andrieu @ 2005-01-29 21:05 UTC (permalink / raw)
  To: radugrigore; +Cc: caml-list

 > Radu Grigore [Sat, 29 Jan 2005]:
 > On Sat, 4 Dec 2004 15:13:44 +0100, Frédéric Gava
 > <frederic.gava@wanadoo.fr> wrote:
 > 
 > > > let cardinal e = fold (fun _ _ x -> succ x) e 0;;
 > > [...] Map are also implemented as balanced
 > > tree (like sets) and therefore, it is easy to add a function of "cardinal".
 > 
 > I agree it's strange not to have "cardinal" in Map.Make. The
 > implementations of Set and Map are likely to be very similar anyway.

Another option is to build on top of the stdlib Map functor another
functor that keeps track of the cardinal of a map :

module CMap = functor (Ord : Map.OrderedType) ->
  (struct
    module M = Map.Make (Ord)
	
    type key = M.key
    type 'a t = int * 'a M.t

    let cardinal = fst

    let empty = 0, M.empty

    let add k v (n, m) = (n + 1, M.add k v m)

    let find k (_, m) = M.find k m
	  
	....
   : sig
       include Map.S
       val cardinal : 'a t -> int
     end)

-- 
   Olivier


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

* Re: [Caml-list] Missing a function
  2005-01-29 19:34             ` Radu Grigore
@ 2005-01-29 19:55               ` Radu Grigore
  2005-01-29 21:05               ` Olivier Andrieu
  2005-02-04 20:18               ` Radu Grigore
  2 siblings, 0 replies; 8+ messages in thread
From: Radu Grigore @ 2005-01-29 19:55 UTC (permalink / raw)
  To: caml-list

On Sat, 29 Jan 2005 21:34:28 +0200, Radu Grigore <radugrigore@gmail.com> wrote:

> for Set the complexity [of cardinal] isn't specified. It is probably
> O(lg n).

Strike that :( I don't know what went in my mind. I should probably
make a habit of waiting 5min before pressing "send" :(

-- 
regards,
 radu
http://rgrig.idilis.ro/


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

* Re: [Caml-list] Missing a function
  2004-12-04 14:13           ` Frédéric Gava
@ 2005-01-29 19:34             ` Radu Grigore
  2005-01-29 19:55               ` Radu Grigore
                                 ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Radu Grigore @ 2005-01-29 19:34 UTC (permalink / raw)
  To: Frédéric Gava; +Cc: sejourne_kevin, caml-list

On Sat, 4 Dec 2004 15:13:44 +0100, Frédéric Gava
<frederic.gava@wanadoo.fr> wrote:

> > let cardinal e = fold (fun _ _ x -> succ x) e 0;;
> [...] Map are also implemented as balanced
> tree (like sets) and therefore, it is easy to add a function of "cardinal".

I agree it's strange not to have "cardinal" in Map.Make. The
implementations of Set and Map are likely to be very similar anyway.

I do not have the ocaml sources handy right now but... Set and Map are
probably RB-trees. In this case there are at least 2 pointers for
links (left and right children),  one pointer for key and, for maps,
one pointer for data.  So a set of N elements occupies at least 12xN
bytes, while a map occupies at least 16xN bytes. If an element counter
is added then the sizes would increase to 16xN and 20xN. That doesn't
seem too bad. The advantages would be:

a. O(1) cardinal function. Right now for Map the user can implement a
O(n) one and for Set the complexity isn't specified. It is probably
O(lg n).

b. O(lg n) indexed access.

Such a change implies minimal modification of existing functions:
Set.cardinal becomes faster (if my guess -- lg n -- was correct) and
add/remove will be _slightly_ slower. The advantage would be
Map.cardinal and indexed access.

-- 
regards,
 radu
http://rgrig.idilis.ro/


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

* Re: [Caml-list] Missing a function
  2004-12-04 13:25         ` [Caml-list] " sejourne_kevin
@ 2004-12-04 14:13           ` Frédéric Gava
  2005-01-29 19:34             ` Radu Grigore
  0 siblings, 1 reply; 8+ messages in thread
From: Frédéric Gava @ 2004-12-04 14:13 UTC (permalink / raw)
  To: sejourne_kevin; +Cc: caml-list

> > I have done a comparaison on three data structures of the stdlib of
OCaml (I
> > do parallel implementation of this data structure) and I find that the
> > module Map does not contain any function for counting the number of
elements
> > of the data structure:
> >    Set, cardinal: t -> int
> >    Hashtbl,  lenght : ('a,'b) t -> int
> >    Map,    you have to do   "let length the_map = fold (fun _ _ i ->
i+1)
> > the_map 0 "
> > And Set and Hastbl have also fold...
> > Is there an explanation (or is it just a missing ;-) ) ?
>
> There is a 'fold' function in functor Make.
> val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
> so :
> let cardinal e = fold (fun _ _ x -> succ x) e 0;;

Hi,

> >    Map,    you have to do   "let length the_map = fold (fun _ _ i ->
i+1)
> let cardinal e = fold (fun _ _ x -> succ x) e 0;;

Ok. That is the same things (except the name) that I have written  in  my
mail... but this method is not very efficient because you apply many time
a function without using its arguments. Map are also implemented as balanced
tree (like sets) and therefore, it is easy to add a function of "cardinal".

Cheers,
Frédéric Gava



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

* Re: [Caml-list] Missing a function
  2004-12-04 11:05       ` Missing a function Frédéric Gava
@ 2004-12-04 13:25         ` sejourne_kevin
  2004-12-04 14:13           ` Frédéric Gava
  0 siblings, 1 reply; 8+ messages in thread
From: sejourne_kevin @ 2004-12-04 13:25 UTC (permalink / raw)
  Cc: caml-list

Frédéric Gava wrote:
> Hi,
> 
> I have done a comparaison on three data structures of the stdlib of OCaml (I
> do parallel implementation of this data structure) and I find that the
> module Map does not contain any function for counting the number of elements
> of the data structure:
>    Set, cardinal: t -> int
>    Hashtbl,  lenght : ('a,'b) t -> int
>    Map,    you have to do   "let length the_map = fold (fun _ _ i -> i+1)
> the_map 0 "
> 
> And Set and Hastbl have also fold...
> 
> Is there an explanation (or is it just a missing ;-) ) ?

There is a 'fold' function in functor Make.
val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b

so :

let cardinal e = fold (fun _ _ x -> succ x) e 0;;



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

end of thread, other threads:[~2005-02-04 20:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-01-30  9:53 Fw: [Caml-list] Missing a function Frédéric Gava
2005-01-30 12:14 ` Olivier Andrieu
  -- strict thread matches above, loose matches on Subject: below --
2004-11-30 20:30 Strange observation on polymorphic '<' Ritesh Kumar
2004-12-01 23:17 ` [Caml-list] " Damien Doligez
2004-12-03  7:24   ` Ritesh Kumar
2004-12-03  8:22     ` Ville-Pertti Keinonen
2004-12-04 11:05       ` Missing a function Frédéric Gava
2004-12-04 13:25         ` [Caml-list] " sejourne_kevin
2004-12-04 14:13           ` Frédéric Gava
2005-01-29 19:34             ` Radu Grigore
2005-01-29 19:55               ` Radu Grigore
2005-01-29 21:05               ` Olivier Andrieu
2005-02-04 20:18               ` Radu Grigore

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