Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Bounded_stack.of_list to of_list_exn #164

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bounded_stack.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let is_empty t = Atomic.get t.head = (0, [])
exception Empty
exception Full

let of_list ?(capacity = Int.max_int) list =
let of_list_exn ?(capacity = Int.max_int) list =
if capacity < List.length list then raise Full
else
let head = Atomic.make_contended (List.length list, List.rev list) in
Expand Down
16 changes: 14 additions & 2 deletions src/bounded_stack.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,22 @@ val create : ?capacity:int -> unit -> 'a t
capacity of [capacity]. The default [capacity] value is [Int.max_int].
*)

val of_list : ?capacity:int -> 'a list -> 'a t
(** [of_list list] creates a new Treiber stack from a list.
val of_list_exn : ?capacity:int -> 'a list -> 'a t
(** [of_list_exn list] creates a new Treiber stack from a list.

@raises Full if the [list] is longer than the capacity of the stack.

{[
# open Saturn.Bounded_stack
# let t : int t = of_list_exn [1;2;3;4]
val t : int t = <abstr>
# pop_opt t
- : int option = Some 4
# pop_opt t
- : int option = Some 3
# length t
- : int = 2
]}
*)

val length : 'a t -> int
Expand Down
6 changes: 2 additions & 4 deletions test/bounded_stack/bounded_stack_dscheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,10 @@ let push_push_with_capacity () =

let pop_pop () =
Atomic.trace (fun () ->
let stack = Stack.create () in
let items_total = 4 in

for i = 1 to items_total do
Stack.try_push stack i |> ignore
done;
let pushed = List.init items_total (fun x -> x + 1) in
let stack = Stack.of_list_exn pushed in

(* two consumers *)
let lists = [ ref []; ref [] ] in
Expand Down
Loading