Skip to content

Commit 2497d27

Browse files
c++: move constants from main to constants.cpp
1 parent 70b8d6e commit 2497d27

3 files changed

Lines changed: 272 additions & 226 deletions

File tree

src/constants.cpp

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
//
2+
// libsemigroups_pybind11
3+
// Copyright (C) 2025 James D. Mitchell
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
//
18+
19+
// libsemigroups headers
20+
#include <libsemigroups/constants.hpp>
21+
22+
// pybind11....
23+
#include <pybind11/operators.h>
24+
#include <pybind11/pybind11.h>
25+
26+
// libsemigroups_pybind11....
27+
#include "main.hpp" // for init_constants
28+
29+
namespace libsemigroups {
30+
31+
void init_constants(py::module& m) {
32+
////////////////////////////////////////////////////////////////////////
33+
// Undefined
34+
////////////////////////////////////////////////////////////////////////
35+
36+
py::class_<Undefined>(m, "Undefined")
37+
.def("__repr__",
38+
[](Undefined const& val) -> std::string { return "UNDEFINED"; })
39+
.def(py::self < Undefined())
40+
.def(
41+
"__eq__",
42+
[](Undefined const& lhop, Undefined const& rhop) -> bool {
43+
return true;
44+
},
45+
py::is_operator())
46+
.def(
47+
"__eq__",
48+
[](Undefined const& lhop, size_t rhop) -> bool {
49+
return lhop == rhop;
50+
},
51+
py::is_operator())
52+
.def(
53+
"__eq__",
54+
[](size_t lhop, Undefined const& rhop) -> bool {
55+
return lhop == rhop;
56+
},
57+
py::is_operator())
58+
.def(
59+
"__eq__",
60+
[](Undefined const& lhop, int rhop) -> bool {
61+
return lhop == rhop;
62+
},
63+
py::is_operator())
64+
.def(
65+
"__eq__",
66+
[](int lhop, Undefined const& rhop) -> bool {
67+
return lhop == rhop;
68+
},
69+
py::is_operator())
70+
.def(
71+
"__eq__",
72+
[](Undefined const& lhop, uint64_t rhop) -> bool {
73+
return lhop == rhop;
74+
},
75+
py::is_operator())
76+
.def(
77+
"__eq__",
78+
[](uint64_t lhop, Undefined const& rhop) -> bool {
79+
return lhop == rhop;
80+
},
81+
py::is_operator())
82+
.def(
83+
"__int__",
84+
[](Undefined const& x) -> size_t { return static_cast<size_t>(x); })
85+
.def("__chr__",
86+
[](Undefined const& x) -> char { return static_cast<char>(x); })
87+
.def("__hash__", [](Undefined const& op) -> int {
88+
return std::hash<uint64_t>{}(static_cast<uint64_t>(op));
89+
});
90+
91+
m.attr("UNDEFINED") = UNDEFINED;
92+
93+
////////////////////////////////////////////////////////////////////////
94+
// PositiveInfinity
95+
////////////////////////////////////////////////////////////////////////
96+
97+
py::class_<PositiveInfinity>(m, "PositiveInfinity")
98+
.def("__repr__",
99+
[](PositiveInfinity const& val) -> std::string {
100+
return u8"+\u221E";
101+
})
102+
.def(pybind11::self < PositiveInfinity())
103+
.def(pybind11::self < NegativeInfinity())
104+
.def(pybind11::self < int())
105+
.def(int() < pybind11::self)
106+
.def(
107+
"__eq__",
108+
[](PositiveInfinity const& lhop,
109+
PositiveInfinity const& rhop) -> bool { return true; },
110+
py::is_operator())
111+
.def(
112+
"__eq__",
113+
[](PositiveInfinity const& lhop,
114+
NegativeInfinity const& rhop) -> bool { return false; },
115+
py::is_operator())
116+
.def(
117+
"__eq__",
118+
[](int lhop, PositiveInfinity const& rhop) -> bool {
119+
return lhop == rhop;
120+
},
121+
py::is_operator())
122+
.def(
123+
"__eq__",
124+
[](PositiveInfinity const& lhop, int rhop) -> bool {
125+
return lhop == rhop;
126+
},
127+
py::is_operator())
128+
.def(
129+
"__eq__",
130+
[](int64_t lhop, PositiveInfinity const& rhop) -> bool {
131+
return lhop == rhop;
132+
},
133+
py::is_operator())
134+
.def(
135+
"__eq__",
136+
[](PositiveInfinity const& lhop, int64_t rhop) -> bool {
137+
return lhop == rhop;
138+
},
139+
py::is_operator())
140+
.def(
141+
"__eq__",
142+
[](uint64_t lhop, PositiveInfinity const& rhop) -> bool {
143+
return lhop == rhop;
144+
},
145+
py::is_operator())
146+
.def(
147+
"__eq__",
148+
[](PositiveInfinity const& lhop, uint64_t rhop) -> bool {
149+
return lhop == rhop;
150+
},
151+
py::is_operator())
152+
.def("to_int",
153+
[](PositiveInfinity const& x) -> int64_t {
154+
return static_cast<int64_t>(x);
155+
})
156+
.def("__hash__", [](PositiveInfinity const& op) -> int {
157+
return std::hash<uint64_t>{}(static_cast<uint64_t>(op));
158+
});
159+
160+
m.attr("POSITIVE_INFINITY") = POSITIVE_INFINITY;
161+
162+
////////////////////////////////////////////////////////////////////////
163+
// NegativeInfinity
164+
////////////////////////////////////////////////////////////////////////
165+
166+
py::class_<NegativeInfinity>(m, "NegativeInfinity")
167+
.def("__repr__",
168+
[](NegativeInfinity const& val) -> std::string {
169+
return u8"-\u221E";
170+
})
171+
.def(pybind11::self < PositiveInfinity())
172+
.def(pybind11::self < NegativeInfinity())
173+
.def(pybind11::self < int())
174+
.def(int() < pybind11::self)
175+
.def(
176+
"__eq__",
177+
[](NegativeInfinity const& lhop,
178+
PositiveInfinity const& rhop) -> bool { return lhop == rhop; },
179+
py::is_operator())
180+
.def(
181+
"__eq__",
182+
[](int lhop, NegativeInfinity const& rhop) -> bool {
183+
return lhop == rhop;
184+
},
185+
py::is_operator())
186+
.def(
187+
"__eq__",
188+
[](NegativeInfinity const& lhop, int rhop) -> bool {
189+
return lhop == rhop;
190+
},
191+
py::is_operator())
192+
.def(
193+
"__eq__",
194+
[](int64_t lhop, NegativeInfinity const& rhop) -> bool {
195+
return lhop == rhop;
196+
},
197+
py::is_operator())
198+
.def(
199+
"__eq__",
200+
[](NegativeInfinity const& lhop, int64_t rhop) -> bool {
201+
return lhop == rhop;
202+
},
203+
py::is_operator())
204+
.def("to_int",
205+
[](NegativeInfinity const& x) -> int64_t {
206+
return static_cast<int64_t>(x);
207+
})
208+
.def("__hash__", [](NegativeInfinity const& op) -> int {
209+
return std::hash<int64_t>{}(static_cast<int64_t>(op));
210+
});
211+
212+
m.attr("NEGATIVE_INFINITY") = NEGATIVE_INFINITY;
213+
214+
////////////////////////////////////////////////////////////////////////
215+
// LimitMax
216+
////////////////////////////////////////////////////////////////////////
217+
218+
py::class_<LimitMax>(m, "LimitMax")
219+
.def("__repr__",
220+
[](LimitMax const& val) -> std::string { return "LIMIT_MAX"; })
221+
.def(pybind11::self < LimitMax())
222+
.def(pybind11::self < int())
223+
.def(int() < pybind11::self)
224+
.def(
225+
"__eq__",
226+
[](int lhop, LimitMax const& rhop) -> bool { return lhop == rhop; },
227+
py::is_operator())
228+
.def(
229+
"__eq__",
230+
[](LimitMax const& lhop, int rhop) -> bool { return lhop == rhop; },
231+
py::is_operator())
232+
.def(
233+
"__eq__",
234+
[](int64_t lhop, LimitMax const& rhop) -> bool {
235+
return lhop == rhop;
236+
},
237+
py::is_operator())
238+
.def(
239+
"__eq__",
240+
[](LimitMax const& lhop, int64_t rhop) -> bool {
241+
return lhop == rhop;
242+
},
243+
py::is_operator())
244+
.def(
245+
"__sub__",
246+
[](LimitMax const& lhs, int rhs) { return lhs - rhs; },
247+
py::is_operator())
248+
.def(
249+
"__rsub__",
250+
[](LimitMax const& rhs, int lhs) { return lhs - rhs; },
251+
py::is_operator())
252+
.def(
253+
"__sub__",
254+
[](LimitMax const& lhs, int64_t rhs) { return lhs - rhs; },
255+
py::is_operator())
256+
.def(
257+
"__rsub__",
258+
[](LimitMax const& rhs, int64_t lhs) { return lhs - rhs; },
259+
py::is_operator())
260+
.def("to_int",
261+
[](LimitMax const& x) -> int { return static_cast<int>(x); })
262+
.def("__hash__", [](LimitMax const& op) -> int {
263+
return std::hash<uint64_t>{}(static_cast<uint64_t>(op));
264+
});
265+
266+
m.attr("LIMIT_MAX") = LIMIT_MAX;
267+
}
268+
} // namespace libsemigroups

0 commit comments

Comments
 (0)