| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
type t = int |
| 12 |
|
| 13 |
external equal : int -> int -> bool = "%eq" |
| 14 |
|
| 15 |
external compare : int -> int -> int = "caml_int_compare" |
| 16 |
|
| 17 |
let hash i = i land 0x3FFFFFFF |
| 18 |
|
| 19 |
module Self = |
| 20 |
struct |
| 21 |
type t = int |
| 22 |
let compare = compare |
| 23 |
end |
| 24 |
|
| 25 |
module Set = Set.Make(Self) |
| 26 |
module Map = |
| 27 |
struct |
| 28 |
include CMap.Make(Self) |
| 29 |
|
| 30 |
type 'a map = 'a CMap.Make(Self).t |
| 31 |
|
| 32 |
type 'a _map = |
| 33 |
| MEmpty |
| 34 |
| MNode of {l:'a map; v:int; d:'a; r:'a map; h:int} |
| 35 |
|
| 36 |
let map_prj : 'a map -> 'a _map = Obj.magic |
| 37 |
let map_inj : 'a _map -> 'a map = Obj.magic |
| 38 |
|
| 39 |
let rec find i s = match map_prj s with |
| 40 |
| MEmpty -> raise Not_found |
| 41 |
| MNode {l; v; d; r; h} -> |
| 42 |
if i < v then find i l |
| 43 |
else if i = v then d |
| 44 |
else find i r |
| 45 |
|
| 46 |
let rec get i s = match map_prj s with |
| 47 |
| MEmpty -> assert false |
| 48 |
| MNode {l; v; d; r; h} -> |
| 49 |
if i < v then get i l |
| 50 |
else if i = v then d |
| 51 |
else get i r |
| 52 |
|
| 53 |
let rec find_opt i s = match map_prj s with |
| 54 |
| MEmpty -> None |
| 55 |
| MNode {l; v; d; r; h} -> |
| 56 |
if i < v then find_opt i l |
| 57 |
else if i = v then Some d |
| 58 |
else find_opt i r |
| 59 |
|
| 60 |
let rec set k v (s : 'a map) : 'a map = match map_prj s with |
| 61 |
| MEmpty -> raise Not_found |
| 62 |
| MNode {l; v=k'; d=v'; r; h} -> |
| 63 |
if k < k' then |
| 64 |
let l' = set k v l in |
| 65 |
if l == l' then s |
| 66 |
else map_inj (MNode {l=l'; v=k'; d=v'; r; h}) |
| 67 |
else if k = k' then |
| 68 |
if v' == v then s |
| 69 |
else map_inj (MNode {l; v=k'; d=v; r; h}) |
| 70 |
else |
| 71 |
let r' = set k v r in |
| 72 |
if r == r' then s |
| 73 |
else map_inj (MNode {l; v=k'; d=v'; r=r'; h}) |
| 74 |
|
| 75 |
let rec modify k f (s : 'a map) : 'a map = match map_prj s with |
| 76 |
| MEmpty -> raise Not_found |
| 77 |
| MNode {l; v; d; r; h} -> |
| 78 |
if k < v then |
| 79 |
let l' = modify k f l in |
| 80 |
if l == l' then s |
| 81 |
else map_inj (MNode {l=l'; v; d; r; h}) |
| 82 |
else if k = v then |
| 83 |
let d' = f v d in |
| 84 |
if d' == d then s |
| 85 |
else map_inj (MNode {l; v; d=d'; r; h}) |
| 86 |
else |
| 87 |
let r' = modify k f r in |
| 88 |
if r == r' then s |
| 89 |
else map_inj (MNode {l; v; d; r=r'; h}) |
| 90 |
|
| 91 |
end |
| 92 |
|
| 93 |
module List = struct |
| 94 |
let mem = List.memq |
| 95 |
let assoc = List.assq |
| 96 |
let mem_assoc = List.mem_assq |
| 97 |
let remove_assoc = List.remove_assq |
| 98 |
end |
| 99 |
|
| 100 |
let min (i : int) j = if i < j then i else j |
| 101 |
|
| 102 |
|
| 103 |
let rec next from upto = |
| 104 |
if from < upto then next (2 * from + 1) upto |
| 105 |
else from |
| 106 |
|
| 107 |
|
| 108 |
module PArray = |
| 109 |
struct |
| 110 |
|
| 111 |
type 'a t = 'a data ref |
| 112 |
and 'a data = |
| 113 |
| Root of 'a option array |
| 114 |
| DSet of int * 'a option * 'a t |
| 115 |
|
| 116 |
let empty n = ref (Root (Array.make n None)) |
| 117 |
|
| 118 |
let rec rerootk t k = match !t with |
| 119 |
| Root _ -> k () |
| 120 |
| DSet (i, v, t') -> |
| 121 |
let next () = match !t' with |
| 122 |
| Root a as n -> |
| 123 |
let v' = Array.unsafe_get a i in |
| 124 |
let () = Array.unsafe_set a i v in |
| 125 |
let () = t := n in |
| 126 |
let () = t' := DSet (i, v', t) in |
| 127 |
k () |
| 128 |
| DSet _ -> assert false |
| 129 |
in |
| 130 |
rerootk t' next |
| 131 |
|
| 132 |
let reroot t = rerootk t (fun () -> ()) |
| 133 |
|
| 134 |
let get t i = |
| 135 |
let () = assert (0 <= i) in |
| 136 |
match !t with |
| 137 |
| Root a -> |
| 138 |
if Array.length a <= i then None |
| 139 |
else Array.unsafe_get a i |
| 140 |
| DSet _ -> |
| 141 |
let () = reroot t in |
| 142 |
match !t with |
| 143 |
| Root a -> |
| 144 |
if Array.length a <= i then None |
| 145 |
else Array.unsafe_get a i |
| 146 |
| DSet _ -> assert false |
| 147 |
|
| 148 |
let set t i v = |
| 149 |
let () = assert (0 <= i) in |
| 150 |
let () = reroot t in |
| 151 |
match !t with |
| 152 |
| DSet _ -> assert false |
| 153 |
| Root a as n -> |
| 154 |
let len = Array.length a in |
| 155 |
if i < len then |
| 156 |
let old = Array.unsafe_get a i in |
| 157 |
if old == v then t |
| 158 |
else |
| 159 |
let () = Array.unsafe_set a i v in |
| 160 |
let res = ref n in |
| 161 |
let () = t := DSet (i, old, res) in |
| 162 |
res |
| 163 |
else match v with |
| 164 |
| None -> t |
| 165 |
| Some _ -> |
| 166 |
let nlen = next len (succ i) in |
| 167 |
let nlen = min nlen Sys.max_array_length in |
| 168 |
let () = assert (i < nlen) in |
| 169 |
let a' = Array.make nlen None in |
| 170 |
let () = Array.blit a 0 a' 0 len in |
| 171 |
let () = Array.unsafe_set a' i v in |
| 172 |
let res = ref (Root a') in |
| 173 |
let () = t := DSet (i, None, res) in |
| 174 |
res |
| 175 |
|
| 176 |
end |
| 177 |
|
| 178 |
module PMap = |
| 179 |
struct |
| 180 |
|
| 181 |
type key = int |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
type 'a t = Empty | Map of Set.t * 'a PArray.t |
| 189 |
|
| 190 |
let empty = Empty |
| 191 |
|
| 192 |
let is_empty = function |
| 193 |
| Empty -> true |
| 194 |
| Map _ -> false |
| 195 |
|
| 196 |
let singleton k x = |
| 197 |
let len = next 19 (k + 1) in |
| 198 |
let len = min Sys.max_array_length len in |
| 199 |
let v = PArray.empty len in |
| 200 |
let v = PArray.set v k (Some x) in |
| 201 |
let s = Set.singleton k in |
| 202 |
Map (s, v) |
| 203 |
|
| 204 |
let add k x = function |
| 205 |
| Empty -> singleton k x |
| 206 |
| Map (s, v) -> |
| 207 |
let s = match PArray.get v k with |
| 208 |
| None -> Set.add k s |
| 209 |
| Some _ -> s |
| 210 |
in |
| 211 |
let v = PArray.set v k (Some x) in |
| 212 |
Map (s, v) |
| 213 |
|
| 214 |
let remove k = function |
| 215 |
| Empty -> Empty |
| 216 |
| Map (s, v) -> |
| 217 |
let s = Set.remove k s in |
| 218 |
if Set.is_empty s then Empty |
| 219 |
else |
| 220 |
let v = PArray.set v k None in |
| 221 |
Map (s, v) |
| 222 |
|
| 223 |
let mem k = function |
| 224 |
| Empty -> false |
| 225 |
| Map (_, v) -> |
| 226 |
match PArray.get v k with |
| 227 |
| None -> false |
| 228 |
| Some _ -> true |
| 229 |
|
| 230 |
let find k = function |
| 231 |
| Empty -> raise Not_found |
| 232 |
| Map (_, v) -> |
| 233 |
match PArray.get v k with |
| 234 |
| None -> raise Not_found |
| 235 |
| Some x -> x |
| 236 |
|
| 237 |
let iter f = function |
| 238 |
| Empty -> () |
| 239 |
| Map (s, v) -> |
| 240 |
let iter k = match PArray.get v k with |
| 241 |
| None -> () |
| 242 |
| Some x -> f k x |
| 243 |
in |
| 244 |
Set.iter iter s |
| 245 |
|
| 246 |
let fold f m accu = match m with |
| 247 |
| Empty -> accu |
| 248 |
| Map (s, v) -> |
| 249 |
let fold k accu = match PArray.get v k with |
| 250 |
| None -> accu |
| 251 |
| Some x -> f k x accu |
| 252 |
in |
| 253 |
Set.fold fold s accu |
| 254 |
|
| 255 |
let exists f m = match m with |
| 256 |
| Empty -> false |
| 257 |
| Map (s, v) -> |
| 258 |
let exists k = match PArray.get v k with |
| 259 |
| None -> false |
| 260 |
| Some x -> f k x |
| 261 |
in |
| 262 |
Set.exists exists s |
| 263 |
|
| 264 |
let for_all f m = match m with |
| 265 |
| Empty -> true |
| 266 |
| Map (s, v) -> |
| 267 |
let for_all k = match PArray.get v k with |
| 268 |
| None -> true |
| 269 |
| Some x -> f k x |
| 270 |
in |
| 271 |
Set.for_all for_all s |
| 272 |
|
| 273 |
let cast = function |
| 274 |
| Empty -> Map.empty |
| 275 |
| Map (s, v) -> |
| 276 |
let bind k = match PArray.get v k with |
| 277 |
| None -> assert false |
| 278 |
| Some x -> x |
| 279 |
in |
| 280 |
Map.bind bind s |
| 281 |
|
| 282 |
let domain = function |
| 283 |
| Empty -> Set.empty |
| 284 |
| Map (s, _) -> s |
| 285 |
|
| 286 |
end |