Skip to content
This repository
tag: v1559
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 505 lines (419 sloc) 16.251 kb
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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
(*
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/>.
*)
exception IteratorEnd

(** Big-Endian Particia tree maps, from "Fast Mergeable Integer Maps", Chris Okasaki, Andrew Gill
@author Louis Gesbert *)

(*In the comments, low means on the right, hence the bits whose value is
smaller. If a number is b_4b_3b_2b_1, then b_1 is the lowest bit and b_4 the highest
When we speak of "bit m" we will mean the bit k=log(m), and we assume
that m=2^k - hence m has only one 1 in his binary representation*)

(*Big-endian means that branches are shared for paths whose higher bits are
equal*)
type key = int

type 'a t =
  | Empty
  (*A tree with one element whose index is the int*)
  | Lf of int * 'a
  (*A tree with two subtree, we assume that the index of both subtree are
equal on bit higher than mask, and the bit at position mask is 0 on the
left subtree, and 1 on the right subtree.
We require that the m lower bits are 01111...11*)
  | Br of int (* prefix *) * int (* branching bit (mask) *) * 'a t * 'a t

let empty = Empty

let is_empty t = Empty = t

let singleton k v = Lf (k,v)

(** Sets bit m to 0, and all lower bit to 1*)
let mask i m = if m > 0 then (i lor (m-1)(*one-ing the bit at post lower than m*)) land (lnot m) (*zeroing bit m*)else 0

(**is bit mth equal to 0*)
let zerobit i m = (0 = i land m)

(* Put all bits to 0 from bit m leftwards until we found the highest bit*)
(**The highest bit, assuming there is one at least at bit m, else 0*)
let highestbit i m =
  let rec aux i =
    let m = i land (-i) in (* lowest bit of i *)
    if i = m (* If i has only one bit, which is the lowest one, or i=0*)
    then i else aux (i - m (*i without its lowest bit*))
  in aux (i land (lnot (m - 1))
            (*bits at position strictly lower than m are zeroed*))

(** Highest bit where p1 and p2 differ at bit m or higher, else 0*)
let branchingbit p1 p2 m = highestbit (p1 lxor p2) m

(** A tree whith two subtrees, t1 and t2, whose indexes, p1 and p2.
We assume that they differ only at position m or higher*)
let join p1 t1 p2 t2 m =
  (*the position of the highest difference at bit m or higher*)
  let m = branchingbit p1 p2 m in
  (*if the first different bit is 0 in p1*)
  if zerobit p1 m
  then Br (mask p1 m, m, t1, t2)
  else Br (mask p1 m, m, t2, t1)


let rec add k v t = match t with
  | Lf (k',_v') ->
      if k = k' then Lf (k, v)
      else join k (Lf (k, v)) k' t 1
  | Br (p,m,t1,t2) when mask k m = p ->
      (*if k and m are equal on bit strictly higher than m*)
      if zerobit k m
      then Br (p, m, add k v t1, t2)
      else Br (p, m, t1, add k v t2)
  | Br (p,m,_t1,_t2) ->
      (*There is a difference on bit m or an higher bit*)
      join k (Lf (k, v)) p t (m lsl 1)
  | Empty -> Lf (k, v)

let replace k rep t =
  let rec aux = function
    | Lf (k', v') ->
        if k = k' then Lf (k, rep (Some v'))
        else join k (Lf (k, rep None)) k' t 1
    | Br (p, m, t1, t2) when mask k m = p ->
        if zerobit k m
        then Br (p, m, aux t1, t2)
        else Br (p, m, t1, aux t2)
    | Br (p, m, _t1, _t2) ->
        join k (Lf (k, rep None)) p t (m lsl 1)
    | Empty -> Lf (k, rep None)
  in
  aux t

