Skip to content

Commit

Permalink
finalize queue api
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Jan 22, 2018
1 parent e4b6288 commit ed02794
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 237 deletions.
17 changes: 9 additions & 8 deletions jscomp/others/bs_Queue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let clear q =
firstSet q null;
lastSet q null

let push q x =
let addDone q x =
let cell = return @@ node
~content:x
~next:null
Expand All @@ -55,8 +55,9 @@ let push q x =
nextSet last cell;
lastSet q cell


let peekOpt q =
let add q x = addDone q x; q

let peek q =
match Js.nullToOption (first q ) with
| None -> None
| Some v -> Some (content v)
Expand All @@ -67,12 +68,12 @@ let peekNull q =
| Some v -> return (content v)


let peekAssert q =
let peekExn q =
match Js.nullToOption (first q ) with
| None -> [%assert "Bs.Queue.Empty"]
| Some v -> content v

let popOpt q =
let pop q =
match Js.nullToOption (first q ) with
| None -> None
| Some x ->
Expand All @@ -88,7 +89,7 @@ let popOpt q =
Some(content x)
end

let popAssert q =
let popExn q =
match Js.nullToOption (first q ) with
| None -> [%assert "Bs.Queue.Empty"]
| Some x ->
Expand Down Expand Up @@ -150,7 +151,7 @@ let rec iterAux f cell =
f (content x) [@bs];
iterAux f (next x)

let iter q f =
let forEach q f =
iterAux f (first q)

let rec foldAux f accu cell =
Expand All @@ -160,7 +161,7 @@ let rec foldAux f accu cell =
let accu = f accu (content x) [@bs] in
foldAux f accu (next x)

let fold q accu f =
let reduce q accu f =
foldAux f accu (first q)

let transfer q1 q2 =
Expand Down
29 changes: 15 additions & 14 deletions jscomp/others/bs_Queue.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,26 @@ val clear : 'a t -> unit
val create : unit -> 'a t
(** Return a new queue, initially empty. *)

val push : 'a t -> 'a -> unit
(** [push q x] adds the element [x] at the end of the queue [q]. *)

val peekOpt : 'a t -> 'a option
val addDone : 'a t -> 'a -> unit
(** [addDone q x] adds the element [x] at the end of the queue [q]. *)
val add : 'a t -> 'a -> 'a t

val peek : 'a t -> 'a option
(** [peekOpt q] returns the first element in queue [q], without removing
it from the queue. *)

val peekNull : 'a t -> 'a Js.null
(** [peekNull q] returns the first element in queue [q], without removing
it from the queue. *)
val peekAssert : 'a t -> 'a
val peekExn : 'a t -> 'a

val popOpt : 'a t -> 'a option
(** [take q] removes and returns the first element in queue [q].*)
val pop : 'a t -> 'a option
(** [pop q] removes and returns the first element in queue [q].*)

val popNull : 'a t -> 'a Js.null
(** [take q] removes and returns the first element in queue [q].*)
(** [popNull q] removes and returns the first element in queue [q].*)

val popAssert : 'a t -> 'a
val popExn : 'a t -> 'a

val copy : 'a t -> 'a t
(** Return a copy of the given queue. *)
Expand All @@ -56,13 +57,13 @@ val isEmpty : 'a t -> bool
val length : 'a t -> int
(** Return the number of elements in a queue. *)

val iter : 'a t -> ('a -> unit [@bs]) -> unit
(** [iter f q] applies [f] in turn to all elements of [q],
val forEach : 'a t -> ('a -> unit [@bs]) -> unit
(** [reduce f q] applies [f] in turn to all elements of [q],
from the least recently entered to the most recently entered.
The queue itself is unchanged. *)

val fold : 'a t -> 'b -> ('b -> 'a -> 'b [@bs]) -> 'b
(** [fold q accu f] is equivalent to [List.foldLeft f accu l],
val reduce : 'a t -> 'b -> ('b -> 'a -> 'b [@bs]) -> 'b
(** [reduce q accu f] is equivalent to [List.reduce f accu l],
where [l] is the list of [q]'s elements. The queue remains
unchanged. *)

Expand All @@ -73,4 +74,4 @@ val transfer : 'a t -> 'a t -> unit
in constant time. *)

val toArray : 'a t -> 'a array
(** First added will be in the beginning of the array *)
(** First added will be in the beginning of the array *)
Loading

0 comments on commit ed02794

Please sign in to comment.