SimiLie
Loading...
Searching...
No Matches
permutation_parity.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8namespace sil {
9
10namespace misc {
11
12/*
13 Given a permutation of unique digits 0...N,
14 returns its parity: +1 for even parity; -1 for odd.
15 If lst contains duplicates, returns 0. If it contains no
16 duplicate but is not a permutation of 0...N, this is undetermined.
17 */
18template <std::size_t N>
19KOKKOS_FUNCTION inline constexpr int permutation_parity(std::array<std::size_t, N> const& lst)
20{
21 int parity = 1;
22 for (std::size_t i = 0; i < N; ++i)
23 for (std::size_t j = i + 1; j < N; ++j) {
24 if (lst[i] == lst[j])
25 return 0;
26 if (lst[i] > lst[j])
27 parity = -parity;
28 }
29 return parity;
30}
31
32} // namespace misc
33
34} // namespace sil
KOKKOS_FUNCTION constexpr int permutation_parity(std::array< std::size_t, N > const &lst)
The top-level namespace of SimiLie.
Definition csr.hpp:15