NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
structures.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2017 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
19
20#ifndef STRUCTURES_HPP
21#define STRUCTURES_HPP
22
23#include <string>
24#include <stdexcept>
25#include <vector>
26#include <cmath>
27#include <algorithm>
28#include "interval.hpp"
29
30long long int intCast(const std::complex<double>&);
31std::string toString(int);
32std::string toString(long long int);
33
34
42{
43 private:
44 mutable std::vector<int> vStorage;
45 bool expand;
46
57 inline int getIndex(size_t n) const
58 {
59 // If single indices are used, they will be expanded
60 // into virtual vectors
61 if (expand)
62 {
63 // If the second index has not been defined,
64 // we'll expand the index using no end
65 if (vStorage.back() == OPEN_END)
66 {
67 if (vStorage.size() == 2)
68 return vStorage.front() + n;
69 else if (vStorage.size() > n+1)
70 return vStorage[n];
71 else
72 return vStorage[vStorage.size()-1] + n - (vStorage.size()-1);
73 }
74
75 // Consider the case that the order of the indices
76 // could be inverted
77 if (vStorage.front() <= vStorage.back() && (int)n + vStorage.front() <= vStorage.back())
78 return vStorage.front() + n;
79 else if (vStorage.front() > vStorage.back() && vStorage.front() - (int)n >= vStorage.back()) // >= because vStorage.back() was not decremented first
80 return vStorage.front() - n;
81 }
82 else if (n < vStorage.size())
83 return vStorage[n];
84
85 return INVALID;
86 }
87
88 public:
89 enum
90 {
91 INVALID = -1,
93 STRING = -3
94 };
95
102 {
103 vStorage.assign({INVALID, INVALID});
104 expand = true;
105 }
106
113 VectorIndex(const VectorIndex& vIndex) : vStorage(vIndex.vStorage), expand(vIndex.expand)
114 {
115 }
116
123 VectorIndex(VectorIndex&& vIndex) : expand(std::move(vIndex.expand))
124 {
125 std::swap(vStorage, vIndex.vStorage);
126 }
127
138 VectorIndex(const mu::value_type* indices, int nResults, int unused)
139 {
140 // Store the indices and convert them to integers
141 // using the intCast() function
142 for (int i = 0; i < nResults; i++)
143 {
144 if (!std::isnan(indices[i].real()) && !std::isinf(indices[i].real()))
145 vStorage.push_back(intCast(indices[i]) - 1);
146 }
147
148 expand = false;
149 }
150
158 VectorIndex(int nStart, int nEnd = INVALID)
159 {
160 vStorage.assign({nStart, nEnd});
161 expand = true;
162 }
163
170 VectorIndex(const std::vector<int>& vIndex)
171 {
172 if (vIndex.size())
173 {
174 vStorage = vIndex;
175 expand = false;
176 }
177 else
178 {
179 vStorage.assign({INVALID, INVALID});
180 expand = true;
181 }
182 }
183
193 {
194 vStorage = vIndex.vStorage;
195 expand = vIndex.expand;
196 return *this;
197 }
198
207 VectorIndex& operator=(const std::vector<int>& vIndex)
208 {
209 if (vIndex.size())
210 {
211 vStorage = vIndex;
212 expand = false;
213 }
214
215 return *this;
216 }
217
238 VectorIndex subidx(size_t pos, size_t nLen = std::string::npos) const
239 {
240 // Consider some strange border cases
241 if (pos >= size())
242 return VectorIndex();
243
244 if (nLen > size() - pos)
245 nLen = size() - pos;
246
247 // Return a single index
248 if (nLen <= 1)
249 return VectorIndex(getIndex(pos));
250
251 // Calculate the starting and ending indices for
252 // single indices. The last index is decremented,
253 // because the first and the last index already
254 // count as two indices.
255 if (expand)
256 return VectorIndex(getIndex(pos), getIndex(pos+nLen-1));
257
258 // Return a copy of the internal storage, if it is already
259 // expanded. The last index is not decremented, because
260 // terminating iterator always points after the last
261 // element in the list
262 return VectorIndex(std::vector<int>(vStorage.begin()+pos, vStorage.begin()+pos+nLen));
263 }
264
277 {
278 if (!expand)
279 {
280 int nMin = min();
281 int nMax = max();
282
283 vStorage.clear();
284 vStorage.assign({nMin, nMax});
285 expand = true;
286 }
287 }
288
298 inline int operator[](size_t n) const
299 {
300 return getIndex(n);
301 }
302
314 size_t size() const
315 {
316 if (vStorage.size() == 2 && !isValid())
317 return 0;
318 else if (vStorage.back() == INVALID)
319 return 1;
320 else if (vStorage.back() == OPEN_END)
321 return -1;
322 else if (vStorage.size() == 2 && expand)
323 return abs(vStorage.back() - vStorage.front()) + 1;
324 else
325 return vStorage.size();
326 }
327
340 size_t numberOfNodes() const
341 {
342 if (!isValid())
343 return 0;
344 else if (vStorage.size() == 2 && ((vStorage.back() == INVALID) xor (vStorage.front() == INVALID)))
345 return 1;
346 else
347 return vStorage.size();
348 }
349
361 bool isOrdered() const
362 {
363 if (isValid())
364 return vStorage.front() <= vStorage.back() || vStorage.back() == INVALID || vStorage.back() == OPEN_END;
365
366 return false;
367 }
368
377 bool isExpanded() const
378 {
379 return expand;
380 }
381
394 void setIndex(size_t nthIndex, int nVal)
395 {
396 // If the size is large enough, simply store
397 // the passed index. Otherwise expand the
398 // index using invalid values and store it
399 // afterwards
400 if (nthIndex < vStorage.size())
401 {
402 vStorage[nthIndex] = nVal;
403 }
404 else
405 {
406 // increase the size by using invalid values
407 while (vStorage.size() <= nthIndex)
408 vStorage.push_back(INVALID);
409
410 vStorage[nthIndex] = nVal;
411
412 // If the last value is not an open end
413 // value and the size of the internal storage
414 // is larger than two, deactivate the expanding
415 if (vStorage.size() > 2 && vStorage.back() != OPEN_END)
416 expand = false;
417 }
418 }
419
430 void push_back(int nVal)
431 {
432 if (expand)
433 {
435 expand = false;
436 }
437
438 vStorage.push_back(nVal);
439 }
440
451 void append(const std::vector<int>& vVector)
452 {
453 if (expand)
454 {
456 expand = false;
457 }
458
459 vStorage.insert(vStorage.end(), vVector.begin(), vVector.end());
460 }
461
473 void append(const VectorIndex& vIndex)
474 {
475 if (vIndex.expand)
476 append(vIndex.getVector());
477 else
478 append(vIndex.vStorage);
479 }
480
491 void prepend(const std::vector<int>& vVector)
492 {
493 if (expand)
494 {
496 expand = false;
497 }
498
499 vStorage.insert(vStorage.begin(), vVector.begin(), vVector.end());
500 }
501
513 void prepend(const VectorIndex& vIndex)
514 {
515 if (vIndex.expand)
516 prepend(vIndex.getVector());
517 else
518 prepend(vIndex.vStorage);
519 }
520
531 std::vector<int> getVector() const
532 {
533 // Expand the single indices stored internally
534 // if needed
535 if (expand)
536 {
537 std::vector<int> vReturn;
538
539 for (size_t i = 0; i < size(); i++)
540 {
541 vReturn.push_back(getIndex(i));
542 }
543
544 return vReturn;
545 }
546
547 return vStorage;
548 }
549
558 int max() const
559 {
560 if (isOpenEnd())
561 return OPEN_END;
562 else if (expand)
563 return std::max(vStorage.front(), vStorage.back());
564
565 return *std::max_element(vStorage.begin(), vStorage.end());
566 }
567
576 int min() const
577 {
578 if (expand && isOpenEnd())
579 return vStorage.front();
580 else if (expand)
581 return std::min(vStorage.front(), vStorage.back());
582
583 int nMin = vStorage.front();
584
585 for (size_t i = 1; i < vStorage.size(); i++)
586 {
587 if (nMin < 0 || (vStorage[i] > 0 && vStorage[i] < nMin))
588 nMin = vStorage[i];
589 }
590
591 return nMin;
592 }
593
601 inline bool isValid() const
602 {
603 return vStorage.front() != INVALID || vStorage.back() != INVALID;
604 }
605
614 inline bool isOpenEnd() const
615 {
616 return vStorage.back() == OPEN_END;
617 }
618
627 inline bool isString() const
628 {
629 return vStorage.front() == STRING || vStorage.back() == STRING;
630 }
631
640 int& front()
641 {
642 return vStorage.front();
643 }
644
653 int& back()
654 {
655 return vStorage.back();
656 }
657
666 const int& front() const
667 {
668 return vStorage.front();
669 }
670
679 const int& back() const
680 {
681 return vStorage.back();
682 }
683
693 int last() const
694 {
695 if (expand && vStorage.back() == INVALID)
696 return vStorage.front();
697
698 return vStorage.back();
699 }
700
712 void setRange(int nMin, int nMax)
713 {
714 // Change the order of the minimal and
715 // maximal value, if needed
716 if (nMin > nMax)
717 {
718 int nTemp = nMin;
719 nMin = nMax;
720 nMax = nTemp;
721 }
722
723 // Compare all values to the defined
724 // interval
725 for (size_t i = 0; i < vStorage.size(); i++)
726 {
727 // Special cases: if the current value
728 // is open end, use the maximal value
729 // passed to this function
730 if (vStorage[i] == OPEN_END)
731 {
732 vStorage[i] = nMax;
733 continue;
734 }
735 else if (vStorage[i] == INVALID)
736 continue;
737
738 if (vStorage[i] < nMin)
739 vStorage[i] = nMin;
740
741 if (vStorage[i] > nMax)
742 vStorage[i] = nMax;
743 }
744 }
745
756 void setOpenEndIndex(int nLast) const
757 {
758 if (vStorage.back() == OPEN_END)
759 vStorage.back() = nLast;
760 }
761
770 std::string to_string() const
771 {
772 if (expand)
773 {
774 std::string sVect;
775
776 if (vStorage.front() == INVALID)
777 sVect += "INVALID";
778 else if (vStorage.front() == STRING)
779 sVect += "#";
780 else
781 sVect += toString(vStorage.front()+1);
782
783 // Do not present the second index if
784 // it corresponds to an invalid value
785 if (vStorage.back() == OPEN_END)
786 sVect += ":inf";
787 else if (vStorage.back() == STRING)
788 sVect += ":#";
789 else if (vStorage.back() != INVALID)
790 sVect += ":"+toString(vStorage.back()+1);
791
792 return sVect;
793 }
794 else
795 {
796 std::string sVect = "{";
797
798 for (size_t i = 0; i < size(); i++)
799 {
800 // Jump over the middle section,
801 // if the vector length is larger
802 // than 5
803 if (size() >= 5u && i == 2)
804 {
805 sVect += "...,";
806 i = size() - 2;
807 }
808
809 if (getIndex(i) == INVALID)
810 sVect += "INVALID";
811 else if (getIndex(i) == OPEN_END)
812 sVect += "inf";
813 else if (getIndex(i) == STRING)
814 sVect += "#";
815 else
816 sVect += toString(getIndex(i)+1);
817
818 if (i + 1 < size())
819 sVect += ",";
820 }
821
822 return sVect + "}";
823 }
824 }
825};
826
827
836template<class T>
837class EndlessVector : public std::vector<T>
838{
839 private:
841 public:
845 EndlessVector() : std::vector<T>(), m_fallback() {}
846
853 EndlessVector(const EndlessVector& vec) : std::vector<T>(vec), m_fallback() {}
854
864 {
865 std::vector<T>::operator=(vec);
866 return *this;
867 }
868
879 T& operator[](size_t n)
880 {
881 if (n < std::vector<T>::size())
882 return std::vector<T>::operator[](n);
883
884 return m_fallback;
885 }
886};
887
888
910{
911 protected:
912 size_t m_start;
913 size_t m_len;
914
926 inline size_t validizeLength(size_t pos, size_t len) const
927 {
928 if (len == std::string::npos || pos+len > m_len)
929 len = m_len - pos;
930
931 return len;
932 }
933
940 virtual inline void clear()
941 {
942 m_start = 0;
943 m_len = 0;
944 }
945
956 inline bool validAbsolutePosition(size_t pos) const
957 {
958 return pos >= m_start && pos < m_start + m_len;
959 }
960
961 public:
962
967
976 virtual inline const std::string* getData() const
977 {
978 return nullptr;
979 }
980
990 inline bool operator==(const StringViewBase& view) const
991 {
992 const std::string* thisString = getData();
993 const std::string* viewString = view.getData();
994 if (thisString && viewString)
995 return thisString->compare(m_start, m_len, *viewString, view.m_start, view.m_len) == 0;
996
997 return false;
998 }
999
1009 inline bool operator==(const std::string& sString) const
1010 {
1011 const std::string* thisString = getData();
1012
1013 if (thisString)
1014 return thisString->compare(m_start, m_len, sString) == 0;
1015
1016 return false;
1017 }
1018
1027 inline bool operator==(const char* sString) const
1028 {
1029 const std::string* thisString = getData();
1030
1031 if (thisString)
1032 return thisString->compare(m_start, m_len, sString) == 0;
1033
1034 return false;
1035 }
1036
1046 inline bool operator!=(const StringViewBase& view) const
1047 {
1048 if (getData() && view.getData())
1049 return getData()->compare(m_start, m_len, *view.getData(), view.m_start, view.m_len) != 0;
1050
1051 return false;
1052 }
1053
1063 inline bool operator!=(const std::string& sString) const
1064 {
1065 if (getData())
1066 return getData()->compare(m_start, m_len, sString) != 0;
1067
1068 return false;
1069 }
1070
1080 inline bool operator!=(const char* sString) const
1081 {
1082 if (getData())
1083 return getData()->compare(m_start, m_len, sString) != 0;
1084
1085 return false;
1086 }
1087
1097 inline bool operator<(const StringViewBase& view) const
1098 {
1099 if (getData() && view.getData())
1100 return getData()->compare(m_start, m_len, *view.getData(), view.m_start, view.m_len) < 0;
1101
1102 return false;
1103 }
1104
1114 inline bool operator<(const std::string& sString) const
1115 {
1116 if (getData())
1117 return getData()->compare(m_start, m_len, sString) < 0;
1118
1119 return false;
1120 }
1121
1131 inline bool operator<=(const StringViewBase& view) const
1132 {
1133 if (getData() && view.getData())
1134 return getData()->compare(m_start, m_len, *view.getData(), view.m_start, view.m_len) <= 0;
1135
1136 return false;
1137 }
1138
1148 inline bool operator<=(const std::string& sString) const
1149 {
1150 if (getData())
1151 return getData()->compare(m_start, m_len, sString) <= 0;
1152
1153 return false;
1154 }
1155
1165 inline bool operator>(const StringViewBase& view) const
1166 {
1167 if (getData() && view.getData())
1168 return getData()->compare(m_start, m_len, *view.getData(), view.m_start, view.m_len) > 0;
1169
1170 return false;
1171 }
1172
1182 inline bool operator>(const std::string& sString) const
1183 {
1184 if (getData())
1185 return getData()->compare(m_start, m_len, sString) > 0;
1186
1187 return false;
1188 }
1189
1199 inline bool operator>=(const StringViewBase& view) const
1200 {
1201 if (getData() && view.getData())
1202 return getData()->compare(m_start, m_len, *view.getData(), view.m_start, view.m_len) >= 0;
1203
1204 return false;
1205 }
1206
1216 inline bool operator>=(const std::string& sString) const
1217 {
1218 if (getData())
1219 return getData()->compare(m_start, m_len, sString) >= 0;
1220
1221 return false;
1222 }
1223
1233 inline std::string operator+(const StringViewBase& view) const
1234 {
1235 if (getData() && view.getData())
1236 return std::string(begin(), end()).append(view.begin(), view.end());
1237
1238 return "";
1239 }
1240
1250 inline std::string operator+(const std::string& sString) const
1251 {
1252 if (getData())
1253 return std::string(begin(), end()).append(sString);
1254
1255 return "";
1256 }
1257
1266 inline const char& front() const
1267 {
1268 if (getData())
1269 return getData()->at(m_start);
1270
1271 throw std::out_of_range("StringView::front");
1272 }
1273
1282 inline const char& back() const
1283 {
1284 if (getData())
1285 return getData()->at(m_start+m_len-1);
1286
1287 throw std::out_of_range("StringView::back");
1288 }
1289
1298 inline std::string::const_iterator begin() const
1299 {
1300 if (getData())
1301 return getData()->begin() + m_start;
1302
1303 return std::string::const_iterator();
1304 }
1305
1314 inline std::string::const_iterator end() const
1315 {
1316 if (getData())
1317 return getData()->begin() + m_start + m_len;
1318
1319 return std::string::const_iterator();
1320 }
1321
1331 inline void trim_front(size_t len)
1332 {
1333 if (len < m_len)
1334 {
1335 m_start += len;
1336 m_len -= len;
1337 }
1338 else
1339 clear();
1340 }
1341
1351 inline void trim_back(size_t len)
1352 {
1353 if (len < m_len)
1354 m_len -= len;
1355 else
1356 clear();
1357 }
1358
1369 inline void strip()
1370 {
1371 const std::string* data = getData();
1372
1373 if (!data)
1374 return;
1375
1376 // Strip leading
1377 while (m_len && isblank(data->operator[](m_start)))
1378 {
1379 m_start++;
1380 m_len--;
1381 }
1382
1383 if (!m_len)
1384 {
1385 clear();
1386 return;
1387 }
1388
1389 // Strip trailing
1390 while (m_len && isblank(data->operator[](m_start+m_len-1)))
1391 {
1392 m_len--;
1393 }
1394 }
1395
1403 inline size_t length() const
1404 {
1405 return m_len;
1406 }
1407
1417 inline std::string to_string() const
1418 {
1419 if (getData())
1420 return getData()->substr(m_start, m_len);
1421
1422 return "";
1423 }
1424
1434 size_t find(const std::string& findstr, size_t pos = 0) const
1435 {
1436 if (getData())
1437 {
1438 size_t fnd = getData()->find(findstr, m_start + pos);
1439
1440 if (validAbsolutePosition(fnd))
1441 return fnd-m_start;
1442 }
1443
1444 return std::string::npos;
1445 }
1446
1456 size_t find(char c, size_t pos = 0) const
1457 {
1458 if (getData())
1459 {
1460 size_t fnd = getData()->find(c, m_start + pos);
1461
1462 if (validAbsolutePosition(fnd))
1463 return fnd-m_start;
1464 }
1465
1466 return std::string::npos;
1467 }
1468
1478 size_t rfind(const std::string& findstr, size_t pos = std::string::npos) const
1479 {
1480 if (getData())
1481 {
1482 pos = validizeLength(m_start, pos);
1483 size_t fnd = getData()->rfind(findstr, m_start + pos);
1484
1485 if (validAbsolutePosition(fnd))
1486 return fnd-m_start;
1487 }
1488
1489 return std::string::npos;
1490 }
1491
1501 size_t rfind(char c, size_t pos = std::string::npos) const
1502 {
1503 if (getData())
1504 {
1505 pos = validizeLength(m_start, pos);
1506 size_t fnd = getData()->rfind(c, m_start + pos);
1507
1508 if (validAbsolutePosition(fnd))
1509 return fnd-m_start;
1510 }
1511
1512 return std::string::npos;
1513 }
1514
1524 size_t find_first_of(const std::string& findstr, size_t pos = 0) const
1525 {
1526 if (getData())
1527 {
1528 size_t fnd = getData()->find_first_of(findstr, m_start + pos);
1529
1530 if (validAbsolutePosition(fnd))
1531 return fnd-m_start;
1532 }
1533
1534 return std::string::npos;
1535 }
1536
1546 size_t find_first_of(char c, size_t pos = 0) const
1547 {
1548 if (getData())
1549 {
1550 size_t fnd = getData()->find_first_of(c, m_start + pos);
1551
1552 if (validAbsolutePosition(fnd))
1553 return fnd-m_start;
1554 }
1555
1556 return std::string::npos;
1557 }
1558
1568 size_t find_first_not_of(const std::string& findstr, size_t pos = 0) const
1569 {
1570 if (getData())
1571 {
1572 size_t fnd = getData()->find_first_not_of(findstr, m_start + pos);
1573
1574 if (validAbsolutePosition(fnd))
1575 return fnd-m_start;
1576 }
1577
1578 return std::string::npos;
1579 }
1580
1590 size_t find_first_not_of(char c, size_t pos = 0) const
1591 {
1592 if (getData())
1593 {
1594 size_t fnd = getData()->find_first_not_of(c, m_start + pos);
1595
1596 if (validAbsolutePosition(fnd))
1597 return fnd-m_start;
1598 }
1599
1600 return std::string::npos;
1601 }
1602
1612 size_t find_last_of(const std::string& findstr, size_t pos = std::string::npos) const
1613 {
1614 if (getData())
1615 {
1616 pos = validizeLength(m_start, pos);
1617 size_t fnd = getData()->find_last_of(findstr, m_start + pos);
1618
1619 if (validAbsolutePosition(fnd))
1620 return fnd-m_start;
1621 }
1622
1623 return std::string::npos;
1624 }
1625
1635 size_t find_last_of(char c, size_t pos = std::string::npos) const
1636 {
1637 if (getData())
1638 {
1639 pos = validizeLength(m_start, pos);
1640 size_t fnd = getData()->find_last_of(c, m_start + pos);
1641
1642 if (validAbsolutePosition(fnd))
1643 return fnd-m_start;
1644 }
1645
1646 return std::string::npos;
1647 }
1648
1658 size_t find_last_not_of(const std::string& findstr, size_t pos = std::string::npos) const
1659 {
1660 if (getData())
1661 {
1662 pos = validizeLength(m_start, pos);
1663 size_t fnd = getData()->find_last_not_of(findstr, m_start + pos);
1664
1665 if (validAbsolutePosition(fnd))
1666 return fnd-m_start;
1667 }
1668
1669 return std::string::npos;
1670 }
1671
1681 size_t find_last_not_of(char c, size_t pos = std::string::npos) const
1682 {
1683 if (getData())
1684 {
1685 pos = validizeLength(m_start, pos);
1686 size_t fnd = getData()->find_last_not_of(c, m_start + pos);
1687
1688 if (validAbsolutePosition(fnd))
1689 return fnd-m_start;
1690 }
1691
1692 return std::string::npos;
1693 }
1694
1695};
1696
1697
1708inline std::string operator+(const std::string& sString, const StringViewBase& view)
1709{
1710 return sString + view.to_string();
1711}
1712
1713
1714// Forward declaration for friendship
1715class StringView;
1716
1717
1727{
1728 private:
1729 friend class StringView;
1730 std::string* m_data;
1731
1741 MutableStringView(std::string* data, size_t start, size_t len) : StringViewBase(), m_data(data)
1742 {
1743 m_start = start;
1744 m_len = len;
1745 }
1746
1747 protected:
1755 virtual inline const std::string* getData() const override
1756 {
1757 return m_data;
1758 }
1759
1769 {
1770 m_data = view.m_data;
1771 m_start = view.m_start;
1772 m_len = view.m_len;
1773 }
1774
1783 void assign(std::string* data)
1784 {
1785 if (!data)
1786 return;
1787
1788 m_data = data;
1789 m_start = 0;
1790 m_len = m_data->length();
1791 }
1792
1800 virtual inline void clear() override
1801 {
1802 m_data = nullptr;
1803 m_start = 0;
1804 m_len = 0;
1805 }
1806
1807 public:
1812
1821 {
1822 assign(data);
1823 }
1824
1833 {
1834 assign(&data);
1835 }
1836
1844 {
1845 assign(view);
1846 }
1847
1855 {
1856 m_data = std::move(view.m_data);
1857 m_start = std::move(view.m_start);
1858 m_len = std::move(view.m_len);
1859 }
1860
1870 {
1871 assign(view);
1872 return *this;
1873 }
1874
1883 MutableStringView& operator=(std::string* data)
1884 {
1885 assign(data);
1886 return *this;
1887 }
1888
1897 MutableStringView& operator=(std::string& data)
1898 {
1899 assign(&data);
1900 return *this;
1901 }
1902
1913 inline char& operator[](size_t pos)
1914 {
1915 return m_data->operator[](m_start+pos);
1916 }
1917
1932 MutableStringView subview(size_t pos = 0, size_t len = std::string::npos) const
1933 {
1934 if (m_data && pos < m_len)
1935 {
1936 len = validizeLength(pos, len);
1937 return MutableStringView(m_data, m_start+pos, len);
1938 }
1939
1940 return MutableStringView();
1941 }
1942
1958 MutableStringView& replace(size_t pos, size_t len, const std::string& s)
1959 {
1960 if (m_data && pos < m_len)
1961 {
1962 len = validizeLength(pos, len);
1963 m_data->replace(m_start+pos, len, s);
1964 m_len += s.length() - len;
1965 }
1966
1967 return *this;
1968 }
1969
1988 MutableStringView& replace(size_t pos, size_t len, const std::string& s, size_t subpos, size_t sublen)
1989 {
1990 if (m_data && pos < m_len)
1991 {
1992 len = validizeLength(pos, len);
1993
1994 if (subpos + sublen > s.length())
1995 sublen = s.length() - subpos;
1996
1997 m_data->replace(m_start+pos, len, s, subpos, sublen);
1998 m_len += sublen - len;
1999 }
2000
2001 return *this;
2002 }
2003
2019 MutableStringView& replace(size_t pos, size_t len, const StringViewBase& view)
2020 {
2021 return replace(pos, len, view.to_string());
2022 }
2023
2024};
2025
2026
2035{
2036 private:
2037 const std::string* m_data;
2038
2048 StringView(const std::string* data, size_t start, size_t len) : StringViewBase(), m_data(data)
2049 {
2050 m_start = start;
2051 m_len = len;
2052 }
2053
2054 protected:
2062 virtual inline const std::string* getData() const override
2063 {
2064 return m_data;
2065 }
2066
2075 void assign(const StringView& view)
2076 {
2077 m_data = view.m_data;
2078 m_start = view.m_start;
2079 m_len = view.m_len;
2080 }
2081
2090 void assign(const MutableStringView& view)
2091 {
2092 m_data = view.m_data;
2093 m_start = view.m_start;
2094 m_len = view.m_len;
2095 }
2096
2105 void assign(const std::string* data)
2106 {
2107 if (!data)
2108 return;
2109
2110 m_data = data;
2111 m_start = 0;
2112 m_len = m_data->length();
2113 }
2114
2122 virtual inline void clear() override
2123 {
2124 m_data = nullptr;
2125 m_start = 0;
2126 m_len = 0;
2127 }
2128
2129 public:
2134
2142 StringView(const std::string* data) : StringView()
2143 {
2144 assign(data);
2145 }
2146
2154 StringView(const std::string& data) : StringView()
2155 {
2156 assign(&data);
2157 }
2158
2168 StringView(const std::string& data, size_t start, size_t len = std::string::npos) : StringView()
2169 {
2170 assign(&data);
2171
2172 if (m_data && start < m_len)
2173 {
2174 len = validizeLength(start, len);
2175 m_start = start;
2176 m_len = len;
2177 }
2178 }
2179
2187 {
2188 assign(view);
2189 }
2190
2199 {
2200 assign(view);
2201 }
2202
2210 {
2211 m_data = std::move(view.m_data);
2212 m_start = std::move(view.m_start);
2213 m_len = std::move(view.m_len);
2214 }
2215
2225 {
2226 assign(view);
2227 return *this;
2228 }
2229
2239 {
2240 assign(view);
2241 return *this;
2242 }
2243
2252 StringView& operator=(const std::string* data)
2253 {
2254 assign(data);
2255 return *this;
2256 }
2257
2266 StringView& operator=(const std::string& data)
2267 {
2268 assign(&data);
2269 return *this;
2270 }
2271
2281 inline const char& operator[](size_t pos) const
2282 {
2283 return m_data->operator[](m_start+pos);
2284 }
2285
2299 StringView subview(size_t pos = 0, size_t len = std::string::npos) const
2300 {
2301 if (m_data && pos < m_len)
2302 {
2303 len = validizeLength(pos, len);
2304 return StringView(m_data, m_start+pos, len);
2305 }
2306
2307 return StringView();
2308 }
2309
2321 {
2322 if (m_data)
2323 return MutableStringView(const_cast<std::string*>(m_data), m_start, m_len);
2324
2325 return MutableStringView();
2326 }
2327
2328};
2329
2330
2341{
2345
2348 {
2349 }
2351 {
2352 std::swap(_idx.row, row);
2353 std::swap(_idx.col, col);
2354 std::swap(_idx.sCompiledAccessEquation, sCompiledAccessEquation);
2355 }
2357 {
2358 row = _idx.row;
2359 col = _idx.col;
2361 return *this;
2362 }
2363};
2364
2365
2370struct Match
2371{
2372 std::string sString;
2373 unsigned int nPos;
2374};
2375
2376
2380struct Point
2381{
2382 double x;
2383 double y;
2384
2385 Point(double _x, double _y) : x(_x), y(_y) {}
2386
2387 void rotate90(int n, const Point& origin = Point(0, 0))
2388 {
2389 x -= origin.x;
2390 y -= origin.y;
2391
2392 double x1 = x;
2393 double y1 = y;
2394
2395 // Remove multiples of 360 degrees
2396 n = n % 4;
2397
2398 // Move negative angles to the corresponding
2399 // positive value
2400 if (n < 0)
2401 n += 4;
2402
2403 // We only have to consider the values 1-3
2404 switch (n)
2405 {
2406 case 1:
2407 x1 = -y;
2408 y1 = x;
2409 break;
2410 case 2:
2411 x1 = -x;
2412 y1 = -y;
2413 break;
2414 case 3:
2415 x1 = y;
2416 y1 = -x;
2417 break;
2418 }
2419
2420 x = x1 + origin.x;
2421 y = y1 + origin.y;
2422 }
2423
2424 void rotate(double dAlpha, const Point& origin = Point(0, 0))
2425 {
2426 // Use the 90 degree rotation optimisation
2427 if (abs(dAlpha / M_PI_2 - rint(dAlpha / M_PI_2)) < 1e-8)
2428 {
2429 rotate90(rint(dAlpha / M_PI_2), origin);
2430 return;
2431 }
2432
2433 double x1 = (x - origin.x) * cos(dAlpha) - (y - origin.y) * sin(dAlpha) + origin.x;
2434 double y1 = (x - origin.x) * sin(dAlpha) + (y - origin.y) * cos(dAlpha) + origin.y;
2435
2436 x = x1;
2437 y = y1;
2438 }
2439
2440 Point operator+(const Point& a) const
2441 {
2442 return Point(x + a.x, y + a.y);
2443 }
2444
2445 Point operator-(const Point& a) const
2446 {
2447 return Point(x - a.x, y - a.y);
2448 }
2449
2450 Point operator*(double a) const
2451 {
2452 return Point(x*a, y*a);
2453 }
2454
2455 Point operator/(double a) const
2456 {
2457 return Point(x/a, y/a);
2458 }
2459};
2460
2461
2468{
2469 std::vector<mu::value_type> vNumVal;
2470 std::vector<std::string> vStringVal;
2471 std::string sReturnedTable;
2473
2475
2476 // clear method
2477 void clear()
2478 {
2479 vNumVal.clear();
2480 vStringVal.clear();
2481 sReturnedTable.clear();
2482 delayDelete = false;
2483 }
2484
2485 // boolean checkers
2486 bool isString() const
2487 {
2488 return vStringVal.size();
2489 }
2490 bool isNumeric() const
2491 {
2492 return vNumVal.size() && !vStringVal.size();
2493 }
2494};
2495
2496
2502{
2503 std::string sName[4] = {"x", "y", "z", "t"};
2505};
2506
2507
2514{
2515 int nKey[2];
2516 // Contains a recursive pointer
2518
2519 // Default constructor
2520 ColumnKeys() : nKey{-1,-1}, subkeys(nullptr) {}
2521 // Destructor recursively deletes the stored pointers
2523 {
2524 if (subkeys)
2525 delete subkeys;
2526 }
2527};
2528
2529
2535{
2536 long long int n;
2537 long long int m;
2538 size_t rows;
2539 size_t cols;
2540
2541 Boundary(long long int i, long long int j, size_t _row, size_t _col) : n(i), m(j), rows(_row), cols(_col) {}
2542
2543 long long int rf()
2544 {
2545 return n;
2546 }
2547
2548 long long int re()
2549 {
2550 return n+rows;
2551 }
2552
2553 long long int cf()
2554 {
2555 return m;
2556 }
2557
2558 long long int ce()
2559 {
2560 return m+cols;
2561 }
2562};
2563
2564#endif
This class extends the std::vector for endlessness.
Definition: structures.hpp:838
EndlessVector()
Default constructor.
Definition: structures.hpp:845
EndlessVector & operator=(const EndlessVector &vec)
Assignment operator overload from same.
Definition: structures.hpp:863
EndlessVector(const EndlessVector &vec)
Copy constructor from same.
Definition: structures.hpp:853
T & operator[](size_t n)
Access operator overload. Will return default constructed instances of the template type T,...
Definition: structures.hpp:879
This class is a mutable version of a string view. It can be used to replace single characters or enti...
MutableStringView & operator=(std::string *data)
Assignment operator for a std::string pointer.
MutableStringView subview(size_t pos=0, size_t len=std::string::npos) const
This member function creates a new MutableStringView class instance using the selected position and l...
MutableStringView & operator=(std::string &data)
Assignment operator for a std::string (non-const) reference.
MutableStringView(std::string &data)
MutableStringView constructor from a (non-const) std::string reference.
MutableStringView & replace(size_t pos, size_t len, const std::string &s)
This member function replaces a range in the internal viewed string with the passed string.
char & operator[](size_t pos)
Implementation of the random access operator. Returns a (non-const) char reference.
MutableStringView(std::string *data)
MutableStringView constructor from a std::string pointer.
MutableStringView & replace(size_t pos, size_t len, const StringViewBase &view)
This member function replaces a range in the internal viewed string with the passed StringViewBase.
std::string * m_data
MutableStringView & replace(size_t pos, size_t len, const std::string &s, size_t subpos, size_t sublen)
This member function replaces a range in the internal viewed string with the passed string....
MutableStringView(MutableStringView &&view)
MutableStringView move constructor.
MutableStringView()
MutableStringView default constructor.
virtual void clear() override
Override to clear the internal pointer as well.
void assign(std::string *data)
Assignment member function from a std::string pointer.
virtual const std::string * getData() const override
Override to return a pointer to the internal string.
MutableStringView & operator=(MutableStringView &view)
Assignment operator for another MutableStringView instance.
MutableStringView(std::string *data, size_t start, size_t len)
Private constructor used by the subview member function.
void assign(MutableStringView &view)
Assignment member function from another MutableStringView instance.
MutableStringView(MutableStringView &view)
MutableStringView copy constrcutor.
This class is a base class for all string view classes.
Definition: structures.hpp:910
bool operator<(const std::string &sString) const
This member function is an overload for the less operator using a const std::string instance.
bool operator==(const StringViewBase &view) const
This member function is an overload for the equality operator using another StringViewBase instance.
Definition: structures.hpp:990
std::string operator+(const StringViewBase &view) const
This member function is an overload for the concatenation operator using another StringViewBase insta...
size_t rfind(const std::string &findstr, size_t pos=std::string::npos) const
Wrapper member function for std::string::rfind()
void strip()
This member function shrinks the viewed section to remove all leading or trailing whitespace characte...
size_t validizeLength(size_t pos, size_t len) const
This private member function evaluates, whether the passed length is part of the viewed section and a...
Definition: structures.hpp:926
void trim_front(size_t len)
This member function can be used to remove characters from the front of the viewed section.
size_t find_first_of(const std::string &findstr, size_t pos=0) const
Wrapper member function for std::string::find_first_of()
const char & back() const
This member function provides a const char reference to the last character in the viewed section.
size_t rfind(char c, size_t pos=std::string::npos) const
Wrapper member function for std::string::rfind()
size_t find_last_of(const std::string &findstr, size_t pos=std::string::npos) const
Wrapper member function for std::string::find_last_of()
size_t find_last_not_of(const std::string &findstr, size_t pos=std::string::npos) const
Wrapper member function for std::string::find_last_not_of()
size_t find_last_of(char c, size_t pos=std::string::npos) const
Wrapper member function for std::string::find_last_of()
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)....
bool operator>=(const StringViewBase &view) const
This member function is an overload for the greater-equal operator using another StringViewBase insta...
StringViewBase()
StringViewBase default constructor.
Definition: structures.hpp:966
bool operator==(const std::string &sString) const
This member function is an overload for the equality operator using a const std::string instance.
size_t find(char c, size_t pos=0) const
Wrapper member function for std::string::find()
bool operator>(const std::string &sString) const
This member function is an overload for the greater operator using a const std::string instance.
bool operator>(const StringViewBase &view) const
This member function is an overload for the greater operator using another StringViewBase instance.
std::string::const_iterator end() const
This member function provides an iterator to the end of the viewed section of the internal string.
void trim_back(size_t len)
This member function can be used to remove characters from the back of the viewed section.
bool operator==(const char *sString) const
This member function is an overload for the equality operator using a const char*.
bool operator<(const StringViewBase &view) const
This member function is an overload for the less operator using another StringViewBase instance.
bool operator<=(const StringViewBase &view) const
This member function is an overload for the less-equal operator using another StringViewBase instance...
bool operator>=(const std::string &sString) const
This member function is an overload for the greater-equal operator using a const std::string instance...
size_t find_first_not_of(char c, size_t pos=0) const
Wrapper member function for std::string::find_first_not_of()
bool operator!=(const std::string &sString) const
This member function is an overload for the inequality operator using a const std::string instance.
virtual const std::string * getData() const
This member function returns a const pointer to the viewed string. Is only used internally.
Definition: structures.hpp:976
std::string operator+(const std::string &sString) const
This member function is an overload for the concatenation operator using a const std::string instance...
size_t find(const std::string &findstr, size_t pos=0) const
Wrapper member function for std::string::find()
size_t length() const
This member function simply returns the length of the viewed section.
size_t find_first_of(char c, size_t pos=0) const
Wrapper member function for std::string::find_first_of()
bool operator<=(const std::string &sString) const
This member function is an overload for the less-equal operator using a const std::string instance.
std::string::const_iterator begin() const
This member function provides an iterator to the beginning of the viewed section of the internal stri...
bool operator!=(const char *sString) const
This member function is an overload for the inequality operator using a const char*.
bool operator!=(const StringViewBase &view) const
This member function is an overload for the inequality operator using another StringViewBase instance...
virtual void clear()
Reset function.
Definition: structures.hpp:940
bool validAbsolutePosition(size_t pos) const
This member function checks, whether the passed (absolute) position is part of the viewed string sect...
Definition: structures.hpp:956
size_t find_last_not_of(char c, size_t pos=std::string::npos) const
Wrapper member function for std::string::find_last_not_of()
size_t find_first_not_of(const std::string &findstr, size_t pos=0) const
Wrapper member function for std::string::find_first_not_of()
This class is the immutable (const) version of a string view. It can be constructed from a MutableStr...
StringView(const StringView &view)
StringView copy constructor.
StringView(StringView &&view)
StringView move constructor.
StringView()
StringView default constructor.
const std::string * m_data
StringView(const std::string *data, size_t start, size_t len)
Private constructor used by the subview member function.
StringView(const std::string *data)
StringView constructor from a const std::string pointer.
StringView & operator=(const StringView &view)
Assignment operator for another StringView instance.
void assign(const StringView &view)
Assignment member function from another StringView instance.
StringView(const std::string &data, size_t start, size_t len=std::string::npos)
StringView constructor from a const std::string reference, a start and a length.
StringView & operator=(const MutableStringView &view)
Assignment operator for a MutableStringView class instance.
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...
StringView & operator=(const std::string &data)
Assignment operator for a const std::string reference.
void assign(const std::string *data)
Assignment member function from a const std::string pointer.
const char & operator[](size_t pos) const
Random access operator, returning a const char reference.
virtual void clear() override
Override to clear the internal pointer as well.
StringView & operator=(const std::string *data)
Assignment operator for a const std::string pointer.
virtual const std::string * getData() const override
Override to return a pointer to the internal string.
MutableStringView make_mutable() const
This member function returns a MutableStringView instance with the data of this instance.
StringView(const std::string &data)
StringView constructor from a const std::string reference.
StringView(const MutableStringView &view)
StringView constructor from a MutableStringView class instance.
void assign(const MutableStringView &view)
Assignment member function from a MutableStringView instance.
This class abstracts all the index logics, i.e. the logical differences between single indices and in...
Definition: structures.hpp:42
const int & back() const
This member function returns a const reference to the final index value stored internally.
Definition: structures.hpp:679
VectorIndex(int nStart, int nEnd=INVALID)
Constructor for single indices.
Definition: structures.hpp:158
void push_back(int nVal)
This function will append the passed index value at the end of the index vector. The internal storage...
Definition: structures.hpp:430
const int & front() const
This member function returns a const reference to the first index value stored internally.
Definition: structures.hpp:666
VectorIndex()
Default constructor.
Definition: structures.hpp:101
bool isValid() const
This member function determines, whether the internal index set is valid.
Definition: structures.hpp:601
void linearize()
This member function linearizes the contents of a vector-described index set. The vectorial informati...
Definition: structures.hpp:276
VectorIndex subidx(size_t pos, size_t nLen=std::string::npos) const
This member function returns a subset of the internal stored index just like the std::string::substr(...
Definition: structures.hpp:238
void setOpenEndIndex(int nLast) const
This member function can be used to replace the open end state with a defined index value although th...
Definition: structures.hpp:756
void prepend(const std::vector< int > &vVector)
This function will prepend the passed vector before the beginning of the index vector....
Definition: structures.hpp:491
int getIndex(size_t n) const
This private memnber function calculates the index corresponding to the selected position n....
Definition: structures.hpp:57
int last() const
This member function returns the last index value, which can be reached by the values stored internal...
Definition: structures.hpp:693
int max() const
This function calculates the maximal index value obtained from the values stored internally.
Definition: structures.hpp:558
size_t size() const
This member function returns the size of the indices stored in this class.
Definition: structures.hpp:314
bool isOpenEnd() const
This member function determines, whether the internal index set has an open end.
Definition: structures.hpp:614
void setRange(int nMin, int nMax)
This member function can be used to force the indices stored internally to be in a defined interval....
Definition: structures.hpp:712
VectorIndex(VectorIndex &&vIndex)
VectorIndex move constructor.
Definition: structures.hpp:123
void append(const std::vector< int > &vVector)
This function will append the passed vector to the end of the index vector. The internal storage is e...
Definition: structures.hpp:451
VectorIndex(const VectorIndex &vIndex)
Copy constructor.
Definition: structures.hpp:113
std::string to_string() const
This member function converts the vector indexes contents into a human-readable string representation...
Definition: structures.hpp:770
bool isOrdered() const
This member function determines, whether the single indices are in the correct order.
Definition: structures.hpp:361
int operator[](size_t n) const
Overload for the access operator. Redirects the control to the private getIndex() member function.
Definition: structures.hpp:298
VectorIndex(const mu::value_type *indices, int nResults, int unused)
Constructor from an array of doubles. The third argument is used only to avoid misinterpretation from...
Definition: structures.hpp:138
std::vector< int > vStorage
Definition: structures.hpp:44
VectorIndex & operator=(const std::vector< int > &vIndex)
Assignment operator overload for STL vectors.
Definition: structures.hpp:207
void setIndex(size_t nthIndex, int nVal)
This member function can be used to set the index at a special position. This will expand the interna...
Definition: structures.hpp:394
bool isString() const
This member function determines, whether the internal index set referres to the table headlines.
Definition: structures.hpp:627
int & back()
This member function returns a reference to the final index value stored internally.
Definition: structures.hpp:653
VectorIndex(const std::vector< int > &vIndex)
Constructor from a STL vector.
Definition: structures.hpp:170
bool isExpanded() const
This member function determines, whether the indices are calculated or actual vectorial indices.
Definition: structures.hpp:377
void prepend(const VectorIndex &vIndex)
This function will append the passed VectorIndex before the beginning of the index vector....
Definition: structures.hpp:513
VectorIndex & operator=(const VectorIndex &vIndex)
Assignment operator overload for the same type.
Definition: structures.hpp:192
std::vector< int > getVector() const
This member function returns a STL vector, which will resemble the indices stored internally....
Definition: structures.hpp:531
int min() const
This member function calculates the minimal index value obtained from the values stored internally.
Definition: structures.hpp:576
int & front()
This member function returns a reference to the first index value stored internally.
Definition: structures.hpp:640
size_t numberOfNodes() const
This member function returns the number of nodes describing the index set, which is stored internally...
Definition: structures.hpp:340
void append(const VectorIndex &vIndex)
This function will append the passed VectorIndex to the end of the index vector. The internal storage...
Definition: structures.hpp:473
CONSTCD11 std::chrono::duration< Rep, Period > abs(std::chrono::duration< Rep, Period > d)
Definition: date.h:1317
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
bool isnan(const value_type &v)
Definition: muParserDef.h:379
std::vector< double > real(const std::vector< value_type > &vVec)
bool isinf(const value_type &v)
Definition: muParserDef.h:374
value_type rint(value_type v)
#define min(a, b)
Definition: resampler.cpp:34
#define max(a, b)
Definition: resampler.cpp:30
This structure contains the information of a two-dimensional boundary.
long long int m
size_t cols
long long int n
long long int re()
long long int cf()
long long int rf()
Boundary(long long int i, long long int j, size_t _row, size_t _col)
size_t rows
long long int ce()
Structure for the sorting functionality: used for the recursive definition of the index columns for s...
ColumnKeys * subkeys
Structure for the four standard variables.
std::string sName[4]
mu::value_type vValue[4][4]
This structure is central for managing the indices of a table or cluster read or write data access....
Indices(Indices &&_idx)
VectorIndex col
Indices(const Indices &_idx)
VectorIndex row
Indices & operator=(const Indices &_idx)
std::string sCompiledAccessEquation
Structure for the findCommand function.
unsigned int nPos
std::string sString
This represents a point in 2D space.
Point operator/(double a) const
void rotate90(int n, const Point &origin=Point(0, 0))
Point(double _x, double _y)
Point operator+(const Point &a) const
void rotate(double dAlpha, const Point &origin=Point(0, 0))
Point operator-(const Point &a) const
double x
double y
Point operator*(double a) const
Structure as wrapper for the return value of procedures (which may be numerical or string values or a...
std::vector< std::string > vStringVal
bool isString() const
bool isNumeric() const
std::string sReturnedTable
std::vector< mu::value_type > vNumVal
std::string operator+(const std::string &sString, const StringViewBase &view)
Inverse concatenation operator for a string instance with a StringViewBase instance.
long long int intCast(const std::complex< double > &)
Casts the real part of the complex number to an integer and avoids rounding errors.
Definition: tools.cpp:1824
std::string toString(int)
Converts an integer to a string without the Settings bloat.