File indexing completed on 2025-12-16 09:24:25
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <cmath>
0012 #include <cstdint>
0013
0014 namespace ActsPlugins::detail {
0015
0016
0017
0018
0019 template <typename T>
0020 class CantorEdge {
0021 T m_value;
0022
0023 public:
0024 CantorEdge(T x, T y, bool sort = true) {
0025 if ((x > y) && sort) {
0026 std::swap(x, y);
0027 }
0028 m_value = y + ((x + y) * (x + y + 1)) / 2;
0029 }
0030
0031 std::pair<T, T> inverse() const {
0032 auto f = [](T w) -> T { return (w * (w + 1)) / 2; };
0033 auto q = [](T w) -> T {
0034 return std::floor((std::sqrt(8 * w + 1) - 1) / 2);
0035 };
0036
0037 auto y = m_value - f(q(m_value));
0038 auto x = q(m_value) - y;
0039
0040 return {x, y};
0041 }
0042
0043 T value() const { return m_value; }
0044
0045 auto operator<=>(const CantorEdge<T>& other) const = default;
0046 };
0047
0048 }