Source file stack_or_counter.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
open! Base
type _ t =
| Stack : 'a Stack.t -> 'a t
| Counter : { mutable length : int } -> unit t
[@@deriving sexp_of]
let of_list list = Stack (Stack.of_list list)
let create_counter ~length =
if length < 0
then
raise_s
[%message "[Stack_or_counter.create_counter] got negative length" (length : int)];
Counter { length }
;;
let length (type a) (t : a t) =
match t with
| Stack s -> Stack.length s
| Counter r -> r.length
;;
let clear (type a) (t : a t) =
match t with
| Stack s -> Stack.clear s
| Counter r -> r.length <- 0
;;
let push (type a) (t : a t) a =
match t with
| Stack s -> Stack.push s a
| Counter r -> r.length <- r.length + 1
;;
let pop_exn (type a) (t : a t) =
match t with
| Stack s -> Stack.pop_exn s
| Counter r ->
if r.length = 0 then raise_s [%message "[Stack_or_counter.pop_exn] of empty stack"];
r.length <- r.length - 1
;;
let iter (type a) (t : a t) ~(f : a -> unit) =
match t with
| Stack s -> Stack.iter s ~f
| Counter r ->
for _ = 1 to r.length do
f ()
done
;;