Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
PositionHeap.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/algorithm>
27#include <ext/iostream>
28
29#include <alib/string>
30#include <alib/set>
31#include <alib/trie>
32
34
35#include <core/components.hpp>
37
38#include <string/LinearString.h>
39
41
42namespace indexes {
43
44namespace stringology {
45
46class GeneralAlphabet;
47
55template < class SymbolType = DefaultSymbolType >
56class PositionHeap final {
61
66
72 void checkTrie ( const ext::trie < SymbolType, unsigned > & trie );
73
74public:
82
89
96
103
110
117 return m_string.getAlphabet ( );
118 }
119
126 return std::move ( m_string ).getAlphabet ( );
127 }
128
135
141 bool removeSymbolFromEdgeAlphabet ( const SymbolType & symbol ) {
142 return m_string.removeSymbol ( symbol );
143 }
144
152 auto operator <=> ( const PositionHeap & other ) const {
153 return std::tie ( getRoot ( ), getAlphabet ( ) ) <=> std::tie ( other.getRoot ( ), other.getAlphabet ( ) ); // indexed string does not need to be compared.
154 }
155
163 bool operator == ( const PositionHeap & other ) const {
164 return std::tie ( getRoot ( ), getAlphabet ( ) ) == std::tie ( other.getRoot ( ), other.getAlphabet ( ) ); // indexed string does not need to be compared.
165 }
166
175 friend ext::ostream & operator << ( ext::ostream & out, const PositionHeap & instance ) {
176 return out << "(PositionHeap " << instance.m_trie << ")";
177 }
178};
179
180} /* namespace stringology */
181
182} /* namespace indexes */
183
184namespace indexes {
185
186namespace stringology {
187
188template < class SymbolType >
189PositionHeap < SymbolType >::PositionHeap ( ext::trie < SymbolType, unsigned > trie, string::LinearString < SymbolType > string ) : m_trie ( std::move ( trie ) ), m_string ( std::move ( string ) ) {
190 checkTrie ( this->m_trie );
191}
192
193template < class SymbolType >
195 for ( const std::pair < const SymbolType, ext::trie < SymbolType, unsigned > > & child : trie.getChildren ( ) ) {
196 if ( ! getAlphabet ( ).count ( child.first ) )
197 throw exception::CommonException ( "Symbol " + ext::to_string ( child.first ) + "not in the alphabet." );
198 checkTrie ( child.second );
199 }
200}
201
202template < class SymbolType >
204 return m_trie;
205}
206
207template < class SymbolType >
209 return std::move ( m_trie );
210}
211
212template < class SymbolType >
214 return m_string;
215}
216
217template < class SymbolType >
219 return std::move ( m_string );
220}
221
222template < class SymbolType >
224 checkTrie ( trie );
225 this->m_trie = std::move ( trie ).clone ( );
226}
227
228} /* namespace stringology */
229
230} /* namespace indexes */
231
232namespace core {
233
234template < class SymbolType >
235struct normalize < indexes::stringology::PositionHeap < common::ranked_symbol < SymbolType > > > {
239
240 return indexes::stringology::PositionHeap < common::ranked_symbol < DefaultSymbolType > > ( std::move ( trie ), std::move ( string ) );
241 }
242
245 string::LinearString < DefaultSymbolType > string = normalize < string::LinearString < SymbolType > >::eval ( std::move ( value ).getString ( ) );
246
247 return indexes::stringology::PositionHeap < > ( std::move ( trie ), std::move ( string ) );
248 }
249};
250
256template < class SymbolType >
257struct normalize < indexes::stringology::PositionHeap < SymbolType > > {
260 string::LinearString < DefaultSymbolType > string = normalize < string::LinearString < SymbolType > >::eval ( std::move ( value ).getString ( ) );
261
262 return indexes::stringology::PositionHeap < > ( std::move ( trie ), std::move ( string ) );
263 }
264};
265
266} /* namespace core */
267
Definition: ranked_symbol.hpp:20
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
static ext::trie< common::ranked_symbol< DefaultSymbolType >, unsigned > normalizeRankedTrie(ext::trie< common::ranked_symbol< SymbolType >, unsigned > &&trie)
Definition: IndexesNormalize.h:70
Position heap string index. Tree like representation of all suffixes. The suffixes are themselves rep...
Definition: PositionHeap.h:56
PositionHeap(ext::trie< SymbolType, unsigned > trie, string::LinearString< SymbolType > string)
Definition: PositionHeap.h:189
const ext::trie< SymbolType, unsigned > & getRoot() const &
Definition: PositionHeap.h:203
ext::set< SymbolType > && getAlphabet() &&
Definition: PositionHeap.h:125
const string::LinearString< SymbolType > & getString() const &
Definition: PositionHeap.h:213
auto operator<=>(const PositionHeap &other) const
Definition: PositionHeap.h:152
bool operator==(const PositionHeap &other) const
Definition: PositionHeap.h:163
friend ext::ostream & operator<<(ext::ostream &out, const PositionHeap &instance)
Definition: PositionHeap.h:175
void setTree(ext::trie< SymbolType, unsigned > trie)
Definition: PositionHeap.h:223
const ext::set< SymbolType > & getAlphabet() const &
Definition: PositionHeap.h:116
bool removeSymbolFromEdgeAlphabet(const SymbolType &symbol)
Definition: PositionHeap.h:141
Linear string.
Definition: LinearString.h:57
const ext::set< SymbolType > & getAlphabet() const &
Definition: LinearString.h:103
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
Definition: RandomStringFactory.cpp:12
Definition: ArithmeticCompression.h:18
static indexes::stringology::PositionHeap< > eval(indexes::stringology::PositionHeap< SymbolType > &&value)
Definition: PositionHeap.h:258
static indexes::stringology::PositionHeap< > eval(indexes::stringology::PositionHeap< common::ranked_symbol< SymbolType > > &&value)
Definition: PositionHeap.h:243
static indexes::stringology::PositionHeap< common::ranked_symbol< DefaultSymbolType > > evalRanked(indexes::stringology::PositionHeap< common::ranked_symbol< SymbolType > > &&value)
Definition: PositionHeap.h:236
Definition: normalize.hpp:13