Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
WildcardLinearString.h
Go to the documentation of this file.
1
6#pragma once
7
8
9#include <ext/algorithm>
10
11#include <alib/set>
12#include <alib/vector>
13
14#include <core/components.hpp>
15
18
20
21#include <core/normalize.hpp>
23
24#include "LinearString.h"
25
26namespace string {
27
28class GeneralAlphabet;
29class WildcardSymbol;
30
43template < class SymbolType = DefaultSymbolType >
44class WildcardLinearString final : public core::Components < WildcardLinearString < SymbolType >, ext::set < SymbolType >, component::Set, GeneralAlphabet, SymbolType, component::Value, WildcardSymbol > {
49
50public:
51
60
68
75 explicit WildcardLinearString ( const std::string & str, char wildcard );
76
82 explicit WildcardLinearString ( const std::string & str );
83
90 explicit WildcardLinearString ( const char * str , char wildcard );
91
98 explicit WildcardLinearString ( const LinearString < SymbolType > & string, SymbolType wildcard );
99
105 explicit WildcardLinearString ( const LinearString < SymbolType > & string );
106
113 return this->template accessComponent < GeneralAlphabet > ( ).get ( );
114 }
115
122 return std::move ( this->template accessComponent < GeneralAlphabet > ( ).get ( ) );
123 }
124
130 void extendAlphabet ( const ext::set < SymbolType > & symbols ) {
131 this->template accessComponent < GeneralAlphabet > ( ).add( symbols );
132 }
133
139 const SymbolType & getWildcardSymbol ( ) const & {
140 return this->template accessComponent < WildcardSymbol > ( ).get ( );
141 }
142
149 return std::move ( this->template accessComponent < WildcardSymbol > ( ).get ( ) );
150 }
151
159 bool setWildcardSymbol ( SymbolType wildcard ) {
160 return this-> template accessComponent < WildcardSymbol > ( ).set ( std::move ( wildcard ) );
161 }
162
168 void appendSymbol ( SymbolType symbol );
169
175 const ext::vector < SymbolType > & getContent ( ) const &;
176
182 ext::vector < SymbolType > && getContent ( ) &&;
183
191 void setContent ( ext::vector < SymbolType > str );
192
198 bool isEmpty ( ) const;
199
207 auto operator <=> ( const WildcardLinearString & other ) const {
208 return std::tie ( m_Data, getAlphabet ( ), getWildcardSymbol ( ) ) <=> std::tie ( other.m_Data, other.getAlphabet ( ), other.getWildcardSymbol ( ) );
209 }
210
218 bool operator == ( const WildcardLinearString & other ) const {
219 return std::tie ( m_Data, getAlphabet ( ), getWildcardSymbol ( ) ) == std::tie ( other.m_Data, other.getAlphabet ( ), other.getWildcardSymbol ( ) );
220 }
221
230 friend ext::ostream & operator << ( ext::ostream & out, const WildcardLinearString & instance ) {
231 out << "(WildcardLinearString";
232 out << " content = " << instance.getContent ( );
233 out << " wildcard = " << instance.getWildcardSymbol ( );
234 out << " alphabet = " << instance.getAlphabet ( );
235 out << ")";
236 return out;
237 }
238
239};
240
241template < class SymbolType >
242WildcardLinearString < SymbolType >::WildcardLinearString(ext::set<SymbolType> alphabet, ext::vector<SymbolType> str, SymbolType wildcard) : core::Components < WildcardLinearString, ext::set < SymbolType >, component::Set, GeneralAlphabet, SymbolType, component::Value, WildcardSymbol > ( std::move ( alphabet ), std::move(wildcard) ) {
243 setContent(std::move(str));
244}
245
246template < class SymbolType >
248}
249
250template < class SymbolType >
251WildcardLinearString < SymbolType >::WildcardLinearString(const std::string & str, char wildcard) : WildcardLinearString ( ext::vector < SymbolType > ( str.begin ( ), str.end ( ) ), SymbolType ( wildcard ) ) {
252}
253
254template < class SymbolType >
255WildcardLinearString < SymbolType >::WildcardLinearString(const std::string & str) : WildcardLinearString ( str, alphabet::WildcardSymbol::instance < char > ( ) ) {
256}
257
258template < class SymbolType >
259WildcardLinearString < SymbolType >::WildcardLinearString(const char* str, char wildcard) : WildcardLinearString ( std::string ( str ), wildcard ) {
260}
261
262template < class SymbolType >
263WildcardLinearString < SymbolType >::WildcardLinearString(const LinearString < SymbolType > & string, SymbolType wildcard) : WildcardLinearString ( string.getAlphabet( ) + ext::set < SymbolType > { wildcard }, string.getContent ( ), wildcard ) {
264}
265
266template < class SymbolType >
267WildcardLinearString < SymbolType >::WildcardLinearString(const LinearString < SymbolType > & string) : WildcardLinearString ( string, alphabet::WildcardSymbol::instance < SymbolType > ( ) ) {
268}
269
270template < class SymbolType >
272 if ( getAlphabet().count ( symbol ) == 0 )
273 throw exception::CommonException ( "Input symbol \"" + ext::to_string ( symbol ) + "\" not in the alphabet." );
274
275 m_Data.push_back ( std::move ( symbol ) );
276}
277
278template < class SymbolType >
280 return this->m_Data;
281}
282
283template < class SymbolType >
285 return std::move ( this->m_Data );
286}
287
288template < class SymbolType >
290 ext::set < SymbolType > minimalAlphabet ( str.begin ( ), str.end ( ) );
291 std::set_difference ( minimalAlphabet.begin ( ), minimalAlphabet.end ( ), getAlphabet ( ).begin ( ), getAlphabet ( ).end ( ), ext::callback_iterator ( [ ] ( const SymbolType & ) {
292 throw exception::CommonException("Input symbols not in the alphabet.");
293 } ) );
294
295 this->m_Data = std::move ( str );
296}
297
298template < class SymbolType >
300 return this->m_Data.empty ( );
301}
302
303} /* namespace string */
304
305namespace core {
306
312template < class SymbolType >
313class SetConstraint< string::WildcardLinearString < SymbolType >, SymbolType, string::GeneralAlphabet > {
314public:
323 static bool used ( const string::WildcardLinearString < SymbolType > & str, const SymbolType & symbol ) {
324 const ext::vector<SymbolType>& content = str.getContent ( );
325 return std::find(content.begin(), content.end(), symbol) != content.end();
326 }
327
337 return true;
338 }
339
347 }
348};
349
355template<class SymbolType >
356class ElementConstraint< string::WildcardLinearString < SymbolType >, SymbolType, string::WildcardSymbol > {
357public:
366 static bool available ( const string::WildcardLinearString < SymbolType > & string, const SymbolType & symbol ) {
367 return string.getAlphabet ( ).count ( symbol );
368 }
369
377 }
378
379};
380
386template < class SymbolType >
387struct normalize < string::WildcardLinearString < SymbolType > > {
390 ext::vector < DefaultSymbolType > content = alphabet::SymbolNormalize::normalizeSymbols ( std::move ( value ).getContent ( ) );
391 DefaultSymbolType wildcard = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( value ).getWildcardSymbol ( ) );
392 return string::WildcardLinearString < > ( std::move ( alphabet ), std::move ( content ), std::move ( wildcard ) );
393 }
394};
395
396} /* namespace core */
397
398extern template class string::WildcardLinearString < >;
399
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
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Definition: components.hpp:181
static bool available(const string::WildcardLinearString< SymbolType > &string, const SymbolType &symbol)
Definition: WildcardLinearString.h:366
static void valid(const string::WildcardLinearString< SymbolType > &, const SymbolType &)
Definition: WildcardLinearString.h:376
Definition: components.hpp:25
static void valid(const string::WildcardLinearString< SymbolType > &, const SymbolType &)
Definition: WildcardLinearString.h:346
static bool used(const string::WildcardLinearString< SymbolType > &str, const SymbolType &symbol)
Definition: WildcardLinearString.h:323
static bool available(const string::WildcardLinearString< SymbolType > &, const SymbolType &)
Definition: WildcardLinearString.h:336
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
Definition: Object.h:16
Linear string.
Definition: LinearString.h:57
Linear wildcard string.
Definition: WildcardLinearString.h:44
bool isEmpty() const
Definition: WildcardLinearString.h:299
const ext::vector< SymbolType > & getContent() const &
Definition: WildcardLinearString.h:279
bool operator==(const WildcardLinearString &other) const
Definition: WildcardLinearString.h:218
void extendAlphabet(const ext::set< SymbolType > &symbols)
Definition: WildcardLinearString.h:130
void setContent(ext::vector< SymbolType > str)
Definition: WildcardLinearString.h:289
friend ext::ostream & operator<<(ext::ostream &out, const WildcardLinearString &instance)
Definition: WildcardLinearString.h:230
const SymbolType & getWildcardSymbol() const &
Definition: WildcardLinearString.h:139
void appendSymbol(SymbolType symbol)
Definition: WildcardLinearString.h:271
bool setWildcardSymbol(SymbolType wildcard)
Definition: WildcardLinearString.h:159
ext::set< SymbolType > && getAlphabet() &&
Definition: WildcardLinearString.h:121
const ext::set< SymbolType > & getAlphabet() const &
Definition: WildcardLinearString.h:112
WildcardLinearString(ext::set< SymbolType > alphabet, ext::vector< SymbolType > str, SymbolType wildcard)
Creates a new instance of the string with a concrete alphabet, content and wildcard.
Definition: WildcardLinearString.h:242
SymbolType && getWildcardSymbol() &&
Definition: WildcardLinearString.h:148
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::WildcardLinearString< > eval(string::WildcardLinearString< SymbolType > &&value)
Definition: WildcardLinearString.h:388
Definition: normalize.hpp:13