Source file Buffer3.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
(******************************************************************************)
(*                                                                            *)
(*                                     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.        *)
(*                                                                            *)
(******************************************************************************)

type 'a buffer =
  | B0
  | B1 of 'a
  | B2 of 'a * 'a
  | B3 of 'a * 'a * 'a

let empty =
  B0

let[@inline] length b =
  match b with
  | B0   -> 0
  | B1 _ -> 1
  | B2 _ -> 2
  | B3 _ -> 3

let[@inline] is_empty b =
  match b with
  | B0 -> true
  | _  -> false

let[@inline] push x0 b =
  match b with
  | B0 ->
      B1 x0
  | B1 x1 ->
      B2 (x0, x1)
  | B2 (x1, x2) ->
      B3 (x0, x1, x2)
  | B3 _ ->
      assert false

let[@inline] pop b =
  match b with
  | B0 ->
      assert false
  | B1 x0 ->
      x0, B0
  | B2 (x0, x1) ->
      x0, B1 x1
  | B3 (x0, x1, x2) ->
      x0, B2 (x1, x2)

let[@inline] first b =
  match b with
  | B0 ->
      assert false
  | B1 x0 ->
      x0
  | B2 (x0, _) ->
      x0
  | B3 (x0, _, _) ->
      x0

let[@inline] inject b x0 =
  match b with
  | B0 ->
      B1 x0
  | B1 x1 ->
      B2 (x1, x0)
  | B2 (x2, x1) ->
      B3 (x2, x1, x0)
  | B3 _ ->
      assert false

let[@inline] eject b =
  match b with
  | B0 ->
      assert false
  | B1 (x0) ->
      B0, x0
  | B2 (x0, x1) ->
      B1 (x0), x1
  | B3 (x0, x1, x2) ->
      B2 (x0, x1), x2

let[@inline] last b =
  match b with
  | B0 ->
      assert false
  | B1 (x0) ->
      x0
  | B2 (_, x1) ->
      x1
  | B3 (_, _, x2) ->
      x2

let[@inline] pop2 b =
  match b with
  | B3 (x0, x1, x2) ->
      x0, x1, B1 x2
  | _ ->
      assert false

let[@inline] eject2 b =
  match b with
  | B3 (x0, x1, x2) ->
      B1 x0, x1, x2
  | _ ->
      assert false

let[@inline] map f b =
  match b with
  | B0 ->
      B0
  | B1 (x0) ->
      B1 (f x0)
  | B2 (x0, x1) ->
      B2 (f x0, f x1)
  | B3 (x0, x1, x2) ->
      B3 (f x0, f x1, f x2)

let[@inline] fold_left f accu b =
  match b with
  | B0 ->
      accu
  | B1 (x0) ->
      let accu = f accu x0 in
      accu
  | B2 (x0, x1) ->
      let accu = f accu x0 in
      let accu = f accu x1 in
      accu
  | B3 (x0, x1, x2) ->
      let accu = f accu x0 in
      let accu = f accu x1 in
      let accu = f accu x2 in
      accu

let[@inline] fold_right f b accu =
  match b with
  | B0 ->
      accu
  | B1 (x0) ->
      let accu = f x0 accu in
      accu
  | B2 (x0, x1) ->
      let accu = f x1 accu in
      let accu = f x0 accu in
      accu
  | B3 (x0, x1, x2) ->
      let accu = f x2 accu in
      let accu = f x1 accu in
      let accu = f x0 accu in
      accu