NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
stringmemory.hpp
Go to the documentation of this file.
1/*****************************************************************************
2 NumeRe: Framework fuer Numerische Rechnungen
3 Copyright (C) 2019 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#include <string>
20#include <map>
21#include <vector>
22
23#include "sorter.hpp"
24
25#ifndef STRINGMEMORY_HPP
26#define STRINGMEMORY_HPP
27
28// Internal string memory implementation. Separated to be able to
29// use Sorter as base class for strings and for the DataFile object
31{
32 private:
34
35 void reorderColumn(const std::vector<int>& vIndex, long long int i1, long long int i2, long long int j1 = 0);
36 virtual int compare(int i, int j, int col) override;
37 virtual bool isValue(int line, int col) override;
38
39 public:
41
42 std::vector<std::vector<std::string> > sStrings;
43
44 std::vector<int> sortElements(long long int i1, long long int i2, long long int j1, long long int j2, const std::string& sSortingExpression);
45};
46
47
48// The string memory class, which will be included into the Cache
49// by inheritance
51{
52 private:
54
55 public:
56 bool writeString(const std::string& _sString, unsigned int _nthString = std::string::npos, unsigned int nCol = 0);
57 std::string readString(unsigned int _nthString = std::string::npos, unsigned int nCol = 0);
58
59 std::string maxString(unsigned int i1 = 0, unsigned int i2 = std::string::npos, unsigned int nCol = 0);
60 std::string maxString(VectorIndex _vLine, VectorIndex _vCol);
61 std::string minString(unsigned int i1 = 0, unsigned int i2 = std::string::npos, unsigned int nCol = 0);
62 std::string minString(VectorIndex _vLine, VectorIndex _vCol);
63 std::string sumString(unsigned int i1 = 0, unsigned int i2 = std::string::npos, unsigned int nCol = 0);
64 std::string sumString(VectorIndex _vLine, VectorIndex _vCol);
65
66 // Returns the number of stored elements in either a
67 // specific column or the maximal line count of the
68 // whole object
69 inline unsigned int getStringElements(unsigned int nCol = std::string::npos) const
70 {
71 if (nCol == std::string::npos)
72 {
73 // No column selected. Return the maximal line count
74 unsigned int nCnt = 0;
75
76 for (unsigned int i = 0; i < _stringIntMem.sStrings.size(); i++)
77 {
78 if (nCnt < _stringIntMem.sStrings[i].size())
79 nCnt = _stringIntMem.sStrings[i].size();
80 }
81
82 return nCnt;
83 }
84 else if (nCol >= _stringIntMem.sStrings.size())
85 return 0;
86 else
87 return _stringIntMem.sStrings[nCol].size();
88 return 0;
89 }
90
91 // Return number of columns
92 inline unsigned int getStringCols() const
93 {
94 return _stringIntMem.sStrings.size();
95 }
96
97 // Removes all elements of a selected column
98 inline bool removeStringElements(unsigned int nCol = 0)
99 {
100 if (nCol < _stringIntMem.sStrings.size())
101 {
102 if (_stringIntMem.sStrings[nCol].size())
103 _stringIntMem.sStrings[nCol].clear();
104 return true;
105 }
106 return false;
107 }
108
109 // Cleares the content of the whole string object
111 {
112 if (_stringIntMem.sStrings.size())
113 {
114 _stringIntMem.sStrings.clear();
115 return true;
116 }
117 return false;
118 }
119
120 // Returns the size of a selected column or the whole
121 // object in bytes
122 inline int getStringSize(unsigned int nCol = std::string::npos) const
123 {
124 if (nCol == std::string::npos)
125 {
126 // No column selected. Return the total size
127 unsigned int nSize = 0;
128
129 // Recursively call this function for a specific column
130 for (unsigned int i = 0; i < _stringIntMem.sStrings.size(); i++)
131 {
132 nSize += getStringSize(i);
133 }
134
135 return nSize;
136 }
137 else if (nCol < _stringIntMem.sStrings.size())
138 {
139 // Selected a valid column
140 if (_stringIntMem.sStrings[nCol].size())
141 {
142 int nSize = 0;
143
144 // Count the number of caracters and multiply it with the
145 // bytesize of a character
146 for (unsigned int i = 0; i < _stringIntMem.sStrings[nCol].size(); i++)
147 nSize += _stringIntMem.sStrings[nCol][i].size() * sizeof(char);
148
149 return nSize;
150 }
151 else
152 return 0;
153 }
154 else
155 return 0;
156 }
157
158 // Wrapper for the internal string sorting function
159 inline std::vector<int> sortStringElements(long long int i1, long long int i2, long long int j1, long long int j2, const std::string& sSortingExpression)
160 {
161 return _stringIntMem.sortElements(i1, i2, j1, j2, sSortingExpression);
162 }
163};
164
165#endif // STRINGMEMORY_HPP
166
167
Abstract parent class to implement the sorting functionality (using Quicksort) on a more generic leve...
Definition: sorter.hpp:31
virtual int compare(int i, int j, int col) override
std::vector< int > sortElements(long long int i1, long long int i2, long long int j1, long long int j2, const std::string &sSortingExpression)
std::vector< std::vector< std::string > > sStrings
virtual bool isValue(int line, int col) override
void reorderColumn(const std::vector< int > &vIndex, long long int i1, long long int i2, long long int j1=0)
bool writeString(const std::string &_sString, unsigned int _nthString=std::string::npos, unsigned int nCol=0)
StringInternalMemory _stringIntMem
std::string maxString(unsigned int i1=0, unsigned int i2=std::string::npos, unsigned int nCol=0)
int getStringSize(unsigned int nCol=std::string::npos) const
unsigned int getStringCols() const
unsigned int getStringElements(unsigned int nCol=std::string::npos) const
std::string readString(unsigned int _nthString=std::string::npos, unsigned int nCol=0)
bool clearStringElements()
std::string minString(unsigned int i1=0, unsigned int i2=std::string::npos, unsigned int nCol=0)
bool removeStringElements(unsigned int nCol=0)
std::vector< int > sortStringElements(long long int i1, long long int i2, long long int j1, long long int j2, const std::string &sSortingExpression)
std::string sumString(unsigned int i1=0, unsigned int i2=std::string::npos, unsigned int nCol=0)
This class abstracts all the index logics, i.e. the logical differences between single indices and in...
Definition: structures.hpp:42