diff --git a/src/bounded_stack.ml b/src/bounded_stack.ml index 0e0f5e26..86194f65 100644 --- a/src/bounded_stack.ml +++ b/src/bounded_stack.ml @@ -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 diff --git a/src/bounded_stack.mli b/src/bounded_stack.mli index 5b79d3db..5974aeed 100644 --- a/src/bounded_stack.mli +++ b/src/bounded_stack.mli @@ -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 = + # pop_opt t + - : int option = Some 4 + # pop_opt t + - : int option = Some 3 + # length t + - : int = 2 + ]} *) val length : 'a t -> int diff --git a/test/bounded_stack/bounded_stack_dscheck.ml b/test/bounded_stack/bounded_stack_dscheck.ml index 06c7dfb4..2f745391 100644 --- a/test/bounded_stack/bounded_stack_dscheck.ml +++ b/test/bounded_stack/bounded_stack_dscheck.ml @@ -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