21 #ifndef LIBSEMIGROUPS_SRC_ELEMENTS_H_ 22 #define LIBSEMIGROUPS_SRC_ELEMENTS_H_ 29 #include <unordered_set> 33 #include "libsemigroups-debug.h" 41 #if !(__GNUC_PREREQ(4, 9)) 42 #define DO_NOT_USE_THREAD_LOCAL 44 #elif defined(__clang__) 45 #if !(__has_feature(cxx_thread_local)) 46 #define DO_NOT_USE_THREAD_LOCAL 129 virtual size_t degree()
const = 0;
301 template <
typename TValueType,
class TSub
class>
308 :
Element(), _vector(new std::vector<TValueType>()) {}
318 :
Element(), _vector(vector) {}
335 return (*_vector)[pos];
342 inline TValueType
at(
size_t pos)
const {
343 return _vector->at(pos);
351 return *(
static_cast<TSubclass const&
>(that)._vector) == *(this->_vector);
360 TSubclass
const& ewvd =
static_cast<TSubclass const&
>(that);
361 if (this->_vector->size() != ewvd._vector->size()) {
362 return this->_vector->size() < ewvd._vector->size();
364 for (
size_t i = 0; i < this->_vector->size(); i++) {
365 if ((*
this)[i] != ewvd[i]) {
366 return (*
this)[i] < ewvd[i];
380 LIBSEMIGROUPS_ASSERT(increase_deg_by == 0);
381 (void) increase_deg_by;
382 std::vector<TValueType>* vector(
new std::vector<TValueType>(*_vector));
383 TSubclass*
copy =
new TSubclass(vector);
399 _vector->assign(xx->_vector->cbegin(), xx->_vector->cend());
413 _vector->
swap(*(xx->_vector));
426 inline typename std::vector<TValueType>::iterator
begin()
const {
427 return _vector->begin();
434 inline typename std::vector<TValueType>::iterator
end()
const {
435 return _vector->end();
442 inline typename std::vector<TValueType>::iterator
cbegin()
const {
443 return _vector->cbegin();
450 inline typename std::vector<TValueType>::iterator
cend()
const {
451 return _vector->cend();
459 template <
typename T>
462 for (
auto const& x : *vec) {
463 seed ^= std::hash<T>{}(x) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
480 template <
typename TValueType,
class TSub
class>
493 this->
_hash_value = this->vector_hash(this->_vector);
527 template <
typename TValueType,
typename TSub
class>
532 ElementWithVectorDataDefaultHash;
540 return this->_vector->size();
549 return this->_vector->size();
559 _lookup.resize(
degree(),
false);
561 for (
auto const& x : *(this->_vector)) {
576 std::vector<TValueType>* vector(
new std::vector<TValueType>());
577 vector->reserve(this->
degree());
578 for (
size_t i = 0; i < this->
degree(); i++) {
579 vector->push_back(i);
581 return new TSubclass(vector);
592 #ifdef DO_NOT_USE_THREAD_LOCAL 593 static std::vector<bool> _lookup;
595 static thread_local std::vector<bool> _lookup;
599 template <
typename TValueType,
typename TSub
class>
600 #ifndef DO_NOT_USE_THREAD_LOCAL 605 = std::vector<bool>();
607 template <
typename TValueType,
typename TSub
class>
609 = std::numeric_limits<TValueType>::max();
622 template <
typename T>
636 if (increase_deg_by == 0) {
639 size_t n = copy->
_vector->size();
640 copy->
_vector->reserve(n + increase_deg_by);
641 for (
size_t i = n; i < n + increase_deg_by; i++) {
657 LIBSEMIGROUPS_ASSERT(x !=
this && y !=
this);
660 size_t const n = this->_vector->size();
661 for (T i = 0; i < n; i++) {
662 (*this->_vector)[i] = (*yy)[(*xx)[i]];
672 size_t deg = this->_vector->size();
673 for (
auto const& val : *(this->_vector)) {
694 template <
typename T>
708 std::vector<T>
const& ran,
711 LIBSEMIGROUPS_ASSERT(dom.size() == ran.size());
712 LIBSEMIGROUPS_ASSERT(dom.empty()
713 || deg >= *std::max_element(dom.begin(), dom.end()));
715 this->_vector->resize(deg + 1,
UNDEFINED);
716 for (
size_t i = 0; i < dom.size(); i++) {
717 (*this->_vector)[dom[i]] = ran[i];
732 size_t deg_this = pp_this->
degree();
733 for (
auto it = pp_this->_vector->end() - 1;
734 it >= pp_this->_vector->begin();
742 size_t deg_that = pp_that.
degree();
743 for (
auto it = pp_that.
_vector->end() - 1;
744 it >= pp_that.
_vector->begin() && deg_that >= deg_this;
753 if (deg_this != deg_that) {
754 return deg_this < deg_that;
757 for (
size_t i = 0; i < deg_this; i++) {
758 if ((*pp_this)[i] != pp_that[i]) {
760 || (pp_that[i] !=
UNDEFINED && (*pp_this)[i] < pp_that[i]);
775 if (increase_deg_by == 0) {
778 size_t n = copy->
_vector->size();
779 copy->
_vector->reserve(n + increase_deg_by);
780 for (
size_t i = n; i < n + increase_deg_by; i++) {
796 LIBSEMIGROUPS_ASSERT(x !=
this && y !=
this);
799 size_t const n = this->
degree();
800 for (T i = 0; i < n; i++) {
816 return this->_vector->size() - std::count(this->_vector->cbegin(),
817 this->_vector->cend(),
846 _trans_blocks_lookup(),
848 this->_vector->resize(2 * degree);
864 _trans_blocks_lookup(),
877 :
Bipartition(new std::vector<u_int32_t>(blocks)) {}
891 size_t degree()
const override;
913 size_t const& thread_id)
override;
926 u_int32_t const_nr_blocks()
const;
931 u_int32_t nr_blocks();
938 u_int32_t nr_left_blocks();
945 u_int32_t nr_right_blocks();
954 bool is_transverse_block(
size_t index);
976 LIBSEMIGROUPS_ASSERT(_nr_blocks == Bipartition::UNDEFINED
977 || _nr_blocks == nr_blocks);
978 _nr_blocks = nr_blocks;
988 LIBSEMIGROUPS_ASSERT(_nr_left_blocks == Bipartition::UNDEFINED
989 || _nr_left_blocks == nr_left_blocks);
990 _nr_left_blocks = nr_left_blocks;
999 LIBSEMIGROUPS_ASSERT(_rank == Bipartition::UNDEFINED || _rank == rank);
1004 u_int32_t fuseit(std::vector<u_int32_t>& fuse, u_int32_t pos);
1005 void init_trans_blocks_lookup();
1007 static std::vector<std::vector<u_int32_t>> _fuse;
1008 static std::vector<std::vector<u_int32_t>> _lookup;
1011 size_t _nr_left_blocks;
1012 std::vector<bool> _trans_blocks_lookup;
1033 template <
typename TValueType,
class TSub
class>
1055 _degree(sqrt(matrix->size())),
1056 _semiring(semiring) {
1057 LIBSEMIGROUPS_ASSERT(semiring !=
nullptr);
1058 LIBSEMIGROUPS_ASSERT(!matrix->empty());
1059 LIBSEMIGROUPS_ASSERT(matrix->size() == _degree * _degree);
1086 _degree(matrix[0].size()),
1087 _semiring(semiring) {
1088 LIBSEMIGROUPS_ASSERT(semiring !=
nullptr);
1089 LIBSEMIGROUPS_ASSERT(!matrix.empty());
1090 LIBSEMIGROUPS_ASSERT(all_of(
1091 matrix.begin(), matrix.end(), [matrix](std::vector<TValueType> row) {
1092 return row.size() == matrix.size();
1095 this->_vector->reserve(matrix.size() * matrix.size());
1096 for (
auto const& row : matrix) {
1097 this->_vector->insert(this->_vector->end(), row.begin(), row.end());
1111 return pow(this->
degree(), 3);
1128 std::vector<TValueType>* vec(
new std::vector<TValueType>());
1129 vec->resize(this->_vector->size(), _semiring->zero());
1130 size_t n = this->
degree();
1131 for (
auto it = vec->begin(); it < vec->end(); it += n + 1) {
1132 (*it) = _semiring->one();
1134 return new TSubclass(vec, _semiring);
1142 LIBSEMIGROUPS_ASSERT(increase_deg_by == 0);
1143 (void) increase_deg_by;
1147 LIBSEMIGROUPS_ASSERT(copy->_semiring ==
nullptr 1148 || copy->_semiring == this->_semiring);
1149 copy->_semiring = _semiring;
1162 LIBSEMIGROUPS_ASSERT(xx->degree() == yy->degree());
1163 LIBSEMIGROUPS_ASSERT(xx->degree() == this->
degree());
1164 LIBSEMIGROUPS_ASSERT(xx !=
this && yy !=
this);
1171 size_t deg = this->
degree();
1173 for (
size_t i = 0; i < deg; i++) {
1174 for (
size_t j = 0; j < deg; j++) {
1175 int64_t v = _semiring->zero();
1176 for (
size_t k = 0; k < deg; k++) {
1177 v = _semiring->plus(
1178 v, _semiring->prod((*xx)[i * deg + k], (*yy)[k * deg + j]));
1180 (*this->_vector)[i * deg + j] = v;
1194 _degree(sqrt(matrix->size())),
1195 _semiring(nullptr) {}
1199 virtual inline void after() {}
1210 template <
typename TValueType>
1213 MatrixOverSemiring<TValueType>> {
1215 MatrixOverSemiring<TValueType>>;
1217 MatrixOverSemiringBase;
1260 explicit ProjectiveMaxPlusMatrix(std::vector<int64_t>* matrix)
1264 inline void after()
final {
1265 int64_t norm = *std::max_element(_vector->begin(), _vector->end());
1266 for (
auto& x : *_vector) {
1267 if (x != LONG_MIN) {
1301 explicit BooleanMat(std::vector<std::vector<bool>>
const& matrix)
1325 explicit BooleanMat(std::vector<bool>* matrix,
1361 size_t degree()
const override;
1384 size_t const& thread_id)
override;
1390 void unite_rows(RecVec<bool>& out,
1392 size_t const& vertex1,
1393 size_t const& vertex2);
1395 void x_dfs(std::vector<bool>& x_seen,
1396 std::vector<bool>& y_seen,
1404 void y_dfs(std::vector<bool>& x_seen,
1405 std::vector<bool>& y_seen,
1413 static std::vector<std::vector<bool>> _x_seen;
1414 static std::vector<std::vector<bool>> _y_seen;
1415 static std::vector<RecVec<bool>> _out;
1416 static std::vector<RecVec<bool>> _tmp;
1419 template <
typename T>
static inline void really_delete_cont(T cont) {
1420 for (
Element const* x : cont) {
1426 template <
typename T>
static inline void really_delete_cont(T* cont) {
1427 for (
Element const* x : *cont) {
1434 #endif // LIBSEMIGROUPS_SRC_ELEMENTS_H_ Class for bipartitions.
Definition: elements.h:835
Element(elm_t type=Element::elm_t::NOT_RWSE)
A constructor.
Definition: elements.h:76
size_t operator()(Element const *x) const
Returns the value of Element::hash_value applied to the Element pointed to by x.
Definition: elements.h:248
BooleanMat(std::vector< bool > *matrix)
A constructor.
Definition: elements.h:1293
Class for partitioned binary relations (PBR).
Definition: elements.h:1337
ElementWithVectorData()
A constructor.
Definition: elements.h:307
ProjectiveMaxPlusMatrix(std::vector< int64_t > *matrix, Semiring< int64_t > const *semiring)
A constructor.
Definition: elements.h:1239
virtual void redefine(Element const *x, Element const *y, size_t const &thread_id)
Multiplies x and y and stores the result in this.
Definition: elements.h:222
The usual Boolean semiring.
Definition: semiring.h:85
Matrices over a semiring.
Definition: elements.h:1211
virtual void swap(Element *x)=0
Swap another Element with this.
virtual size_t degree() const =0
Returns the degree of an Element.
MatrixOverSemiringBase(std::vector< TValueType > *matrix, Semiring< TValueType > const *semiring)
A constructor.
Definition: elements.h:1052
Matrices over the boolean semiring.
Definition: elements.h:1281
Class for signed partitions of the set .
Definition: blocks.h:45
std::vector< TValueType >::iterator cend() const
Returns a const iterator.
Definition: elements.h:450
bool operator<(Element const &that) const override
Returns true if this is less than that.
Definition: elements.h:359
MatrixOverSemiringBase(std::vector< TValueType > *matrix)
Constructs a MatrixOverSemiringBase with whose underlying semiring is not defined. The underlying semiring must be set by any class deriving from this one.
Definition: elements.h:1192
Bipartition(std::vector< u_int32_t > const &blocks)
A constructor.
Definition: elements.h:876
virtual void cache_hash_value() const =0
Calculate and cache a hash value.
MatrixOverSemiringBase(std::vector< std::vector< TValueType >> const &matrix, Semiring< TValueType > const *semiring)
A constructor.
Definition: elements.h:1083
std::vector< TValueType >::iterator cbegin() const
Returns a const iterator.
Definition: elements.h:442
size_t _hash_value
This data member holds a cached version of the hash value of an Element. It is stored here if it is e...
Definition: elements.h:281
Abstract base class for elements using a vector to store their defining data.
Definition: elements.h:302
Provides a call operator for comparing Elements via pointers.
Definition: elements.h:232
virtual void copy(Element const *x)=0
Copy another Element into this.
PartialPerm(std::vector< T > const &dom, std::vector< T > const &ran, size_t deg)
A constructor.
Definition: elements.h:707
Abstract base class for semigroup elements.
Definition: elements.h:57
void redefine(Element const *x, Element const *y) override
Multiply x and y and stores the result in this.
Definition: elements.h:793
void set_nr_blocks(size_t nr_blocks)
Set the cached number of blocks.
Definition: elements.h:975
size_t complexity() const override
Returns the approximate time complexity of multiplying two matrices.
Definition: elements.h:1110
Template class for partial permutations.
Definition: elements.h:695
Matrices over a semiring.
Definition: elements.h:1034
ElementWithVectorData(std::vector< TValueType > const &vector)
A constructor.
Definition: elements.h:326
ElementWithVectorData(std::vector< TValueType > *vector)
A constructor.
Definition: elements.h:317
Element * really_copy(size_t increase_deg_by=0) const override
Returns a pointer to a copy of this.
Definition: elements.h:1141
size_t operator()(Element const *x, Element const *y) const
Returns true if x and y point to equal Element's.
Definition: elements.h:234
void reset_hash_value() const
Reset the cached value used by Element::hash_value.
Definition: elements.h:265
Type for Element objects not arising from a rewriting system RWS.
Definition: elements.h:69
Bipartition(std::vector< u_int32_t > *blocks)
A constructor.
Definition: elements.h:860
TValueType operator[](size_t pos) const
Returns the pos entry in the vector containing the defining data.
Definition: elements.h:334
void swap(Element *x) override
Swap another Element with this.
Definition: elements.h:410
ProjectiveMaxPlusMatrix(std::vector< std::vector< int64_t >> const &matrix, Semiring< int64_t > const *semiring)
A constructor.
Definition: elements.h:1252
void cache_hash_value() const override
This method implements the default hash function for an ElementWithVectorData, which uses ElementWith...
Definition: elements.h:492
elm_t get_type() const
Returns the type libsemigroups::Element::elm_t of an Element object.
Definition: elements.h:86
Element * really_copy(size_t increase_deg_by=0) const override
Returns a pointer to a copy of this.
Definition: elements.h:772
Namespace for everything in the libsemigroups library.
Definition: blocks.cc:32
size_t hash_value() const
Return the hash value of an Element.
Definition: elements.h:136
bool operator<(const Element &that) const override
Returns true if this is less than that.
Definition: elements.h:728
void set_rank(size_t rank)
Set the cached rank.
Definition: elements.h:998
void copy(Element const *x) override
Copy another Element into this.
Definition: elements.h:395
elm_t
This enum contains some different types of Element.
Definition: elements.h:65
static size_t vector_hash(std::vector< T > const *vec)
Returns a hash value for a vector provided there is a specialization of std::hash for the template ty...
Definition: elements.h:460
Abstract base class for elements using a vector to store their defining data and the default hash fun...
Definition: elements.h:481
virtual bool operator==(const Element &that) const =0
Returns true if this equals that.
virtual void really_delete()=0
Deletes the defining data of an Element.
void set_nr_left_blocks(size_t nr_left_blocks)
Set the cached number of left blocks.
Definition: elements.h:987
static size_t const UNDEFINED
UNDEFINED value.
Definition: elements.h:274
std::vector< TValueType >::iterator begin() const
Returns an iterator.
Definition: elements.h:426
std::vector< TValueType > * _vector
The vector containing the defining data of this.
Definition: elements.h:471
virtual ~Element()
A default destructor.
Definition: elements.h:83
virtual void redefine(Element const *x, Element const *y)
Multiplies x and y and stores the result in this.
Definition: elements.h:198
Element * identity() const override
Returns the identity matrix with dimension of this.
Definition: elements.h:1127
virtual Element * really_copy(size_t increase_deg_by=0) const =0
Returns a new element completely independent of this.
virtual bool operator<(const Element &that) const =0
Returns true if this is less than that.
Subclass of Element that wraps an libsemigroups::rws_word_t.
Definition: rwse.h:37
size_t crank() const
Returns the rank of a partial permutation.
Definition: elements.h:815
BooleanMat(std::vector< std::vector< bool >> const &matrix)
A constructor.
Definition: elements.h:1301
std::vector< TValueType >::iterator end() const
Returns an iterator.
Definition: elements.h:434
Element * really_copy(size_t increase_deg_by=0) const override
Returns a pointer to a copy of this.
Definition: elements.h:379
Semiring< TValueType > const * semiring() const
Returns a pointer to the Semiring over which the matrix is defined.
Definition: elements.h:1102
Provides a call operator returning a hash value for an Element via a pointer.
Definition: elements.h:245
size_t degree() const override
Returns the dimension of the matrix.
Definition: elements.h:1118
virtual Element * identity() const =0
Returns a new copy of the identity element.
Class for projective max-plus matrices.
Definition: elements.h:1229
virtual size_t complexity() const =0
Returns the approximate time complexity of multiplying two Element objects in a given subclass...
TValueType at(size_t pos) const
Returns the pos entry in the vector containing the defining data.
Definition: elements.h:342
Bipartition(size_t degree)
A constructor.
Definition: elements.h:842
void really_delete() override
Deletes the defining data of an ElementWithVectorData.
Definition: elements.h:418
bool operator==(Element const &that) const override
Returns true if this equals that.
Definition: elements.h:350
void redefine(Element const *x, Element const *y) override
Multiply x and y and stores the result in this.
Definition: elements.h:1159