package sek

  1. Overview
  2. Docs

Source file ShortPersistentSequence.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
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
(******************************************************************************)
(*                                                                            *)
(*                                    Sek                                     *)
(*                                                                            *)
(*          Arthur Charguéraud, Émilie Guermeur and François Pottier          *)
(*                                                                            *)
(*  Copyright Inria. All rights reserved. This file is distributed under the  *)
(*  terms of the GNU Lesser General Public License as published by the Free   *)
(*  Software Foundation, either version 3 of the License, or (at your         *)
(*  option) any later version, as described in the file LICENSE.              *)
(*                                                                            *)
(******************************************************************************)

open PublicSettings
open PrivateSignatures

module[@inline] Make
    (S : PSEQ)
    (T : THRESHOLD)
= struct

  open T

  (* We define the following types of sequences:

     * [Zero] represents an empty sequence.

     * [One] represents a singleton sequence.

     * [Short] represents a sequence whose length is comprised between
       2 and [threshold], included.

     * [Long] represents a sequence whose length is greater than [threshold].

     The constructors [Zero], [One], and [Short] carry a default value. *)

  (* I believe that this code works even if [threshold] is less than 2.
     In that case, the [Short] representation is simply never used. *)

  type 'a t =
    | Zero of { default: 'a }
    | One   of { default: 'a; x: 'a }
    | Short of { default: 'a; a: 'a array }
    | Long  of 'a S.t

  let default s =
    match s with
    | Zero { default }
    | One { default; _ }
    | Short { default; _ } ->
        default
    | Long s ->
        S.default s

  let length s =
    match s with
    | Zero _ ->
        0
    | One _ ->
        1
    | Short { a; _ } ->
        Array.length a
    | Long s ->
        S.length s

  let is_empty s =
    match s with
    | Zero _ ->
        true
    | One _
    | Short _
    | Long _ ->
        (* A [Long] sequence must be nonempty. *)
        false

  let check s =
    match s with
    | Zero _
    | One _ ->
        ()
    | Short { a; _ } ->
        let n = Array.length a in
        assert (2 <= n && n <= threshold)
    | Long s ->
        let n = S.length s in
        assert (n > threshold);
        S.check s

  (* Ensure [check] has zero cost in release mode. *)

  let[@inline] check s =
    assert (check s; true)

  let print print s =
    let open PPrint in
    let open PPrint.OCaml in
    match s with
    | Zero _ ->
        !^ "Zero"
    | One { x; _ } ->
        !^ "One" ^^ record "pseq" [
          "model", flowing_list print [x]
        ]
    | Short { a; _ } ->
        !^ "Short" ^^ record "pseq" [
          "model", flowing_list print (Array.to_list a)
        ]
    | Long s ->
        variant "pseq" "Long" 3 [ S.print print s ]

  let create default =
    Zero { default }

  (* [of_array_segment_one_short] is a special case of [of_array_segment]
     with the additional precondition [0 < k && k <= threshold]. *)

  let of_array_segment_one_short default a i k =
    (* [i] and [k] must represent a valid range in the array [a]. *)
    assert (ArrayExtra.is_valid_segment a i k);
    (* This additional requirement must hold. *)
    assert (0 < k && k <= threshold);
    if k = 1 then
      One { default; x = a.(i) }
    else
      Short { default; a = Array.sub a i k }

  let of_array_segment default a i k =
    (* [i] and [k] must represent a valid range in the array [a]. *)
    assert (ArrayExtra.is_valid_segment a i k);
    if k = 0 then
      Zero { default }
    else if threshold < k then
      Long (S.of_array_segment default a i k)
    else
      of_array_segment_one_short default a i k

  let[@inline] of_array default a =
    of_array_segment default a 0 (Array.length a)

  let make default size x =
    assert (0 <= size);
    if size = 0 then
      Zero { default }
    else if size = 1 then
      One { default; x }
    else if size <= threshold then
      Short { default; a = Array.make size x }
    else
      Long (S.make default size x)

  let init default size f =
    assert (0 <= size);
    if size = 0 then
      Zero { default }
    else if size = 1 then
      One { default; x = f 0 }
    else if size <= threshold then
      Short { default; a = Array.init size f }
    else
      Long (S.init default size f)

  let to_array s =
    match s with
    | Zero _ -> [||]
    | One { x }  -> [|x|]
    | Short { a; _ } -> Array.copy a
    | Long s -> S.to_array s

  (* [of_short_array_destructive default a] is analogous to the code that is
     commented out in [wrap] (below), but expects a short array [a] instead of
     a sequence [s], and is allowed to steal this array. *)

  let of_short_array_destructive default a =
    let n = Array.length a in
    assert (n <= threshold);
    if n = 0 then
      Zero { default }
    else if n = 1 then
      One { default; x = a.(0) }
    else
      Short { default; a }

  let wrap_long s =
    assert (threshold < S.length s);
    Long s

  let wrap s =
    let n = S.length s in
    if threshold < n then
      Long s
    else
      let default = S.default s in
      if n = 0 then
        Zero { default }
      else if n = 1 then
        One { default; x = S.peek Front s }
      else
        let a = Array.make n default in
        let i = ref 0 in
        S.iter Front (fun x -> a.(!i) <- x; i := !i + 1) s;
        Short { default; a }

  let unwrap s =
    match s with
    | Zero { default } ->
        S.create default
    | One { default; x } ->
        S.push Back (S.create default) x
    | Short { default; a } ->
        S.of_array default a
    | Long s ->
        s

  let array_push pov a x =
    let n = Array.length a in
    let b = Array.make (n+1) x in
    let i = match pov with Front -> 1 | Back -> 0 in
    Array.blit a 0 b i n;
    b

  let[@specialise] push pov s x =
    match s with
    | Zero { default } ->
        One { default; x }
    | One { default; x = y } ->
        let a =
          match pov with
          | Front ->
              [| x; y |]
          | Back ->
              [| y; x |]
        in
        Short { default; a }
    | Short { default; a } ->
        let n = Array.length a in
        if n < threshold then
          Short { default; a = array_push pov a x }
        else
          Long (S.push pov (S.of_array default a) x)
            (* equivalent to [wrap (S.push pov (unwrap s) x)] *)
    | Long s ->
        Long (S.push pov s x)

  let[@specialise] pop pov s =
    match s with
    | Zero _ ->
        raise Empty
    | One { default; x } ->
        x, Zero { default }
    | Short { default; a } ->
        let n = Array.length a in
        assert (2 <= n && n <= threshold);
        begin match pov with
          | Front ->
              let x = a.(0) in
              x, of_array_segment_one_short default a 1 (n-1)
          | Back ->
              let x = a.(n-1) in
              x, of_array_segment_one_short default a 0 (n-1)
        end
    | Long s ->
        let x, s = S.pop pov s in
        x, wrap s

  let[@specialise] peek pov s =
    match s with
    | Zero _ ->
        raise Empty
    | One { x; _ } ->
        x
    | Short { a; _ } ->
        assert (2 <= Array.length a);
        begin match pov with
        | Front ->
            a.(0)
        | Back ->
            let n = Array.length a in
            a.(n-1)
        end
    | Long s ->
        S.peek pov s

  let[@specialise] array_iter pov f a =
    match pov with
    | Front ->
        Array.iter f a
    | Back ->
        let n = Array.length a in
        for i = n - 1 downto 0 do
          f a.(i)
        done

  let[@specialise] iter pov g s =
    match s with
    | Zero _ ->
        ()
    | One { x; _ } ->
        g x
    | Short { a; _ } ->
        array_iter pov g a
    | Long s ->
        S.iter pov g s

  let concat s1 s2 =
    match s1, s2 with
    | Zero _, s
    | s, Zero _ ->
        s
    | One { x; _ }, _ ->
        push Front s2 x
    | _, One { x; _ } ->
        push Back s1 x
    | Short { default; a = a1 }, Short { a = a2; _ }
      when Array.length a1 + Array.length a2 <= threshold ->
        Short { default; a = Array.append a1 a2 }
        (* TODO: could optimize the subcase where the sum of the lengths
           exceeds [threshold] by blitting into a chunk. *)
    | (Short _ | Long _), (Short _ | Long _) ->
        let s1 = unwrap s1
        and s2 = unwrap s2 in
        Long (S.concat s1 s2)

  let split s i =
    assert (0 <= i && i <= length s);
    if i = 0 then
      let default = default s in
      Zero { default }, s
    else if i = length s then
      let default = default s in
      s, Zero { default }
    else
      match s with
      | Zero _
      | One _ ->
          (* Already dealt with above. *)
          assert false
      | Short { default; a } ->
          let n = Array.length a in
          of_array_segment_one_short default a 0 i,
          of_array_segment_one_short default a i (n-i)
      | Long s ->
          let s1, s2 = S.split s i in
          wrap s1, wrap s2

  let get s i =
    assert (0 <= i && i < length s);
    match s with
    | Zero _ ->
        assert false
    | One { x; _ } ->
        x
    | Short { a; _ } ->
        Array.get a i
    | Long s ->
        S.get s i

  let set s i x' =
    assert (0 <= i && i < length s);
    match s with
    | Zero _ ->
        assert false
    | One { x; default } ->
        if x == x' then s else
        let x = x' in
        One { x; default }
    | Short { a; default } ->
        let x = Array.get a i in
        if x == x' then s else
        let a = Array.copy a in
        Array.set a i x';
        Short { a; default }
    | Long s as original ->
        let s' = S.set s i x' in
        if s == s' then original else
        let s = s' in
        Long s

end