Skip to content

Commit

Permalink
Rename of_list -> of_list_exn and change a dscheck test to use it.
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrm committed Nov 23, 2024
1 parent e50b636 commit 98c5d2a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
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

0 comments on commit 98c5d2a

Please sign in to comment.