Source

ocaml-core / base / core / lib / doubly_linked.ml

The branch 'bash' does not exist.
  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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

open Sexplib

module List = Core_list

exception Attempt_to_mutate_list_during_iteration

let phys_equal = (==)

module Header : sig
  type t
  val create : unit -> t
  val length : t -> int
  val equal : t -> t -> bool
  val incr_length : by:int -> t -> unit
  val with_iteration : t -> (unit -> 'a) -> 'a
  val merge : t -> t -> [ `Same_already | `Merged ]
end = struct

  type s = {
    mutable length : int;
    mutable pending_iterations : int;
  }

  type t = s Union_find.t

  let create () = Union_find.create { length = 1; pending_iterations = 0; }

  let equal (t1 : t) t2 = Union_find.same_class t1 t2

  let length t = (Union_find.get t).length

  let incr_length ~by:n t =
    let s = Union_find.get t in
    if s.pending_iterations > 0 then raise Attempt_to_mutate_list_during_iteration;
    s.length <- s.length + n

  let with_iteration t f =
    let s = Union_find.get t in
    s.pending_iterations <- s.pending_iterations + 1;
    let res = try Result.Ok (f ()) with e -> Result.Error e in
    s.pending_iterations <- s.pending_iterations - 1;
    match res with
    | Result.Ok v -> v
    | Result.Error e -> raise e

  let merge (t1 : t) t2 =
    if Union_find.same_class t1 t2 then `Same_already else begin
      let n1 = length t1 in
      let n2 = length t2 in
      with_iteration t1 (fun () ->
        with_iteration t2 (fun () ->
          Union_find.union t1 t2;
          Union_find.set t1 {
            length = n1 + n2;
            pending_iterations = 0;
          }));
      `Merged
    end

end

module Elt : sig
  type 'a t with sexp_of
  val header : 'a t -> Header.t
  val equal : 'a t -> 'a t -> bool
  val create : 'a -> 'a t
  val value : 'a t -> 'a
  val unlink : 'a t -> unit
  val split_or_splice_after : 'a t -> 'a t -> unit
  val split_or_splice_before : 'a t -> 'a t -> unit
  val insert_after : 'a t -> 'a -> 'a t
  val insert_before : 'a t -> 'a -> 'a t
  val unlink_after : 'a t -> 'a t
  val unlink_before : 'a t -> 'a t
  val next : 'a t -> 'a t
  val prev : 'a t -> 'a t
end = struct

  type 'a t = {
    value : 'a;
    mutable prev : 'a t;
    mutable next : 'a t;
    mutable header : Header.t;
  }

  let equal = phys_equal

  let next t = t.next
  let prev t = t.prev
  let header t = t.header

  let create_aux v header =
    let rec t = {
      value = v;
      prev = t;
      next = t;
      header = header;
    } in
    t

  let is_singleton t = equal t t.prev

  let sexp_of_t sexp_of_a t = sexp_of_a t.value

  let create v = create_aux v (Header.create ())

  let value t = t.value

  (*
    [split_or_splice] is sufficient as the lone primitive for
    accomplishing all pointer updates on cyclic loops of list nodes.
    It takes two "gaps" between adjacent linked list nodes.  If the gaps
    point into the same list, the result is that it will be split into
    two lists afterwards.  If the gaps point into different lists, the
    result is that they will be spliced together into one list afterwards.

      Before                      After
          -----+        +-----         -----+               +-----
             A |  <-->  | B               A |  <---   --->  | B
          -----+        +-----         -----+      \ /      +-----
                                                    X
          -----+        +-----         -----+      / \      +-----
             C |  <-->  | D               C |  <---   --->  | D
          -----+        +-----         -----+               +-----
  *)

  let split_or_splice ~prev1:a ~next1:b ~prev2:c ~next2:d =
    a.next <- d; d.prev <- a;
    c.next <- b; b.prev <- c

  let split_or_splice_after t1 t2 =
    split_or_splice
     ~next1:t1.next
     ~prev1:t1.next.prev
     ~next2:t2.next
     ~prev2:t2.next.prev

  let split_or_splice_before t1 t2 =
    split_or_splice
     ~prev1:t1.prev
     ~next1:t1.prev.next
     ~prev2:t2.prev
     ~next2:t2.prev.next

  let insert_before t v =
    Header.incr_length t.header ~by:1;
    let node = create_aux v t.header in
    split_or_splice_before t node;
    node

  let insert_after t v =
    Header.incr_length t.header ~by:1;
    let node = create_aux v t.header in
    split_or_splice_after t node;
    node

  let unlink_before t =
    let node = t.prev in
    if is_singleton node then node else begin
      Header.incr_length t.header ~by:(-1);
      split_or_splice_before t node;
      node.header <- Header.create ();
      node
    end

  let unlink_after t =
    let node = t.next in
    if is_singleton node then node else begin
      Header.incr_length t.header ~by:(-1);
      split_or_splice_after t node;
      node.header <- Header.create ();
      node
    end

  let unlink t = ignore (unlink_after t.prev)

