|
From: Amit D. <ami...@gm...> - 2006-06-15 15:46:09
|
I actually have similar code, but I tend to use it over floats more than
ints. Maybe this calls for a functor-based design?
module type NUMBER =
sig
type t
val zero : t
val unit : t
val add : t -> t -> t
val negate : t -> t
end
module Counter( N : NUMBER ) =
struct
let get_ref counter thing =
try
Hashtbl.find counter thing
with
Not_found ->
let r = ref N.zero in
Hashtbl.add counter thing r;
r
let incr counter thing =
let r = get_ref counter thing in
r := N.add !r N.one
(...etc...)
end
On 6/9/06, Richard Jones < ri...@an...> wrote:
>
>
> FWIW I wrote a simple Counter module, based on Hashtbl, which I find
> useful. I wonder if this would be a good addition to Extlib?
>
> Rich.
>
> ----------------------------------------------------------------------
> (** Basic counting module.
> * $Id: counter.mli,v 1.2 2006/06/08 15:24:47 rich Exp $
> *)
>
> type 'a t
> (** Count items of type ['a]. *)
>
> val create : unit -> 'a t
> (** Create a new counter. *)
>
> val incr : 'a t -> 'a -> unit
> (** [incr counter thing] adds one to the count of [thing]s in [counter].
> *)
>
> val decr : 'a t -> 'a -> unit
> (** [decr counter thing] subtracts one from the count of [thing]s in
> [counter]. *)
>
> val add : 'a t -> 'a -> int -> unit
> (** [add counter thing n] adds [n] to the count of [thing]s in [counter].
> *)
>
> val sub : 'a t -> 'a -> int -> unit
> (** [sub counter thing n] subtracts [n] from the count of [thing]s in
> [counter]. *)
>
> val set : 'a t -> 'a -> int -> unit
> (** [set counter thing n] sets the count of [thing]s to [n]. *)
>
> val get : 'a t -> 'a -> int
> (** [get counter thing] returns the count of [thing]s. *)
>
> val read : 'a t -> (int * 'a) list
> (** [read counter] reads the frequency of each thing. They are sorted
> * with the thing appearing most frequently first.
> *)
>
> val length : 'a t -> int
> (** Return the number of distinct things. See also {!Counter.total} *)
>
> val total : 'a t -> int
> (** Return the number of things counted (the total number of counts).
> * See also {!Counter.length}
> *)
>
> val clear : 'a t -> unit
> (** [clear counter] clears the counter. *)
> ----------------------------------------------------------------------
> (* Basic counting module.
> * $Id: counter.ml,v 1.2 2006/06/08 15:24:47 rich Exp $
> *)
>
> type 'a t = ('a, int ref) Hashtbl.t
>
> let create () =
> Hashtbl.create 13
>
> let get_ref counter thing =
> try
> Hashtbl.find counter thing
> with
> Not_found ->
> let r = ref 0 in
> Hashtbl.add counter thing r;
> r
>
> let incr counter thing =
> let r = get_ref counter thing in
> incr r
>
> let decr counter thing =
> let r = get_ref counter thing in
> decr r
>
> let add counter thing n =
> let r = get_ref counter thing in
> r := !r + n
>
> let sub counter thing n =
> let r = get_ref counter thing in
> r := !r - n
>
> let set counter thing n =
> let r = get_ref counter thing in
> r := n
>
> let get counter thing =
> try
> !(Hashtbl.find counter thing)
> with
> Not_found -> 0
>
> let read counter =
> let counts =
> Hashtbl.fold (fun thing r xs -> (!r, thing) :: xs) counter [] in
> List.sort (fun (a, _) (b, _) -> compare (b : int) (a : int)) counts
>
> let length = Hashtbl.length
>
> let total counter =
> let total = ref 0 in
> Hashtbl.iter (fun _ r -> total := !total + !r) counter;
> !total
>
> let clear counter =
> Hashtbl.clear counter
> ----------------------------------------------------------------------
>
> --
> Richard Jones, CTO Merjis Ltd.
> Merjis - web marketing and technology - http://merjis.com
> Team Notepad - intranets and extranets for business -
> http://team-notepad.com
>
>
> _______________________________________________
> ocaml-lib-devel mailing list
> oca...@li...
> https://lists.sourceforge.net/lists/listinfo/ocaml-lib-devel
>
|