perm_to_lehm_idx!

Notedocblock
perm_to_lehm_idx!(P::Vector{<:Integer}, used::Vector{<:Integer}) -> Int

Version of perm_to_lehm_idx without allocations: convert a permutation P of 1:n into its 1-based lexicographic index, using the preallocated work vector used.

used is modified: on return, every entry is 1. Reset it with fill!(used, 0) before the next call. P is not modified.

Arguments

  • P: a permutation of 1:n, for example the result of sortperm.
  • used: work vector of length n whose entries are all zero on input.

Returns

  • The index of P, an integer in 1:factorial(n).

Examples

used = zeros(Int, 3)
StatsOrdinalPatterns.perm_to_lehm_idx!([3, 1, 2], used)  # 5
fill!(used, 0)  # required before the next call