Skip to content

Commit e193e59

Browse files
committed
add non-allocating isperm function
1 parent e72ed73 commit e193e59

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/TupleTools.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,22 @@ Permute the elements of tuple `t` according to the permutation in `p`.
265265
@inline _permute(t::NTuple{N,Any}, p::NTuple{N,Int}) where {N} = getindices(t, p)
266266
@inline _permute(t::NTuple{N,Any}, p) where {N} = ntuple(n->t[p[n]], StaticLength(N))
267267

268+
"""
269+
isperm(p) -> ::Bool
270+
271+
A non-allocating alternative to Base.isperm(p) that is much faster for small permutations.
272+
"""
273+
function isperm(p)
274+
N = length(p)
275+
@inbounds for i = 1:N
276+
1 <= p[i] <= N || return false
277+
for j = i+1:N
278+
p[i] == p[j] && return false
279+
end
280+
end
281+
return true
282+
end
283+
268284
"""
269285
invperm(p::NTuple{N,Int}) -> ::NTuple{N,Int}
270286

0 commit comments

Comments
 (0)