package menhirGLR

  1. Overview
  2. Docs

Source file MiniBabySet.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
(******************************************************************************)
(*                                                                            *)
(*                                    Menhir                                  *)
(*                                                                            *)
(*   Copyright Inria. All rights reserved. This file is distributed under     *)
(*   the terms of the GNU Library General Public License version 2, with a    *)
(*   special exception on linking, as described in the file LICENSE.          *)
(*                                                                            *)
(******************************************************************************)

(* This is a stripped-down copy of the weight-balanced binary trees found in
   the library Baby by François Pottier. *)

type 'v tree =
  | TLeaf
  | TNode of { l : 'v tree; v : 'v; r : 'v tree; w : int }

let[@inline] weight t =
  match t with
  | TLeaf ->
      1
  | TNode { w; _ } ->
      w

let[@inline] cardinal t =
  weight t - 1

let alpha =
  29 (* in percent *)

let[@inline] not_left_heavy wl wr =
  alpha * wl <= (100-alpha) * wr

let[@inline] left_heavy wl wr =
  not (not_left_heavy wl wr)

let[@inline] not_right_heavy wl wr =
  not_left_heavy wr wl

let[@inline] right_heavy wl wr =
  not (not_right_heavy wl wr)

let[@inline] like_weights wl wr =
  not_left_heavy wl wr && not_right_heavy wl wr

let[@inline] siblings l r =
  like_weights (weight l) (weight r)

let rec check t =
  match t with
  | TLeaf ->
      ()
  | TNode { l; r; w; _ } ->
      check l;
      check r;
      assert (w = weight l + weight r);
      assert (siblings l r)

let[@inline] create'' w l v r =
  assert (w = weight l + weight r);
  (* This assertion can fail, (hopefully) just because a double rotation
     can temporarily create a node that does not satisfy the invariant.
  assert (siblings l r);
   *)
  TNode { l; v; r; w }

let[@inline] create l v r =
  let w = weight l + weight r in
  create'' w l v r

let[@inline] create' wl l v wr r =
  assert (wl = weight l && wr = weight r);
  let w = wl + wr in
  create'' w l v r

let[@inline] singleton x =
  let w = 2 in
  create'' w TLeaf x TLeaf

let impossible () =
  assert false

let rotate_left l v r =
  match r with
  | TLeaf -> impossible()
  | TNode { l = rl; v = rv; r = rr; _ } ->
  create (create l v rl) rv rr

let rotate_right l v r =
  match l with
  | TLeaf -> impossible()
  | TNode { l = ll; v = lv; r = lr; _ } ->
  create ll lv (create lr v r)

let balance_right_heavy wl l v wr r =
  assert (wl = weight l && wr = weight r);
  assert (right_heavy wl wr);
  match r with
  | TLeaf -> impossible()
  | TNode { l = rl; v = rv; r = rr; _ } ->
  let wrl = weight rl in
  let wrr = wr - wrl in
  assert (wrr = weight rr);
  if like_weights wl wrl && like_weights (wl + wrl) wrr then
    (* [rotate_left l v r] *)
    let w = wl + wr in
    create'' w (create' wl l v wrl rl) rv rr
  else
    rotate_left l v (rotate_right rl rv rr)

let balance_left_heavy wl l v wr r =
  assert (wl = weight l && wr = weight r);
  assert (left_heavy wl wr);
  match l with
  | TLeaf -> impossible()
  | TNode { l = ll; v = lv; r = lr; _ } ->
  let wll = weight ll in
  let wlr = wl - wll in
  assert (wlr = weight lr);
  if like_weights wlr wr && like_weights wll (wlr + wr) then
    (* [rotate_right l v r] *)
    let w = wl + wr in
    create'' w ll lv (create' wlr lr v wr r)
  else
    rotate_right (rotate_left ll lv lr) v r

(* The following functions are unused.

let[@inline] balance_maybe_right_heavy wl l v wr r =
  assert (wl = weight l && wr = weight r);
  assert (not_left_heavy wl wr);
  if not_right_heavy wl wr then
    create' wl l v wr r
  else
    balance_right_heavy wl l v wr r

let[@inline] balance_maybe_left_heavy wl l v wr r =
  assert (wl = weight l && wr = weight r);
  assert (not_right_heavy wl wr);
  if not_left_heavy wl wr then
    create' wl l v wr r
  else
    balance_left_heavy wl l v wr r

let rec join_maybe_left_heavy l v wr r =
  assert (wr = weight r);
  let wl = weight l in
  assert (not_right_heavy wl wr);
  if not_left_heavy wl wr then
    create' wl l v wr r
  else
    join_left_heavy wl l v wr r

and join_left_heavy wl l v wr r =
  assert (wl = weight l && wr = weight r);
  assert (left_heavy wl wr);
  match l with
  | TLeaf -> impossible()
  | TNode { l = ll; v = lv; r = lr; _ } ->
  let wll = weight ll in
  let wlr = wl - wll in
  assert (wlr = weight lr);
  balance_maybe_right_heavy
    wll ll
    lv
    (wlr + wr) (join_maybe_left_heavy lr v wr r)

let rec join_maybe_right_heavy wl l v r =
  assert (wl = weight l);
  let wr = weight r in
  assert (not_left_heavy wl wr);
  if not_right_heavy wl wr then
    create' wl l v wr r
  else
    join_right_heavy wl l v wr r

and join_right_heavy wl l v wr r =
  assert (wl = weight l && wr = weight r);
  assert (right_heavy wl wr);
  match r with
  | TLeaf -> impossible()
  | TNode { l = rl; v = rv; r = rr; _ } ->
  let wrl = weight rl in
  let wrr = wr - wrl in
  assert (wrr = weight rr);
  balance_maybe_left_heavy
    (wl + wrl) (join_maybe_right_heavy wl l v rl)
    rv
    wrr rr

let join l v r =
  let wl = weight l and wr = weight r in
  if not_left_heavy wl wr then
    if not_right_heavy wl wr then
      create' wl l v wr r
    else
      join_right_heavy wl l v wr r
  else
    join_left_heavy wl l v wr r
 *)

let rec quasi_siblings l r =
  if weight l <= weight r then
    like_weights (weight l) (weight r - 1) ||
    like_weights (weight l + 1) (weight r)
  else
    quasi_siblings r l

let join_quasi_siblings l v r =
  assert (quasi_siblings l r);
  let wl = weight l and wr = weight r in
  if not_left_heavy wl wr then
    if not_right_heavy wl wr then
      create' wl l v wr r
    else
      balance_right_heavy wl l v wr r
  else
    balance_left_heavy wl l v wr r

let empty =
  TLeaf

let rec add cmp x t =
  match t with
  | TLeaf ->
      singleton x
  | TNode { l; v; r; _ } ->
      let c = cmp x v in
      if c = 0 then
        t
      else if c < 0 then
        let l' = add cmp x l in
        if l == l' then t else join_quasi_siblings l' v r
      else
        let r' = add cmp x r in
        if r == r' then t else join_quasi_siblings l v r'

let rec add_absent cmp x t =
  match t with
  | TLeaf ->
      singleton x
  | TNode { l; v; r; _ } ->
      let c = cmp x v in
      assert (c <> 0);
      if c < 0 then
        let l' = add_absent cmp x l in
        if l == l' then t else join_quasi_siblings l' v r
      else
        let r' = add_absent cmp x r in
        if r == r' then t else join_quasi_siblings l v r'

let rec mem cmp x t =
  match t with
  | TLeaf ->
      false
  | TNode { l; v; r; _ } ->
      let c = cmp x v in
      c = 0 || mem cmp x (if c < 0 then l else r)

let rec find cmp x t =
  match t with
  | TLeaf ->
      None
  | TNode { l; v; r; _ } ->
      let c = cmp x v in
      if c = 0 then Some v
      else find cmp x (if c < 0 then l else r)

let rec iter f t =
  match t with
  | TLeaf ->
      ()
  | TNode { l; v; r; _ } ->
      iter f l; f v; iter f r

let rec for_all p t =
  match t with
  | TLeaf ->
      true
  | TNode { l; v; r; _ } ->
      for_all p l && p v && for_all p r

let[@inline] is_singleton t =
  match t with
  | TLeaf ->
      false
  | TNode { w; _ } ->
      (* [w] is [weight t]. The weight of a singleton is 2. Relying on the
         weight removes the need to read the fields [l] and [r]. *)
      w = 2

let[@inline] extract_singleton t =
  match t with
  | TNode { l; v; r; w } ->
      assert (l = TLeaf && r = TLeaf && w = 2);
      v
  | _ ->
      assert false