Source

cadastr / src / cd_SortedArray.ml

Full commit
(* реализация пока очень дубовая, но использует уже написанный код. *)

module S = Cd_SortedArraySet.SortedArraySet
;

type t 'k = S.t 'k ('k * int)
;

value (create : ('k -> 'k -> int) -> array 'k -> t 'k) cmpf src =
(*
  let cmpf' = fun (a, _ai) (b, _bi) -> cmpf a b in
*)
  let src_copied = Array.copy src in
  let () = Array.sort cmpf src_copied in
  let src_copied_indexed = Array.mapi (fun i x -> (x, i)) src_copied in
  let r = S.create fst cmpf in
  ( S.replace_with_array r src_copied_indexed
  ; r
  )
;

value (lookup_index_opt : t 'k -> 'k -> option int) a k =
  match S.lookup_opt a k with
  [ None -> None
  | Some (_k, i) -> Some i
  ]
;


value get_keys = S.get_keys
;


exception Intersects
;


value intersects first second =
  try
    ( S.iter_diff
        ~both:(fun _k _v1 _v2 -> raise Intersects)
        ~first ~second
    ; False
    )
  with
  [ Intersects -> True ]
;