julia> unique(collect(permutations([1;0;0;0;1])))10-element Array{Array{Int64,1},1}: [1,0,0,0,1] [1,0,0,1,0] [1,0,1,0,0] [1,1,0,0,0] [0,1,0,0,1] [0,1,0,1,0] [0,1,1,0,0] [0,0,1,0,1] [0,0,1,1,0] [0,0,0,1,1]
I don't know of a builtin function, but it looks like your code will first construct an array with all permutations and then reduce that to just the unique ones. I haven't used it yet, so hope I don't send you in the wrong direction, but you might have a look at Lazy.jl. That might help you avoid the initial step of constructing the full array.
#lexicographic premutations generation, By Donald Knuth
function lpremutations{T}(a::T)
b=Vector{T}()
sort!(a)
n=length(a)
while(true)
push!(b,copy(a))
j=n-1
while(a[j]>=a[j+1])
j-=1
j==0 && return(b)
end
l=n
while(a[j]>=a[l])
l-=1
end
tmp=a[l]
a[l]=a[j]
a[j]=tmp
k=j+1
l=n
while(k<l)
tmp=a[k]
a[k]=a[l]
a[l]=tmp
k+=1
l-=1
end
end
end
using StatsBase
using Iterators
function uniquepermutations(base)
zpos = Vector{Vector{Vector{Int}}}()
zval = Vector{Int}()
left = length(base)
for (v,c) in countmap(base)
push!(zpos,collect(subsets(collect(1:left),c)))
push!(zval,v)
left -= c
end
res = Vector{Vector{Int}}()
for a in product(zpos...)
slots = collect(1:length(base))
perm = zeros(length(base))
for (val,b) in zip(zval,a)
perm[slots[b]] = val
slots[b] = 0
slots = filter(x->x>0,slots)
end
push!(res,perm)
end
res
end
Familiarity with `countmap`,`subsets`,`product`,`zip` from StatsBase and Iterators helps. The logic is straight-forward.
function uniquepermutations(base)
zpos = Vector{Vector{Vector{Int}}}()
zval = Vector{eltype(base)}()
left = length(base)
for (v,c) in countmap(base)
push!(zpos,collect(subsets(collect(1:left),c)))
push!(zval,v)
left -= c
end
res = Vector{Vector{eltype(base)}}()
for a in product(zpos...)
slots = collect(1:length(base))
perm = similar(base)
I sense pull requests :P
for (i1,i2) in permutations(vector,indices=true,Gray=true)
theperm[[i1,i2]] = theperm[[i2,i1]]
@show theperm
end
would go over all permutations.
The syntax would be different and this is not a Gray order use-case.
But Gray ordering is useful and compact.