diff --git a/src/PermPlain.jl b/src/PermPlain.jl index 72dcda3..6c71300 100644 --- a/src/PermPlain.jl +++ b/src/PermPlain.jl @@ -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 @@ -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) @@ -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}() @@ -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 ##