Macaulay2 Engine
Loading...
Searching...
No Matches
WordTable.hpp
Go to the documentation of this file.
1#ifndef _word_table_hpp_
2#define _word_table_hpp_
3
48
49#include <cstddef>
50#include <ostream>
51#include <tuple>
52#include <utility>
53#include <vector>
54
55#include "MemoryBlock.hpp" // for MemoryBlock
56
57class Word;
58class WordWithData;
59
60// TODO
61// have a vector std::vector<int> of indices of each word.
62// an index of -1 means that this element has been removed.
63// changes to code:
64// insert: push_back of the index, return that index.
65// routines that search over words in the table:
66// each should continue from any element with -1, ignoring it.
67// retire(index): set index to -1, set word to null's.
68// move Word code to Word.hpp DONE
69
70using Overlap = std::tuple<int,int,int,bool>;
71
89{
90 // abstract table class for Word's
91public:
92 friend std::ostream& operator<<(std::ostream& o, const WordTable& wordTable);
93
95
97
98 void clear() { mMonomials.clear(); }
99
100 size_t monomialCount() const { return mMonomials.size(); }
101
102 size_t insert(Word w);
103
104 size_t insert(Word w, std::vector<Overlap>& newRightOverlaps);
105
106 // access routine
107 const Word& operator[](int index) const { return mMonomials[index]; }
108
109 // lookup routines
110
111 // return all pairs (i,j), where
112 // the i-th word in the table is w (say)
113 // j is a position in word
114 // such that w appears in word starting at position j.
115 void subwords(Word word,
116 std::vector<std::pair<int,int>>& output) const;
117
118 // sets 'output' to the first pair (i,j), where
119 // the i-th word in the table is w (say)
120 // j is a position in word
121 // such that w appears in word starting at position j.
122 // if such a match is found, output is set, and true is returned.
123 // if not, false is returned.
124 bool subword(Word word,
125 std::pair<int,int>& output) const;
126
127 // returns true if some element in the table is a prefix/suffix of 'word'.
128 // In this case, 'output' is set to the index of the corresponding element.
129 bool isPrefix(Word word, int& output) const;
130 bool isSuffix(Word word, int& output) const;
131
132 auto isNontrivialSuperword(Word word, int index1, int index2) const -> bool;
133
134 // return all pairs (i,j), where
135 // the i-th word in the table is w (say)
136 // j is a position in w
137 // such that word appears in w starting at position j.
138 void superwords(Word word,
139 std::vector<std::pair<int,int>>& output) const;
140
141 // given 'word', find all left over laps with elements of the table.
142 // A left overlap of 'alpha' and 'beta' is:
143 // a prefix of alpha is a suffix of beta.
144 // i.e. alpha = a.b
145 // beta = c.a (a,b,c are words)
146 // returned Overlap for this overlap:
147 void leftOverlaps(std::vector<Overlap>& newLeftOverlaps) const;
148
149 // find (right) overlaps with most recent added word 'w'.
150 void rightOverlaps(std::vector<Overlap>& newRightOverlaps) const;
151
152 // moved to public for use in retiring unnecessary overlaps
153 static void subwordPositions(Word word1,
154 Word word2,
155 std::vector<int>& result_start_indices);
156
157private:
158 // returns true if word1 is a prefix/suffix of word2
159 static bool isPrefixOf(Word word1, Word word2);
160 static bool isSuffixOf(Word word1, Word word2);
161
162 static bool subwordPosition(Word word1,
163 Word word2,
164 int& result_start_index);
165
166 // overlaps here: suffix of word1 == prefix of word2.
167 // overlap value is the start of prefix of word2 in word1.
168 static void overlaps(Word word1,
169 Word word2,
170 std::vector<int>& result_overlaps);
171
172private:
173 std::vector<Word> mMonomials;
174 std::vector<int> mIndices; // -1 means word was retired, cannot be used in this class any longer.
175
177};
178
193{
194 // abstract table class for WordWithData's
195
196 // all searching functions will ignore a WordWithData whose corresponding entry in mIndices is
197 // set to -1.
198
199 // Any subword lookups are affected by ecart degree (see subword description below)
200public:
201 friend std::ostream& operator<<(std::ostream& o, const WordWithDataTable& wordWithDataTable);
202
204
206
207 void clear() { mMonomials.clear(); mIndices.clear(); }
208
209 size_t monomialCount() const { return mMonomials.size(); }
210
211 size_t insert(WordWithData w);
212
213 size_t insert(WordWithData w, std::vector<Overlap>& newRightOverlaps);
214
215 // access routine
216 const WordWithData& operator[](int index) const { return mMonomials[index]; }
217
218 // lookup routines
219
220 // return all pairs (i,j), where
221 // the i-th word in the table is w (say)
222 // j is a position in word
223 // such that w appears in word starting at position j.
224 // ** ecart degree affects matches as well. see description
225 // ** of subword below.
227 std::vector<std::pair<int,int>>& output) const;
228
229 // sets 'output' to the first pair (i,j), where
230 // the i-th word in the table is w (say)
231 // j is a position in word
232 // such that w appears in word starting at position j.
233 // ** also, ecart degree is checked as well to see if
234 // ** ecart degree of w is less than or equal to that of 'word'
235 // if such a match is found, output is set, and true is returned.
236 // if not, false is returned.
238 std::pair<int,int>& output) const;
239
240 // returns true if some element in the table is a prefix/suffix of 'word'.
241 // In this case, 'output' is set to the index of the corresponding element.
242 bool isPrefix(WordWithData word, int& output) const;
243 bool isSuffix(WordWithData word, int& output) const;
244
245 auto isNontrivialSuperword(WordWithData word, int index1, int index2) const -> bool;
246
247 // return all pairs (i,j), where
248 // the i-th word in the table is w (say)
249 // j is a position in w
250 // such that word appears in w starting at position j.
252 std::vector<std::pair<int,int>>& output) const;
253
254 // given 'word', find all left over laps with elements of the table.
255 // A left overlap of 'alpha' and 'beta' is:
256 // a prefix of alpha is a suffix of beta.
257 // i.e. alpha = a.b
258 // beta = c.a (a,b,c are words)
259 // returned Overlap for this overlap:
260 void leftOverlaps(std::vector<Overlap>& newLeftOverlaps) const;
261
262 // find (right) overlaps with most recent added word 'w'.
263 void rightOverlaps(std::vector<Overlap>& newRightOverlaps) const;
264
265 // retire the ith word in the table
266 void retire(int retiree) { mIndices[retiree] = -1; }
267
268private:
269 // returns true if word1 is a prefix/suffix of word2
270 static bool isPrefixOf(WordWithData word1, WordWithData word2);
271 static bool isSuffixOf(WordWithData word1, WordWithData word2);
272
273 static void subwordPositions(WordWithData word1,
274 WordWithData word2,
275 std::vector<int>& result_start_indices);
276
277 static bool subwordPosition(WordWithData word1,
278 WordWithData word2,
279 int& result_start_index);
280
281 // overlaps here: suffix of word1 == prefix of word2.
282 // overlap value is the start of prefix of word2 in word1.
283 static void overlaps(WordWithData word1,
284 WordWithData word2,
285 std::vector<int>& result_overlaps);
286
287private:
288 // WARNING TODO: If this is to be used, one must make copies of the word part
289 // in order to work with our approach to autoreduction.
290 std::vector<WordWithData> mMonomials;
291 std::vector<int> mIndices; // -1 means word was retired, cannot be used in this class any longer.
292};
293
294
295std::ostream& operator<<(std::ostream& o, const WordTable& wordTable);
296std::ostream& operator<<(std::ostream& o, const WordWithDataTable& wordWithDataTable);
297
298
299#endif
300
301// Local Variables:
302// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
303// indent-tabs-mode: nil
304// End:
305
Bump-pointer arena allocator for transient inner-loop allocations.
std::vector< int > word
std::tuple< int, int, int, bool > Overlap
std::ostream & operator<<(std::ostream &o, const WordTable &wordTable)
Definition WordTable.cpp:4
Thin RAII wrapper around memtailor::Arena providing bump-pointer array allocation with optional mutex...
Non-owning view of a non-commutative word: [begin, end) of int variable indices.
Definition Word.hpp:56
size_t monomialCount() const
const Word & operator[](int index) const
void superwords(Word word, std::vector< std::pair< int, int > > &output) const
void rightOverlaps(std::vector< Overlap > &newRightOverlaps) const
static void subwordPositions(Word word1, Word word2, std::vector< int > &result_start_indices)
Definition WordTable.cpp:38
bool isPrefix(Word word, int &output) const
bool isSuffix(Word word, int &output) const
std::vector< int > mIndices
void subwords(Word word, std::vector< std::pair< int, int > > &output) const
std::vector< Word > mMonomials
static void overlaps(Word word1, Word word2, std::vector< int > &result_overlaps)
auto isNontrivialSuperword(Word word, int index1, int index2) const -> bool
void leftOverlaps(std::vector< Overlap > &newLeftOverlaps) const
friend std::ostream & operator<<(std::ostream &o, const WordTable &wordTable)
Definition WordTable.cpp:4
static bool isPrefixOf(Word word1, Word word2)
Definition WordTable.cpp:86
static bool subwordPosition(Word word1, Word word2, int &result_start_index)
Definition WordTable.cpp:59
void clear()
Definition WordTable.hpp:98
bool subword(Word word, std::pair< int, int > &output) const
MemoryBlock mMonomialSpace
size_t insert(Word w)
Definition WordTable.cpp:17
static bool isSuffixOf(Word word1, Word word2)
Index of Words (non-commutative monomials) with subword, prefix/suffix, and overlap lookup used by th...
Definition WordTable.hpp:89
Word plus its ecart degree and heft degree — the value type WordWithDataTable stores.
Definition Word.hpp:116
friend std::ostream & operator<<(std::ostream &o, const WordWithDataTable &wordWithDataTable)
static void overlaps(WordWithData word1, WordWithData word2, std::vector< int > &result_overlaps)
bool subword(WordWithData word, std::pair< int, int > &output) const
static void subwordPositions(WordWithData word1, WordWithData word2, std::vector< int > &result_start_indices)
bool isSuffix(WordWithData word, int &output) const
std::vector< int > mIndices
void superwords(WordWithData word, std::vector< std::pair< int, int > > &output) const
static bool subwordPosition(WordWithData word1, WordWithData word2, int &result_start_index)
void retire(int retiree)
bool isPrefix(WordWithData word, int &output) const
auto isNontrivialSuperword(WordWithData word, int index1, int index2) const -> bool
size_t insert(WordWithData w)
static bool isPrefixOf(WordWithData word1, WordWithData word2)
void leftOverlaps(std::vector< Overlap > &newLeftOverlaps) const
const WordWithData & operator[](int index) const
size_t monomialCount() const
static bool isSuffixOf(WordWithData word1, WordWithData word2)
void subwords(WordWithData word, std::vector< std::pair< int, int > > &output) const
void rightOverlaps(std::vector< Overlap > &newRightOverlaps) const
std::vector< WordWithData > mMonomials
Variant of WordTable where each stored monomial carries an additional ecart-degree datum that gates s...