File: join_hash.ml

package info (click to toggle)
jocaml 4.01.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,736 kB
  • ctags: 23,836
  • sloc: ml: 111,262; ansic: 32,746; sh: 6,057; lisp: 4,230; makefile: 3,861; asm: 3,734; awk: 88; perl: 45; fortran: 21; sed: 19; cs: 9
file content (71 lines) | stat: -rw-r--r-- 2,477 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(***********************************************************************)
(*                                                                     *)
(*                           Objective Caml                            *)
(*                                                                     *)
(*            Luc Maranget, projet Moscova, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 2005 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(* $Id: join_hash.ml 7746 2006-11-20 16:47:41Z maranget $ *)

open Join_misc

type ('a,'b) t =
  {
    add : ('a * 'b) -> unit ;
    add_once : ('a * 'b) -> 'b option ;
    find : 'a -> 'b ;
    find_remove : 'a -> 'b ;
    iter : ('a -> 'b -> unit) -> unit ;
    iter_empty : ('a -> 'b -> unit) -> unit ;
    remove : 'a -> unit ;
    perform : 'a -> 'b -> ('b -> 'b) -> unit ;
  } 

let create () =
  let t =  Hashtbl.create 17
  and c = controler_create () in
  {
    add = protect_write c (fun (key,v) -> Hashtbl.add t key v) ;
    add_once =
      protect_write c
        (fun (key,v) ->
          try
            Some (Hashtbl.find t key)
          with
          | Not_found ->
              Hashtbl.add t key v ;
              None) ;
    find = protect_read c (fun key -> Hashtbl.find t key) ;
    find_remove =
     protect_write c
       (fun key ->
	 let r = Hashtbl.find t key in
	 Hashtbl.remove t key ;
	 r) ;
   iter = protect_read c
     (fun do_it -> Hashtbl.iter do_it t) ;
   iter_empty = protect_write c
     (fun do_it -> Hashtbl.iter do_it t ; Hashtbl.clear t) ;
   remove = protect_write c
     (fun key -> Hashtbl.remove t key) ;
   perform  = protect_write c
     (fun key default perf ->
       try
	 Hashtbl.replace t key (perf (Hashtbl.find t key))
       with Not_found -> Hashtbl.replace t key (perf default))
  } 


let add t key v = t.add (key, v)
and add_once t key v = t.add_once (key, v)
and find t key = t.find key
and find_remove t key = t.find_remove key
and iter t do_it = t.iter do_it
and iter_empty t do_it = t.iter_empty do_it
and remove t key = t.remove key
and perform t key default perf = t.perform key default perf