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; 2+ 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] 2+ 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; 2+ 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] 2+ messages in thread

end of thread, other threads:[~2005-01-30 12:15 UTC | newest]

Thread overview: 2+ 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

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