ContactKey.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #ifndef PLAYRHO_DYNAMICS_CONTACTS_CONTACTKEY_HPP
22 #define PLAYRHO_DYNAMICS_CONTACTS_CONTACTKEY_HPP
23 
26 
28 #include <utility>
29 #include <algorithm>
30 #include <functional>
31 #include <cassert>
32 
33 namespace playrho {
34 
37 {
38 public:
39  PLAYRHO_CONSTEXPR inline ContactKey() noexcept
40  {
41  // Intentionally empty
42  }
43 
46  m_ids{std::minmax(fp1, fp2)}
47  {
48  // Intentionally empty
49  }
50 
52  PLAYRHO_CONSTEXPR inline ContactCounter GetMin() const noexcept
53  {
54  return std::get<0>(m_ids);
55  }
56 
58  PLAYRHO_CONSTEXPR inline ContactCounter GetMax() const noexcept
59  {
60  return std::get<1>(m_ids);
61  }
62 
63 private:
67  std::pair<ContactCounter, ContactCounter> m_ids{
68  static_cast<ContactCounter>(-1), static_cast<ContactCounter>(-1)
69  };
70 };
71 
73 PLAYRHO_CONSTEXPR inline bool operator== (const ContactKey lhs, const ContactKey rhs) noexcept
74 {
75  return lhs.GetMin() == rhs.GetMin() && lhs.GetMax() == rhs.GetMax();
76 }
77 
79 PLAYRHO_CONSTEXPR inline bool operator!= (const ContactKey lhs, const ContactKey rhs) noexcept
80 {
81  return !(lhs == rhs);
82 }
83 
85 PLAYRHO_CONSTEXPR inline bool operator< (const ContactKey lhs, const ContactKey rhs) noexcept
86 {
87  return (lhs.GetMin() < rhs.GetMin())
88  || ((lhs.GetMin() == rhs.GetMin()) && (lhs.GetMax() < rhs.GetMax()));
89 }
90 
92 PLAYRHO_CONSTEXPR inline bool operator<= (const ContactKey lhs, const ContactKey rhs) noexcept
93 {
94  return (lhs.GetMin() < rhs.GetMin())
95  || ((lhs.GetMin() == rhs.GetMin()) && (lhs.GetMax() <= rhs.GetMax()));
96 }
97 
99 PLAYRHO_CONSTEXPR inline bool operator> (const ContactKey lhs, const ContactKey rhs) noexcept
100 {
101  return (lhs.GetMin() > rhs.GetMin())
102  || ((lhs.GetMin() == rhs.GetMin()) && (lhs.GetMax() > rhs.GetMax()));
103 }
104 
106 PLAYRHO_CONSTEXPR inline bool operator>= (const ContactKey lhs, const ContactKey rhs) noexcept
107 {
108  return (lhs.GetMin() > rhs.GetMin())
109  || ((lhs.GetMin() == rhs.GetMin()) && (lhs.GetMax() >= rhs.GetMax()));
110 }
111 
112 namespace d2 {
113 
114 class Fixture;
115 class Contact;
116 
118 using KeyedContactPtr = std::pair<ContactKey, Contact*>;
119 
121 ContactKey GetContactKey(const Fixture& fixtureA, ChildCounter childIndexA,
122  const Fixture& fixtureB, ChildCounter childIndexB) noexcept;
123 
125 ContactKey GetContactKey(const Contact& contact) noexcept;
126 
129 {
130  return std::get<1>(value);
131 }
132 
133 } // namespace d2
134 } // namespace playrho
135 
136 namespace std
137 {
139  template <>
140  struct hash<playrho::ContactKey>
141  {
144 
146  using result_type = std::size_t;
147 
149  PLAYRHO_CONSTEXPR inline std::size_t operator()(const playrho::ContactKey& key) const
150  {
151  // Use simple and fast Knuth multiplicative hash...
152  const auto a = std::size_t{key.GetMin()} * 2654435761u;
153  const auto b = std::size_t{key.GetMax()} * 2654435761u;
154  return a ^ b;
155  }
156  };
157 } // namespace std
158 
159 #endif // PLAYRHO_DYNAMICS_CONTACTS_CONTACTKEY_HPP