Skip to content
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

Bring in deprecated permute!! and permutecols!! #314

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sanitizers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ function sorteigs!(ϵ::AbstractVector, ψ::AbstractMatrix)
p = Vector{Int}(undef, length(ϵ))
p´ = similar(p)
sortperm!(p, ϵ, by = real, alg = Base.DEFAULT_UNSTABLE)
Base.permute!!(ϵ, copy!(p´, p))
Base.permutecols!!(ψ, copy!(p´, p))
permute!!(ϵ, copy!(p´, p))
permutecols!!(ψ, copy!(p´, p))
return ϵ, ψ
end

Expand Down
54 changes: 54 additions & 0 deletions src/tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,60 @@ lengths_to_offsets(v::NTuple{<:Any,Integer}) = (0, cumsum(v)...)
lengths_to_offsets(v) = prepend!(cumsum(v), 0)
lengths_to_offsets(f::Function, v) = prepend!(accumulate((i,j) -> i + f(j), v; init = 0), 0)

# Taken from Base julia, now deprecated there
function permute!!(a, p::AbstractVector{<:Integer})
Base.require_one_based_indexing(a, p)
count = 0
start = 0
while count < length(a)
ptr = start = findnext(!iszero, p, start+1)::Int
temp = a[start]
next = p[start]
count += 1
while next != start
a[ptr] = a[next]
p[ptr] = 0
ptr = next
next = p[next]
count += 1
end
a[ptr] = temp
p[ptr] = 0
end
a
end

# like permute!! applied to each row of a, in-place in a (overwriting p).
function permutecols!!(a::AbstractMatrix, p::AbstractVector{<:Integer})
Base.require_one_based_indexing(a, p)
count = 0
start = 0
while count < length(p)
ptr = start = findnext(!iszero, p, start+1)::Int
next = p[start]
count += 1
while next != start
swapcols!(a, ptr, next)
p[ptr] = 0
ptr = next
next = p[next]
count += 1
end
p[ptr] = 0
end
a
end

function swapcols!(a::AbstractMatrix, i, j)
i == j && return
cols = axes(a,2)
@boundscheck i in cols || throw(BoundsError(a, (:,i)))
@boundscheck j in cols || throw(BoundsError(a, (:,j)))
for k in axes(a,1)
@inbounds a[k,i],a[k,j] = a[k,j],a[k,i]
end
end

#endregion

############################################################################################
Expand Down
Loading