-
Notifications
You must be signed in to change notification settings - Fork 176
Add iterator for combinations #4735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/Combinatorics/EnumerativeCombinatorics/combinations.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| @doc raw""" | ||
| combinations(n::Int, k::Int) | ||
|
|
||
| Return an iterator over all $k$-combinations of ${1,...,n}$, produced in | ||
| lexicographically ascending order. | ||
|
|
||
| # Examples | ||
|
|
||
| ```jldoctest | ||
| julia> C = combinations(4, 3) | ||
| Iterator over the 3-combinations of 1:4 | ||
|
|
||
| julia> collect(C) | ||
| 4-element Vector{Combination{Int64}}: | ||
| [1, 2, 3] | ||
| [1, 2, 4] | ||
| [1, 3, 4] | ||
| [2, 3, 4] | ||
| ``` | ||
| """ | ||
| combinations(n::Int, k::Int) = Combinations(1:n, k) | ||
|
|
||
| @doc raw""" | ||
| combinations(v::AbstractVector, k::Int) | ||
|
|
||
| Return an iterator over all `k`-combinations of a given vector `v` produced in | ||
| lexicographically ascending order of the indices. | ||
|
|
||
| # Examples | ||
|
|
||
| ```jldoctest | ||
| julia> C = combinations(['a', 'b', 'c', 'd'], 3) | ||
| Iterator over the 3-combinations of ['a', 'b', 'c', 'd'] | ||
|
|
||
| julia> collect(C) | ||
| 4-element Vector{Combination{Char}}: | ||
| ['a', 'b', 'c'] | ||
| ['a', 'b', 'd'] | ||
| ['a', 'c', 'd'] | ||
| ['b', 'c', 'd'] | ||
| ``` | ||
| """ | ||
| function combinations(v::AbstractVector{T}, k::Int) where T | ||
| return Combinations(v, k) | ||
| end | ||
|
|
||
| Combinations(v::AbstractArray{T}, k::Int) where T = Combinations(v, length(v), k) | ||
|
|
||
| @inline function Base.iterate(C::Combinations, state = [min(C.k - 1, i) for i in 1:C.k]) | ||
| if C.k == 0 # special case to generate 1 result for k = 0 | ||
| if isempty(state) | ||
| return Combination(eltype(C.v)[]), [0] | ||
| end | ||
| return nothing | ||
| end | ||
| for i in C.k:-1:1 | ||
| state[i] += 1 | ||
| if state[i] > (C.n - (C.k - i)) | ||
| continue | ||
| end | ||
| for j in i+1:C.k | ||
| state[j] = state[j - 1] + 1 | ||
| end | ||
| break | ||
| end | ||
| if state[1] > C.n - C.k + 1 | ||
| return nothing | ||
| end | ||
| return Combination(C.v[state]), state | ||
| end | ||
|
|
||
| Base.length(C::Combinations) = binomial(C.n, C.k) | ||
|
|
||
| Base.eltype(::Type{Combinations{T}}) where {T} = Combination{eltype(T)} | ||
|
|
||
| function Base.show(io::IO, C::Combinations) | ||
| print(io, "Iterator over the ", C.k, "-combinations of ", C.v) | ||
| end | ||
|
|
||
| ################################################################################ | ||
| # | ||
| # Array-like functionality | ||
| # | ||
| ################################################################################ | ||
|
|
||
| function Base.show(io::IO, ::MIME"text/plain", P::Combination) | ||
| p = data(P) | ||
| if isempty(p) | ||
| print(io, "Empty combination") | ||
| return | ||
| end | ||
| print(io, p) | ||
| end | ||
|
|
||
| data(P::Combination) = P.v | ||
|
|
||
| function Base.size(P::Combination) | ||
| return size(data(P)) | ||
| end | ||
|
|
||
| function Base.length(P::Combination) | ||
| return length(data(P)) | ||
| end | ||
|
|
||
| function Base.getindex(P::Combination, i::IntegerUnion) | ||
| return getindex(data(P), Int(i)) | ||
| end | ||
|
|
||
| function Base.setindex!(P::Combination, x::IntegerUnion, i::IntegerUnion) | ||
| return setindex!(data(P), x, i) | ||
| end | ||
|
|
||
| function Base.copy(P::Combination) | ||
| return Combination(copy(data(P))) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
test/Combinatorics/EnumerativeCombinatorics/combinations.jl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| @testset "combinations" begin | ||
| let | ||
| C = combinations(4, 3) | ||
| @test length(C) == 4 | ||
| @test collect(C) == [[1, 2, 3], | ||
| [1, 2, 4], | ||
| [1, 3, 4], | ||
| [2, 3, 4]] | ||
| @test eltype(C) === Combination{Int} | ||
| C = combinations(100, 3) | ||
| @test length(C) == binomial(100, 3) | ||
| C = combinations(4, 5) | ||
| @test length(C) == 0 | ||
|
|
||
| C = combinations('a':'d', 3) | ||
| @test length(C) == 4 | ||
| @test collect(C) == [['a', 'b', 'c'], | ||
| ['a', 'b', 'd'], | ||
| ['a', 'c', 'd'], | ||
| ['b', 'c', 'd']] | ||
| @test eltype(C) === Combination{Char} | ||
| C = combinations('a':'d', 10) | ||
| @test length(C) == 0 | ||
| end | ||
|
|
||
| for n in 1:8, k in 1:n | ||
| @test collect(combinations(n, k)) == collect(combinations(1:n, k)) | ||
| end | ||
|
|
||
| @test collect(combinations(["1", "2", "3", "4"], 0)) == [String[]] | ||
| @test collect(combinations(["1", "2", "3", "4"], 1)) == [["1"], ["2"], ["3"], ["4"]] | ||
| @test collect(combinations(["1", "2", "3", "4"], 2)) == | ||
| [["1", "2"], ["1", "3"], ["1", "4"], ["2", "3"], ["2", "4"], ["3", "4"]] | ||
| @test collect(combinations(["1", "2", "3", "4"], 3)) == | ||
| [["1", "2", "3"], ["1", "2", "4"], ["1", "3", "4"], ["2", "3", "4"]] | ||
| @test collect(combinations(["1", "2", "3", "4"], 4)) == [["1", "2", "3", "4"]] | ||
| @test collect(combinations(["1", "2", "3", "4"], 5)) == String[] | ||
| @test collect(combinations(["1", "2", "3", "4"], 6)) == String[] | ||
|
|
||
| v = collect(combinations(5, 3)) | ||
| @test issorted(v) | ||
| @test allunique(v) | ||
|
|
||
| @test collect(combinations(0, 0)) == [Int[]] | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.