Skip to content

Commit

Permalink
fix conversion among cycle, sparse, list
Browse files Browse the repository at this point in the history
	modified:   src/PermPlain.jl
  • Loading branch information
jlapeyre committed Dec 12, 2014
1 parent eed1574 commit 65afb80
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions src/PermPlain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ function listtosparse{T<:Real}(p::AbstractVector{T})
pv = p[i]
if pv != i
data[i] = pv
# data[pv] = i # correct order!
pv > maxk ? maxk = pv : nothing
end
end
Expand All @@ -498,13 +499,14 @@ function cycstosparse{T<:Real}(cycs::AbstractArray{Array{T,1},1})
data = Dict{T,T}()
maxk = zero(T)
for c in cycs
data[c[1]] = c[end]
# println(" *** doing ", c[end], " ", c[1])
for i in 2:length(c)
# println("doing ", c[i], " ", c[i-1])
pv = c[i]
pv = c[1]
data[c[end]] = pv
# pv > maxk ? maxk = pv : nothing
c[end] > maxk ? maxk = c[end] : nothing
for i in 1:length(c)-1
pv = c[i]
data[pv] = c[i+1]
pv > maxk ? maxk = pv : nothing
data[pv] = c[i-1]
end
end
return (data,maxk)
Expand All @@ -513,14 +515,13 @@ end
function sparsetolist{T}(sp::Dict{T,T})
p = [one(T):convert(T,maximum(sp)[1])]
for (i,v) in sp
p[v] = i
p[i] = v
end
return p
end

function sparsetocycles{T}(sp::Dict{T,T})
cycs = Array(Array{T,1},0)
# ks = collect(values(sp))
ks = collect(keys(sp))
n = length(ks)
seen = Dict{T,Bool}()
Expand All @@ -539,26 +540,57 @@ function sparsetocycles{T}(sp::Dict{T,T})
didsee == false ? (push!(cycs,cyc); break) : nothing
k1 = k
cyc = Array(T,0)
# push!(cyc,k)
# println(" out pushing $k")
nseen = nseen + 1
while true
push!(cyc,k)
# println(" 1 in pushing $k")
seen[k] = true
k = sp[k]
nseen = nseen + 1
if k == k1
break
end
# println(" 2 in pushing $k")
# push!(cyc,k)
end
push!(cycs,reverse!(cyc)) # inefficient
# reverse!(cyc) # inefficient
push!(cycs,cyc)
end
return cycs
end

function sparsecycleslengths{T}(sp::Dict{T,T})
cyclens = Array(Int,0)
ks = collect(keys(sp))
n = length(ks)
seen = Dict{T,Bool}()
for k in ks seen[k] = false end
k = ks[1]
nseen = 0
while nseen <= n
didsee = false
for i in ks
if seen[i] == false
k = i
didsee = true
break
end
end
didsee == false ? (push!(cyclens,nincyc); break) : nothing
k1 = k
nincyc = 0
nseen = nseen + 1
while true
nincyc += 1
seen[k] = true
k = sp[k]
nseen = nseen + 1
if k == k1
break
end
end
push!(cyclens,nincyc) # inefficient
end
return cyclens
end


## Output ##

Expand Down

0 comments on commit 65afb80

Please sign in to comment.