package containers
A modular, clean and powerful extension of the OCaml standard library
Install
Dune Dependency
Authors
Maintainers
Sources
v2.8.tar.gz
md5=03b80e963186e91ddac62ef645bf7fb2
sha512=c8f434808be540c16926bf03d89f394d33fc2d092f963a7b6d412481229e0a96290f1ad7c7d522415115d35426b7aa0b3fda4b991ddc321dad279d402c9a0c0b
doc/src/containers.data/CCSimple_queue.ml.html
Source file CCSimple_queue.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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
(* This file is free software, part of containers. See file "license" for more details. *) (** {1 Functional queues (fifo)} *) type 'a iter = ('a -> unit) -> unit type 'a sequence = ('a -> unit) -> unit type 'a printer = Format.formatter -> 'a -> unit type 'a klist = unit -> [`Nil | `Cons of 'a * 'a klist] type 'a gen = unit -> 'a option type 'a t = { hd : 'a list; tl : 'a list; } (** Queue containing elements of type 'a *) let empty = { hd = []; tl = []; } (* invariant: if hd=[], then tl=[] *) let make_ hd tl = match hd with | [] -> {hd=List.rev tl; tl=[] } | _::_ -> {hd; tl; } let list_is_empty = function | [] -> true | _::_ -> false let is_empty q = list_is_empty q.hd let push x q = make_ q.hd (x :: q.tl) let snoc q x = push x q let peek_exn q = match q.hd with | [] -> assert (list_is_empty q.tl); invalid_arg "Queue.peek" | x::_ -> x let peek q = match q.hd with | [] -> None | x::_ -> Some x let pop_exn q = match q.hd with | [] -> assert (list_is_empty q.tl); invalid_arg "Queue.peek" | x::hd' -> let q' = make_ hd' q.tl in x, q' let pop q = try Some (pop_exn q) with Invalid_argument _ -> None (*$Q Q.(list small_int) (fun l -> \ let q = of_list l in \ equal CCInt.equal (Gen.unfold pop q |> of_gen) q) *) let junk q = try let _, q' = pop_exn q in q' with Invalid_argument _ -> q let map f q = { hd=List.map f q.hd; tl=List.map f q.tl; } let rev q = make_ q.tl q.hd (*$Q Q.(list small_int) (fun l -> \ equal CCInt.equal (of_list l |> rev) (of_list (List.rev l))) Q.(list small_int) (fun l -> \ let q = of_list l in \ equal CCInt.equal q (q |> rev |> rev)) *) let length q = List.length q.hd + List.length q.tl (*$Q Q.(list small_int)(fun l -> \ length (of_list l) = List.length l) *) (*$Q Q.(list small_int)(fun l -> \ equal CCInt.equal (of_list l) (List.fold_left snoc empty l)) *) let fold f acc q = let acc' = List.fold_left f acc q.hd in List.fold_right (fun x acc -> f acc x) q.tl acc' (* iterate on a list in reverse order *) let rec rev_iter_ f l = match l with | [] -> () | x :: tl -> rev_iter_ f tl; f x let iter f q = List.iter f q.hd; rev_iter_ f q.tl let to_list q = fold (fun acc x->x::acc) [] q |> List.rev let add_list q l = List.fold_left snoc q l let of_list l = add_list empty l let to_iter q = fun k -> iter k q let add_iter q seq = let q = ref q in seq (fun x -> q := push x !q); !q let of_iter s = add_iter empty s let of_seq = of_iter let to_seq = to_iter let add_seq = add_iter (*$Q Q.(list small_int) (fun l -> \ equal CCInt.equal \ (of_seq (Iter.of_list l)) \ (of_list l)) Q.(list small_int) (fun l -> \ l = (of_list l |> to_seq |> Iter.to_list)) *) let add_std_seq q l = add_iter q (fun k -> Seq.iter k l) let of_std_seq l = add_std_seq empty l let to_std_seq q = let rec aux1 l () = match l with | [] -> aux2 (List.rev q.tl) () | x :: tl -> Seq.Cons (x, aux1 tl) and aux2 l () = match l with | [] -> Seq.Nil | x :: tl -> Seq.Cons (x, aux2 tl) in aux1 q.hd let rec klist_iter_ k f = match k() with | `Nil -> () | `Cons (x,tl) -> f x; klist_iter_ tl f let add_klist q l = add_seq q (klist_iter_ l) let of_klist l = add_klist empty l let to_klist q = let rec aux1 l () = match l with | [] -> aux2 (List.rev q.tl) () | x :: tl -> `Cons (x, aux1 tl) and aux2 l () = match l with | [] -> `Nil | x :: tl -> `Cons (x, aux2 tl) in aux1 q.hd let rec gen_iter g f = match g() with | None -> () | Some x -> f x; gen_iter g f let add_gen q g = add_seq q (gen_iter g) let of_gen g = add_gen empty g let to_gen q = let st = ref (`Left q.hd) in let rec aux () = match !st with | `Stop -> None | `Left [] -> st := `Right q.tl; aux() | `Left (x::tl) -> st := `Left tl; Some x | `Right [] -> st := `Stop; None | `Right (x::tl) -> st := `Right tl; Some x in aux let rec klist_equal eq l1 l2 = match l1(), l2() with | `Nil, `Nil -> true | `Nil, _ | _, `Nil -> false | `Cons (x1,l1'), `Cons (x2,l2') -> eq x1 x2 && klist_equal eq l1' l2' let equal eq q1 q2 = klist_equal eq (to_klist q1) (to_klist q2) (*$Q Q.(pair (list small_int)(list small_int)) (fun (l1,l2) -> \ equal CCInt.equal (of_list l1)(of_list l2) = (l1=l2)) *) let append q1 q2 = add_seq q1 (fun yield -> to_seq q2 yield) (*$Q Q.(pair (list small_int)(list small_int)) (fun (l1,l2) -> \ equal CCInt.equal \ (append (of_list l1)(of_list l2)) \ (of_list (List.append l1 l2))) *) module Infix = struct let (>|=) q f = map f q let (<::) = snoc let (@) = append end include Infix (** {2 IO} *) let pp ?(sep=fun out () -> Format.fprintf out ",@ ") pp_item out l = let first = ref true in iter (fun x -> if !first then first := false else sep out (); pp_item out x) l
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>