1 (* Basic counting module.
3 Copyright (C) 2006 Merjis Ltd.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 type 'a t = ('a, int ref) Hashtbl.t
25 let get_ref counter thing =
27 Hashtbl.find counter thing
31 Hashtbl.add counter thing r;
34 let incr counter thing =
35 let r = get_ref counter thing in
38 let decr counter thing =
39 let r = get_ref counter thing in
42 let add counter thing n =
43 let r = get_ref counter thing in
46 let sub counter thing n =
47 let r = get_ref counter thing in
50 let set counter thing n =
51 let r = get_ref counter thing in
54 (* Don't use get_ref, to avoid unnecessarily creating 'ref 0's. *)
55 let get counter thing =
57 !(Hashtbl.find counter thing)
61 (* This is a common pair of operations, worth optimising. *)
62 let incr_get counter thing =
63 let r = get_ref counter thing in
67 let zero = Hashtbl.remove
74 if r <> 0 then (r, thing) :: xs
77 List.sort (fun (a, _) (b, _) -> compare (b : int) (a : int)) counts
79 let length = Hashtbl.length
83 Hashtbl.iter (fun _ r -> total := !total + !r) counter;
86 let clear = Hashtbl.clear