end

type 'a t = 'a Elt.t option ref

let create (type a) () : a t = ref None

let is_empty t = Option.is_none !t

let equal (t : _ t) t' = phys_equal t t'

let of_list = function
  | [] -> create ()
  | x :: xs ->
    let first = Elt.create x in
    let _last = List.fold xs ~init:first ~f:Elt.insert_after in
    ref (Some first)

let fold_elt t ~init ~f =
  match !t with
  | None -> init
  | Some first ->
    Header.with_iteration (Elt.header first) (fun () ->
      let rec loop acc elt =
        let acc = f acc elt in
        let next = Elt.next elt in
        if phys_equal next first then acc else loop acc next
      in
      loop init first)

let find_elt (type a) t ~f =
  let module M = struct exception Found of a Elt.t end in
  try
    fold_elt t ~init:() ~f:(fun () elt ->
      if f (Elt.value elt) then raise (M.Found elt));
    None
  with
    M.Found elt -> Some elt

include Container.Make (struct
  type 'a t_ = 'a t
  type 'a t = 'a t_
  let fold t ~init ~f = fold_elt t ~init ~f:(fun acc elt -> f acc (Elt.value elt))
end)

let fold_left t ~init ~f = fold t ~init ~f

let fold_right t ~init ~f =
  match !t with
  | None -> init
  | Some first ->
    Header.with_iteration (Elt.header first) (fun () ->
      let rec loop acc elt =
        let prev = Elt.prev elt in
        let acc = f acc (Elt.value prev) in
        if phys_equal prev first
        then acc
        else loop acc prev
      in
      loop init first
    )

let to_list t = fold_right t ~init:[] ~f:(fun tl x -> x :: tl)

let length t =
  match !t with
  | None -> 0
  | Some first -> Header.length (Elt.header first)

let sexp_of_t sexp_of_a t = List.sexp_of_t sexp_of_a (to_list t)
let t_of_sexp a_of_sexp s = of_list (List.t_of_sexp a_of_sexp s)

let copy t = of_list (to_list t)

let clear t = (t := None)

exception Transfer_src_and_dst_are_same_list

let transfer ~src ~dst =
  if phys_equal src dst then raise Transfer_src_and_dst_are_same_list;
  match !src with
  | None -> ()
  | Some src_head ->
    match !dst with
    | None -> dst := Some src_head; clear src
    | Some dst_head ->
      match Header.merge (Elt.header src_head) (Elt.header dst_head) with
      | `Same_already -> raise Transfer_src_and_dst_are_same_list
      | `Merged -> Elt.split_or_splice_before dst_head src_head; clear src

let filter_inplace t ~f =
  let to_remove =
    List.rev
      (fold_elt t ~init:[] ~f:(fun elts elt ->
        if f (Elt.value elt) then elts else elt :: elts))
  in
  List.iter to_remove ~f:(fun elt ->
    begin
      match !t with
      | None -> ()
      | Some head ->
          if Elt.equal head elt then begin
            let next_elt = Elt.next elt in
            t := if Elt.equal head next_elt then None else Some next_elt
          end
    end;
    Elt.unlink elt)

