Skip to content

Commit

Permalink
Use method dispatch on Val{}
Browse files Browse the repository at this point in the history
  • Loading branch information
Samayel committed Sep 13, 2015
1 parent 6282b48 commit 0a049e8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 74 deletions.
8 changes: 2 additions & 6 deletions src/Math/Combinatorics/combination.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

# combinations(a, k) = Base.combinations(a, k)

Base.combinations{T}(a::AbstractArray{T,1}, k::Integer, mode::Symbol) = begin
mode [:unique, :repeated] || error("Mode must be :unique or :repeated")
mode == :repeated ?
multicombinations(a, k) :
combinations(a, k)
end
Base.combinations{T}(a::AbstractArray{T,1}, k::Integer, ::Val{:unique}) = combinations(a, k)
Base.combinations{T}(a::AbstractArray{T,1}, k::Integer, ::Val{:repeated}) = multicombinations(a, k)
10 changes: 4 additions & 6 deletions src/Math/Combinatorics/permutation.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@

# permutations(a) = Base.permutations(a)

Base.permutations{T}(a::AbstractArray{T,1}, mode::Symbol) =
permutations(a, length(a), mode)
Base.permutations{T}(a::AbstractArray{T,1}, k::Integer, mode::Symbol) = begin
mode [:unique, :repeated] || error("Mode must be :unique or :repeated")
Permutations(a, k, mode == :repeated)
end
Base.permutations{T}(a::AbstractArray{T,1}, mode::Val{:unique}) = permutations(a, length(a), mode)
Base.permutations{T}(a::AbstractArray{T,1}, mode::Val{:repeated}) = permutations(a, length(a), mode)
Base.permutations{T}(a::AbstractArray{T,1}, k::Integer, ::Val{:unique}) = Permutations(a, k, false)
Base.permutations{T}(a::AbstractArray{T,1}, k::Integer, ::Val{:repeated}) = Permutations(a, k, true)

immutable Permutations{T}
a::T
Expand Down
46 changes: 23 additions & 23 deletions test/Math/Combinatorics/combination.jl
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
function test_combination_combinations()
@test collect(combinations(Int[], 0, :unique)) == Array{Int64,1}[[]]
@test length(combinations(Int[], 0, :unique)) == 1
@test eltype(combinations(Int[], 0, :unique)) == Array{Int64,1}
@test collect(combinations(Int[], 0, Val{:unique}())) == Array{Int64,1}[[]]
@test length(combinations(Int[], 0, Val{:unique}())) == 1
@test eltype(combinations(Int[], 0, Val{:unique}())) == Array{Int64,1}

@test collect(combinations(Int[], 1, :unique)) == Array{Int64,1}[]
@test length(combinations(Int[], 1, :unique)) == 0
@test eltype(combinations(Int[], 1, :unique)) == Array{Int64,1}
@test collect(combinations(Int[], 1, Val{:unique}())) == Array{Int64,1}[]
@test length(combinations(Int[], 1, Val{:unique}())) == 0
@test eltype(combinations(Int[], 1, Val{:unique}())) == Array{Int64,1}

