libsemigroups  v3.0.0
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 {
40
48 enum class Order : uint8_t {
50 none = 0,
51
55
60
64
65 // wreath TODO(later)
66 };
67
95 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
96 bool lexicographical_compare(T const& x, T const& y) {
98 x.cbegin(), x.cend(), y.cbegin(), y.cend());
99 }
100
128 template <typename T>
129 bool lexicographical_compare(T* const x, T* const y) {
131 x->cbegin(), x->cend(), y->cbegin(), y->cend());
132 }
133
167 template <typename T>
168 bool operator()(T const& x, T const& y) const {
169 return lexicographical_compare(x, y);
170 }
171
191 template <typename T>
193 std::initializer_list<T> y) const {
195 x.begin(), x.end(), y.begin(), y.end());
196 }
197
220 // TODO(v3) remove this?
221 template <typename T>
222 bool operator()(T first1, T last1, T first2, T last2) const {
223 return std::lexicographical_compare(first1, last1, first2, last2);
224 }
225 };
226
266 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
267 bool shortlex_compare(T const& first1,
268 T const& last1,
269 T const& first2,
270 T const& last2) {
271 return (last1 - first1) < (last2 - first2)
272 || ((last1 - first1) == (last2 - first2)
273 && std::lexicographical_compare(first1, last1, first2, last2));
274 }
275
305 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
306 bool shortlex_compare(T const& x, T const& y) {
307 return shortlex_compare(x.cbegin(), x.cend(), y.cbegin(), y.cend());
308 }
309
339 template <typename T>
340 bool shortlex_compare(T* const x, T* const y) {
341 return shortlex_compare(x->cbegin(), x->cend(), y->cbegin(), y->cend());
342 }
343
375 template <typename T>
376 bool operator()(T const& x, T const& y) const {
377 return shortlex_compare(x, y);
378 }
379 };
380
416 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
417 bool recursive_path_compare(T const& first1,
418 T last1,
419 T const& first2,
420 T last2) noexcept {
421 bool lastmoved = false;
422 --last1;
423 --last2;
424 while (true) {
425 if (last1 < first1) {
426 if (last2 < first2) {
427 return lastmoved;
428 }
429 return true;
430 }
431 if (last2 < first2) {
432 return false;
433 }
434 if (*last1 == *last2) {
435 last1--;
436 last2--;
437 } else if (*last1 < *last2) {
438 last1--;
439 lastmoved = false;
440 } else if (*last2 < *last1) {
441 last2--;
442 lastmoved = true;
443 }
444 }
445 }
446
473 template <typename T, typename = std::enable_if_t<!rx::is_input_or_sink_v<T>>>
474 bool recursive_path_compare(T const& x, T const& y) noexcept {
475 return recursive_path_compare(x.cbegin(), x.cend(), y.cbegin(), y.cend());
476 }
477
505 template <typename T>
506 bool recursive_path_compare(T* const x, T* const y) noexcept {
508 x->cbegin(), x->cend(), y->cbegin(), y->cend());
509 }
510
541 template <typename T>
542 bool operator()(T const& x, T const& y) const noexcept {
543 return recursive_path_compare(x, y);
544 }
545 };
546
547 // end orders_group
549
550} // namespace libsemigroups
551
552#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:267
bool lexicographical_compare(T const &x, T const &y)
Compare two objects of the same type using std::lexicographical_compare.
Definition order.hpp:96
Order
The possible orderings of words and strings.
Definition order.hpp:48
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:417
@ none
No ordering.
Definition order.hpp:50
@ shortlex
Definition order.hpp:54
@ lex
Definition order.hpp:59
@ recursive
Definition order.hpp:63
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:147
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:192
bool operator()(T const &x, T const &y) const
Call operator that compares x and y using std::lexicographical_compare.
Definition order.hpp:168
bool operator()(T first1, T last1, T first2, T last2) const
Call operator that compares iterators using std::lexicographical_compare.
Definition order.hpp:222
A stateless struct with binary call operator using recursive_path_compare.
Definition order.hpp:524
bool operator()(T const &x, T const &y) const noexcept
Call operator that compares x and y using recursive_path_compare.
Definition order.hpp:542
A stateless struct with binary call operator using shortlex_compare.
Definition order.hpp:356
bool operator()(T const &x, T const &y) const
Call operator that compares x and y using shortlex_compare.
Definition order.hpp:376