9 for (
auto i = vec.begin(); i != vec.end(); ++i)
20 suffixTreeNode.
dump(o,0);
29 std::cout <<
"Root" << std::endl;
33 for (
auto j = 0; j < depth; ++j) o <<
" ";
38 for (
auto j = 0; j < depth; ++j) o <<
" ";
43 for (
auto j = 0; j < depth; ++j) o <<
" ";
44 o <<
"SuffixTreeLabel : ";
55 pair.second->dump(o,depth+1);
63 o <<
"Patterns in table: {";
70 o <<
"}" << std::endl;
83 return Word(vec.begin(),vec.begin()+lengthOfPrefix);
89 return Word(vec.begin()+indexOfSuffix,vec.end());
97 return (tmpA == a) && (tmpB == b);
105 std::cout << a <<
" " << b <<
" " << startIndex << std::endl;
106 if (b.
size() + 1 < a.
size())
return false;
109 return (tmpA == tmpB);
125 for (
auto i =
p->childrenBegin(); i !=
p->childrenEnd(); ++i)
138 std::vector<Overlap> tmpOverlaps;
139 return insert(w,tmpOverlaps);
146 auto plList = std::vector<int> {};
147 Label tempS(w.begin(),w.end());
149 tempS.push_back(-(wordNum + 1));
152 bool isFullPattern =
true;
153 while (
s.size() != 0)
158 else insertType =
insertStepC(v,v->parent()->suffixLink(),
Word(v->arcLabel()),
s,isFullPattern);
159 auto newv = std::get<0>(insertType);
160 auto roRoot = std::get<1>(insertType);
161 auto newLocus = std::get<2>(insertType);
163 if (roRoot !=
nullptr)
167 for(
auto pl : plList)
169 auto ro0 = newLocus->getPatternNumber();
170 auto ro1 =
mMonomials[ro0].size() - roRoot->label().size();
172 rightOverlaps.push_back(std::make_tuple(ro0,ro1,ro2,
true));
175 if (v !=
mRoot &&
s.size() == 2)
177 v->setSuffixLink(
mRoot);
180 isFullPattern =
false;
190 for (
auto i = ss.begin(); i != ss.end(); ++i)
200 return insert(tmpWord, rightOverlaps);
205 std::vector<Word> tmpWords;
206 for (
auto i = labs.begin(); i != labs.end(); ++i)
208 tmpWords.push_back(
Word(*i));
210 return insert(tmpWords, rightOverlaps);
233 auto f = std::get<0>(iwType);
234 auto betaHat = std::get<1>(iwType);
235 if (f->arcLabel().size() == betaHat.size())
246 d->setPatternLeafCount(f->patternLeafCount());
247 d->addToPatternLeafCount(isFullPattern);
248 d->addToPatternLeafCount(f->isFullPattern());
253 if (w->label().size() == 1)
254 return std::make_tuple(d,f,w);
256 return std::make_tuple(d,
nullptr,w);
267 auto newy = std::get<0>(clType);
268 auto f = std::get<1>(clType);
269 auto pre = std::get<2>(clType);
274 tmpLabel =
suffix(tmpLabel, newy->label().size() - tmpNode->label().size());
281 if (tmpNode->label().size() != 0 && v->arcLabel().size() == 1)
282 return std::make_tuple(tmpNode,tmpNode,v);
284 return std::make_tuple(tmpNode,
nullptr,v);
289 p->setPatternLeafCount(f->patternLeafCount());
290 p->addToPatternLeafCount(isFullPattern);
291 p->addToPatternLeafCount(f->isFullPattern());
293 tmpLabel =
suffix(tmpLabel,pre.size());
297 if (tmpLabel.size() == 1)
298 return std::make_tuple(
p,
p,w);
300 return std::make_tuple(
p,
nullptr,w);
309 auto p = f->parent();
311 p->removeChild(f->arcLabel());
312 f->dropFromArcLabel(
prefix.size());
331 y->addToPatternLeafCount(incrementLeafCount);
334 auto match =
findMatch(tmpNode,tmpLabel);
335 while (std::get<0>(match) !=
nullptr && std::get<1>(match) ==
Word(std::get<0>(match)->arcLabel()))
337 tmpNode = std::get<0>(match);
338 tmpLabel =
suffix(tmpLabel,std::get<1>(match).size());
339 tmpNode->addToPatternLeafCount(incrementLeafCount);
342 return std::make_tuple(tmpNode,std::get<0>(match),std::get<1>(match));
354 if (beta.size() == 0)
return std::make_tuple(
x,beta);
357 auto f = std::get<0>(
findMatch(tmpNode,betaHat));
360 std::cout <<
"Oooops" << std::endl;
361 std::cout << *tmpNode << std::endl;
362 std::cout << betaHat << std::endl;
364 while (f->arcLabel().size() < betaHat.size())
367 betaHat =
suffix(betaHat,f->arcLabel().size());
368 f = std::get<0>(
findMatch(tmpNode,betaHat));
370 return std::make_tuple(f, betaHat);
381 for (
auto i = y->childrenBegin(); i != y->childrenEnd(); ++i)
391 return std::make_pair(f,pre);
399 if (v->numChildren() == 0)
return;
402 output.push_back(
x->getPatternNumber());
413 auto retval = std::vector<SuffixTreeNode*> {};
414 if (v->numChildren() == 0 && v->isFullPattern())
419 if ((v->numChildren() == 0 && !v->isFullPattern()) || v->patternLeafCount() == 0)
423 for (
auto i = v->childrenBegin(); i != v->childrenEnd(); ++i)
426 std::copy(tmp.begin(),tmp.end(),std::back_inserter(retval));
435 while (i <
s.size() && i < t.size() &&
s.begin()[i] == t.begin()[i]) i++;
443 if (v->numChildren() == 0)
448 for (
auto x = v->childrenBegin();
x != v->childrenEnd(); ++
x)
461 auto tmp = std::vector<std::pair<int,int>> {};
463 if (tmp.size() == 0)
return false;
466 std::cout << (*tmp.begin()).first <<
" " << (*tmp.begin()).second << std::endl;
467 output = *(tmp.begin());
474 return subwords(w,output,
false);
478 std::vector<std::pair<int,int>>& output,
479 bool onlyFirst)
const ->
bool
488 std::cout << *(this->
mRoot) << std::endl;
489 std::cout << w << std::endl;
490 while (tmpLabel.
size() != 0)
497 auto newcLocus = std::get<0>(swType);
498 auto newbeta = std::get<1>(swType);
499 auto leaf = std::get<2>(swType);
500 auto wasPattern = std::get<3>(swType);
504 std::cout <<
Word(leaf->label()) <<
" " << tmpLabel << std::endl << std::flush;
505 auto tmp = std::make_pair(leaf->getPatternNumber(),pos);
506 output.push_back(tmp);
508 if (onlyFirst)
return true;
513 tmpLabel =
suffix(tmpLabel,1);
514 std::cout << *cLocus <<
" " << tmpLabel << std::endl;
523 if (beta.size() == 0)
526 auto f = std::get<0>(elType);
527 auto betaHat = std::get<1>(elType);
528 if (f->arcLabel().size() == betaHat.size())
531 return std::make_tuple(f->parent(),betaHat,
nullptr,
false);
538 auto newy = std::get<0>(clType);
539 auto f = std::get<1>(clType);
540 auto pre = std::get<2>(clType);
542 return std::make_tuple(newy,pre,
nullptr,
false);
544 return std::make_tuple(newy,pre,f,f->isFullPattern());
550 auto tmp = std::vector<std::pair<int,int>> {};
552 if (tmp.size() == 0)
return false;
555 output = *(tmp.begin());
563 return superwords(w,output,
false);
570 auto y = std::get<0>(clType);
571 auto f = std::get<1>(clType);
572 auto pre = std::get<2>(clType);
575 else if (f ==
nullptr)
577 auto leaves = std::vector<SuffixTreeNode*> {};
579 for (
auto g : leaves)
581 output.push_back(std::make_pair(g->getPatternNumber(),
mMonomials[g->getPatternNumber()].size() - g->label().size() + 1));
582 if (onlyFirst)
return true;
585 else if (f->isLeaf() && f->label().size() == w.size() + 1)
587 output.push_back(std::make_pair(f->getPatternNumber(),
mMonomials[f->getPatternNumber()].size() - f->label().size() + 1));
596 std::vector<std::pair<int,int>>& output,
597 bool avoidLast)
const ->
void
599 auto tmpNode =
mRoot;
601 auto match =
findMatch(tmpNode,tmpLabel);
602 auto f = std::get<0>(match);
603 auto pre = std::get<1>(match);
605 while (f !=
nullptr && pre ==
Word(f->arcLabel()))
610 tmpLabel =
suffix(tmpLabel,pre.size());
613 for (
auto i = tmpNode->childrenBegin(); i != tmpNode->childrenEnd(); ++i)
615 if (i->second->isLeaf() && !i->second->isFullPattern())
617 patternNum = i->second->getPatternNumber();
618 if (!(avoidLast && patternNum ==
mMonomials.size()-1))
619 output.push_back(std::make_pair(patternNum,
620 mMonomials[patternNum].size()-tmpNode->label().size()));
624 f = std::get<0>(match);
625 pre = std::get<1>(match);
631 if (pre.size() != 0 && !f->isFullPattern())
633 patternNum = f->getPatternNumber();
634 if (!(avoidLast && patternNum ==
mMonomials.size()-1))
635 output.push_back(std::make_pair(patternNum,
mMonomials[patternNum].size()-pre.size()));
682 std::vector<std::pair<int,int>> subs {};
686 auto index = std::get<0>(sw);
687 auto pos = std::get<1>(sw);
688 if (index != index1 && index != index2)
return true;
689 if (index != index1 && pos == 0)
return true;
690 if (index != index2 && pos ==
word.size() -
mMonomials[index2].size())
return true;
699 if (mMonomials.size() == 0)
return;
700 auto tmpLabel = *(mMonomials.end()-1);
701 auto pairs = std::vector<std::pair<int,int>> {};
702 leftOverlaps(
Word(tmpLabel),pairs,
true);
705 output.push_back(std::make_tuple(
p.first,
p.second, mMonomials.size()-1,
true));
Word prefix(const Word vec, int lengthOfPrefix)
bool isPatternPrefix(const Word a, const Word b, int startIndex)
std::ostream & operator<<(std::ostream &o, const SuffixTreeNode &suffixTreeNode)
void outputLabel(std::ostream &o, const Label &vec)
bool concatenatesTo(const Word a, const Word b, const Word c)
void outputPatterns(std::ostream &o, const SuffixTree &suffixTree)
Word suffix(const Word vec, int indexOfSuffix)
std::tuple< SuffixTreeNode *, SuffixTreeNode *, SuffixTreeNode * > InsertType
std::tuple< SuffixTreeNode *, SuffixTreeNode *, Word > ContractedLocusType
void outputLabel(std::ostream &o, const Label &vec)
std::tuple< SuffixTreeNode *, Word, SuffixTreeNode *, bool > SubwordsType
std::tuple< SuffixTreeNode *, Word > ExtendedLocusType
SuffixTree / SuffixTreeNode — experimental generalised suffix tree for non-commutative leading-word l...
Word and WordWithData — non-owning views over the flat-int encoding of a non-commutative word.
auto subwordsStepC(SuffixTreeNode *x, const Word &beta, const Word &s) const -> SubwordsType
auto contractedLocus(SuffixTreeNode *y, const Word &s, bool incrementLeafCount=false) const -> ContractedLocusType
auto insertStepD(SuffixTreeNode *y, const Word &s, bool isFullPattern) -> InsertType
auto extendedLocus(SuffixTreeNode *x, const Word &beta) const -> ExtendedLocusType
auto patternLeaves(SuffixTreeNode *v, std::vector< int > &output) const -> void
auto sharedPrefix(const Word s, const Word t) const -> Word
void leftOverlaps(std::vector< Overlap > &output) const
size_t insert(const Word &w)
auto isNontrivialSuperword(const Word &word, int index1, int index2) const -> bool
void destroyChildren(SuffixTreeNode *node) const
bool superwords(const Word &word, std::vector< std::pair< int, int > > &output) const
auto findMatch(SuffixTreeNode *y, const Word &s) const -> ExtendedLocusType
bool subwords(const Word &word, std::vector< std::pair< int, int > > &output) const
auto insertStepC(SuffixTreeNode *v, SuffixTreeNode *x, const Word &beta, const Word &s, bool isFullPattern) -> InsertType
bool subword(const Word &word, std::pair< int, int > &output) const
const Word operator[](int index) const
Here begin the functions to make the new class swappable with the old WordTable.
auto patternLeavesWorker(SuffixTreeNode *v) const -> std::vector< SuffixTreeNode * >
std::vector< Label > mMonomials
auto allLeaves(SuffixTreeNode *v, std::vector< SuffixTreeNode * > &output) const -> void
auto splitArc(SuffixTreeNode *f, const Word &prefix) -> SuffixTreeNode *
auto superword(const Word &w, std::pair< int, int > &output) const -> bool
auto subwordsStepD(SuffixTreeNode *y, const Word &s) const -> SubwordsType
std::ostream & dump(std::ostream &, int depth, bool dumpChildren=true) const
SuffixTreeNode * mSuffixLink
std::map< Label, SuffixTreeNode * > mChildren
One node of a generalised suffix tree built over the inserted non-commutative monomial patterns.
const int * begin() const
Non-owning view of a non-commutative word: [begin, end) of int variable indices.
TermIterator< Nterm > begin(Nterm *ptr)
TermIterator< Nterm > end(Nterm *)