package containers

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file CCPersistentArray.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
(*
copyright (c) 2013-2015, Guillaume Bury
all rights reserved.

redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.  redistributions in binary
form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with
the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*)


(* Persistent arrays *)

type 'a t = 'a data ref
and 'a data =
  | Array of 'a array
  | Diff of int * 'a * 'a t

let make n a = ref (Array (Array.make n a))
let init n f = ref (Array (Array.init n f))

let rec _reroot t k = match !t with
  | Array a -> k a
  | Diff (i, v, t') ->
    _reroot t' (fun a ->
      let v' = a.(i) in
      a.(i) <- v;
      t := Array a;
      t' := Diff(i, v', t);
      k a
    )

let reroot t = match !t with
  | Array a -> a
  | _ -> _reroot t (fun x -> x)

let copy t = ref (Array(Array.copy (reroot t)))

let get t i = match !t with
  | Array a -> a.(i)
  |  _ -> (reroot t).(i)

let set t i v =
  let a = reroot t in
  let old = a.(i) in
  a.(i) <- v;
  let t' = ref (Array a) in
  t := Diff (i, old, t');
  t'

let length t = Array.length (reroot t)

let map f t = ref (Array (Array.map f (reroot t)))
let mapi f t = ref (Array (Array.mapi f (reroot t)))

let iter f t = Array.iter f (reroot t)
let iteri f t = Array.iteri f (reroot t)

let fold_left f acc t = Array.fold_left f acc (reroot t)
let fold_right f t acc = Array.fold_right f (reroot t) acc

let append a b =
  let n = length a in
  init (n + length b)
    (fun i -> if i < n then get a i else get b (i-n))

let flatten a =
  let a = reroot a in
  let n = Array.fold_left (fun acc x -> acc + length x) 0 a in
  let i = ref 0 in  (* index in [a] *)
  let j = ref 0 in  (* index in [a.(!i)] *)
  init n
    (fun _ ->
       while !j = length a.(!i) do
         incr i;
         j := 0
       done;
       let x = get a.(!i) !j in
       incr j;
       x
    )

let flat_map f a =
  let a' = map f a in
  flatten a'

(*$T
  of_list [ of_list [1]; of_list []; of_list [2;3;4]; of_list [5]; of_list [6;7]] \
    |> flatten |> to_list =  [1;2;3;4;5;6;7]
  of_list [ of_list []; of_list []; of_list []] |> flatten |> length = 0
  of_list [] |> flatten |> length = 0
*)

let to_array t = Array.copy (reroot t)
let of_array a = init (Array.length a) (fun i -> a.(i))

let to_list t = Array.to_list (reroot t)
let of_list l = ref (Array (Array.of_list l))

let rev_in_place_ a i ~len =
  if len=0 then ()
  else
    for k = 0 to (len-1)/2 do
      let t = a.(i+k) in
      a.(i+k) <- a.(i+len-1-k);
      a.(i+len-1-k) <- t;
    done

let of_rev_list l =
  let a = Array.of_list l in
  rev_in_place_ a 0 ~len:(Array.length a);
  ref (Array a)

type 'a sequence = ('a -> unit) -> unit
type 'a gen = unit -> 'a option

let to_seq a yield = iter yield a

let of_seq seq =
  let l = ref [] in
  seq (fun x -> l := x :: !l);
  of_rev_list !l

let rec gen_iter_ f g = match g() with
  | None -> ()
  | Some x -> f x ; gen_iter_ f g

let of_gen g =
  let l = ref [] in
  gen_iter_ (fun x -> l := x :: !l) g;
  of_rev_list !l

let to_gen a =
  let i = ref 0 in
  let n = length a in
  fun () ->
    if !i = n then None
    else (
      let x = get a !i in
      incr i;
      Some x
    )

(*$Q
  Q.(list int) (fun l -> \
    of_list l |> to_gen |> of_gen |> to_list = l)
*)

type 'a printer = Format.formatter -> 'a -> unit

let pp pp_item out v =
  Format.fprintf out "[|";
  iteri
    (fun i x ->
       if i > 0 then Format.fprintf out ";@ ";
       pp_item out x
    ) v;
  Format.fprintf out "|]"

OCaml

Innovation. Community. Security.