libsemigroups  v3.1.2
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
constants.hpp
1//
2// libsemigroups - C++ library for semigroups and monoids
3// Copyright (C) 2019-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// This file contains functionality for various constant values used in
20// libsemigroups.
21
22// TODO(later)
23// 1. NegativeInfinity could be comparable with unsigned integers (always <).
24// 2. specialisation of operator<< for ostringstream for better printing. I
25// couldn't immediately get this to work.
26
27#ifndef LIBSEMIGROUPS_CONSTANTS_HPP_
28#define LIBSEMIGROUPS_CONSTANTS_HPP_
29
30#include <cinttypes> // for int64_t
31#include <limits> // for numeric_limits
32#include <type_traits> // for is_integral
33
34namespace libsemigroups {
35 namespace detail {
36
37 struct Min {
38 template <typename T>
39 constexpr T operator()() const noexcept {
40 static_assert(std::is_integral_v<T>,
41 "can only call Min with an integral type");
43 }
44 };
45
46 struct Max {
47 template <typename T>
48 constexpr T operator()() const noexcept {
49 static_assert(std::is_integral_v<T>,
50 "can only call Max with an integral type");
52 }
53 };
54
55 template <int64_t TOffset, typename TMaxOrMin>
56 struct Constant {
57 static_assert(std::is_same_v<TMaxOrMin, Max>
58 || std::is_same_v<TMaxOrMin, Min>,
59 "template parameter TMaxOrMin must be Max or Min");
60
61 Constant() = default;
62 Constant(Constant const&) = default;
63 Constant(Constant&&) = default;
64 Constant& operator=(Constant const&) = default;
65 Constant& operator=(Constant&&) = default;
66 ~Constant() = default;
67
68 template <typename T, typename = std::enable_if_t<!std::is_enum_v<T>, T>>
69 constexpr operator T() const noexcept {
70 static_assert(
71 std::is_integral_v<T>
72 && (std::is_signed_v<T> || std::is_same_v<TMaxOrMin, Max>),
73 "the template parameter T must be an integral type, and either "
74 "unsigned or the template parameter TMaxOrMin must be Max.");
75 return TMaxOrMin().template operator()<T>() + TOffset;
76 }
77 };
78 } // namespace detail
79
81 // Constant values
83
90
95 using Undefined = detail::Constant<0, detail::Max>;
96
101 using PositiveInfinity = detail::Constant<-1, detail::Max>;
102
107 using LimitMax = detail::Constant<-2, detail::Max>;
108
113 using NegativeInfinity = detail::Constant<0, detail::Min>;
114
122 extern Undefined const UNDEFINED;
123
133
142 extern LimitMax const LIMIT_MAX;
143
153
155 // Operators for all constants
157
158 // Note that for some reason Catch requires that the comparison functions are
159 // in the namespace detail.
160
161#ifndef LIBSEMIGROUPS_PARSED_BY_DOXYGEN
162 namespace detail {
163
164 // operator==
165 // No SFINAE required, since the functions delegated to don't exist.
166 template <int64_t R, typename S, typename T>
167 constexpr bool operator==(Constant<R, S> const& lhs,
168 T const& rhs) noexcept {
169 return lhs.operator T() == rhs;
170 }
171
172 template <int64_t R, typename S, typename T>
173 constexpr bool operator==(T const& lhs,
174 Constant<R, S> const& rhs) noexcept {
175 return rhs.operator T() == lhs;
176 }
177
178 template <int64_t R1, typename S1, int64_t R2, typename S2>
179 constexpr bool operator==(Constant<R1, S1> const&,
180 Constant<R2, S2> const&) noexcept {
181 return std::is_same_v<S1, S2> && R1 == R2;
182 }
183
184 // operator!=
185 // No SFINAE required, since the functions delegated to don't exist.
186 template <int64_t R, typename S, typename T>
187 constexpr bool operator!=(Constant<R, S> const& lhs,
188 T const& rhs) noexcept {
189 return !(lhs == rhs);
190 }
191
192 template <int64_t R, typename S, typename T>
193 constexpr bool operator!=(T const& lhs,
194 Constant<R, S> const& rhs) noexcept {
195 return !(lhs == rhs);
196 }
197
198 template <int64_t R1, typename S1, int64_t R2, typename S2>
199 constexpr bool operator!=(Constant<R1, S1> const& lhs,
200 Constant<R2, S2> const& rhs) noexcept {
201 return !(lhs == rhs);
202 }
203
204 // operator>
205 // No SFINAE required, since the functions delegated to don't exist.
206 template <int64_t R, typename S, typename T>
207 constexpr bool operator>(Constant<R, S> const& lhs, T const& rhs) noexcept {
208 return rhs < lhs;
209 }
210
211 template <int64_t R, typename S, typename T>
212 constexpr bool operator>(T const& lhs, Constant<R, S> const& rhs) noexcept {
213 return rhs < lhs;
214 }
215
216 template <int64_t R, typename S>
217 constexpr bool operator>(Constant<R, S> const&,
218 Constant<R, S> const&) noexcept {
219 return false;
220 }
221
222 template <int64_t R, typename S>
223 constexpr bool operator<(Constant<R, S> const&,
224 Constant<R, S> const&) noexcept {
225 return false;
226 }
227
228 // No further operator< for Constant and Constant unless given explicitly
229
231 // Operators for specific constants
233
234 // PositiveInfinity is not less than any integral value, or
235 // NegativeInfinity.
236 template <typename T, typename SFINAE = bool>
237 constexpr auto operator<(PositiveInfinity const&, T const&) noexcept
238 -> std::enable_if_t<std::is_integral_v<T>
239 || std::is_same_v<NegativeInfinity, T>,
240 SFINAE> {
241 return false;
242 }
243
244 // Every integral value, and negative infinity, is less than
245 // PositiveInfinity.
246 template <typename T, typename SFINAE = bool>
247 constexpr auto operator<(T const&, PositiveInfinity const&) noexcept
248 -> std::enable_if_t<std::is_integral_v<T>
249 || std::is_same_v<NegativeInfinity, T>,
250 SFINAE> {
251 return true;
252 }
253
254 // NegativeInfinity is less than every integral value.
255 template <typename T, typename SFINAE = bool>
256 constexpr auto operator<(NegativeInfinity const&, T const&) noexcept
257 -> std::enable_if_t<std::is_integral_v<T>, SFINAE> {
258 return true;
259 }
260
261 // No integral value is less than NegativeInfinity.
262 template <typename T, typename SFINAE = bool>
263 constexpr auto operator<(T const&, NegativeInfinity const&) noexcept
264 -> std::enable_if_t<std::is_integral_v<T>, SFINAE> {
265 return false;
266 }
267
268 // LimitMax is compared by implicit conversion with any integral value.
269 template <typename T, typename SFINAE = bool>
270 constexpr auto operator<(LimitMax const& lhs, T const& rhs) noexcept
271 -> std::enable_if_t<std::is_integral_v<T>, SFINAE> {
272 return lhs.operator T() < rhs;
273 }
274
275 // LimitMax is compared by implicit conversion with any integral value.
276 template <typename T, typename SFINAE = bool>
277 constexpr auto operator<(T const& lhs, LimitMax const& rhs) noexcept
278 -> std::enable_if_t<std::is_integral_v<T>, SFINAE> {
279 return lhs < rhs.operator T();
280 }
281
282 template <typename T, typename SFINAE = T>
283 constexpr auto operator-(LimitMax const& lhs, T const& rhs) noexcept
284 -> std::enable_if_t<std::is_integral_v<T>, SFINAE> {
285 return lhs.operator T() - rhs;
286 }
287
288 template <typename T, typename SFINAE = T>
289 constexpr auto operator-(T const& lhs, LimitMax const& rhs) noexcept
290 -> std::enable_if_t<std::is_integral_v<T>, SFINAE> {
291 return lhs - rhs.operator T();
292 }
293 } // namespace detail
294#endif // LIBSEMIGROUPS_PARSED_BY_DOXYGEN
295} // namespace libsemigroups
296#endif // LIBSEMIGROUPS_CONSTANTS_HPP_
bool operator>(Bipartition const &x, Bipartition const &y)
Compare bipartitions.
Definition bipart.hpp:1658
bool operator!=(Bipartition const &x, Bipartition const &y)
Check bipartitions for inequality.
Definition bipart.hpp:1637
detail::Constant< 0, detail::Max > Undefined
Type for undefined values.
Definition constants.hpp:95
NegativeInfinity const NEGATIVE_INFINITY
Value for negative infinity.
detail::Constant<-1, detail::Max > PositiveInfinity
Type for positive infinity.
Definition constants.hpp:101
Undefined const UNDEFINED
Value for something undefined.
detail::Constant< 0, detail::Min > NegativeInfinity
Type for negative infinity.
Definition constants.hpp:113
PositiveInfinity const POSITIVE_INFINITY
Value for positive infinity.
LimitMax const LIMIT_MAX
Value for the maximum of something.
detail::Constant<-2, detail::Max > LimitMax
Type for the maximum value of something.
Definition constants.hpp:107
bool operator==(Presentation< Word > const &lhop, Presentation< Word > const &rhop)
Compare for equality.
Definition presentation.hpp:2475
T max(T... args)
T min(T... args)
Namespace for everything in the libsemigroups library.
Definition action.hpp:44