package kot

  1. Overview
  2. Docs

Source file NonCatenableDeque.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
(******************************************************************************)
(*                                                                            *)
(*                                     Kot                                    *)
(*                                                                            *)
(*                         Juliette Ponsonnet, ENS Lyon                       *)
(*                         François Pottier, Inria Paris                      *)
(*                                                                            *)
(*       Copyright 2025--2025 Inria. All rights reserved. This file is        *)
(*       distributed under the terms of the GNU Library General Public        *)
(*       License, with an exception, as described in the file LICENSE.        *)
(*                                                                            *)
(******************************************************************************)

module B = Buffer3

type 'a deque =
  'a nonempty_deque option

and 'a nonempty_deque =
  'a triple ref

and 'a triple = {
  prefix: 'a B.buffer;
  child : ('a * 'a) deque;
  suffix: 'a B.buffer;
}

let empty =
  None

let[@inline] is_empty d =
  match d with None -> true | Some _ -> false

let rec check : type a. a deque -> unit = fun d ->
  match d with None -> () | Some d ->
  let { prefix; child; suffix } = !d in
  (* The buffers and child cannot simultaneously be empty. *)
  assert (not (B.is_empty prefix && is_empty child && B.is_empty suffix));
  check child

let rec length : type a. int -> int -> a deque -> int = fun s m d ->
  match d with None -> s | Some d ->
  let { prefix; child; suffix } = !d in
  let s = s + m * (B.length prefix + B.length suffix) in
  let m = 2 * m in
  length s m child

let[@inline] length d =
  length 0 1 d

let[@inline] assemble prefix child suffix =
  if B.is_empty prefix && is_empty child && B.is_empty suffix then
    None
  else
    Some (ref { prefix; child; suffix })

let rec pop_nonempty : type a. a nonempty_deque -> a * a deque = fun d ->
  let { prefix; child; suffix } = !d in
  match B.is_empty prefix, child with
  | true, Some c ->
      (* The front buffer is empty; the child deque is nonempty. *)
      (* Pop a pair [(x, y)] off the child deque. *)
      let (x, y), child = pop_nonempty c in
      (* Inject [x] and [y] into the (empty) front buffer. *)
      (* Update the deque [d] in place. *)
      let prefix = B.B2 (x, y) in
      d := { prefix; child; suffix };
      (* Extract [x]. Create a new deque whose front buffer is a singleton. *)
      let prefix = B.B1 (y) in
      x, assemble prefix child suffix
  | false, _ ->
      (* The front buffer is nonempty. *)
      (* Pop an element [x] off the front buffer. *)
      let x, prefix = B.pop prefix in
      x, assemble prefix child suffix
  | true, None ->
      (* The front buffer and child deque are empty. *)
      (* The rear buffer must be nonempty. *)
      assert (not (B.is_empty suffix));
      (* Pop an element [x] off the rear buffer. *)
      let x, suffix = B.pop suffix in
      x, assemble prefix child suffix

let pop_opt d =
  match d with
  | None ->
      None
  | Some d ->
      Some (pop_nonempty d)

let pop d =
  match d with
  | None ->
      invalid_arg "Deque.pop: deque is empty"
  | Some d ->
      pop_nonempty d

let[@inline] singleton x =
  let prefix = B.B1 (x)
  and child  = None
  and suffix = B.empty in
  Some (ref { prefix; child; suffix })

let rec push_nonempty : type a. a -> a nonempty_deque -> a deque = fun w d ->
  let { prefix; child; suffix } = !d in
  match prefix with
  | B.B3 (x, y, z) ->
      (* The front buffer is full. *)
      (* Extract two elements [y] and [z] out of the front buffer
         and push the pair [(y, z)] into the child deque. *)
      let child = push (y, z) child in
      let prefix = B.B1 (x) in
      (* Update the deque [d] in place. *)
      d := { prefix; child; suffix };
      (* Push [w] into what remains of the front buffer. *)
      let prefix = B.B2 (w, x) in
      Some (ref { prefix; child; suffix })
  | B.B2 _
  | B.B1 _
  | B.B0 ->
      (* Push [w] into the front buffer. *)
      let prefix = B.push w prefix in
      Some (ref { prefix; child; suffix })

and push : type a. a -> a deque -> a deque = fun x d ->
  match d with
  | None ->
      singleton x
  | Some d ->
      push_nonempty x d

let rec eject_nonempty : type a. a nonempty_deque -> a deque * a = fun d ->
  let { prefix; child; suffix } = !d in
  match B.is_empty suffix, child with
  | true, Some c ->
    (* The rear buffer is empty; the child deque is nonempty. *)
    (* Eject a pair [(x, y)] off the child deque. *)
    let child, (x, y) = eject_nonempty c in
    (* Inject [x] and [y] into the (empty) rear buffer. *)
    (* Update the deque [d] in place. *)
    let suffix = B.B2 (x, y) in d := { prefix; child; suffix };
    (* Extract [y]. Create a new deque whose rear buffer is a singleton. *)
    let suffix = B.B1 (x) in
    assemble prefix child suffix, y
  | false, _ ->
    (* The rear buffer is nonempty. *)
    (* Eject an element [x] off the rear buffer. *)
    let suffix, x = B.eject suffix in
    assemble prefix child suffix, x
  | true, None ->
    (* The rear buffer and child deque are empty. *)
    (* The front buffer must be nonempty. *)
    assert (not (B.is_empty prefix));
    (* Eject an element [x] off the front buffer. *)
    let prefix, x = B.eject prefix in
    assemble prefix child suffix, x

let eject_opt d = match d with
  | None -> None
  | Some d -> Some (eject_nonempty d)

let eject d = match d with
  | None -> invalid_arg "Deque.eject: deque is empty"
  | Some d -> eject_nonempty d

let rec inject_nonempty : type a. a nonempty_deque -> a -> a deque = fun d w ->
  let { prefix; child; suffix } = !d in
  match suffix with
    | B.B3 (x, y, z) ->
      (* The rear buffer is full. *)
      (* Extract two elements [y] and [z] out of the rear buffer and inject the pair [(y, z)] into the child deque. *)
      let child = inject child (x, y) in
      let suffix = B.B1 (z) in
      (* Update the deque [d] in place. *)
      d := { prefix; child; suffix };
      (* Inject [w] into what remains of the rear buffer. *)
      let suffix = B.B2 (z, w) in
      Some (ref { prefix; child; suffix })
    | B.B2 _ | B.B1 _ | B.B0 ->
      (* Inject [w] into the rear buffer. *)
      let suffix = B.inject suffix w in
      Some (ref { prefix; child; suffix })

and inject : type a. a deque -> a -> a deque =
  fun d x -> match d with
  | None -> singleton x
  | Some d -> inject_nonempty d x

let rec map_nonempty : type a b. (a -> b) -> a nonempty_deque -> b nonempty_deque =
  fun f d ->
    let { prefix; child; suffix } = !d in
    let prefix = B.map f prefix in
    let child = map (fun (a, b) -> (f a, f b)) child in
    let suffix = B.map f suffix in
    ref { prefix; child; suffix }
and map : type a b. (a -> b) -> a deque -> b deque =
  fun f ->
  function
  | None -> None
  | Some d -> Some (map_nonempty f d)

let rec fold_left : type a b. (b -> a -> b) -> b -> a deque -> b =
  fun f y ->
  function
  | None -> y
  | Some d ->
    let { prefix; child; suffix } = !d in
    let y = B.fold_left f y prefix in
    let y = fold_left (fun y (x0,x1) -> f (f y x0) x1) y child in
    let y = B.fold_left f y suffix in
    y