Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
LinearString.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
27#include <ext/algorithm>
28
29#include <alib/set>
30#include <alib/vector>
31
32#include <core/components.hpp>
33
35
37
38#include <core/normalize.hpp>
40
41namespace string {
42
43class GeneralAlphabet;
44
56template < class SymbolType = DefaultSymbolType >
57class LinearString final : public core::Components < LinearString < SymbolType >, ext::set < SymbolType >, component::Set, GeneralAlphabet > {
62
63public:
67 explicit LinearString ( );
68
76
83
89 explicit LinearString ( const std::string & str );
90
96 explicit LinearString ( const char * str );
97
104 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
105 }
106
113 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
114 }
115
121 void extendAlphabet ( const ext::set < SymbolType > & symbols ) {
122 this->template accessComponent < GeneralAlphabet > ( ).add( symbols );
123 }
124
130 void appendSymbol ( SymbolType symbol );
131
137 const ext::vector < SymbolType > & getContent ( ) const &;
138
144 ext::vector < SymbolType > && getContent ( ) &&;
145
153 void setContent ( ext::vector < SymbolType > str );
154
160 bool empty ( ) const;
161
167 size_t size ( ) const;
168
176 auto operator <=> ( const LinearString & other ) const {
177 return std::tie ( m_Data, getAlphabet() ) <=> std::tie ( other.m_Data, other.getAlphabet() );
178 }
179
187 bool operator == ( const LinearString & other ) const {
188 return std::tie ( m_Data, getAlphabet() ) == std::tie ( other.m_Data, other.getAlphabet() );
189 }
190
199 friend ext::ostream & operator << ( ext::ostream & out, const LinearString & instance ) {
200 out << "(LinearString";
201 out << " content = " << instance.getContent ( );
202 out << " alphabet = " << instance.getAlphabet ( );
203 out << ")";
204 return out;
205 }
206};
207
208template < class SymbolType >
210 setContent(std::move(str));
211}
212
213template < class SymbolType >
215}
216
217template < class SymbolType >
219}
220
221template < class SymbolType >
222LinearString < SymbolType >::LinearString(const std::string & str) : LinearString ( ext::vector < SymbolType > ( str.begin ( ), str.end ( ) ) ) {
223}
224
225template < class SymbolType >
226LinearString < SymbolType >::LinearString(const char* str) : LinearString ( std::string ( str ) ) {
227}
228
229template < class SymbolType >
231 if ( getAlphabet().count ( symbol ) == 0 )
232 throw exception::CommonException ( "Input symbol \"" + ext::to_string ( symbol ) + "\" not in the alphabet." );
233
234 m_Data.push_back ( std::move ( symbol ) );
235}
236
237template < class SymbolType >
239 return this->m_Data;
240}
241
242template < class SymbolType >
244 return std::move ( this->m_Data );
245}
246
247template < class SymbolType >
249 ext::set < SymbolType > minimalAlphabet ( str.begin ( ), str.end ( ) );
250 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const SymbolType & ) {
251 throw exception::CommonException("Input symbols not in the alphabet.");
252 } ) );
253
254 this->m_Data = std::move ( str );
255}
256
257template < class SymbolType >
259 return this->m_Data.empty ( );
260}
261
262template < class SymbolType >
264 return this->m_Data.size ( );
265}
266
267} /* namespace string */
268
269namespace core {
270
276template < class SymbolType >
277class SetConstraint< string::LinearString < SymbolType >, SymbolType, string::GeneralAlphabet > {
278public:
287 static bool used ( const string::LinearString < SymbolType > & str, const SymbolType & symbol ) {
288 const ext::vector<SymbolType>& content = str.getContent ( );
289 return std::find(content.begin(), content.end(), symbol) != content.end();
290 }
291
301 return true;
302 }
303
310 static void valid ( const string::LinearString < SymbolType > &, const SymbolType & ) {
311 }
312};
313
319template < class SymbolType >
320struct normalize < string::LinearString < SymbolType > > {
323 ext::vector < DefaultSymbolType > content = alphabet::SymbolNormalize::normalizeSymbols ( std::move ( value ).getContent ( ) );
324 return string::LinearString < > ( std::move ( alphabet ), std::move ( content ) );
325 }
326};
327
328template < class SymbolType >
329struct normalize < string::LinearString < common::ranked_symbol < SymbolType > > > {
333
334 return string::LinearString < common::ranked_symbol < DefaultSymbolType > > ( std::move ( alphabet ), std::move ( content ) );
335 }
336
339 ext::vector < DefaultSymbolType > content = alphabet::SymbolNormalize::normalizeSymbols ( std::move ( value ).getContent ( ) );
340 return string::LinearString < > ( std::move ( alphabet ), std::move ( content ) );
341 }
342};
343
344} /* namespace core */
345
346extern template class string::LinearString < >;
347
static ext::vector< common::ranked_symbol< DefaultSymbolType > > normalizeRankedSymbols(ext::vector< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:113
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static ext::set< common::ranked_symbol< DefaultSymbolType > > normalizeRankedAlphabet(ext::set< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:59
static ext::vector< DefaultSymbolType > normalizeSymbols(ext::vector< SymbolType > &&symbols)
Definition: SymbolNormalize.h:86
Definition: ranked_symbol.hpp:20
Definition: components.hpp:181
static void valid(const string::LinearString< SymbolType > &, const SymbolType &)
Definition: LinearString.h:310
static bool available(const string::LinearString< SymbolType > &, const SymbolType &)
Definition: LinearString.h:300
static bool used(const string::LinearString< SymbolType > &str, const SymbolType &symbol)
Definition: LinearString.h:287
Definition: setComponents.hpp:26
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Output iterator calling a callback function on assignment.
Definition: iterator.hpp:923
Definition: ostream.h:14
Definition: set.hpp:44
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
Linear string.
Definition: LinearString.h:57
const ext::set< SymbolType > & getAlphabet() const &
Definition: LinearString.h:103
bool operator==(const LinearString &other) const
Definition: LinearString.h:187
friend ext::ostream & operator<<(ext::ostream &out, const LinearString &instance)
Definition: LinearString.h:199
ext::set< SymbolType > && getAlphabet() &&
Definition: LinearString.h:112
bool empty() const
Definition: LinearString.h:258
const ext::vector< SymbolType > & getContent() const &
Definition: LinearString.h:238
void setContent(ext::vector< SymbolType > str)
Definition: LinearString.h:248
void appendSymbol(SymbolType symbol)
Definition: LinearString.h:230
LinearString()
Creates a new instance of the string with an empty content.
Definition: LinearString.h:214
size_t size() const
Definition: LinearString.h:263
void extendAlphabet(const ext::set< SymbolType > &symbols)
Definition: LinearString.h:121
Definition: BarSymbol.cpp:12
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: normalize.hpp:10
Definition: sigHandler.cpp:20
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
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
Definition: RandomStringFactory.cpp:12
static string::LinearString< > eval(string::LinearString< SymbolType > &&value)
Definition: LinearString.h:321
static string::LinearString< > eval(string::LinearString< common::ranked_symbol< SymbolType > > &&value)
Definition: LinearString.h:337
static string::LinearString< common::ranked_symbol< DefaultSymbolType > > evalRanked(string::LinearString< common::ranked_symbol< SymbolType > > &&value)
Definition: LinearString.h:330
Definition: normalize.hpp:13