package batteries

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file batQueue.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
(*
 * BatQueue - Extended operations on queues
 * Copyright (C) 1996 Xavier Leroy
 *               2008 David Teller, LIFO, Universite d'Orleans
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)

include Queue

type 'a enumerable = 'a t

let map f q =
  let r = create () in
  iter (fun x -> add (f x) r) q;
  r
(*$T map
  create () |> map (fun x -> x) |> is_empty

  create () |> tap (add 1) |> map (fun x -> x+1) \
  |> enum |> BatList.of_enum |> (=) [2]

  create () |> tap (add 1) |> tap (add 2) |> map (fun x -> x+1) \
  |> enum |> BatList.of_enum |> (=) [2;3]

  let q = Queue.create () in \
  for i = 1 to 5 do Queue.push i q; done; \
  let q = map ((+) 10) q in \
  BatList.of_enum (enum q) = [11;12;13;14;15]
*)

let filter f q =
  let r = create () in
  iter (fun x -> if f x then add x r) q;
  r
(*$T filter
  create () |> filter (fun n -> n>3) |> is_empty
  create () |> tap (add 1) |> filter (fun n -> n>3) |> is_empty
  create () |> tap (add 1) |> tap (add 2) |> filter (fun n -> n>3) |> is_empty
  create () |> tap (add 1) |> tap (add 2) |> filter (fun n -> n>1) |> enum |> BatList.of_enum |> (=) [2]
  create () |> tap (add 1) |> tap (add 2) |> filter (fun n -> n>0) |> enum |> BatList.of_enum |> (=) [1;2]
 *)

let filter_map f q =
  let r = create () in
  iter (fun x ->
        match f x with
          | None -> ()
          | Some v -> add v r) q;
  r
(*$T filter_map
  create () |> filter_map (fun n -> None) |> is_empty

  create () |> tap (add 1) \
  |> filter_map (fun n -> if n>3 then Some (n+1) else None) |> is_empty

  create () |> tap (add 1) |> tap (add 2) \
  |> filter_map (fun n -> if n>3 then Some (n+1) else None) |> is_empty

  create () |> tap (add 1) |> tap (add 2) \
  |> filter_map (fun n -> if n>1 then Some (n+1) else None) |> enum \
  |> BatList.of_enum |> (=) [3]

  create () |> tap (add 1) |> tap (add 2) \
  |> filter_map (fun n -> if n>0 then Some (n+1) else None) |> enum \
  |> BatList.of_enum |> (=) [2;3]
 *)

let filter_inplace f q =
  BatConcreteQueue.(filter_inplace f (of_abstr q))
(*$T filter_inplace
  let q1 = Queue.create () in \
  for i = 1 to 5 do Queue.push i q1; done; \
  let q2,q3 = Queue.copy q1, Queue.copy q1 in \
  filter_inplace (fun a -> List.mem a [2;4]) q1; \
  filter_inplace (fun a -> List.mem a [3]) q2; \
  filter_inplace (fun a -> List.mem a []) q3; \
  length q1 = 2 && \
  length q2 = 1 && \
  length q3 = 0 && \
  BatList.of_enum (enum q1) = [2;4] && \
  BatList.of_enum (enum q2) = [3] && \
  BatList.of_enum (enum q3) = []
*)

let of_enum e =
  let q = create () in
  BatEnum.iter (fun x -> push x q) e;
  q
(*$Q of_enum
  (Q.list Q.int) (fun l -> \
    let e = BatList.enum l in \
    BatEnum.equal (=) (enum (of_enum (BatEnum.clone e))) e \
  )
*)

let enum q = BatEnum.from (fun () -> try pop q with Empty -> raise BatEnum.No_more_elements)
(*$T enum
  let q = Queue.create () in \
  for i = 0 to 10 do Queue.push i q; done; \
  let e = enum q in \
  let i = ref (-1) in \
  BatEnum.count e = 11 && BatEnum.for_all (fun elt -> incr i; !i = elt) e
*)

let print ?(first="") ?(last="") ?(sep="") print_a out t =
  BatEnum.print ~first ~last ~sep print_a out (enum (copy t))
(*$T print
  BatIO.to_string (print ~sep:"," ~first:"[" ~last:"]" BatInt.print) (of_enum (BatArray.enum [|2;4;66|])) = "[2,4,66]"
*)

let compare cmp a b = BatEnum.compare cmp (enum a) (enum b)
let equal eq a b = BatEnum.equal eq (enum a) (enum b)

module Exceptionless = struct
  let peek q = try Some (peek q) with Empty -> None
  let take q = try Some (take q) with Empty -> None
  (*$T
    Exceptionless.peek (Queue.create ()) = None
    Exceptionless.take (Queue.create ()) = None
  *)
end
OCaml

Innovation. Community. Security.