Skip to content

Commit

Permalink
rename minchunksize -> minsize
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenbauer committed Sep 24, 2024
1 parent 7c5b8a4 commit 0bad0d8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions docs/src/gettingstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ inds = 7:7

Note that if `n` is set, chunks will have the most even distribution of sizes possible. If `size` is set, chunks will have the same size, except, possibly, the very last chunk.

When using `n`, we also support a `minchunksize` keyword argument that allows you to set a desired minimum chunk size. This will soften the effect of `n` and will decrease the number of chunks if the size of each chunk is too small.
When using `n`, we also support a `minsize` keyword argument that allows you to set a desired minimum chunk size. This will soften the effect of `n` and will decrease the number of chunks if the size of each chunk is too small.

```jldoctest; setup=:(using ChunkSplitters; x = [1.2, 3.4, 5.6, 7.8, 9.1, 10.11, 11.12];)
julia> collect(index_chunks(x; n=5, minchunksize=2))
julia> collect(index_chunks(x; n=5, minsize=2))
3-element Vector{UnitRange{Int64}}:
1:3
4:5
6:7
```

Note how only three chunks were created, because the `minchunksize` option took precedence over `n`.
Note how only three chunks were created, because the `minsize` option took precedence over `n`.

## Enumeration

Expand Down
16 changes: 8 additions & 8 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
n::Union{Nothing, Integer}=nothing,
size::Union{Nothing, Integer}=nothing,
[split::Split=Consecutive(),]
[minchunksize::Union{Nothing,Integer}=nothing,]
[minsize::Union{Nothing,Integer}=nothing,]
)
Returns an iterator that splits the *indices* of `collection` into
Expand All @@ -22,10 +22,10 @@ The keyword arguments `n` and `size` are mutually exclusive.
approximately the same number of indices (as far as possible).
If `split = RoundRobin()`, indices will be assigned to chunks in a round-robin fashion.
* `minchunksize` can be used to specify the minimum size of a chunk,
* `minsize` can be used to specify the minimum size of a chunk,
and can be used in combination with the `n` keyword. If, for the given `n`, the chunks
are smaller than `minchunksize`, the number of chunks will be decreased to ensure that
each chunk is at least `minchunksize` long.
are smaller than `minsize`, the number of chunks will be decreased to ensure that
each chunk is at least `minsize` long.
### Noteworthy
Expand Down Expand Up @@ -72,7 +72,7 @@ function index_chunks end
n::Union{Nothing, Integer}=nothing,
size::Union{Nothing, Integer}=nothing,
[split::Split=Consecutive(),]
[minchunksize::Union{Nothing,Integer}=nothing,]
[minsize::Union{Nothing,Integer}=nothing,]
)
Returns an iterator that splits the *elements* of `collection` into
Expand All @@ -92,10 +92,10 @@ The keyword arguments `n` and `size` are mutually exclusive.
approximately the same number of elements (as far as possible).
If `split = RoundRobin()`, elements will be assigned to chunks in a round-robin fashion.
* `minchunksize` can be used to specify the minimum size of a chunk,
* `minsize` can be used to specify the minimum size of a chunk,
and can be used in combination with the `n` keyword. If, for the given `n`, the chunks
are smaller than `minchunksize`, the number of chunks will be decreased to ensure that
each chunk is at least `minchunksize` long.
are smaller than `minsize`, the number of chunks will be decreased to ensure that
each chunk is at least `minsize` long.
### Noteworthy
Expand Down
32 changes: 16 additions & 16 deletions src/internals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ struct IndexChunks{T,C<:Constraint,S<:Split} <: AbstractChunksIterator{T,C,S}
size::Int
end

function ViewChunks(s::Split; collection, n=nothing, size=nothing, minchunksize=nothing)
function ViewChunks(s::Split; collection, n=nothing, size=nothing, minsize=nothing)
is_chunkable(collection) || err_not_chunkable(collection)
isnothing(n) && isnothing(size) && err_missing_input()
!isnothing(size) && !isnothing(n) && err_mutually_exclusive("size", "n")
C, n, size = _set_C_n_size(collection, n, size, minchunksize)
C, n, size = _set_C_n_size(collection, n, size, minsize)
return ViewChunks{typeof(collection),C,typeof(s)}(collection, n, size)
end

