Skip to content

Commit b33985e

Browse files
constants: impl hashing
1 parent 062776b commit b33985e

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

src/main.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,18 @@ The valid values are:
150150
"__int__",
151151
[](Undefined const& x) -> size_t { return static_cast<size_t>(x); })
152152
.def("__chr__",
153-
[](Undefined const& x) -> char { return static_cast<char>(x); });
153+
[](Undefined const& x) -> char { return static_cast<char>(x); })
154+
.def("__hash__", [](Undefined const& op) -> int {
155+
return std::hash<uint64_t>{}(static_cast<uint64_t>(op));
156+
});
154157

155158
m.attr("UNDEFINED") = UNDEFINED;
156159

157160
#ifdef LIBSEMIGROUPS_EIGEN_ENABLED
158161
m.attr("LIBSEMIGROUPS_EIGEN_ENABLED")
159162
= static_cast<bool>(LIBSEMIGROUPS_EIGEN_ENABLED);
160163
#else
161-
m.attr("LIBSEMIGROUPS_EIGEN_ENABLED") = false;
164+
m.attr("LIBSEMIGROUPS_EIGEN_ENABLED") = false;
162165
#endif
163166

164167
#ifdef LIBSEMIGROUPS_HPCOMBI_ENABLED
@@ -253,7 +256,7 @@ The valid values are:
253256
#ifdef VERSION_INFO
254257
m.attr("__version__") = VERSION_INFO;
255258
#else
256-
m.attr("__version__") = "dev";
259+
m.attr("__version__") = "dev";
257260
#endif
258261

259262
////////////////////////////////////////////////////////////////////////
@@ -351,8 +354,12 @@ default.
351354
return lhop == rhop;
352355
},
353356
py::is_operator())
354-
.def("to_int", [](PositiveInfinity const& x) -> int64_t {
355-
return static_cast<int64_t>(x);
357+
.def("to_int",
358+
[](PositiveInfinity const& x) -> int64_t {
359+
return static_cast<int64_t>(x);
360+
})
361+
.def("__hash__", [](PositiveInfinity const& op) -> int {
362+
return std::hash<uint64_t>{}(static_cast<uint64_t>(op));
356363
});
357364

358365
m.attr("POSITIVE_INFINITY") = POSITIVE_INFINITY;
@@ -395,8 +402,12 @@ default.
395402
return lhop == rhop;
396403
},
397404
py::is_operator())
398-
.def("to_int", [](NegativeInfinity const& x) -> int64_t {
399-
return static_cast<int64_t>(x);
405+
.def("to_int",
406+
[](NegativeInfinity const& x) -> int64_t {
407+
return static_cast<int64_t>(x);
408+
})
409+
.def("__hash__", [](NegativeInfinity const& op) -> int {
410+
return std::hash<int64_t>{}(static_cast<int64_t>(op));
400411
});
401412

402413
m.attr("NEGATIVE_INFINITY") = NEGATIVE_INFINITY;
@@ -444,7 +455,10 @@ default.
444455
[](LimitMax const& rhs, int64_t lhs) { return lhs - rhs; },
445456
py::is_operator())
446457
.def("to_int",
447-
[](LimitMax const& x) -> int { return static_cast<int>(x); });
458+
[](LimitMax const& x) -> int { return static_cast<int>(x); })
459+
.def("__hash__", [](LimitMax const& op) -> int {
460+
return std::hash<uint64_t>{}(static_cast<uint64_t>(op));
461+
});
448462

449463
m.attr("LIMIT_MAX") = LIMIT_MAX;
450464

tests/test_constants.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def test_UNDEFINED(): # pylint: disable=invalid-name
4141
assert not UNDEFINED > UNDEFINED
4242
assert not UNDEFINED < UNDEFINED
4343

44+
d = {UNDEFINED: 0}
45+
assert UNDEFINED in d
46+
4447

4548
def test_POSITIVE_INFINITY(): # pylint: disable=invalid-name
4649
"""
@@ -79,6 +82,9 @@ def test_POSITIVE_INFINITY(): # pylint: disable=invalid-name
7982
assert POSITIVE_INFINITY > 100
8083
assert POSITIVE_INFINITY > NEGATIVE_INFINITY
8184

85+
d = {POSITIVE_INFINITY: 0}
86+
assert POSITIVE_INFINITY in d
87+
8288

8389
def test_NEGATIVE_INFINITY(): # pylint: disable=invalid-name
8490
"""
@@ -117,6 +123,9 @@ def test_NEGATIVE_INFINITY(): # pylint: disable=invalid-name
117123
assert not NEGATIVE_INFINITY > 100
118124
assert not NEGATIVE_INFINITY > POSITIVE_INFINITY
119125

126+
d = {NEGATIVE_INFINITY: 0}
127+
assert NEGATIVE_INFINITY in d
128+
120129

121130
def test_LIMIT_MAX(): # pylint: disable=invalid-name
122131
"""
@@ -151,3 +160,6 @@ def test_LIMIT_MAX(): # pylint: disable=invalid-name
151160
assert 100 < LIMIT_MAX
152161
assert not 0 > LIMIT_MAX
153162
assert not 100 > LIMIT_MAX
163+
164+
d = {LIMIT_MAX: 0}
165+
assert LIMIT_MAX in d

0 commit comments

Comments
 (0)