Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
SuffixTrie.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <ext/iostream>
27#include <ext/algorithm>
28
29#include <alib/string>
30#include <alib/set>
31#include <alib/trie>
32#include <alib/optional>
33
35
36#include <core/components.hpp>
38
39#include <core/normalize.hpp>
42
43namespace indexes {
44
45namespace stringology {
46
47class GeneralAlphabet;
48
54template < class SymbolType = DefaultSymbolType >
55class SuffixTrie final : public core::Components < SuffixTrie < SymbolType >, ext::set < SymbolType >, component::Set, GeneralAlphabet > {
60
66 void checkTrie ( const ext::trie < SymbolType, std::optional < unsigned > > & trie );
67
68public:
74 explicit SuffixTrie ( ext::set < SymbolType > edgeAlphabet );
75
82 explicit SuffixTrie ( ext::set < SymbolType > edgeAlphabet, ext::trie < SymbolType, std::optional < unsigned > > trie );
83
89 explicit SuffixTrie ( ext::trie < SymbolType, std::optional < unsigned > > trie );
90
97
104
111 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
112 }
113
120 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
121 }
122
128 void setTree ( ext::trie < SymbolType, std::optional < unsigned > > trie );
129
135 bool removeSymbolFromEdgeAlphabet ( const SymbolType & symbol ) {
136 return this->template accessComponent < GeneralAlphabet > ( ).remove ( symbol );
137 }
138
146 auto operator <=> ( const SuffixTrie & other ) const {
147 return std::tie ( getRoot ( ), getAlphabet ( ) ) <=> std::tie ( other.getRoot ( ), other.getAlphabet ( ) );
148 }
149
157 bool operator == ( const SuffixTrie & other ) const {
158 return std::tie ( getRoot ( ), getAlphabet ( ) ) == std::tie ( other.getRoot ( ), other.getAlphabet ( ) );
159 }
160
169 friend ext::ostream & operator << ( ext::ostream & out, const SuffixTrie & instance ) {
170 return out << "(SuffixTrie " << instance.m_trie << ")";
171 }
172};
173
174} /* namespace stringology */
175
176} /* namespace indexes */
177
178namespace indexes {
179
180namespace stringology {
181
182template < class SymbolType >
183SuffixTrie < SymbolType >::SuffixTrie ( ext::set < SymbolType > edgeAlphabet ) : SuffixTrie ( std::move ( edgeAlphabet ), ext::trie < SymbolType, std::optional < unsigned > > ( std::optional < unsigned > ( ) ) ) {
184}
185
186template < class SymbolType >
187SuffixTrie < SymbolType >::SuffixTrie ( ext::set < SymbolType > edgeAlphabet, ext::trie < SymbolType, std::optional < unsigned > > trie ) : core::Components < SuffixTrie, ext::set < SymbolType >, component::Set, GeneralAlphabet > ( std::move ( edgeAlphabet ) ), m_trie ( std::move ( trie ) ) {
188 checkTrie ( this->m_trie );
189}
190
191template < class SymbolType >
193}
194
195template < class SymbolType >
197 for ( const std::pair < const SymbolType, ext::trie < SymbolType, std::optional < unsigned > > > & child : trie.getChildren ( ) ) {
198 if ( ! getAlphabet ( ).count ( child.first ) )
199 throw exception::CommonException ( "Symbol " + ext::to_string ( child.first ) + "not in the alphabet." );
200 checkTrie ( child.second );
201 }
202}
203
204template < class SymbolType >
206 return m_trie;
207}
208
209template < class SymbolType >
211 return std::move ( m_trie );
212}
213
214template < class SymbolType >
216 checkTrie ( trie );
217 this->m_trie = std::move ( trie ).clone ( );
218}
219
220} /* namespace stringology */
221
222} /* namespace indexes */
223
224namespace core {
225
231template < class SymbolType >
232class SetConstraint < indexes::stringology::SuffixTrie < SymbolType >, SymbolType, indexes::stringology::GeneralAlphabet > {
233
242 static bool used ( const ext::trie < SymbolType, std::optional < unsigned > > & trie, const SymbolType & symbol ) {
243 for ( const std::pair < const SymbolType, ext::trie < SymbolType, std::optional < unsigned > > > & child : trie.getChildren ( ) ) {
244 if ( symbol == child.first || checkTrie ( trie, child.second ) )
245 return true;
246 }
247 return false;
248 }
249
250public:
259 static bool used ( const indexes::stringology::SuffixTrie < SymbolType > & index, const SymbolType & symbol ) {
260 return used ( index.getRoot ( ), symbol );
261 }
262
272 return true;
273 }
274
282 }
283};
284
290template < class SymbolType >
291struct normalize < indexes::stringology::SuffixTrie < SymbolType > > {
295
296 return indexes::stringology::SuffixTrie < > ( std::move ( alphabet ), std::move ( trie ) );
297 }
298};
299
300} /* namespace core */
301
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
Definition: components.hpp:181
static bool used(const indexes::stringology::SuffixTrie< SymbolType > &index, const SymbolType &symbol)
Definition: SuffixTrie.h:259
static void valid(const indexes::stringology::SuffixTrie< SymbolType > &, const SymbolType &)
Definition: SuffixTrie.h:281
static bool available(const indexes::stringology::SuffixTrie< SymbolType > &, const SymbolType &)
Definition: SuffixTrie.h:271
Definition: setComponents.hpp:26
static bool used(const Derived &object, const ComponentType &element)
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Definition: ostream.h:14
Definition: set.hpp:44
Class introducing a trie with interface trying to be close to the interface of standard library conta...
Definition: trie.hpp:47
static ext::trie< DefaultSymbolType, unsigned > normalizeTrie(ext::trie< SymbolType, unsigned > &&trie)
Definition: IndexesNormalize.h:60
Suffix trie string index. Tree like representation of all suffixes. Nodes of the trie are optionally ...
Definition: SuffixTrie.h:55
const ext::set< SymbolType > & getAlphabet() const &
Definition: SuffixTrie.h:110
void setTree(ext::trie< SymbolType, std::optional< unsigned > > trie)
Definition: SuffixTrie.h:215
const ext::trie< SymbolType, std::optional< unsigned > > & getRoot() const &
Definition: SuffixTrie.h:205
auto operator<=>(const SuffixTrie &other) const
Definition: SuffixTrie.h:146
bool removeSymbolFromEdgeAlphabet(const SymbolType &symbol)
Definition: SuffixTrie.h:135
SuffixTrie(ext::set< SymbolType > edgeAlphabet)
Definition: SuffixTrie.h:183
ext::set< SymbolType > && getAlphabet() &&
Definition: SuffixTrie.h:119
friend ext::ostream & operator<<(ext::ostream &out, const SuffixTrie &instance)
Definition: SuffixTrie.h:169
bool operator==(const SuffixTrie &other) const
Definition: SuffixTrie.h:157
Definition: BarSymbol.cpp:12
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: normalize.hpp:10
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
Definition: CompressedBitParallelTreeIndex.h:40
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
Definition: ArithmeticCompression.h:18
static indexes::stringology::SuffixTrie< > eval(indexes::stringology::SuffixTrie< SymbolType > &&value)
Definition: SuffixTrie.h:292
Definition: normalize.hpp:13