package varray

  1. Overview
  2. Docs

Source file grow.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
let ( /^ ) a b = 1 + (a - 1) / b

let rec log2 = function
  | 0 | 1 -> 0
  | n -> 1 + log2 (n /^ 2)

let pow2 n = 1 lsl n

module Make (V : Varray_sig.TIER)
: Varray_sig.VARRAY with type 'a elt = 'a V.elt and type 'a array = 'a V.array
= struct

  module Tier = V

  module Array = V.Array
  type 'a elt = 'a V.elt
  type 'a array = 'a V.array

  type 'a t =
    { mutable lc : int
    ; mutable protected : bool
    ; mutable small : 'a V.t
    ; mutable large : 'a V.t
    }

  let length t =
    V.length t.small + V.length t.large

  let lc_for = function
    | n when n <= 0 -> 1
    | n -> log2 n /^ V.depth

  let empty () =
    { lc = 0
    ; protected = false
    ; small = V.create ~capacity:1
    ; large = V.empty ()
    }

  let is_empty t = V.is_empty t.small && V.is_empty t.large

  let make n x =
    let lc = lc_for n in
    { lc
    ; protected = false
    ; small = V.make ~lc n x
    ; large = V.empty ()
    }

  let init n f =
    let lc = lc_for n in
    { lc
    ; protected = false
    ; small = V.init ~lc ~offset:0 n f
    ; large = V.empty ()
    }

  let protect t f =
    if t.protected
    then f ()
    else begin
      t.protected <- true ;
      match f () with
      | v -> t.protected <- false ; v
      | exception e -> t.protected <- false ; raise e
    end

  let check_protection t =
    if t.protected
    then failwith "Varray modification during protected traversal"

  let get t i =
    let lc = t.lc in
    match i - V.length t.small with
    | j when i >= 0 && j < 0 ->
        V.get ~lc t.small i
    | j when j >= 0 && j < V.length t.large ->
        V.get ~lc:(lc + 1) t.large j
    | _ ->
        invalid_arg "index out of bounds"

  let set t i x =
    let lc = t.lc in
    match i - V.length t.small with
    | j when i >= 0 && j < 0 ->
        V.set ~lc t.small i x
    | j when j >= 0 && j < V.length t.large ->
        V.set ~lc:(lc + 1) t.large j x
    | _ ->
        invalid_arg "index out of bounds"

  let do_swap t =
    t.lc <- 1 + t.lc ;
    t.small <- t.large ;
    t.large <- V.empty ()

  let swap t =
    if V.is_empty t.small && not (V.is_empty t.large)
    then do_swap t

  let is_growing t = length t >= V.capacity ~lc:t.lc

  let pow2_depth = pow2 V.depth

  let incr_capacity t =
    swap t ;
    if is_growing t
    then begin
      if not (V.is_empty t.large)
      then begin
        assert (not (V.is_empty t.small)) ;
        let lc = t.lc in
        let tl = V.pop_back ~lc t.small in
        let lc = t.lc + 1 in
        V.push_front ~lc t.large tl ;
        if V.is_empty t.small
        then do_swap t
      end
      else begin
        let tl = V.pop_back ~lc:t.lc t.small in
        let lc = 1 + t.lc in
        t.large <- V.make ~lc 1 tl
      end
    end

  let insert_at t i x =
    check_protection t ;
    incr_capacity t ;
    match i - V.length t.small with
    | j when j <= 0 ->
        V.insert_at ~lc:t.lc t.small i x ;
        incr_capacity t
    | j ->
        V.insert_at ~lc:(t.lc + 1) t.large j x

  let is_shrinking t =
    length t * pow2_depth < V.capacity ~lc:t.lc

  let decr_capacity t =
    swap t ;
    if is_shrinking t
    then begin
      if not (V.is_empty t.large)
      then begin
        let lc = t.lc in
        assert (not (V.is_full ~lc t.small)) ;
        V.push_back ~lc t.small (V.pop_front ~lc:(lc + 1) t.large)
      end
      else if t.lc > 1 && not (V.is_empty t.small)
      then begin
        let lc = t.lc in
        let hd = V.pop_front ~lc t.small in
        let lc = t.lc - 1 in
        t.lc <- lc ;
        t.large <- t.small ;
        t.small <- V.make ~lc 1 hd ;
      end
    end

  let pop_at t i =
    check_protection t ;
    decr_capacity t ;
    match i - V.length t.small with
    | j when j < 0 ->
        let x = V.pop_at ~lc:t.lc t.small i in
        decr_capacity t ;
        x
    | j ->
        V.pop_at ~lc:(t.lc + 1) t.large j

  let delete_at t i = ignore (pop_at t i)

  let push_back t x =
    check_protection t ;
    incr_capacity t ;
    let lc = t.lc in
    if V.is_empty t.large
    then V.push_back ~lc t.small x
    else V.push_back ~lc:(lc + 1) t.large x

  let push_front t x =
    check_protection t ;
    incr_capacity t ;
    let lc = t.lc in
    V.push_front ~lc t.small x ;
    incr_capacity t

  let pop_front t =
    check_protection t ;
    decr_capacity t ;
    let x = V.pop_front ~lc:t.lc t.small in
    decr_capacity t ;
    x

  let pop_back t =
    check_protection t ;
    decr_capacity t ;
    if V.is_empty t.large
    then V.pop_back ~lc:t.lc t.small
    else V.pop_back ~lc:(t.lc + 1) t.large

end
OCaml

Innovation. Community. Security.