Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
CyclicString.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
59template < class SymbolType = DefaultSymbolType >
60class CyclicString final : public core::Components < CyclicString < SymbolType >, ext::set < SymbolType >, component::Set, GeneralAlphabet > {
65
66public:
70 explicit CyclicString ( );
71
79
86
92 explicit CyclicString ( const std::string & str );
93
99 explicit CyclicString ( const char * str );
100
107 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
108 }
109
116 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
117 }
118
125 this->template accessComponent < GeneralAlphabet > ( ).add ( std::move ( symbols ) );
126 }
127
133 const ext::vector < SymbolType > & getContent ( ) const &;
134
140 ext::vector < SymbolType > && getContent ( ) &&;
141
149 void setContent ( ext::vector < SymbolType > str );
150
156 bool isEmpty ( ) const;
157
165 auto operator <=> ( const CyclicString & other ) const {
166 return std::tie ( m_Data, getAlphabet ( ) ) <=> std::tie ( other.m_Data, other.getAlphabet ( ) );
167 }
168
176 bool operator == ( const CyclicString & other ) const {
177 return std::tie ( m_Data, getAlphabet ( ) ) == std::tie ( other.m_Data, other.getAlphabet ( ) );
178 }
179
188 friend ext::ostream & operator << ( ext::ostream & out, const CyclicString & instance ) {
189 out << "(CyclicString";
190 out << " content = " << instance.getContent ( );
191 out << " alphabet = " << instance.getAlphabet ( );
192 out << ")";
193 return out;
194 }
195};
196
197template < class SymbolType >
199 setContent(std::move(str));
200}
201
202template < class SymbolType >
204}
205
206template < class SymbolType >
208}
209
210template < class SymbolType >
211CyclicString < SymbolType >::CyclicString(const std::string& str) : CyclicString ( ext::vector < SymbolType > ( str.begin ( ), str.end ( ) ) ) {
212}
213
214template < class SymbolType >
215CyclicString < SymbolType >::CyclicString ( const char * str ) : CyclicString ( std::string ( str ) ) {
216}
217
218template < class SymbolType >
220 return this->m_Data;
221}
222
223template < class SymbolType >
225 return std::move ( this->m_Data );
226}
227
228//serves as both move and copy content setter
229template < class SymbolType >
231 ext::set < SymbolType > minimalAlphabet ( str.begin ( ), str.end ( ) );
232 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const SymbolType & ) {
233 throw exception::CommonException("Input symbols not in the alphabet.");
234 } ) );
235
236 m_Data = std::move(str);
237}
238
239template < class SymbolType >
241 return this->m_Data.empty ( );
242}
243
244} /* namespace string */
245
246namespace core {
247
253template < class SymbolType >
254class SetConstraint< string::CyclicString < SymbolType >, SymbolType, string::GeneralAlphabet > {
255public:
264 static bool used ( const string::CyclicString < SymbolType > & str, const SymbolType & symbol ) {
265 const ext::vector<SymbolType>& content = str.getContent ( );
266 return std::find(content.begin(), content.end(), symbol) != content.end();
267 }
268
278 return true;
279 }
280
287 static void valid ( const string::CyclicString < SymbolType > &, const SymbolType & ) {
288 }
289};
290
296template < class SymbolType >
297struct normalize < string::CyclicString < SymbolType > > {
300 ext::vector < DefaultSymbolType > content = alphabet::SymbolNormalize::normalizeSymbols ( std::move ( value ).getContent ( ) );
301 return string::CyclicString < > ( std::move ( alphabet ), std::move ( content ) );
302 }
303};
304
305} /* namespace core */
306
307extern template class string::CyclicString < >;
308
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static ext::vector< DefaultSymbolType > normalizeSymbols(ext::vector< SymbolType > &&symbols)
Definition: SymbolNormalize.h:86
Definition: components.hpp:181
static bool available(const string::CyclicString< SymbolType > &, const SymbolType &)
Definition: CyclicString.h:277
static bool used(const string::CyclicString< SymbolType > &str, const SymbolType &symbol)
Definition: CyclicString.h:264
static void valid(const string::CyclicString< SymbolType > &, const SymbolType &)
Definition: CyclicString.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
Cyclic string.
Definition: CyclicString.h:60
const ext::vector< SymbolType > & getContent() const &
Definition: CyclicString.h:219
ext::set< SymbolType > && getAlphabet() &&
Definition: CyclicString.h:115
void extendAlphabet(ext::set< SymbolType > symbols)
Definition: CyclicString.h:124
bool isEmpty() const
Definition: CyclicString.h:240
bool operator==(const CyclicString &other) const
Definition: CyclicString.h:176
CyclicString()
Creates a new instance of the string with an empty content.
Definition: CyclicString.h:203
friend ext::ostream & operator<<(ext::ostream &out, const CyclicString &instance)
Definition: CyclicString.h:188
void setContent(ext::vector< SymbolType > str)
Definition: CyclicString.h:230
const ext::set< SymbolType > & getAlphabet() const &
Definition: CyclicString.h:106
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
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::CyclicString< > eval(string::CyclicString< SymbolType > &&value)
Definition: CyclicString.h:298
Definition: normalize.hpp:13