Skip to content
This repository
tag: v1836
Fetching contributors…

Cannot retrieve contributors at this time

file 114 lines (97 sloc) 3.058 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
(*
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 MaxSize
exception UnknownCell

type 'a t =
    { mutable array : 'a array
    ; init : 'a
    ; mutable length : int }

let make ?size n init =
  let size = match size with Some s -> s | _ -> n in
  { array = Array.make size init
  ; init = init
  ; length = n }

let create = make

let clear t =
  t.array <- [||];
  t.length <- 0

let get a i =
  if i < a.length then Array.unsafe_get a.array i
  else raise UnknownCell
    (** problème: set peut renvoyer (sans qu'on le sache) un tableau frais
cela est gênant si on dépend d'effets de bord
*)

let set a i v =
  let l = Array.length a.array in
  if i < l then begin
    Array.unsafe_set a.array i v ;
    if i >= a.length then a.length <- succ i
  end
  else if i < Sys.max_array_length then
    let n = max i (min Sys.max_array_length (2 * l)) in
    begin
      a.array <- Array.init n (
        fun j ->
          if j < l then Array.unsafe_get a.array j
          else if j = i then v
          else a.init
      ) ;
      a.length <- succ i
    end
  else raise MaxSize

let length a = a.length

let real_length a = Array.length a.array

let append a b =
  let la = length a
  and lb = length b in
  let l = la + lb in
  { array = Array.init l (
      fun i ->
        if i < la then Array.unsafe_get a.array i
        else Array.unsafe_get b.array (i - la)
    )
  ; init = if la>0 || lb>0 then Array.unsafe_get (if la > 0 then a else b).array 0 else a.init
  ; length = la + lb }

let fold_left f init a =
  let rec aux acc i =
    if i = length a then acc
    else aux (f acc (Array.unsafe_get a.array i)) (succ i)
  in aux init 0

let fold_left_i f init a =
  let rec aux acc i =
    if i = length a then acc
    else aux (f acc (Array.unsafe_get a.array i) ~i) (succ i)
  in aux init 0

let delete a pfrom pto =
  let offset = pto - pfrom in
  if offset > 0 then begin
    for i = pto to pred (length a) do
      a.array.(i - offset) <- a.array.(i)
    done ;
    a.length <- a.length - offset
  end else failwith "delete failed: empty region"

let insert a pos b =
  let la = length a
  and lb = length b in
  let l = la + lb in
  if l <= real_length a then begin
    for i = pred l downto pos + lb do
      a.array.(i) <- a.array.(i - lb)
    done ;
    for i = pos to pred (pos + lb) do
      a.array.(i) <- b.array.(i - pos)
    done ;
    a.length <- a.length + lb
  end else
    raise (Base.NotImplemented "insert")
Something went wrong with that request. Please try again.