function IndexChunks(s::Split; collection, n=nothing, size=nothing, minchunksize=nothing)
function IndexChunks(s::Split; collection, n=nothing, size=nothing, minsize=nothing)
is_chunkable(collection) || err_not_chunkable(collection)
isnothing(n) && isnothing(size) && err_missing_input()
!isnothing(size) && !isnothing(n) && err_mutually_exclusive("size", "n")
C, n, size = _set_C_n_size(collection, n, size, minchunksize)
C, n, size = _set_C_n_size(collection, n, size, minsize)
return IndexChunks{typeof(collection),C,typeof(s)}(collection, n, size)
end

Expand All @@ -42,19 +42,19 @@ function index_chunks(collection;
n::Union{Nothing,Integer}=nothing,
size::Union{Nothing,Integer}=nothing,
split::Split=Consecutive(),
minchunksize::Union{Nothing,Integer}=nothing,
minsize::Union{Nothing,Integer}=nothing,
)
return IndexChunks(split; collection, n, size, minchunksize)
return IndexChunks(split; collection, n, size, minsize)
end

# public API
function chunks(collection;
n::Union{Nothing,Integer}=nothing,
size::Union{Nothing,Integer}=nothing,
split::Split=Consecutive(),
minchunksize::Union{Nothing,Integer}=nothing,
minsize::Union{Nothing,Integer}=nothing,
)
return ViewChunks(split; collection, n, size, minchunksize)
return ViewChunks(split; collection, n, size, minsize)
end

# public API
Expand All @@ -63,19 +63,19 @@ is_chunkable(::AbstractArray) = true
is_chunkable(::Tuple) = true
is_chunkable(::AbstractChunksIterator) = true

_set_minchunksize(minchunksize::Nothing) = 1
function _set_minchunksize(minchunksize::Integer)
minchunksize < 1 && throw(ArgumentError("minchunksize must be >= 1"))
return minchunksize
_set_minsize(minsize::Nothing) = 1
function _set_minsize(minsize::Integer)
minsize < 1 && throw(ArgumentError("minsize must be >= 1"))
return minsize
end
function _set_C_n_size(collection, n::Nothing, size::Integer, minchunksize)
!isnothing(minchunksize) && err_mutually_exclusive("size", "minchunksize")
function _set_C_n_size(collection, n::Nothing, size::Integer, minsize)
!isnothing(minsize) && err_mutually_exclusive("size", "minsize")
size < 1 && throw(ArgumentError("size must be >= 1"))
return FixedSize, 0, size
end
function _set_C_n_size(collection, n::Integer, size::Nothing, minchunksize)
function _set_C_n_size(collection, n::Integer, size::Nothing, minsize)
n < 1 && throw(ArgumentError("n must be >= 1"))
mcs = _set_minchunksize(minchunksize)
mcs = _set_minsize(minsize)
nmax = min(length(collection) ÷ mcs, n)
FixedCount, nmax, 0
end
Expand Down
10 changes: 5 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ end
local c = f(1:l; size=s)
@test all(length(c[i]) == length(c[i+1]) for i in 1:length(c)-2) # only the last chunk may have different length
end
@test collect(f(1:10; n=2, minchunksize=2)) == [1:5, 6:10]
@test collect(f(1:10; n=5, minchunksize=3)) == [1:4, 5:7, 8:10]
@test collect(f(1:11; n=10, minchunksize=3)) == [1:4, 5:8, 9:11]
@test_throws ArgumentError f(1:10; n=2, minchunksize=0)
@test_throws ArgumentError f(1:10; size=2, minchunksize=2)
@test collect(f(1:10; n=2, minsize=2)) == [1:5, 6:10]
@test collect(f(1:10; n=5, minsize=3)) == [1:4, 5:7, 8:10]
@test collect(f(1:11; n=10, minsize=3)) == [1:4, 5:8, 9:11]
@test_throws ArgumentError f(1:10; n=2, minsize=0)
@test_throws ArgumentError f(1:10; size=2, minsize=2)
end
end
end
Expand Down

0 comments on commit 0bad0d8

Please sign in to comment.