-
Notifications
You must be signed in to change notification settings - Fork 125
/
simpleListMap.ml
81 lines (60 loc) · 2.03 KB
/
simpleListMap.ml
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
72
73
74
75
76
77
78
79
80
81
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* -- Normal log -- *)
let debug fmt =
#<If:DEBUG_DB$minlevel 5> Printf.fprintf stdout ("[37m[ListMap][0m"^^fmt^^"\n%!")
#<Else> Printf.ifprintf stderr ("[37m[ListMap][0m"^^fmt^^"\n%!")
#<End>
let print fmt = Logger.info ("[ListMap] "^^fmt)
let error fmt = Logger.error ("[ListMap] "^^fmt)
(* -- *)
type key = Revision.t
type 'a t = (Revision.t * 'a) list
let empty = []
let is_empty = BaseList.is_empty
let add k v lst =
match lst with
| [] -> [k,v]
| (kk,_)::tl ->
(match Revision.compare k kk with
| 0 -> (k,v) :: tl
| 1 -> (k,v) :: lst
| -1 ->
(debug "Try to insert an older revision : %s, head is %s"
(Revision.to_string k) (Revision.to_string kk);
BaseList.insert_sorted ~cmp:(fun (a,_) (b,_) -> (Revision.compare a b) * -1) ~conflict:(fun a _ -> [a]) (k,v) lst
)
| _ -> assert false)
let fold f lst acc =
let f = fun a (k,v) -> f k v a in
List.fold_left f acc lst
let iter f lst =
let f = fun (k,v) -> f k v in
List.iter f lst
let rev_iter f lst =
iter f (List.rev lst)
let find = List.assoc
let find_inf key lst =
let rec aux = function
| [] -> raise Not_found
| (k,v)::y ->
if Revision.compare k key > 0 then aux y
else k,v
in aux lst
let size = List.length
let max = function
| [] -> raise Not_found
| x::_ -> x
let keys lst = fst (List.split lst)
let remove_last = List.tl