References
Index
ChunkSplitters.chunks
ChunkSplitters.index_chunks
ChunkSplitters.Consecutive
ChunkSplitters.RoundRobin
ChunkSplitters.Split
Iterators
ChunkSplitters.index_chunks
— Functionindex_chunks(collection;
n::Union{Nothing, Integer}=nothing,
size::Union{Nothing, Integer}=nothing,
[split::Split=Consecutive(),]
[minsize::Union{Nothing,Integer}=nothing,]
)
Returns an iterator that splits the indices of collection
into n
-many chunks (if n
is given) or into chunks of a certain size (if size
is given). The returned iterator can be used to process chunks of indices of collection
one after another. If you want to process chunks of elements of collection
, check out chunks(...)
instead.
The keyword arguments n
and size
are mutually exclusive.
Keyword arguments (optional)
split
can be used to determine the splitting strategy, i.e. the distribution of the indices among chunks. Ifsplit = Consecutive()
(default), chunks will hold consecutive indices and will hold approximately the same number of indices (as far as possible). Ifsplit = RoundRobin()
, indices will be assigned to chunks in a round-robin fashion.minsize
can be used to specify the minimum size of a chunk, and can be used in combination with then
keyword. If, for the givenn
, the chunks are smaller thanminsize
, the number of chunks will be decreased to ensure that each chunk is at leastminsize
long.
Noteworthy
If you need a running chunk index you can combine chunks
with enumerate
. In particular, enumerate(index_chunks(...))
can be used in conjuction with @threads
.
Requirements
The type of the input collection
must have at least firstindex
, lastindex
, and length
functions defined, as well as ChunkSplitters.is_chunkable(::typeof(collection)) = true
. Out of the box, AbstractArray
s and Tuple
s are supported.
Examples
julia> using ChunkSplitters
julia> x = rand(7);
julia> collect(index_chunks(x; n=3))
3-element Vector{UnitRange{Int64}}:
1:3
4:5
6:7
julia> collect(enumerate(index_chunks(x; n=3)))
3-element Vector{Tuple{Int64, UnitRange{Int64}}}:
(1, 1:3)
(2, 4:5)
(3, 6:7)
julia> collect(index_chunks(1:7; size=3))
3-element Vector{UnitRange{Int64}}:
1:3
4:6
7:7
ChunkSplitters.chunks
— Functionchunks(collection;
n::Union{Nothing, Integer}=nothing,
size::Union{Nothing, Integer}=nothing,
[split::Split=Consecutive(),]
[minsize::Union{Nothing,Integer}=nothing,]
)
Returns an iterator that splits the elements of collection
into n
-many chunks (if n
is given) or into chunks of a certain size (if size
is given). To avoid copies, chunks will generally hold a view into the original collection. The returned iterator can be used to process chunks of elements of collection
one after another. If you want to process chunks of indices of collection
, check out index_chunks(...)
instead.
The keyword arguments n
and size
are mutually exclusive.
Keyword arguments (optional)
split
can be used to determine the splitting strategy, i.e. the distribution of the indices among chunks. Ifsplit = Consecutive()
(default), chunks will hold consecutive elements and will hold approximately the same number of elements (as far as possible). Ifsplit = RoundRobin()
, elements will be assigned to chunks in a round-robin fashion.minsize
can be used to specify the minimum size of a chunk, and can be used in combination with then
keyword. If, for the givenn
, the chunks are smaller thanminsize
, the number of chunks will be decreased to ensure that each chunk is at leastminsize
long.
Noteworthy
If you need a running chunk index you can combine chunks
with enumerate
. In particular, enumerate(chunks(...))
can be used in conjuction with @threads
.
Requirements
In addition to the requirements for index_chunks
(see docstring), the type of the input collection
must have an implementation of view
, especially view(::typeof(collection), ::UnitRange)
and view(::typeof(collection), ::StepRange)
. Out of the box, AbstractArray
s and Tuple
s are supported.
Examples
julia> using ChunkSplitters
julia> x = [1.2, 3.4, 5.6, 7.8, 9.0];
julia> collect(chunks(x; n=3))
3-element Vector{SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}}:
[1.2, 3.4]
[5.6, 7.8]
[9.0]
julia> collect(enumerate(chunks(x; n=3)))
3-element Vector{Tuple{Int64, SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}}}:
(1, [1.2, 3.4])
(2, [5.6, 7.8])
(3, [9.0])
julia> collect(chunks(x; size=3))
2-element Vector{SubArray{Float64, 1, Vector{Float64}, Tuple{UnitRange{Int64}}, true}}:
[1.2, 3.4, 5.6]
[7.8, 9.0]
Splitting strategies
ChunkSplitters.Split
— TypeSubtypes can be used to indicate a splitting strategy for chunks
and index_chunks
(split
keyword argument).
ChunkSplitters.Consecutive
— TypeChunks will hold consecutive indices/elements and approximately the same number of them (as far as possible).
ChunkSplitters.RoundRobin
— TypeElements/indices will be assigned to chunks in a round-robin fashion.