File: stdcompat__queue.ml.in

package info (click to toggle)
ocaml-stdcompat 20~git20240529-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,540 kB
  • sloc: ml: 34,099; sh: 861; makefile: 249
file content (81 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download
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
include Queue

@BEGIN_BEFORE_4_08_0@
let take_opt q =
  try Some (take q)
  with Empty -> raise Empty

let peek_opt q =
  try Some (peek q)
  with Empty -> raise Empty
@END_BEFORE_4_08_0@

@BEGIN_BEFORE_4_07_0@
@BEGIN_WITH_MAGIC@
@BEGIN_FROM_4_03_0@
@BEGIN_FROM_4_00_0@
[@@@ocaml.warning "-37"]
@END_FROM_4_00_0@

type 'a cell =
  | Nil
  | Cons of { content: 'a; mutable next: 'a cell }

type 'a internal = {
  mutable length: int;
  mutable first: 'a cell;
  mutable last: 'a cell
}

let to_seq (q : 'a t) =
  let q : 'a internal = Obj.magic q in
  let rec aux c () = match c with
    | Nil -> Stdcompat__seq.Nil
    | Cons { content=x; next; } -> Stdcompat__seq.Cons (x, aux next)
  in
  aux q.first
@END_FROM_4_03_0@
@BEGIN_BEFORE_4_03_0@
type 'a cell = {
    content: 'a;
    mutable next: 'a cell
  }

type 'a internal = {
    mutable length: int;
    mutable tail: 'a cell
  }

let to_seq (q : 'a t) =
  let q : 'a internal = Obj.magic q in
  if q.length = 0 then
    Stdcompat__seq.empty
  else
    begin
      let tail = q.tail in
      let rec aux cell () =
        let tail' =
          if cell == tail then
            Stdcompat__seq.empty
          else
            aux cell.next in
        Stdcompat__seq.Cons (cell.content, tail') in
      aux tail.next
    end
@END_BEFORE_4_03_0@
@END_WITH_MAGIC@
@BEGIN_WITHOUT_MAGIC@
let to_list q =
  fold (fun accu content -> content :: accu) [] q

let to_seq q =
  Stdcompat__list.to_seq (List.rev (to_list q))
@END_WITHOUT_MAGIC@

let add_seq q i = Stdcompat__seq.iter (fun x -> push x q) i

let of_seq g =
  let q = create() in
  add_seq q g;
  q
@END_BEFORE_4_07_0@