Skip to content
This repository
tag: v735
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 252 lines (229 sloc) 6.37 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
(*
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/>.
*)
(* CF mli *)

include Array
let is_empty a = Array.length a = 0
let unsafe_create l = create l (Obj.magic 0)
let copy_memory a =
  let l = length a in
  if l = 0 then [||] else begin
    let res = unsafe_create l in
    for i = 0 to pred l do
      unsafe_set res i (unsafe_get a i)
    done;
    res
  end
let unsafe_blit a1 ofs1 a2 ofs2 len =
  if ofs1 < ofs2 then
    (* Top-down copy *)
    for i = len - 1 downto 0 do
      unsafe_set a2 (ofs2 + i) (unsafe_get a1 (ofs1 + i))
    done
  else
    (* Bottom-up copy *)
    for i = 0 to len - 1 do
      unsafe_set a2 (ofs2 + i) (unsafe_get a1 (ofs1 + i))
    done
let append_memory a1 a2 =
  let l1 = Array.length a1 and l2 = Array.length a2 in
  if l1 = 0 && l2 = 0 then [||] else begin
    let r = unsafe_create (l1 + l2) (*This deactivates [float] optimizations*) in
    for i = 0 to l1 - 1 do Array.unsafe_set r i (Array.unsafe_get a1 i) done;
    for i = 0 to l2 - 1 do Array.unsafe_set r (i + l1) (Array.unsafe_get a2 i) done;
    r
  end
let swap a i j =
  let t = a.(i) in
  a.(i) <- a.(j) ;
  a.(j) <- t
let insert_sorted ?(cmp=Pervasives.compare) ?(dupl=false) x a =
  let l = Array.length a in
  let insert_and_blit x i =
    let t = unsafe_create (succ l) in
    t.(i) <- x;
    unsafe_blit a 0 t 0 i;
    unsafe_blit a i t (i+1) (l-i);
    t
  in
  let rec aux i t =
    if i < l then
      let c = cmp x a.(i) in
      if c < 0 then insert_and_blit x i
      else if c=0 then (if not dupl then t else insert_and_blit x i)
      else aux (i+1) t
    else insert_and_blit x i
  in aux 0 a

(*
Mathieu Wed Nov 3 16:03:15 CET 2010
I've benched VS snd (fold_left (fun (i, acc) x -> (succ i, f acc x i)) (0, init) a),
The GC die in case of too much tuple allocations.
*)
let fold_left_i f acc a =
  let p = ref (-1) in
  let f acc e =
    incr(p);
    f acc e !p
  in
  Array.fold_left f acc a

(** max et min d'un tableau (doit être non vide) *)
let max, min =
  let oper op a =
    let l = length a in
    assert (l > 0) ;
    let r = ref a.(0) in
    for i = 1 to pred l do
      r := op !r (unsafe_get a i)
    done ;
    !r
  in
  (fun a -> oper Pervasives.max a),
  (fun a -> oper Pervasives.min a)

(** argmax et argmin d'un tableau (doit être non vide) *)
let argmax, argmin =
  let arg_oper op a =
    let l = length a in
    assert (l > 0) ;
    let m = ref 0 in
    for i = 1 to pred l do
      if op (unsafe_get a i) (unsafe_get a !m) then m := i
    done ;
    !m
  in
  (fun a -> arg_oper (>) a),
  (fun a -> arg_oper (<) a)

let map_some f a =
  let a' = map f a in
  let nb = fold_left (fun acc -> function Some _ -> succ acc | _ -> acc) 0 a' in
  let pos = ref 0 in
  init nb (
    fun _ ->
      while not(Option.is_some a'.(!pos)) do incr pos done ;
      let r = match a'.(!pos) with Some r -> r | _ -> assert false in
      incr pos ; r
  )

let mapi_some f a =
  let a' = mapi f a in
  let nb = fold_left (fun acc -> function Some _ -> succ acc | _ -> acc) 0 a' in
  let pos = ref 0 in
  init nb (
    fun _ ->
      while not(Option.is_some a'.(!pos)) do incr pos done ;
      let r = match a'.(!pos) with Some r -> r | _ -> assert false in
      incr pos ; r
  )

let map2 f a1 a2 =
  let l = length a1 in
  assert (l = length a2) ;
  init l (fun i -> f (unsafe_get a1 i) (unsafe_get a2 i))

let fill_some a v pfrom pto =
  for i = pfrom to pred pto do
    match a.(i) with
    | None -> a.(i) <- Some v
    | _ -> ()
  done

let mem a tab =
  let l = length tab in
  let rec aux i =
    if i >= l then false
    else if unsafe_get tab i = a then true else aux (succ i)
  in aux 0

let exists fct tab =
  let l = length tab in
  let rec aux i =
    if i >= l then false
    else if fct (unsafe_get tab i) then true else aux (succ i)
  in aux 0

let split a =
  let len = Array.length a in
  if len = 0 then ([||], [||])
  else
    let (left_init, right_init) = a.(0) in
    let left = Array.make len left_init
    and right= Array.make len right_init in
    for i = 0 to len - 1 do
      let (left_item, right_item) = unsafe_get a i in
      unsafe_set left i left_item;
      unsafe_set right i right_item
    done;
    (left, right)

let find a x =
  let l = length a in
  let rec aux i =
    if i >= l then raise Not_found
    else if unsafe_get a i = x then i else aux (succ i)
  in aux 0

let compare cmp a b =
  if a == b then 0 else
  let len_a = length a in
  let len_b = length b in
  let rec aux i =
    if i >= len_a then
      if i >= len_b then 0 else -1
    else if i >= len_b then
      1
    else
      let va = unsafe_get a i in
      let vb = unsafe_get b i in
      let cmp = cmp va vb in
      if cmp <> 0 then cmp
      else aux (succ i)
  in aux 0


let to_string f a =
  let rec aux acc i =
    if i < 0 then acc
    else aux ((f a.(i))^acc) (pred i)
  in
    let l = length a in
    if l = 0 then "[||]"
    else aux "" (l -1);;

let print f a =
  let rec aux acc i =
    if i = 0 then "[|"^(f a.(i))^acc
    else aux (";"^(f a.(i))^acc) (pred i)
  in
    let l = length a in
    if l = 0 then "[||]"
    else aux "|]" (l-1);;

let filteri fct a =
  let len = length a in
  if len = 0
  then a
  else
    let rec aux filter acc i =
      if i = len
      then
        filter, acc
      else
        let ai = unsafe_get a i in
        if fct i ai
        then
          aux (succ filter) (ai :: acc) (succ i)
        else
          aux filter acc (succ i)
    in
    let filter, acc = aux 0 [] 0 in
    let fa = create filter (unsafe_get a 0) in
    let rec fill acc i =
      match acc with
      | [] -> ()
      | hd :: tl -> (
          unsafe_set fa i hd ;
          fill tl (pred i)
        )
    in
    fill acc (pred filter) ;
    fa

let filter fct a =
  let fct _ a = fct a in
  filteri fct a
Something went wrong with that request. Please try again.