exception Elt_does_not_belong_to_list

let first_elt t = !t
let last_elt t = Option.map ~f:Elt.prev !t

let first t = Option.map ~f:Elt.value (first_elt t)
let last  t = Option.map ~f:Elt.value (last_elt  t)

let is_first t elt =
  match !t with
  | None -> raise Elt_does_not_belong_to_list
  | Some first ->
    if Header.equal (Elt.header first) (Elt.header elt) then
      Elt.equal elt first
    else
      raise Elt_does_not_belong_to_list

let is_last t elt =
  match !t with
  | None -> raise Elt_does_not_belong_to_list
  | Some first ->
    if Header.equal (Elt.header first) (Elt.header elt) then begin
      let last = Elt.prev first in
      Elt.equal elt last
    end else
      raise Elt_does_not_belong_to_list

let prev t elt =
  match !t with
  | None -> raise Elt_does_not_belong_to_list
  | Some first ->
    if Elt.equal elt first then
      None
    else if Header.equal (Elt.header first) (Elt.header elt) then
      Some (Elt.prev elt)
    else
      raise Elt_does_not_belong_to_list

let next t elt =
  match !t with
  | None -> raise Elt_does_not_belong_to_list
  | Some first ->
    let last = Elt.prev first in
    if Elt.equal elt last then
      None
    else if Header.equal (Elt.header first) (Elt.header elt) then
      Some (Elt.next elt)
    else
      raise Elt_does_not_belong_to_list

let insert_after t elt v =
  match !t with
  | None -> raise Elt_does_not_belong_to_list
  | Some first ->
    if Header.equal (Elt.header first) (Elt.header elt) then
      Elt.insert_after elt v
    else
      raise Elt_does_not_belong_to_list

let insert_before t elt v =
  match !t with
  | None -> raise Elt_does_not_belong_to_list
  | Some first ->
    if Elt.equal elt first then begin
      let new_elt = Elt.insert_before first v in
      t := Some new_elt;
      new_elt
    end else if Header.equal (Elt.header first) (Elt.header elt) then
      Elt.insert_before elt v
    else
      raise Elt_does_not_belong_to_list

let insert_empty t v =
  let new_elt = Elt.create v in
  t := Some new_elt;
  new_elt

let insert_last t v =
  match !t with
  | None -> insert_empty t v
  | Some first -> Elt.insert_before first v

let insert_first t v =
  match !t with
  | None -> insert_empty t v
  | Some first ->
    let new_elt = Elt.insert_before first v in
    t := Some new_elt;
    new_elt

let remove_last t =
  match !t with
  | None -> None
  | Some first ->
    let last = Elt.unlink_before first in
    if Elt.equal first last then t := None;
    Some (Elt.value last)

let remove_first t =
  match !t with
  | None -> None
  | Some first ->
    let second = Elt.next first in
    ignore (Elt.unlink first);
    t := if Elt.equal first second then None else Some second;
    Some (Elt.value first)

let remove t elt =
  match !t with
  | None -> raise Elt_does_not_belong_to_list
  | Some first ->
    if Elt.equal elt first then
      ignore (remove_first t)
    else if Header.equal (Elt.header first) (Elt.header elt) then
      Elt.unlink elt
    else
      raise Elt_does_not_belong_to_list

let invariant t =
  match !t with
  | None -> ()
  | Some head ->
    let header = Elt.header head in
    let rec loop n elt =
      let next_elt = Elt.next elt in
      let prev_elt = Elt.prev elt in
      assert (Elt.equal elt (Elt.prev next_elt));
      assert (Elt.equal elt (Elt.next prev_elt));
      assert (Header.equal (Elt.header elt) header);
      if Elt.equal next_elt head then n else loop (n + 1) next_elt
    in
    let len = loop 1 head in
    assert (len = Header.length header)