NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
stringdatastructures.cpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2022 Erik Haenel et al.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17******************************************************************************/
18
20#include "../../kernel.hpp"
21
22
32{
33 if (sStr.length() && sStr.front() == '"')
34 return sStr.subview(1, sStr.length()-2);
35
36 return sStr;
37}
38
39
49std::string StringVector::makeLocalString(const std::string& sStr)
50{
51 if (sStr.front() == '"')
52 return "\"" + toInternalString(sStr) + "\"";
53
54 return sStr;
55}
56
57
68{
69 if (size() == 1)
70 return makePureString(front());
71 else if (i < size())
72 return makePureString(at(i));
73
74 return StringView(m_sDUMMY);
75}
76
77
89{
90 if (i >= size() && size() > 1)
91 return NAN;
92
93 if (is_string(i))
94 return NAN;
95
96 if (getVectorized(i) == "true")
97 return 1.0;
98
99 if (getVectorized(i) == "false")
100 return 0.0;
101
102 // Use the numerical parser here
104 _parser.SetExpr(getVectorized(i));
105 return _parser.Eval();
106}
107
108
119{
120 if (i >= size() && size() > 1)
121 return false;
122
123 if (is_string(i))
124 return getVectorized(i).length() > 0;
125
126 if (getVectorized(i) == "true")
127 return true;
128
129 if (getVectorized(i) == "false")
130 return false;
131
132 // Use the numerical parser here
134 _parser.SetExpr(getVectorized(i));
135 return _parser.Eval() != 0.0;
136}
137
138
147{
148 std::vector<std::string>::assign(sVect.begin(), sVect.end());
149}
150
151
159void StringVector::assign(const std::vector<bool>& vect)
160{
161 resize(vect.size());
162
163 for (size_t i = 0; i < vect.size(); i++)
164 {
165 std::vector<std::string>::operator[](i) = vect[i] ? "true" : "false";
166 }
167}
168
169
173StringVector::StringVector() : std::vector<std::string>()
174{ }
175
176
186StringVector::StringVector(const std::vector<std::string>& vect) : std::vector<std::string>(vect)
187{ }
188
189
197StringVector::StringVector(const std::string& sStr) : std::vector<std::string>(1, sStr)
198{ }
199
200
208StringVector::StringVector(const char* sStr) : std::vector<std::string>(1, std::string(sStr))
209{ }
210
211
219StringVector::StringVector(bool val) : std::vector<std::string>(1, toString(val))
220{ }
221
222
229StringVector::StringVector(const StringVector& vect) : std::vector<std::string>()
230{
231 assign(vect);
232}
233
234
242StringVector::StringVector(const std::vector<bool>& vect) : std::vector<std::string>()
243{
244 assign(vect);
245}
246
247
255StringVector::StringVector(size_t n, const std::string& sStr) : std::vector<std::string>(n, sStr)
256{ }
257
258
268{
269 return convert_internal("");
270}
271
272
281StringVector StringVector::convert_internal(const std::string& sInternal)
282{
283 return StringVector("\"" + sInternal + "\"");
284}
285
286
295StringVector StringVector::convert_literal(const std::string& sLiteral)
296{
297 return StringVector(makeLocalString(sLiteral));
298}
299
300
310{
311 assign(sVect);
312 return *this;
313}
314
315
324StringVector& StringVector::operator=(const std::vector<bool>& vect)
325{
326 assign(vect);
327 return *this;
328}
329
330
339void StringVector::push_back(const std::string& sStr)
340{
341 std::vector<std::string>::push_back("\"" + sStr + "\"");
342}
343
344
353void StringVector::push_back(const char* sStr)
354{
355 std::vector<std::string>::push_back("\"" + std::string(sStr) + "\"");
356}
357
358
368{
369 std::vector<std::string>::push_back(toString(vVal, 20));
370}
371
372
381void StringVector::push_back(size_t nVal)
382{
383 std::vector<std::string>::push_back(toString(nVal));
384}
385
386
396{
397 std::vector<std::string>::push_back(toString(nVal));
398}
399
400
409void StringVector::push_back(long long int nVal)
410{
411 std::vector<std::string>::push_back(toString(nVal));
412}
413
414
424{
425 std::vector<std::string>::push_back(toString(nVal));
426}
427
428
440void StringVector::push_generic(const std::string& sStr)
441{
442 std::vector<std::string>::push_back(makeLocalString(sStr));
443}
444
445
454bool StringVector::is_string(size_t i) const
455{
456 if (i < size())
457 return at(i).front() == '"';
458 else if (size() == 1)
459 return front().front() == '"';
460
461 return false;
462}
463
464
474void StringVector::convert_to_string(size_t i, size_t minChars)
475{
476 if (i < size() && !is_string(i))
477 {
478 size_t len = std::vector<std::string>::operator[](i).length();
479 std::vector<std::string>::operator[](i) = "\"" + (len < minChars ? std::string(minChars-len, '0') : "") + std::vector<std::string>::operator[](i) + "\"";
480 }
481}
482
483
492{
493 if (i < size())
494 return makePureString(at(i));
495
496 return StringView(m_sDUMMY);
497}
498
499
510{
511 if (i < size())
512 return StringArg(at(i));
513 else if (size() == 1)
514 return StringArg(at(0));
515
516 return StringArg();
517}
518
519
528std::string StringVector::getMasked(size_t i) const
529{
530 if (i < size())// && is_string(i))
531 {
532 const char NEWSTRING = (char)23;
533 std::string sRet = at(i);
534
535 // Go through the complete string without the first
536 // and the last character
537 for (size_t i = 1; i < sRet.length() - 1; i++)
538 {
539 // Escape backslashes
540 if (sRet[i] == '\\' && sRet[i + 1] != '"' && sRet[i + 1] != ' ')
541 {
542 sRet.insert(i + 1, " ");
543 i++;
544 }
545
546 // Escape quotation marks
547 if (sRet[i] == '"' && sRet[i - 1] != '\\' && sRet[i + 1] != NEWSTRING && sRet.find('"', i + 1) != std::string::npos)
548 {
549 sRet.insert(i, "\\");
550 i++;
551 }
552
553 // Replace the new string character with a comma
554 if (sRet[i] == NEWSTRING)
555 {
556 sRet[i] = ',';
557 if (sRet[i + 1] == '"')
558 i++;
559 }
560 }
561
562 return sRet;
563 }
564 //else if (i < size())
565 // return at(i);
566
567 return m_sDUMMY;
568}
569
570
579std::string& StringVector::getRef(size_t i)
580{
581 if (i < size())
582 return std::vector<std::string>::operator[](i);
583
584 throw std::out_of_range("Requested element " + toString(i) + ", which is greater than this->size().");
585}
586
587
595std::vector<bool> StringVector::operator==(const StringVector& sVect) const
596{
597 std::vector<bool> vRet(std::max(size(), sVect.size()));
598
599 for (size_t i = 0; i < vRet.size(); i++)
600 {
601 if (is_string(i) != sVect.is_string(i))
602 vRet[i] = false;
603 else if (is_string(i))
604 vRet[i] = getVectorized(i) == sVect.getVectorized(i);
605 else
606 vRet[i] = getNumericalVectorized(i) == sVect.getNumericalVectorized(i);
607 }
608
609 return vRet;
610}
611
612
620std::vector<bool> StringVector::operator!=(const StringVector& sVect) const
621{
622 std::vector<bool> vRet(std::max(size(), sVect.size()));
623
624 for (size_t i = 0; i < vRet.size(); i++)
625 {
626 if (is_string(i) != sVect.is_string(i))
627 vRet[i] = true;
628 else if (is_string(i))
629 vRet[i] = getVectorized(i) != sVect.getVectorized(i);
630 else
631 vRet[i] = getNumericalVectorized(i) != sVect.getNumericalVectorized(i);
632 }
633
634 return vRet;
635}
636
637
645std::vector<bool> StringVector::operator<(const StringVector& sVect) const
646{
647 std::vector<bool> vRet(std::max(size(), sVect.size()));
648
649 for (size_t i = 0; i < vRet.size(); i++)
650 {
651 if (is_string(i) != sVect.is_string(i))
652 vRet[i] = false;
653 else if (is_string(i))
654 vRet[i] = getVectorized(i) < sVect.getVectorized(i);
655 else
656 vRet[i] = getNumericalVectorized(i).real() < sVect.getNumericalVectorized(i).real();
657 }
658
659 return vRet;
660}
661
662
670std::vector<bool> StringVector::operator<=(const StringVector& sVect) const
671{
672 std::vector<bool> vRet(std::max(size(), sVect.size()));
673
674 for (size_t i = 0; i < vRet.size(); i++)
675 {
676 if (is_string(i) != sVect.is_string(i))
677 vRet[i] = false;
678 else if (is_string(i))
679 vRet[i] = getVectorized(i) <= sVect.getVectorized(i);
680 else
681 vRet[i] = getNumericalVectorized(i).real() <= sVect.getNumericalVectorized(i).real();
682 }
683
684 return vRet;
685}
686
687
695std::vector<bool> StringVector::operator>(const StringVector& sVect) const
696{
697 std::vector<bool> vRet(std::max(size(), sVect.size()));
698
699 for (size_t i = 0; i < vRet.size(); i++)
700 {
701 if (is_string(i) != sVect.is_string(i))
702 vRet[i] = false;
703 else if (is_string(i))
704 vRet[i] = getVectorized(i) > sVect.getVectorized(i);
705 else
706 vRet[i] = getNumericalVectorized(i).real() > sVect.getNumericalVectorized(i).real();
707 }
708
709 return vRet;
710}
711
712
720std::vector<bool> StringVector::operator>=(const StringVector& sVect) const
721{
722 std::vector<bool> vRet(std::max(size(), sVect.size()));
723
724 for (size_t i = 0; i < vRet.size(); i++)
725 {
726 if (is_string(i) != sVect.is_string(i))
727 vRet[i] = false;
728 else if (is_string(i))
729 vRet[i] = getVectorized(i) >= sVect.getVectorized(i);
730 else
731 vRet[i] = getNumericalVectorized(i).real() >= sVect.getNumericalVectorized(i).real();
732 }
733
734 return vRet;
735}
736
737
746std::vector<bool> StringVector::and_f(const StringVector& sVect) const
747{
748 std::vector<bool> vRet(std::max(size(), sVect.size()));
749
750 for (size_t i = 0; i < vRet.size(); i++)
751 {
752 vRet[i] = getBooleanVectorized(i) && sVect.getBooleanVectorized(i);
753 }
754
755 return vRet;
756}
757
758
767std::vector<bool> StringVector::or_f(const StringVector& sVect) const
768{
769 std::vector<bool> vRet(std::max(size(), sVect.size()));
770
771 for (size_t i = 0; i < vRet.size(); i++)
772 {
773 vRet[i] = getBooleanVectorized(i) || sVect.getBooleanVectorized(i);
774 }
775
776 return vRet;
777}
778
779
788std::vector<bool> StringVector::xor_f(const StringVector& sVect) const
789{
790 std::vector<bool> vRet(std::max(size(), sVect.size()));
791
792 for (size_t i = 0; i < vRet.size(); i++)
793 {
794 vRet[i] = getBooleanVectorized(i) xor sVect.getBooleanVectorized(i);
795 }
796
797 return vRet;
798}
799
800
809{
810 StringVector vRet(std::max(size(), sVect.size()));
811
812 for (size_t i = 0; i < vRet.size(); i++)
813 {
814 if (is_string(i) && sVect.is_string(i))
815 vRet.getRef(i) = "\"" + (getVectorized(i) + sVect.getVectorized(i)) + "\"";
816 else
817 vRet.getRef(i) = getVectorized(i) + std::string("+") + sVect.getVectorized(i).to_string();
818 }
819
820 return vRet;
821}
822
823
832{
833 assign(operator+(sVect));
834 return *this;
835}
836
837
846StringVector StringVector::operator+(const std::string& sLiteral) const
847{
848 return operator+(StringVector(sLiteral));
849}
850
851
860StringVector& StringVector::operator+=(const std::string& sLiteral)
861{
862 assign(operator+(StringVector(sLiteral)));
863 return *this;
864}
865
866
877StringVector& StringVector::evalIfElse(const StringVector& sLogicals, const StringVector& sIfBranch, const StringVector& sElseBranch)
878{
879 // Only one logical result?
880 if (sLogicals.size() == 1)
881 {
882 if (sLogicals.getBooleanVectorized(0))
883 assign(sIfBranch);
884 else
885 assign(sElseBranch);
886
887 return *this;
888 }
889
890 StringVector vRet(std::max({sLogicals.size(), sIfBranch.size(), sElseBranch.size()}));
891
892 for (size_t i = 0; i < vRet.size(); i++)
893 {
894 if (sLogicals.getBooleanVectorized(i))
895 vRet.getRef(i) = sIfBranch.getArg(i).getRef();
896 else
897 vRet.getRef(i) = sElseBranch.getArg(i).getRef();
898 }
899
900 assign(vRet);
901 return *this;
902}
903
static NumeReKernel * getInstance()
This static member function returns a a pointer to the singleton instance of the kernel.
Definition: kernel.hpp:221
mu::Parser & getParser()
Definition: kernel.hpp:281
Simply container to provide the data for a StringView instance usable by all the string functions.
std::string & getRef()
Get a reference to the contained string.
This class is an extension to the std::vector<std::string> to provide the vector-like functionalities...
StringVector & operator=(const StringVector &sVect)
Assignment operator overload for a StringVector instance.
StringView operator[](size_t i) const
Return a view to the i-th element.
void push_back(const std::string &sStr)
Append a string to the end of this vector. Will be stored as local string.
std::string & getRef(size_t i)
Return a reference to the i-th element in this string.
std::vector< bool > operator!=(const StringVector &sVect) const
Inequality operator overload.
std::string getMasked(size_t i) const
Return the contained string with the internal quotation marks as escaped variants.
std::vector< bool > operator<(const StringVector &sVect) const
Less-than operator overload.
StringVector()
Default constructor.
std::vector< bool > operator==(const StringVector &sVect) const
Equality operator overload.
void convert_to_string(size_t i, size_t minChars=0)
Convert the i-th element to a string.
const std::string m_sDUMMY
static StringVector convert_internal(const std::string &sInternal)
Static member function to create a StringVector instance from an internal string.
static StringVector empty_string()
Static member function to create a StringVector with one component with zero length.
void push_generic(const std::string &sStr)
Append a generic string value to the end of this vector. Depending on the existence of surrounding qu...
mu::value_type getNumericalVectorized(size_t i) const
Get either the i-th element or the 1st element converted as numerical value, if this vector has only ...
std::vector< bool > xor_f(const StringVector &sVect) const
This member function represents an XOR operator.
std::vector< bool > operator>=(const StringVector &sVect) const
Greater-equal operator overload.
bool is_string(size_t i) const
Check whether the i-th element represents a string or a numeric value.
static StringView makePureString(StringView sStr)
Static member function to convert the contained string into a pur C++ string.
std::vector< bool > operator<=(const StringVector &sVect) const
Lesser-equal operator overload.
std::vector< bool > or_f(const StringVector &sVect) const
This member function represents an OR operator.
StringVector & operator+=(const std::string &sLiteral)
Append a string literal to this StringVector instance.
StringVector operator+(const std::string &sLiteral) const
Concatenate a StringVector and a string literal.
std::vector< bool > and_f(const StringVector &sVect) const
This member function represents an AND operator.
std::vector< bool > operator>(const StringVector &sVect) const
Greater-than operator overload.
StringVector & evalIfElse(const StringVector &sLogicals, const StringVector &sIfBranch, const StringVector &sElseBranch)
Assign the results of an if-else construct to this StringVector instance.
bool getBooleanVectorized(size_t i) const
Get either the i-th element or the 1st element converted as boolean, if this vector has only one comp...
StringView getVectorized(size_t i) const
Get either the i-th element or the 1st element, if this vector has only one component (and is a singl...
StringArg getArg(size_t i) const
Create a StringArg instance from the i-th or the 1st element, if this vector is a singleton.
void assign(const StringVector &sVect)
Assign another StringVector instance.
static StringVector convert_literal(const std::string &sLiteral)
Static member function to create a StringVector instance from a string literal.
static std::string makeLocalString(const std::string &sStr)
Static member function to convert the string literal to an internal string for this class.
const char & front() const
This member function provides a const char reference to the first character in the viewed section.
std::string to_string() const
This member function returns a copy of the viewed section of the string (via std::string::substr)....
size_t length() const
This member function simply returns the length of the viewed section.
This class is the immutable (const) version of a string view. It can be constructed from a MutableStr...
StringView subview(size_t pos=0, size_t len=std::string::npos) const
This member function creates a new StringView class instance using the selected position and length a...
void SetExpr(StringView a_sExpr)
Set the expression. Triggers first time calculation thus the creation of the bytecode and scanning of...
value_type Eval()
Single-value wrapper around the vectorized overload of this member function.
Mathematical expressions parser.
Definition: muParser.h:51
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
#define max(a, b)
Definition: resampler.cpp:30
#define NEWSTRING
std::string toInternalString(std::string sStr)
Converts a string literal to the internal representation in tables and clusters.
std::string toString(int)
Converts an integer to a string without the Settings bloat.