diff --git a/sequence.ml b/sequence.ml index 893f6c7..eaa8611 100644 --- a/sequence.ml +++ b/sequence.ml @@ -149,9 +149,9 @@ module MList = struct | Cons of 'a array * int ref * 'a node ref (* build and call callback on every element *) - let of_seq_with seq k = + let of_seq_with ?(init_size=8) seq k = let start = ref Nil in - let chunk_size = ref 8 in + let chunk_size = ref init_size in (* fill the list. prev: tail-reference from previous node *) let prev, cur = ref start, ref Nil in seq @@ -175,8 +175,8 @@ module MList = struct !prev := !cur; !start - let of_seq seq = - of_seq_with seq (fun _ -> ()) + let of_seq ?init_size seq = + of_seq_with seq ?init_size (fun _ -> ()) let is_empty = function | Nil -> true @@ -239,22 +239,22 @@ end (** Iterate on the sequence, storing elements in a data structure. The resulting sequence can be iterated on as many times as needed. *) -let persistent seq = - let l = MList.of_seq seq in +let persistent ?init_size seq = + let l = MList.of_seq ?init_size seq in MList.to_seq l type 'a lazy_state = | LazySuspend | LazyCached of 'a t -let persistent_lazy (seq:'a t) = +let persistent_lazy ?init_size (seq:'a t) = let r = ref LazySuspend in fun k -> match !r with | LazyCached seq' -> seq' k | LazySuspend -> (* here if this traversal is interruted, no caching occurs *) - let seq' = MList.of_seq_with seq k in + let seq' = MList.of_seq_with ?init_size seq k in r := LazyCached (MList.to_seq seq') (** Sort the sequence. Eager, O(n) ram and O(n ln(n)) time. *) diff --git a/sequence.mli b/sequence.mli index 39444ee..d903a40 100644 --- a/sequence.mli +++ b/sequence.mli @@ -151,13 +151,16 @@ val fmap : ('a -> 'b option) -> 'a t -> 'b t val intersperse : 'a -> 'a t -> 'a t (** Insert the single element between every element of the sequence *) -val persistent : 'a t -> 'a t +val persistent : ?init_size:int -> 'a t -> 'a t (** Iterate on the sequence, storing elements in a data structure. The resulting sequence can be iterated on as many times as needed. {b Note}: calling persistent on an already persistent sequence - will still make a new copy of the sequence! *) + will still make a new copy of the sequence! -val persistent_lazy : 'a t -> 'a t + The optional argument [init_size] control the initial size of the underlying data structure. For very small sequences, it is more efficient to have [init_size] bigger than the length of your sequence. + *) + +val persistent_lazy : ?init_size:int -> 'a t -> 'a t (** Lazy version of {!persistent}. When calling [persistent_lazy s], a new sequence [s'] is immediately returned (without actually consuming [s]) in constant time; the first time [s'] is iterated on, @@ -166,7 +169,10 @@ val persistent_lazy : 'a t -> 'a t {b warning}: on the first traversal of [s'], if the traversal is interrupted prematurely ({!take}, etc.) then [s'] will not be - memorized, and the next call to [s'] will traverse [s] again. *) + memorized, and the next call to [s'] will traverse [s] again. + + The optional argument [init_size] control the initial size of the underlying data structure. For very small sequences, it is more efficient to have [init_size] bigger than the length of your sequence. + *) val sort : ?cmp:('a -> 'a -> int) -> 'a t -> 'a t (** Sort the sequence. Eager, O(n) ram and O(n ln(n)) time.