1 (* Basic counting module.
2 * (C) Copyright 2003-2006 Merjis Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * 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