Release coccinelle-0.2.3rc2
[bpt/coccinelle.git] / commons / oassoc.ml
CommitLineData
91eba41f
C
1open Common
2
34e49164
C
3open Ocollection
4
5(* assoc, also called map or dictionnary *)
ae4735db 6class virtual ['a,'b] oassoc =
34e49164
C
7object(o: 'o)
8 inherit ['a * 'b] ocollection
ae4735db 9
34e49164
C
10 method virtual assoc: 'a -> 'b
11 method virtual delkey: 'a -> 'o
ae4735db 12
34e49164 13 (* pre: must be in *)
ae4735db
C
14 method replkey: ('a * 'b) -> 'o =
15 fun (k,v) -> o#add (k,v)
34e49164
C
16
17 (* pre: must not be in *)
18 (* method add: ('a * 'b) -> 'o = *)
19
91eba41f 20 (*
ae4735db 21 method keys =
91eba41f
C
22 List.map fst (o#tolist)
23 *)
24 method virtual keys: 'a list (* or 'a oset ? *)
34e49164 25
ae4735db 26 method find: 'a -> 'b = fun k ->
34e49164
C
27 o#assoc k
28
ae4735db
C
29 method find_opt: 'a -> 'b option = fun k ->
30 try
113803cf
C
31 let res = o#assoc k in
32 Some res
33 with Not_found -> None
34
ae4735db
C
35 method haskey: 'a -> bool = fun k ->
36 try (ignore(o#assoc k); true)
34e49164 37 with Not_found -> false
ae4735db
C
38
39 method apply: 'a -> ('b -> 'b) -> 'o = fun k f ->
40 let old = o#assoc k in
34e49164
C
41 o#replkey (k, f old)
42
43 (* apply default, assoc_default, take in class parameters a default value *)
ae4735db
C
44 method apply_with_default: 'a -> ('b -> 'b) -> (unit -> 'b) -> 'o =
45 fun k f default ->
46 let old =
34e49164
C
47 try o#assoc k
48 with Not_found -> default ()
49 in
50 o#replkey (k, f old)
51
ae4735db 52 method apply_with_default2 = fun k f default ->
91eba41f 53 o#apply_with_default k f default +> ignore
ae4735db 54
91eba41f 55
34e49164 56end