@test collect(combinations(["a", "b", "c", "d"], 2, :unique)) == Array[
@test collect(combinations(["a", "b", "c", "d"], 2, Val{:unique}())) == Array[
["a", "b"],
["a", "c"],
["a", "d"],
["b", "c"],
["b", "d"],
["c", "d"],
]
@test length(combinations(["a", "b", "c", "d"], 2, :unique)) == 6
@test eltype(combinations(["a", "b", "c", "d"], 2, :unique)) == Array{ASCIIString,1}
@test length(combinations(["a", "b", "c", "d"], 2, Val{:unique}())) == 6
@test eltype(combinations(["a", "b", "c", "d"], 2, Val{:unique}())) == Array{ASCIIString,1}

@test collect(combinations(["a", "b"], 3, :unique)) == Array{ASCIIString,1}[]
@test length(combinations(["a", "b"], 3, :unique)) == 0
@test collect(combinations(["a", "b"], 3, Val{:unique}())) == Array{ASCIIString,1}[]
@test length(combinations(["a", "b"], 3, Val{:unique}())) == 0
end

function test_combination_combinations_with_repetition()
@test collect(combinations(Int[], 0, :repeated)) == Array{Int64,1}[[]]
@test length(combinations(Int[], 0, :repeated)) == 1
@test eltype(combinations(Int[], 0, :repeated)) == Array{Int64,1}
@test collect(combinations(Int[], 0, Val{:repeated}())) == Array{Int64,1}[[]]
@test length(combinations(Int[], 0, Val{:repeated}())) == 1
@test eltype(combinations(Int[], 0, Val{:repeated}())) == Array{Int64,1}

@test collect(combinations(Int[], 1, :repeated)) == Array{Int64,1}[]
@test length(combinations(Int[], 1, :repeated)) == 0
@test eltype(combinations(Int[], 1, :repeated)) == Array{Int64,1}
@test collect(combinations(Int[], 1, Val{:repeated}())) == Array{Int64,1}[]
@test length(combinations(Int[], 1, Val{:repeated}())) == 0
@test eltype(combinations(Int[], 1, Val{:repeated}())) == Array{Int64,1}

@test collect(combinations(["a", "b", "c", "d"], 2, :repeated)) == Array[
@test collect(combinations(["a", "b", "c", "d"], 2, Val{:repeated}())) == Array[
["a", "a"],
["a", "b"],
["a", "c"],
Expand All @@ -43,20 +43,20 @@ function test_combination_combinations_with_repetition()
["c", "d"],
["d", "d"],
]
@test length(combinations(["a", "b", "c", "d"], 2, :repeated)) == 10
@test eltype(combinations(["a", "b", "c", "d"], 2, :repeated)) == Array{ASCIIString,1}
@test length(combinations(["a", "b", "c", "d"], 2, Val{:repeated}())) == 10
@test eltype(combinations(["a", "b", "c", "d"], 2, Val{:repeated}())) == Array{ASCIIString,1}

@test collect(combinations(["a", "b"], 3, :repeated)) == Array[
@test collect(combinations(["a", "b"], 3, Val{:repeated}())) == Array[
["a", "a", "a"],
["a", "a", "b"],
["a", "b", "b"],
["b", "b", "b"],
]
@test length(combinations(["a", "b"], 3, :repeated)) == 4
@test length(combinations(["a", "b"], 3, Val{:repeated}())) == 4
end

function test_combination_combinations_unknown_mode()
@test_throws ErrorException combinations(["a", "b", "c", "d"], 2, :unknown)
@test_throws MethodError combinations(["a", "b", "c", "d"], 2, Val{:unknown}())
end

function test_combination_all()
Expand Down
78 changes: 39 additions & 39 deletions test/Math/Combinatorics/permutation.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
function test_permutation_permutations()
@test collect(permutations(Int[], :unique)) == Array{Int64,1}[[]]
@test length(permutations(Int[], :unique)) == 1
@test eltype(permutations(Int[], :unique)) == Array{Int64,1}
@test collect(permutations(Int[], Val{:unique}())) == Array{Int64,1}[[]]
@test length(permutations(Int[], Val{:unique}())) == 1
@test eltype(permutations(Int[], Val{:unique}())) == Array{Int64,1}

@test collect(permutations(["a", "b", "c"], :unique)) == Array[
@test collect(permutations(["a", "b", "c"], Val{:unique}())) == Array[
["a", "b", "c"],
["a", "c", "b"],
["b", "a", "c"],
["b", "c", "a"],
["c", "a", "b"],
["c", "b", "a"],
]
@test length(permutations(["a", "b", "c"], :unique)) == 6
@test eltype(permutations(["a", "b", "c"], :unique)) == Array{ASCIIString,1}
@test length(permutations(["a", "b", "c"], Val{:unique}())) == 6
@test eltype(permutations(["a", "b", "c"], Val{:unique}())) == Array{ASCIIString,1}
end

function test_permutation_permutations_with_repetition()
@test collect(permutations(Int[], :repeated)) == Array{Int64,1}[[]]
@test length(permutations(Int[], :repeated)) == 1
@test eltype(permutations(Int[], :repeated)) == Array{Int64,1}
@test collect(permutations(Int[], Val{:repeated}())) == Array{Int64,1}[[]]
@test length(permutations(Int[], Val{:repeated}())) == 1
@test eltype(permutations(Int[], Val{:repeated}())) == Array{Int64,1}

@test collect(permutations(["a", "b", "c"], :repeated)) == Array[
@test collect(permutations(["a", "b", "c"], Val{:repeated}())) == Array[
["a", "a", "a"],
["a", "a", "b"],
["a", "a", "c"],
Expand Down Expand Up @@ -49,24 +49,24 @@ function test_permutation_permutations_with_repetition()
["c", "c", "b"],
["c", "c", "c"],
]
@test length(permutations(["a", "b", "c"], :repeated)) == 27
@test eltype(permutations(["a", "b", "c"], :repeated)) == Array{ASCIIString,1}
@test length(permutations(["a", "b", "c"], Val{:repeated}())) == 27
@test eltype(permutations(["a", "b", "c"], Val{:repeated}())) == Array{ASCIIString,1}
end

function test_permutation_permutations_unknown_mode()
@test_throws ErrorException permutations(["a", "b", "c"], :unknown)
@test_throws MethodError permutations(["a", "b", "c"], Val{:unknown}())
end

function test_permutation_kpermutations()
@test collect(permutations(Int[], 0, :unique)) == Array{Int64,1}[[]]
@test length(permutations(Int[], 0, :unique)) == 1
@test eltype(permutations(Int[], 0, :unique)) == Array{Int64,1}
@test collect(permutations(Int[], 0, Val{:unique}())) == Array{Int64,1}[[]]
@test length(permutations(Int[], 0, Val{:unique}())) == 1
@test eltype(permutations(Int[], 0, Val{:unique}())) == Array{Int64,1}

@test collect(permutations(Int[], 1, :unique)) == Array{Int64,1}[]
@test length(permutations(Int[], 1, :unique)) == 0
@test eltype(permutations(Int[], 1, :unique)) == Array{Int64,1}
@test collect(permutations(Int[], 1, Val{:unique}())) == Array{Int64,1}[]
@test length(permutations(Int[], 1, Val{:unique}())) == 0
@test eltype(permutations(Int[], 1, Val{:unique}())) == Array{Int64,1}

@test collect(permutations(["a", "b", "c", "d"], 2, :unique)) == Array[
@test collect(permutations(["a", "b", "c", "d"], 2, Val{:unique}())) == Array[
["a", "b"],
["a", "c"],
["a", "d"],
Expand All @@ -80,10 +80,10 @@ function test_permutation_kpermutations()
["d", "b"],
["d", "c"],
]
@test length(permutations(["a", "b", "c", "d"], 2, :unique)) == 12
@test eltype(permutations(["a", "b", "c", "d"], 2, :unique)) == Array{ASCIIString,1}
@test length(permutations(["a", "b", "c", "d"], 2, Val{:unique}())) == 12
@test eltype(permutations(["a", "b", "c", "d"], 2, Val{:unique}())) == Array{ASCIIString,1}

@test collect(permutations(["a", "b", "c", "d"], 3, :unique)) == Array[
@test collect(permutations(["a", "b", "c", "d"], 3, Val{:unique}())) == Array[
["a", "b", "c"],
["a", "b", "d"],
["a", "c", "b"],
Expand All @@ -109,23 +109,23 @@ function test_permutation_kpermutations()
["d", "c", "a"],
["d", "c", "b"],
]
@test length(permutations(["a", "b", "c", "d"], 3, :unique)) == 24
@test eltype(permutations(["a", "b", "c", "d"], 3, :unique)) == Array{ASCIIString,1}
@test length(permutations(["a", "b", "c", "d"], 3, Val{:unique}())) == 24
@test eltype(permutations(["a", "b", "c", "d"], 3, Val{:unique}())) == Array{ASCIIString,1}

@test collect(permutations(["a", "b"], 3, :unique)) == Array{ASCIIString,1}[]
@test length(permutations(["a", "b"], 3, :unique)) == 0
@test collect(permutations(["a", "b"], 3, Val{:unique}())) == Array{ASCIIString,1}[]
@test length(permutations(["a", "b"], 3, Val{:unique}())) == 0
end

function test_permutation_kpermutations_with_repetition()
@test collect(permutations(Int[], 0, :repeated)) == Array{Int64,1}[[]]
@test length(permutations(Int[], 0, :repeated)) == 1
@test eltype(permutations(Int[], 0, :repeated)) == Array{Int64,1}
@test collect(permutations(Int[], 0, Val{:repeated}())) == Array{Int64,1}[[]]
@test length(permutations(Int[], 0, Val{:repeated}())) == 1
@test eltype(permutations(Int[], 0, Val{:repeated}())) == Array{Int64,1}

@test collect(permutations(Int[], 1, :repeated)) == Array{Int64,1}[]
@test length(permutations(Int[], 1, :repeated)) == 0
@test eltype(permutations(Int[], 1, :repeated)) == Array{Int64,1}
@test collect(permutations(Int[], 1, Val{:repeated}())) == Array{Int64,1}[]
@test length(permutations(Int[], 1, Val{:repeated}())) == 0
@test eltype(permutations(Int[], 1, Val{:repeated}())) == Array{Int64,1}

@test collect(permutations(["a", "b", "c", "d"], 2, :repeated)) == Array[
@test collect(permutations(["a", "b", "c", "d"], 2, Val{:repeated}())) == Array[
["a", "a"],
["a", "b"],
["a", "c"],
Expand All @@ -143,10 +143,10 @@ function test_permutation_kpermutations_with_repetition()
["d", "c"],
["d", "d"],
]
@test length(permutations(["a", "b", "c", "d"], 2, :repeated)) == 4^2
@test eltype(permutations(["a", "b", "c", "d"], 2, :repeated)) == Array{ASCIIString,1}
@test length(permutations(["a", "b", "c", "d"], 2, Val{:repeated}())) == 4^2
@test eltype(permutations(["a", "b", "c", "d"], 2, Val{:repeated}())) == Array{ASCIIString,1}

@test collect(permutations(["a", "b"], 3, :repeated)) == Array[
@test collect(permutations(["a", "b"], 3, Val{:repeated}())) == Array[
["a", "a", "a"],
["a", "a", "b"],
["a", "b", "a"],
Expand All @@ -156,11 +156,11 @@ function test_permutation_kpermutations_with_repetition()
["b", "b", "a"],
["b", "b", "b"],
]
@test length(permutations(["a", "b"], 3, :repeated)) == 2^3
@test length(permutations(["a", "b"], 3, Val{:repeated}())) == 2^3
end

function test_permutation_kpermutations_unknown_mode()
@test_throws ErrorException permutations(["a", "b", "c", "d"], 2, :unknown)
@test_throws MethodError permutations(["a", "b", "c", "d"], 2, Val{:unknown}())
end

function test_permutation_all()
Expand Down

0 comments on commit 0a049e8

Please sign in to comment.