libsemigroups  v3.1.2
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
order.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// TODO(later)
20// 1. add the others (recursive path words) from test-todd-coxeter.cpp
21// 2. add some documentation
22
23#ifndef LIBSEMIGROUPS_ORDER_HPP_
24#define LIBSEMIGROUPS_ORDER_HPP_
25
26#include <cstddef> // for size_t
27#include <vector> // for vector
28
29#include "ranges.hpp" // for shortlex_compare
30
31#include "ranges.hpp"
32
33namespace libsemigroups {
45 enum class Order : uint8_t {
47 none = 0,
48
52
57
61
62 // wreath TODO(later)
63 };
64
73
101 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
102 bool lexicographical_compare(T const& x, T const& y) {
104 x.cbegin(), x.cend(), y.cbegin(), y.cend());
105 }
106
134 template <typename T>
135 bool lexicographical_compare(T* const x, T* const y) {
137 x->cbegin(), x->cend(), y->cbegin(), y->cend());
138 }
139
173 template <typename T>
174 bool operator()(T const& x, T const& y) const {
175 return lexicographical_compare(x, y);
176 }
177
197 template <typename T>
199 std::initializer_list<T> y) const {
201 x.begin(), x.end(), y.begin(), y.end());
202 }
203
226 // TODO(v3) remove this?
227 template <typename T>
228 bool operator()(T first1, T last1, T first2, T last2) const {
229 return std::lexicographical_compare(first1, last1, first2, last2);
230 }
231 };
232
272 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
273 bool shortlex_compare(T const& first1,
274 T const& last1,
275 T const& first2,
276 T const& last2) {
277 return (last1 - first1) < (last2 - first2)
278 || ((last1 - first1) == (last2 - first2)
279 && std::lexicographical_compare(first1, last1, first2, last2));
280 }
281
311 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
312 bool shortlex_compare(T const& x, T const& y) {
313 return shortlex_compare(x.cbegin(), x.cend(), y.cbegin(), y.cend());
314 }
315
345 template <typename T>
346 bool shortlex_compare(T* const x, T* const y) {
347 return shortlex_compare(x->cbegin(), x->cend(), y->cbegin(), y->cend());
348 }
349
381 template <typename T>
382 bool operator()(T const& x, T const& y) const {
383 return shortlex_compare(x, y);
384 }
385 };
386
422 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
423 bool recursive_path_compare(T const& first1,
424 T last1,
425 T const& first2,
426 T last2) noexcept {
427 bool lastmoved = false;
428 --last1;
429 --last2;
430 while (true) {
431 if (last1 < first1) {
432 if (last2 < first2) {
433 return lastmoved;
434 }
435 return true;
436 }
437 if (last2 < first2) {
438 return false;
439 }
440 if (*last1 == *last2) {
441 last1--;
442 last2--;
443 } else if (*last1 < *last2) {
444 last1--;
445 lastmoved = false;
446 } else if (*last2 < *last1) {
447 last2--;
448 lastmoved = true;
449 }
450 }
451 }
452
479 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
480 bool recursive_path_compare(T const& x, T const& y) noexcept {
481 return recursive_path_compare(x.cbegin(), x.cend(), y.cbegin(), y.cend());
482 }
483
511 template <typename T>
512 bool recursive_path_compare(T* const x, T* const y) noexcept {
514 x->cbegin(), x->cend(), y->cbegin(), y->cend());
515 }
516
547 template <typename T>
548 bool operator()(T const& x, T const& y) const noexcept {
549 return recursive_path_compare(x, y);
550 }
551 };
552
553 // end orders_group
555
556} // namespace libsemigroups
557
558#endif // LIBSEMIGROUPS_ORDER_HPP_
bool shortlex_compare(T const &first1, T const &last1, T const &first2, T const &last2)
Compare two objects of the same type using the short-lex reduction ordering.
Definition order.hpp:273
bool lexicographical_compare(T const &x, T const &y)
Compare two objects of the same type using std::lexicographical_compare.
Definition order.hpp:102
bool recursive_path_compare(T const &first1, T last1, T const &first2, T last2) noexcept
Compare two objects of the same type using the recursive path comparison.
Definition order.hpp:423
Order
The possible orderings of words and strings.
Definition order.hpp:45
@ none
No ordering.
Definition order.hpp:47
@ shortlex
Definition order.hpp:51
@ lex
Definition order.hpp:56
@ recursive
Definition order.hpp:60
T lexicographical_compare(T... args)
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
A stateless struct with binary call operator using std::lexicographical_compare.
Definition order.hpp:153
bool operator()(std::initializer_list< T > x, std::initializer_list< T > y) const
Call operator that compares x and y given initializer lists using std::lexicographical_compare.
Definition order.hpp:198
bool operator()(T const &x, T const &y) const
Call operator that compares x and y using std::lexicographical_compare.
Definition order.hpp:174
bool operator()(T first1, T last1, T first2, T last2) const
Call operator that compares iterators using std::lexicographical_compare.
Definition order.hpp:228
A stateless struct with binary call operator using recursive_path_compare.
Definition order.hpp:530
bool operator()(T const &x, T const &y) const noexcept
Call operator that compares x and y using recursive_path_compare.
Definition order.hpp:548
A stateless struct with binary call operator using shortlex_compare.
Definition order.hpp:362
bool operator()(T const &x, T const &y) const
Call operator that compares x and y using shortlex_compare.
Definition order.hpp:382