Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
array.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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
include Stdlib.Array type 'a t = 'a array let is_empty (xs: 'a t): bool = length xs = 0 let valid_index (i: int) (xs: 'a t): bool = 0 <= i && i < length xs let has_some (xs: 'a t): bool = 0 < length xs let first (xs: 'a t): 'a = assert (has_some xs); get xs 0 let last (xs: 'a t): 't = let len = length xs in assert (0 < len); get xs (len - 1) let foldi_left (f: 'a -> int -> 'b -> 'a) (start: 'a) (xs: 'b t): 'a = fold_left (fun (start, idx) e -> f start idx e, idx + 1) (start, 0) xs |> fst let push (x: 'a) (xs: 'a array): 'a array = let len = length xs in let xs_new = make (len + 1) x in blit xs 0 xs_new 0 len; xs_new let push_front (x: 'a) (xs: 'a array): 'a array = let len = length xs in let xs_new = make (len + 1) x in blit xs 0 xs_new 1 len; xs_new let insert (i: int) (x: 'a) (xs: 'a array): 'a array = assert (0 <= i); assert (i <= length xs); (* 0 1 ... (i-1) i ... (len-1) ^ insert here *) let len = length xs in let arr = make (len + 1) x in blit xs 0 arr 0 i; blit xs i arr (i + 1) (len - i); arr let replace (i: int) (x: 'a) (xs: 'a array): 'a array = assert (0 <= i); assert (i < length xs); (* 0 1 ... i ... (len-1) ^ replace *) if get xs i == x then xs else begin let arr = copy xs in set arr i x; arr end let remove (i: int) (xs: 'a array): 'a array = assert (0 <= i); assert (i < length xs); (* 0 1 ... i (i + 1) ... (len-1) ^ remove *) let len = length xs in assert (0 < len); let arr = make (len - 1) (get xs 0) in blit xs 0 arr 0 i; blit xs (i + 1) arr i (len - (i + 1)); arr let remove_first (xs: 'a array): 'a array = let len = length xs in assert (0 < len); sub xs 1 (len - 1) let remove_last (xs: 'a array): 'a array = let len = length xs in assert (0 < len); sub xs 0 (len - 1) let find (p: 'a -> bool) (arr: 'a array): int option = let len = length arr in let rec find_from i = if i = len then None else if p (get arr i) then Some i else find_from (i + 1) in find_from 0 let binsearch (compare: 'key -> 'key -> int) (key_of: 'a -> 'key) (key: 'key) (arr: 'a array) : int * bool = (* Search the position of [key] in [arr] which is sorted without duplicates. Result: i, exact_flag with [key <= project arr.(i)] If [exact_flag] is set, the key in position [i] is exactly [key], otherwise the key in position [i] is strictly greater than [key]. Corner case: [i = length arr, exact_flag = false]. This corresponds to a fictitious key of [+ infinity] at position [length arr]. Precondition: The array must be sorted and does not contain duplicates. *) let len = length arr in if len = 0 then len, false else if len = 1 then let cmp = compare key (get arr 0 |> key_of) in if cmp <= 0 then 0, cmp = 0 else len, false else let rec search lower upper = (* Invariant: 0 <= lower < upper < len arr.(lower) < key < arr.(upper) *) if lower + 1 = upper then upper, false else let mid = lower + (upper - lower) / 2 in assert (lower < mid); assert (mid < upper); let cmp = compare key (get arr mid |> key_of) in if cmp = 0 then mid, true else if cmp < 0 then search lower mid else search mid upper in let lower, upper = 0, len - 1 in let cmp = compare key (get arr lower |> key_of) in if cmp <= 0 then (* key is less or equal the first element *) lower, cmp = 0 else (* key is greater than the first element *) let cmp = compare key (get arr upper |> key_of) in if cmp < 0 then (* invariant for [search] satisfied. *) search lower upper else if cmp = 0 then (* exact match with the last element *) upper, true else (* key is greater than all elements *) len, false module Map (Key: Interfaces.SORTABLE) = struct type key = Key.t type 'a t = (Key.t * 'a) array let cardinal (map: 'a t): int = length map let is_empty (map: 'a t): bool = cardinal map = 0 let bindings (map: 'a t): (Key.t * 'a) list = to_list map let fold_left (f: 'a -> Key.t -> 'b -> 'a) (start: 'a) (map: 'b t): 'a = Stdlib.Array.fold_left (fun a (key, value) -> f a key value) start map let fold_right (f: 'a -> Key.t -> 'b -> 'a) (start: 'a) (map: 'b t) = Stdlib.Array.fold_right (fun (key, value) result -> f result key value) map start let index_of (key: Key.t) (map: 'a t): int option = let len = length map in let i, exact = binsearch Key.compare fst key map in if i = len || not exact then None else Some i let pair (i: int) (map: 'a t): Key.t * 'a = assert (i < cardinal map); get map i let find_opt (key: Key.t) (map: 'a t): 'a option = Option.map (fun i -> snd (get map i)) (index_of key map) let mem (key: Key.t) (map: 'a t): bool = index_of key map <> None let empty: 'a t = [||] let singleton (key: Key.t) (value: 'a): 'a t = [| key, value |] let add (key: Key.t) (value: 'a) (map: 'a t): 'a t = let i,exact = binsearch Key.compare fst key map in if exact then replace i (key, value) map else insert i (key, value) map let update (key: Key.t) (f: 'a option -> 'a option) (map: 'a t): 'a t = let i, exact = binsearch Key.compare fst key map in if exact then match f (Some (get map i |> snd)) with | None -> remove i map | Some value -> replace i (key, value) map else match f None with | None -> map | Some value -> insert i (key, value) map let remove (key: Key.t) (map: 'a t): 'a t = let i, exact = binsearch Key.compare fst key map in if exact then remove i map else map end module Set (Key: Interfaces.SORTABLE) = struct module M = Map (Key) type item = Key.t type t = unit M.t let cardinal (set: t): int = M.cardinal set let is_empty = M.is_empty let fold_left (f: 'a -> Key.t -> 'a) (start: 'a) (set: t): 'a = M.fold_left (fun res key _ -> f res key) start set let fold_right (f: 'a -> Key.t -> 'a) (start: 'a) (set: t): 'a = M.fold_right (fun res key _ -> f res key) start set let elements (set: t): Key.t list = fold_right (fun lst key -> key :: lst) [] set let element (i: int) (set: t): Key.t = assert (0 <= i); assert (i < cardinal set); M.pair i set |> fst let index_of = M.index_of let empty = M.empty let singleton (e: Key.t): t = M.singleton e () let mem = M.mem let add (e: Key.t) (set: t): t = M.add e () set let remove = M.remove end (* Unit Tests * ========== *) (* Binary search *) let%test _ = binsearch Int.compare Fun.id 100 [||] = (0, false) let%test _ = binsearch Int.compare Fun.id 99 [|100|] = (0, false) let%test _ = binsearch Int.compare Fun.id 100 [|100|] = (0, true) let%test _ = binsearch Int.compare Fun.id 101 [|100|] = (1, false) (* Set *) module SetInt = Set (Int) let insert_downward (lower: int) (beyond: int) (set: SetInt.t): SetInt.t = let rec insert i set = if i = lower then set else let i = i - 1 in insert i (SetInt.add i set) in insert beyond set let insert_upward (lower: int) (beyond: int) (set: SetInt.t): SetInt.t = let rec insert i set = if i = beyond then set else insert (i + 1) (SetInt.add i set) in insert lower set let%test _ = insert_upward 0 3 SetInt.empty = [|0,(); 1,(); 2,()|] let%test _ = insert_downward 0 3 SetInt.empty = [|0,(); 1,(); 2,()|] let%test _ = insert_downward 0 3 SetInt.empty |> SetInt.remove 1 = [|0,(); 2,()|] let%test _ = insert_downward 0 3 SetInt.empty |> SetInt.remove 4 = [|0,(); 1,(); 2,()|] let%test _ = (insert_upward 0 3 SetInt.empty |> insert_downward 0 3) = [|0,(); 1,(); 2,()|] let%test _ = (insert_upward 0 3 SetInt.empty |> insert_downward 3 6) = [|0,(); 1,(); 2,(); 3,(); 4,(); 5,()|] let%test _ = (insert_downward 0 3 SetInt.empty |> SetInt.remove 0) = [|1,(); 2,()|] let%test _ = let set = insert_downward 0 3 SetInt.empty in SetInt.index_of 3 set = None let%test _ = let set = insert_downward 0 3 SetInt.empty in SetInt.index_of 0 set = Some 0