libsemigroups
rwse.h
1 //
2 // libsemigroups - C++ library for semigroups and monoids
3 // Copyright (C) 2016 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 #ifndef LIBSEMIGROUPS_SRC_RWSE_H_
20 #define LIBSEMIGROUPS_SRC_RWSE_H_
21 
22 #include <algorithm>
23 #include <stack>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "elements.h"
29 #include "rws.h"
30 
31 namespace libsemigroups {
32 
37  class RWSE : public Element {
38  private:
39  RWSE(RWS* rws, rws_word_t* w, bool reduce)
40  : Element(Element::elm_t::RWSE), _rws(rws), _rws_word(w) {
41  if (reduce) {
42  _rws->rewrite(_rws_word);
43  }
44  }
45 
46  public:
58  RWSE(RWS* rws, rws_word_t* w) : RWSE(rws, w, true) {
59  LIBSEMIGROUPS_ASSERT(w != nullptr);
60  }
61 
70  RWSE(RWS& rws, rws_word_t const& w) : RWSE(&rws, new rws_word_t(w)) {}
71 
75  RWSE(RWS& rws, letter_t const& a)
76  : RWSE(&rws, RWS::letter_to_rws_word(a)) {}
77 
81  RWSE(RWS& rws, word_t const& w) : RWSE(&rws, RWS::word_to_rws_word(w)) {}
82 
88  bool operator==(Element const& that) const override {
89  LIBSEMIGROUPS_ASSERT(_rws_word != nullptr);
90  LIBSEMIGROUPS_ASSERT(static_cast<RWSE const&>(that)._rws_word != nullptr);
91  return *(static_cast<RWSE const&>(that)._rws_word) == *(this->_rws_word);
92  }
93 
99  // TODO should use the reduction ordering of RWS.
100  bool operator<(const Element& that) const override;
101 
107  Element* really_copy(size_t increase_deg_by) const override;
108 
113  void copy(Element const* x) override;
114  void swap(Element* x) override;
115 
119  void really_delete() override {
120  delete _rws_word;
121  _rws_word = nullptr;
122  }
123 
132  size_t complexity() const override {
133  return Semigroup::LIMIT_MAX;
134  }
135 
142  size_t degree() const override {
143  return 0;
144  }
145 
152  Element* identity() const override {
153  return new RWSE(_rws, new rws_word_t());
154  }
155 
159  void cache_hash_value() const override {
160  LIBSEMIGROUPS_ASSERT(_rws_word != nullptr);
161  this->_hash_value = std::hash<rws_word_t>()(*_rws_word);
162  }
163 
175  void redefine(Element const* x, Element const* y) override;
176 
178  rws_word_t const* get_rws_word() const {
179  return _rws_word;
180  }
181 
182  private:
183  // TODO const!
184  RWS* _rws;
185  rws_word_t* _rws_word;
186  };
187 } // namespace libsemigroups
188 
189 #endif // LIBSEMIGROUPS_SRC_RWSE_H_
void cache_hash_value() const override
Calculates a hash value for this object which is cached.
Definition: rwse.h:159
static index_t const LIMIT_MAX
This variable is used to indicate the maximum possible limit that can be used with Semigroup::enumera...
Definition: semigroups.h:864
Element(elm_t type=Element::elm_t::NOT_RWSE)
A constructor.
Definition: elements.h:76
void rewrite(rws_word_t *w) const
Rewrites the word pointed to by w in-place according to the current rules in the rewriting system...
Definition: rws.cc:195
std::vector< letter_t > word_t
Type for a word over the generators of a semigroup.
Definition: semigroups.h:49
void really_delete() override
Deletes the underlying rws_word_t that this object wraps.
Definition: rwse.h:119
void swap(Element *x) override
Swap another Element with this.
Definition: rwse.cc:54
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 semigroup elements.
Definition: elements.h:57
size_t degree() const override
Returns the degree of an RWSE.
Definition: rwse.h:142
bool operator<(const Element &that) const override
Returns true if this is less than that and false if it is not.
Definition: rwse.cc:25
Element * really_copy(size_t increase_deg_by) const override
Returns a pointer to a copy of this.
Definition: rwse.cc:38
rws_word_t const * get_rws_word() const
Returns a pointer to the rws_word_t used to create this.
Definition: rwse.h:178
void redefine(Element const *x, Element const *y) override
Multiply x and y and stores the result in this.
Definition: rwse.cc:62
std::string rws_word_t
Type for words for rewriting systems.
Definition: rws.h:37
Namespace for everything in the libsemigroups library.
Definition: blocks.cc:32
void copy(Element const *x) override
Copy x into this.
Definition: rwse.cc:46
size_t complexity() const override
Returns the approximate time complexity of multiplying two RWSE&#39;s.
Definition: rwse.h:132
RWSE(RWS *rws, rws_word_t *w)
Constructor from a rewriting system and a word.
Definition: rwse.h:58
Element * identity() const override
Return the identity RWSE.
Definition: rwse.h:152
This class is used to represent a string rewriting system defining a finitely presented monoid or sem...
Definition: rws.h:122
bool operator==(Element const &that) const override
Returns true if this equals that.
Definition: rwse.h:88
size_t letter_t
Type for the index of a generator of a semigroup.
Definition: semigroups.h:46
Subclass of Element that wraps an libsemigroups::rws_word_t.
Definition: rwse.h:37
RWSE(RWS &rws, letter_t const &a)
Constructor from a rewriting system and a letter.
Definition: rwse.h:75
RWSE(RWS &rws, rws_word_t const &w)
Constructor from a rewriting system and a word.
Definition: rwse.h:70
RWSE(RWS &rws, word_t const &w)
Constructor from a rewriting system and a word.
Definition: rwse.h:81