Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CantorEdge.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CantorEdge.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <cmath>
12 #include <cstdint>
13 
14 namespace Acts::detail {
15 
19 template <typename T>
20 class CantorEdge {
22 
23  public:
24  CantorEdge(T x, T y, bool sort = true) {
25  if ((x > y) and sort) {
26  std::swap(x, y);
27  }
28  m_value = y + ((x + y) * (x + y + 1)) / 2;
29  }
30 
31  std::pair<T, T> inverse() const {
32  auto f = [](T w) -> T { return (w * (w + 1)) / 2; };
33  auto q = [](T w) -> T {
34  return std::floor((std::sqrt(8 * w + 1) - 1) / 2);
35  };
36 
37  auto y = m_value - f(q(m_value));
38  auto x = q(m_value) - y;
39 
40  return {x, y};
41  }
42 
43  T value() const { return m_value; }
44 };
45 
46 template <typename T>
47 bool operator==(const CantorEdge<T> &a, const CantorEdge<T> &b) {
48  return a.value() == b.value();
49 }
50 
51 template <typename T>
52 bool operator<(const CantorEdge<T> &a, const CantorEdge<T> &b) {
53  return a.value() < b.value();
54 }
55 
56 } // namespace Acts::detail