mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 03:05:29 -05:00
Add an argument to Sequence.persistent to choose initial chunk size.
This commit is contained in:
parent
dcbee64684
commit
e4691be2a5
2 changed files with 18 additions and 12 deletions
16
sequence.ml
16
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. *)
|
||||
|
|
|
|||
14
sequence.mli
14
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.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue