libsemigroups  v3.0.0
C++ library for semigroups and monoids
Loading...
Searching...
No Matches
pbr.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 the declaration of the PBR class.
20
21#ifndef LIBSEMIGROUPS_PBR_HPP_
22#define LIBSEMIGROUPS_PBR_HPP_
23
24#include <cstddef> // for size_t
25#include <cstdint> // for uint32_t, int32_t
26#include <initializer_list> // for initializer_list
27#include <iosfwd> // for ostream, ostringstream
28#include <string> // for string
29#include <type_traits> // for forward
30#include <utility> // for forward
31#include <vector> // for vector, operator<, operator==, allocator
32
33#include "adapters.hpp" // for Hash
34#include "types.hpp" // for enable_if_is_same
35
36namespace libsemigroups {
37
52
62 class PBR {
63 public:
67 template <typename T>
69
73 template <typename T>
75
80 PBR() = delete;
81
85 PBR(PBR const&) = default;
86
90 PBR(PBR&&) = default;
91
95 PBR& operator=(PBR const&) = default;
96
99 PBR& operator=(PBR&&) = default;
100
120
123
132 explicit PBR(size_t n);
133
137
163
177 size_t degree() const noexcept;
178
191 size_t number_of_points() const noexcept;
192
213 PBR const& y,
214 size_t thread_id = 0);
215
239 void product_inplace(PBR const& x, PBR const& y, size_t thread_id = 0);
240
254 // TODO(later): a better explanation of what equality means for PBRs
255 bool operator==(PBR const& that) const {
256 return _vector == that._vector;
257 }
258
273 bool operator<(PBR const& that) const {
274 return _vector < that._vector;
275 }
276
291 return _vector[i];
292 }
293
308 std::vector<uint32_t> const& operator[](size_t i) const {
309 return _vector[i];
310 }
311
327
342 std::vector<uint32_t> const& at(size_t i) const;
343
359 // not noexcept because Hash<T>::operator() isn't
360 size_t hash_value() const {
361 return Hash<std::vector<std::vector<uint32_t>>>()(_vector);
362 }
363
369
375
376 private:
378 };
379
383 namespace pbr {
401 PBR one(size_t n);
402
420 PBR one(PBR const& x);
421
422 // TODO(later) analogue of bipartition::underlying_partition?
423
437
452
467
492
493 } // namespace pbr
494
495 // end pbr_group
497
498 namespace detail {
500 process_left_right(PBR::vector_type<int32_t> left,
502 }
503
527 template <typename Return, typename... T>
528 [[nodiscard]] enable_if_is_same<Return, PBR> make(T... args) {
529 // TODO(later) throw_if_bad_args
530 PBR result(std::forward<T>(args)...);
531 pbr::throw_if_invalid(result);
532 return result;
533 }
534
545
568 template <typename Return>
573
577 template <typename Return>
583
607 template <typename Return>
610 return PBR(detail::process_left_right(left, right));
611 }
612
626 [[nodiscard]] std::string to_human_readable_repr(PBR const& x);
627
645 PBR operator*(PBR const& x, PBR const& y);
646
665 inline bool operator!=(PBR const& x, PBR const& y) {
666 return !(x == y);
667 }
668
675 inline bool operator>(PBR const& x, PBR const& y) {
676 return y < x;
677 }
678
686 inline bool operator<=(PBR const& x, PBR const& y) {
687 return x < y || x == y;
688 }
689
697 inline bool operator>=(PBR const& x, PBR const& y) {
698 return y <= x;
699 }
700
701 namespace detail {
702
703 template <typename T>
704 struct IsPBRHelper : std::false_type {};
705
706 template <>
707 struct IsPBRHelper<PBR> : std::true_type {};
708
709 } // namespace detail
710
717 template <typename T>
718 static constexpr bool IsPBR = detail::IsPBRHelper<T>::value;
719
721 // Adapters
723
730
737 template <>
738 struct Complexity<PBR> {
755 size_t operator()(PBR const& x) const noexcept {
756 return 8 * x.degree() * x.degree() * x.degree();
757 }
758 };
759
766 template <>
767 struct Degree<PBR> {
785 size_t operator()(PBR const& x) const noexcept {
786 return x.degree();
787 }
788 };
789
796 template <>
797 struct Hash<PBR> {
815 size_t operator()(PBR const& x) const {
816 return x.hash_value();
817 }
818 };
819
826 template <>
827 struct One<PBR> {
843 PBR operator()(PBR const& x) const {
844 return pbr::one(x);
845 }
846
864 PBR operator()(size_t N = 0) const {
865 return pbr::one(N);
866 }
867 };
868
875 template <>
876 struct Product<PBR> {
900 void operator()(PBR& xy, PBR const& x, PBR const& y, size_t thread_id = 0) {
901 xy.product_inplace_no_checks(x, y, thread_id);
902 }
903 };
904
911 template <>
914 void operator()(PBR&, size_t) {}
915 };
916
917 // end adapters_pbr_group
919
920} // namespace libsemigroups
921#endif // LIBSEMIGROUPS_PBR_HPP_
Class for representing PBRs.
Definition pbr.hpp:62
enable_if_is_same< Return, PBR > make(T... args)
Construct and check a PBR.
Definition pbr.hpp:528
friend std::ostringstream & operator<<(std::ostringstream &, PBR const &)
Insertion operator.
PBR(PBR &&)=default
Default move constructor.
void product_inplace(PBR const &x, PBR const &y, size_t thread_id=0)
Multiply two PBR objects and store the product in this.
PBR & operator=(PBR &&)=default
Default move assignment operator.
size_t number_of_points() const noexcept
Returns the number of points of a PBR.
bool operator>=(PBR const &x, PBR const &y)
Convenience function that just calls operator<=.
Definition pbr.hpp:697
std::vector< uint32_t > & operator[](size_t i)
Returns a reference to the points adjacent to a given point.
Definition pbr.hpp:290
PBR()=delete
Deleted.
bool operator>(PBR const &x, PBR const &y)
Convenience function that just calls operator<.
Definition pbr.hpp:675
size_t degree() const noexcept
Returns the degree of a PBR.
std::vector< uint32_t > const & operator[](size_t i) const
Returns a const reference to the points adjacent to a given point.
Definition pbr.hpp:308
PBR(vector_type< int32_t > left, vector_type< int32_t > right)
Construct from adjacencies 1 to n and -1 to -n.
std::vector< std::vector< T > > const & vector_type
Type of constructor argument.
Definition pbr.hpp:68
bool operator<=(PBR const &x, PBR const &y)
Convenience function that just calls operator< and operator==.
Definition pbr.hpp:686
PBR(initializer_list_type< int32_t > left, initializer_list_type< int32_t > right)
Construct from adjacencies 1 to n and -1 to -n.
std::string to_human_readable_repr(PBR const &x)
Return a human readable representation of a PBR.
PBR operator*(PBR const &x, PBR const &y)
Multiply two PBRs.
void product_inplace_no_checks(PBR const &x, PBR const &y, size_t thread_id=0)
Multiply two PBR objects and store the product in this.
friend std::ostream & operator<<(std::ostream &, PBR const &)
Insertion operator.
PBR(initializer_list_type< uint32_t > x)
Construct from adjacencies 0 to 2n - 1.
size_t hash_value() const
Returns a hash value for a PBR.
Definition pbr.hpp:360
std::vector< uint32_t > const & at(size_t i) const
Returns a const reference to the points adjacent to a given point, with bounds checking.
PBR(PBR const &)=default
Default copy constructor.
bool operator!=(PBR const &x, PBR const &y)
Compare two PBRs for inequality.
Definition pbr.hpp:665
PBR(size_t n)
Construct empty PBR of given degree.
bool operator<(PBR const &that) const
Compare for less.
Definition pbr.hpp:273
std::vector< uint32_t > & at(size_t i)
Returns a reference to the points adjacent to a given point, with bounds checking.
PBR(vector_type< uint32_t > x)
Construct from adjacencies 0 to 2n - 1.
PBR & operator=(PBR const &)=default
Default copy assignment operator.
std::initializer_list< std::vector< T > > const & initializer_list_type
Type of constructor argument.
Definition pbr.hpp:74
T forward(T... args)
@ right
Definition action.hpp:95
@ left
Definition action.hpp:92
enable_if_is_same< Return, Blocks > make(Container const &cont)
Check the arguments, construct a Blocks object, and check it.
Definition bipart.hpp:798
std::enable_if_t< std::is_same_v< Given, Expected >, Expected > enable_if_is_same
Alias equal to the second template parameter if both template parameters are equal.
Definition types.hpp:50
Namespace for PBR helper functions.
Definition pbr.hpp:383
void throw_if_invalid(PBR const &x)
Throws if a PBR is invalid.
Definition pbr.hpp:487
PBR one(size_t n)
Returns the identity PBR with specified degree.
void throw_if_adjacencies_unsorted(PBR const &x)
Throws if a PBR has a list of points related to a point that is not sorted.
void throw_if_entry_out_of_bounds(PBR const &x)
Throws if a PBR has a point related to a point that is greater than degree().
void throw_if_not_even_length(PBR const &x)
Throws if a PBR has an odd number of points.
Namespace for everything in the libsemigroups library.
Definition action.hpp:44
static constexpr bool IsPBR
Helper variable template.
Definition pbr.hpp:718
size_t operator()(PBR const &x) const noexcept
Returns the approximate time complexity of multiplying PBRs.
Definition pbr.hpp:755
Adapter for the complexity of multiplication.
Definition adapters.hpp:121
size_t operator()(PBR const &x) const noexcept
Returns the degree of x.
Definition pbr.hpp:785
Adapter for the degree of an element.
Definition adapters.hpp:159
size_t operator()(PBR const &x) const
Returns a hash value for x.
Definition pbr.hpp:815
Adapter for hashing.
Definition adapters.hpp:446
void operator()(PBR &, size_t)
Do nothing.
Definition pbr.hpp:914
Adapter for increasing the degree of an element.
Definition adapters.hpp:199
PBR operator()(PBR const &x) const
Returns the identity PBR with degree x.degree().
Definition pbr.hpp:843
PBR operator()(size_t N=0) const
Returns the identity PBR with specified degree.
Definition pbr.hpp:864
Adapter for the identity element of the given type.
Definition adapters.hpp:246
void operator()(PBR &xy, PBR const &x, PBR const &y, size_t thread_id=0)
Multiply two PBR objects and store the product in a third.
Definition pbr.hpp:900
Adapter for the product of two elements.
Definition adapters.hpp:284