let rec add_fi f k v t = match t with
  | Lf (k',v') ->
      if k = k' then Lf (k, f k v v')
      else join k (Lf (k,v)) k' t 1
  | Br (p,m,t1,t2) when mask k m = p ->
      if zerobit k m
      then Br (p, m, add_fi f k v t1, t2)
      else Br (p, m, t1, add_fi f k v t2)
  | Br (p,m,_t1,_t2) ->
      join k (Lf (k, v)) p t (m lsl 1)
  | Empty -> Lf (k,v)

let rec safe_add k v t =
  add_fi (fun _ _ -> raise (Invalid_argument "Base.Map.safe_add")) k v t

let rec find_opt k t = match t with
  | Br (_,m,tl,tr) -> if zerobit k m then find_opt k tl else find_opt k tr
  | Lf (k',v) -> if k = k' then Some v else None
  | Empty -> None

let rec findi_opt k t = match find_opt k t with
  | Some v -> Some (k, v)
  | None -> None

let find k t = match find_opt k t with Some v -> v | None -> raise Not_found
let findi k t = match find_opt k t with Some v -> k,v | None -> raise Not_found

let rec remove k t = match t with
  | Br (_,_,Lf(k',_v),t') when k' = k -> t'
  | Br (_,_,t',Lf(k',_v)) when k' = k -> t'
  | Br (p,m,t1,t2) ->
      if zerobit k m
      then Br(p,m,remove k t1,t2)
      else Br(p,m,t1,remove k t2)
  | Lf (k',_v) when k = k' -> Empty
  | n -> n

(* on_miss specify what to do when the key is not found
take the part of intmap that should contains the key *)
let rec update_gen k f t ~on_miss = match t with
  | Br (p,m,tl,tr) ->
      if zerobit k m
      then Br (p,m,update_gen k f tl ~on_miss,tr)
      else Br (p,m,tl,update_gen k f tr ~on_miss)
  | Lf (k',v) when k=k' -> Lf (k,f v)
  | n -> on_miss n

let raise_Not_found _ = raise Not_found
let update k f t = update_gen k f t ~on_miss:raise_Not_found
let update_default k f default t = update_gen k f t ~on_miss:(fun m -> add k default m)

let rec mem k t = match t with
  | Br (_,m,tl,tr) -> if zerobit k m then mem k tl else mem k tr
  | Lf (k',_v) when k=k' -> true
  | _ -> false

(* We want some functions to be ordered. The patricia tree is ordered by
lexicographical order of bits, that is 0 < 1 < max_int < min_int < -1.
The only problem may be the first node when m = 0b1000..., ie m = min_int
(<=> m < 0).
So we embed the functions with the following hack, only modifying the
top-level call (so that the cost is negligible) ; it breaks the tree, but
returns a prefix suitable for integer comparison. *)
let ordertop f t = match t with
  | Br (_p,m,tl,tr) -> if m < 0 then f (Br (-1, m, tr, tl)) else f t
  | t -> f t

(* "repairs" the root of t' according to t, to call after ordertop for map-like functions *)
let ordertop_ret t t' = match t, t' with
  | Br (p,m,_,_), Br (_,_,tl,tr) ->
      if m < 0 then Br (p, m, tr, tl) else t'
  | _, _ -> t'

let ordertop_and_ret f t = ordertop_ret t (ordertop f t)

let rec iter f t = match t with
  | Br (_,_,tl,tr) -> iter f tl; iter f tr
  | Lf (k,v) -> f k v
  | Empty -> ()
let iter f = ordertop (iter f)

let rec rev_iter f t = match t with
  | Br (_,_,tl,tr) -> rev_iter f tr; rev_iter f tl
  | Lf (k,v) -> f k v
  | Empty -> ()
let rev_iter f = ordertop (rev_iter f)

let rec map f t = match t with
  | Br (p,m,tl,tr) -> Br (p, m, map f tl, map f tr)
  | Lf (k,v) -> Lf (k, f v)
  | Empty -> Empty
let map f = ordertop_and_ret (map f)

let rec mapi f t = match t with
  | Br (p,m,tl,tr) -> Br (p, m, mapi f tl, mapi f tr)
  | Lf (k,v) -> Lf (k, f k v)
  | Empty -> Empty
let mapi f = ordertop_and_ret (mapi f)

let rec fold f t acc = match t with
  | Br (_,_,tl,tr) -> fold f tr (fold f tl acc)
  | Lf (k,v) -> f k v acc
  | Empty -> acc
let fold f t acc = ordertop (fun t -> fold f t acc) t

let fold_range_compare _ = assert false

let rec fold_range f t k1 k2 acc = match t with
  | Br (p,_,tl,tr) ->
      let acc = if k1 <= p then fold_range f tl k1 k2 acc else acc in
      if p < k2 then fold_range f tr k1 k2 acc else acc
  | Lf (k,v) -> if k1 <= k && k <= k2 then f k v acc else acc
  | Empty -> acc
let fold_range f t k1 k2 acc = ordertop (fun t -> fold_range f t k1 k2 acc) t

let fold_length ~start:_ ~length:_ = assert false (* TODO as soon as needed *)

let rec fold_rev f t acc = match t with
  | Br (_,_,tl,tr) -> fold_rev f tl (fold_rev f tr acc)
  | Lf (k,v) -> f k v acc
  | Empty -> acc
let fold_rev f t acc = ordertop (fun t -> fold_rev f t acc) t

let rec fold_map f t acc = match t with
  | Br (p,m,tl,tr) ->
      let acc, tl = fold_map f tl acc in
      let acc, tr = fold_map f tr acc in
      acc, Br (p, m, tl, tr)
  | Lf (k,v) ->
      let acc, v = f k v acc in acc, Lf (k, v)
  | Empty -> acc, Empty
let fold_map f t acc =
  let acc, t' = ordertop (fun t -> fold_map f t acc) t in
  acc, ordertop_ret t t'


let filter_val f t =
  fold (fun k v acc -> if f v then add k v acc else acc) t empty

let filter_keys f t =
  fold (fun k v acc -> if f k then add k v acc else acc) t empty

let rec compare f t1 t2 = match t1, t2 with
  | Br (p1,m1,tl1,tr1), Br (p2,m2,tl2,tr2) ->
      let k = Pervasives.compare (p1,m1) (p2,m2) in
      if k <> 0 then k else
        let k = compare f tl1 tl2 in
        if k <> 0 then k
        else compare f tr1 tr2
  | Lf (k1,v1), Lf (k2, v2) ->
      let k = Pervasives.compare k1 k2 in
      if k <> 0 then k
      else f v1 v2
  | Empty, Empty -> 0
  | Br _, Lf _ | Br _, Empty | Lf _, Empty -> -1
  | Lf _, Br _ | Empty, Br _ | Empty, Lf _ -> 1

let rec equal f t1 t2 = match (t1, t2) with
  | Br (p1,m1,tl1,tr1), Br (p2,m2,tl2,tr2) ->
      p1 = p2 && m1 = m2 && equal f tl1 tl2 && equal f tr1 tr2
  | Lf (k1,v1), Lf (k2, v2) ->
      k1 = k2 && f v1 v2
  | Empty, Empty -> true
  | _, _ -> false

module Iter : (IterSig.S with type +'a element = int * 'a and type +'a structure = 'a t) = struct
  type 'a structure = 'a t
  type 'a element = int * 'a
  type 'a t = ('a structure) list

  let rec make_aux acc = function
    | Empty -> acc
    | Br (_,_,tl,tr) -> make_aux (tr :: acc) tl
    | Lf (k,v) -> Lf (k,v) :: acc

  let make m = make_aux [] m

  let get = function
    | Lf (k,v) :: _l -> k, v
    | [] -> raise IteratorEnd
    | _ -> assert false

  let next = function
    | Lf (_k, _v) :: Br (_,_,tl,tr) :: l -> make_aux (tr :: l) tl
    | Lf (_k, _v) :: l -> l
    | [] -> raise IteratorEnd
    | _ -> assert false

  let at_end i = i = []

  (* TODO raise notimplemented, not just failwith *)
  let remaining _i = failwith "NotImplemented: IntMap.Iter.remaining"
end

(*this is not efficient, but at this time, this function is not called and is
here only to confom to the signature*)
(*todo: look if ordertop is needed, and can be applied to both tree*)
let fold_map2 f t t' acc =
  (* the result of the map, the accumulator and the iterator*)
  let rec aux iter acc = function
    | Br (p,m,tl,tr) ->
        let acc, tl, iter = aux iter acc tl in
        let acc, tr, iter = aux iter acc tr in
        acc, Br (p, m, tl, tr), iter
    | Lf (k,v) ->
        let ()=if Iter.at_end iter
        then invalid_arg "intMap.fold_map2" in
        let k',v' = Iter.get iter in
        let () = assert (k=k')
        and iter = Iter.next iter in
        let acc, v = f k v v' acc in
        acc, Lf (k, v), iter
    | Empty ->
        acc, Empty, iter
  in
  let iter' = Iter.make t' in
  let acc, t, iter' = aux iter' acc t in
  if Iter.at_end iter' then acc, t
  else invalid_arg "intMap.fold_map2"


module RevIter : (IterSig.S with type +'a element = int * 'a and type +'a structure = 'a t) = struct
  type 'a structure = 'a t
  type 'a element = int * 'a
  type 'a t = ('a structure) list

  let rec make_aux acc = function
    | Empty -> acc
    | Br (_,_,tl,tr) -> make_aux (tl :: acc) tr
    | Lf (k,v) -> Lf (k,v) :: acc

  let make m = make_aux [] m

  let get = function
    | Lf (k,v) :: _l -> k, v
    | [] -> raise IteratorEnd
    | _ -> assert false

  let next = function
    | Lf (_k, _v) :: Br (_,_,tl,tr) :: l -> make_aux (tl :: l) tr
    | Lf (_k, _v) :: l -> l
    | [] -> raise IteratorEnd
    | _ -> assert false

  let at_end i = i = []

  let remaining _i = failwith "NotImplemented: IntMap.Iter.remaining"
end

let rec min t = match t with
  | Br (_,_,tl,_tr) -> min tl
  | Lf (k,v) -> k,v
  | Empty -> raise Not_found
let min t = ordertop min t

(*
Choose is min, because they are no value except on leaf
hence I must go to a leaf to find something. And min already
find a leaf
*)
let choose = min

let choose_opt t =
  try Some (choose t) with | Not_found -> None

let rec max t = match t with
  | Br (_,_m,_tl,tr) -> max tr
  | Lf (k,v) -> k,v
  | Empty -> raise Not_found
let max t = ordertop max t

let rec find_inf k t = match t with
  | Br (p,_,tl,tr) ->
      if k <= p then find_inf k tl
      else (try find_inf k tr with Not_found -> max tl)
  | Lf (k',v) -> if k' <= k then k',v else raise Not_found
  | Empty -> raise Not_found
let find_inf k t = ordertop (find_inf k) t

let rec find_sup k t = match t with
  | Br (p,_,tl,tr) ->
      if k <= p
      then (try find_sup k tl with Not_found -> min tr)
      else find_sup k tr
  | Lf (k',v) -> if k' >= k then k',v else raise Not_found
  | Empty -> raise Not_found
let find_sup k t = ordertop (find_sup k) t

let nearest k t =
  let b = try Some (find_inf k t) with Not_found -> None in
  let a = try Some (find_sup k t) with Not_found -> None in
  match b,a with
  | None, Some x | Some x, None -> x
  | None, None -> raise Not_found
  | Some (kb,vb), Some (ka,va) ->
      if k - kb <= ka - k then kb, vb
      else ka, va

let from_list l = List.fold_left (fun t (k,v) -> add k v t) empty l


let fold_assoc k v acc = (k, v) :: acc
let to_list t = fold_rev fold_assoc t []
let ordered_list t = fold_rev fold_assoc t []
let rev_ordered_list t = fold fold_assoc t []

let keys t = fold_rev (fun k _ acc -> k::acc) t []
let elts t = fold_rev (fun _ v acc -> v::acc) t []

(* FIXME: Random according to the distribution of bits, not the actual map *)
let rec random t = match t with
  | Br (_p,_m,tl,tr) -> if Random.int 2 = 0 then random tl else random tr
  | Lf (k,v) -> k, v
  | Empty -> raise Not_found

let size t = fold (fun _ _ i -> i+1) t 0

let rec height t = match t with
  | Br (_p,_m,tl,tr) -> Pervasives.max (height tl) (height tr) + 1
  | Lf _ -> 1
  | Empty -> 0

let rec merge_i f t1 t2 = match t1, t2 with
  | Br (p1,m1,tl1,tr1), Br (p2,m2,tl2,tr2) ->
      if p1 = p2 && m1 = m2 then (* same node *)
        Br (p1, m1, merge_i f tl1 tl2, merge_i f tr1 tr2)
      else if m2 lsr 1 > m1 lsr 1 && mask p1 m2 = p2 then (* p1 is a sub-node of p2 *)
        if zerobit p1 m2
        then Br (p2, m2, merge_i f t1 tl2, tr2)
        else Br (p2, m2, tl2, merge_i f t1 tr2)
      else if m1 lsr 1 > m2 lsr 1 && mask p2 m1 = p1 then (* p2 is a sub-node of p1 *)
        if zerobit p2 m1
        then Br (p1,m1,merge_i f tl1 t2 ,tr1)
        else Br (p1,m1,tl1,merge_i f tr1 t2)
      else (* disjoint nodes *)
        join p1 t1 p2 t2 m1
  | Lf (k1,v1), Lf (k2,v2) when k1 = k2 -> Lf (k1, f k1 v1 v2)
  | Lf (k1,v1), t2 -> add_fi f k1 v1 t2
  | t1, Lf (k2,v2) -> add_fi (fun k v2 v1 -> f k v1 v2) k2 v2 t1
  | Empty, t | t, Empty -> t

let merge f t1 t2 = merge_i (fun _ -> f) t1 t2
let safe_merge t1 t2 =
  merge (fun _ _ -> raise (Invalid_argument "Base.Map.safe_merge")) t1 t2

let rec decons = function
  | Empty -> invalid_arg "IntMap.decons"
  | Lf (key, val_) -> Empty, key, val_, Empty
  | Br (_prefix, _mask, left, right) -> (
      match left, right with
      | Empty, Empty -> invalid_arg "IntMap.decons"
      | left, Lf (key, val_)
      | Lf (key, val_), left
          -> left, key, val_, Empty
      | br_left, br_right ->
          let l_left, key, val_, l_right = decons br_left in
          l_left, key, val_, (merge (fun _ a -> a) l_right br_right)
    )

let rename f t =
  fold (fun k v t -> add (f k) v t) t empty

(* cf doc *)
let pp sep ppe fmt t =
  let fiter elt val_ =
    ppe fmt elt val_ ;
    Format.fprintf fmt sep
  in
  iter fiter t

let compare_key : int -> int -> int = Pervasives.compare

let diff map1 map2 =
  fold (fun k v acc ->
          if mem k map2 then
            acc
          else
            add k v acc
       ) map1 empty
let diff2 map1 map2 map3 =
  fold (fun k v acc ->
          if mem k map2 && not (mem k map3) then
            acc
          else
            add k v acc
       ) map1 empty

  let example_diff s1 s2 =
    let diff_ = diff s1 s2 in
    match choose_opt diff_ with
    | Some elt -> Some elt
    | None ->
        let diff = diff s2 s1 in
        match choose_opt diff with
        | Some elt -> Some elt
        | None -> None

let from_sorted_array _ = assert false
Something went wrong with that request. Please try again.