KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
DataSet.cpp
Go to the documentation of this file.
1#include "kids/DataSet.h"
2
3#include <complex>
4#include <cstring>
5#include <fstream>
6#include <iomanip>
7#include <iostream>
8#include <map>
9#include <memory>
10#include <tuple>
11#include <type_traits>
12#include <vector>
13
14#include "kids/Exception.h"
15#include "kids/Shape.h"
16#include "kids/Types.h"
17#include "kids/Variable.h"
18#include "kids/concat.h"
19
20namespace PROJECT_NS {
21
24 _data = std::shared_ptr<DataType>(new DataType());
25}
26
27template <typename T>
28T* DataSet::def(const std::string& key, Shape S, const std::string& info) {
30 std::shared_ptr<DataType> d_ptr = _data;
31
32 DataSet* currentNode = this;
33 for (size_t i = 0; i < kh.terms.size() - 1; ++i) {
34 auto& node = (*d_ptr)[kh.terms[i]];
35 if (!node) node = std::shared_ptr<DataSet>(new DataSet());
36
37 currentNode = static_cast<DataSet*>(node.get());
38 d_ptr = currentNode->_data;
39 }
40
41 std::shared_ptr<Node>& leaf_node = (*d_ptr)[kh.terms.back()];
42 if (!leaf_node) leaf_node = std::shared_ptr<Tensor<T>>(new Tensor<T>(S, info));
43
44 if (leaf_node->type() != as_enum<T>()) { //
45 throw std::runtime_error("doubly conflicted definition!");
46 }
47 auto leaf_node_ts = static_cast<Tensor<T>*>(leaf_node.get());
48 if (S.size() != leaf_node_ts->size()) { //
49 throw std::runtime_error("doubly conflicted definition!");
50 }
51 return leaf_node_ts->data();
52}
53kids_int* DataSet::def(VARIABLE<kids_int>& var) { return def_int(var.name(), var.shape(), var.doc()); }
54kids_real* DataSet::def(VARIABLE<kids_real>& var) { return def_real(var.name(), var.shape(), var.doc()); }
55kids_complex* DataSet::def(VARIABLE<kids_complex>& var) { return def_complex(var.name(), var.shape(), var.doc()); }
56
57
58kids_int* DataSet::def_int(const std::string& key, Shape S, const std::string& info) {
59 return def<kids_int>(key, S, info);
60}
61kids_int* DataSet::def_int(const std::string& key, kids_int* arr_in, Shape S, const std::string& info) {
62 kids_int* arr = def_int(key, S, info);
63 for (int i = 0; i < S.size(); ++i) arr[i] = arr_in[i];
64 return arr;
65}
66kids_int* DataSet::def_int(const std::string& key, const std::string& key_in, const std::string& info) {
67 auto inode = node(key_in);
68 if (inode->type() == kids_dataset_type) { //
69 throw std::runtime_error(std::string{key_in} + " : failed copying dataset");
70 }
71 auto inode_ts = static_cast<Tensor<kids_int>*>(inode);
72 return def_int(key, inode_ts->data(), inode_ts->size(), info);
73}
74DataSet& DataSet::_def_int(const std::string& key, Shape S, const std::string& info) {
75 def_int(key, S, info);
76 return *this;
77}
78
79kids_real* DataSet::def_real(const std::string& key, Shape S, const std::string& info) {
80 return def<kids_real>(key, S, info);
81}
82kids_real* DataSet::def_real(const std::string& key, kids_real* arr_in, Shape S, const std::string& info) {
83 kids_real* arr = def_real(key, S, info);
84 for (int i = 0; i < S.size(); ++i) arr[i] = arr_in[i];
85 return arr;
86}
87kids_real* DataSet::def_real(const std::string& key, const std::string& key_in, const std::string& info) {
88 auto inode = node(key_in);
89 if (inode->type() == kids_dataset_type) { //
90 throw std::runtime_error(std::string{key_in} + " : failed copying dataset");
91 }
92 auto inode_ts = static_cast<Tensor<kids_real>*>(inode);
93 return def_real(key, inode_ts->data(), inode_ts->size(), info);
94}
95DataSet& DataSet::_def_real(const std::string& key, Shape S, const std::string& info) {
96 def_real(key, S, info);
97 return *this;
98}
99
100kids_complex* DataSet::def_complex(const std::string& key, Shape S, const std::string& info) {
101 return def<kids_complex>(key, S, info);
102}
103kids_complex* DataSet::def_complex(const std::string& key, kids_complex* arr_in, Shape S, const std::string& info) {
104 kids_complex* arr = def_complex(key, S, info);
105 for (int i = 0; i < S.size(); ++i) arr[i] = arr_in[i];
106 return arr;
107}
108kids_complex* DataSet::def_complex(const std::string& key, const std::string& key_in, const std::string& info) {
109 auto inode = node(key_in);
110 if (inode->type() == kids_dataset_type) { //
111 throw std::runtime_error(std::string{key_in} + " : failed copying dataset");
112 }
113 auto inode_ts = static_cast<Tensor<kids_complex>*>(inode);
114 return def_complex(key, inode_ts->data(), inode_ts->size(), info);
115}
116DataSet& DataSet::_def_complex(const std::string& key, Shape S, const std::string& info) {
117 def_complex(key, S, info);
118 return *this;
119}
120
121
122DataSet& DataSet::_def(const std::string& key, const std::string& key_in, const std::string& info) {
123 auto leaf_node = node(key_in);
124 switch (leaf_node->type()) {
125 case kids_int_type:
126 def_int(key, key_in, info);
127 break;
128 case kids_real_type:
129 def_real(key, key_in, info);
130 break;
132 def_complex(key, key_in, info);
133 break;
134 default:
135 break;
136 }
137 return *this;
138}
139
140DataSet& DataSet::_undef(const std::string& key) {
142 std::shared_ptr<DataType> d_ptr = _data;
143
144 DataSet* currentNode = this;
145 for (size_t i = 0; i < kh.terms.size() - 1; ++i) {
146 auto& node = (*d_ptr)[kh.terms[i]];
147 if (!node) return *this;
148
149 currentNode = static_cast<DataSet*>(node.get());
150 d_ptr = currentNode->_data;
151 }
152 auto it = d_ptr->find(kh.terms.back());
153 if (it != d_ptr->end()) {
154 std::cout << "\n";
155 it->second.reset();
156 }
157 return *this;
158}
159
160std::tuple<kids_dtype, void*, Shape*> DataSet::obtain(const std::string& key) {
161 auto&& leaf_node = node(key);
162 switch (leaf_node->type()) {
163 case kids_int_type: {
164 auto&& conv_node = static_cast<Tensor<kids_int>*>(leaf_node);
165 return std::make_tuple(kids_int_type, conv_node->data(), &(conv_node->shape()));
166 break;
167 }
168 case kids_real_type: {
169 auto&& conv_node = static_cast<Tensor<kids_real>*>(leaf_node);
170 return std::make_tuple(kids_real_type, conv_node->data(), &(conv_node->shape()));
171 break;
172 }
173 case kids_complex_type: {
174 auto&& conv_node = static_cast<Tensor<kids_complex>*>(leaf_node);
175 return std::make_tuple(kids_complex_type, conv_node->data(), &(conv_node->shape()));
176 break;
177 }
178 default: {
179 throw std::runtime_error("bad obtain!");
180 break;
181 }
182 }
183}
184
185Node* DataSet::node(const std::string& key) {
187 std::shared_ptr<DataType> d_ptr = _data;
188
189 DataSet* currentNode = this;
190 for (size_t i = 0; i < kh.terms.size() - 1; ++i) {
191 auto& node = (*d_ptr)[kh.terms[i]];
192 if (!node) throw std::runtime_error(std::string{key} + " : access undefined key!");
193
194 currentNode = static_cast<DataSet*>(node.get());
195 d_ptr = currentNode->_data;
196 }
197
198 auto& leaf_node = (*d_ptr)[kh.terms.back()];
199 if (!leaf_node) throw std::runtime_error(std::string{key} + " : access undefined key!");
200 return leaf_node.get();
201}
202
203DataSet* DataSet::at(const std::string& key) {
204 auto leaf_node = node(key);
205 if (leaf_node->type() == kids_dataset_type) {
206 return static_cast<DataSet*>(leaf_node);
207 } else {
208 throw std::runtime_error("bad conversion!");
209 }
210 return nullptr;
211}
212
213std::string DataSet::help(const std::string& name) {
214 std::ostringstream os;
215 std::vector<std::tuple<std::string, Node*>> stack;
216
217 Node* inode = this;
218 if (name != "") inode = node(name);
219
220 stack.push_back(std::make_tuple("", inode));
221 while (!stack.empty()) {
222 auto [parent, currentNode] = stack.back();
223 stack.pop_back();
224
225 std::shared_ptr<DataType> d_ptr = static_cast<DataSet*>(currentNode)->_data;
226
227 for (auto& i : (*d_ptr)) {
228 std::string key = (parent == "") ? i.first : parent + "." + i.first;
229 Node* inode = i.second.get();
230 if (inode->type() == kids_dataset_type) {
231 stack.push_back(std::make_tuple(key, inode));
232 } else {
233 os << key << ":\n\t" << inode->help("") << "\n";
234 }
235 }
236 }
237 return os.str();
238}
239
240std::string DataSet::repr() {
241 std::ostringstream os;
242 std::vector<std::tuple<std::string, Node*>> stack;
243
244 stack.push_back(std::make_tuple("", this));
245
246 while (!stack.empty()) {
247 auto [parent, currentNode] = stack.back();
248 stack.pop_back();
249 std::shared_ptr<DataType> d_ptr = static_cast<DataSet*>(currentNode)->_data;
250 for (auto& i : (*d_ptr)) {
251 std::string key = (parent == "") ? i.first : parent + "." + i.first;
252 if (!i.second) continue;
253 Node* inode = i.second.get();
254
255 if (inode->type() == kids_dataset_type) {
256 stack.push_back(std::make_tuple(key, inode));
257 } else {
258 os << key << "\n" << inode->repr() << "\n\n";
259 }
260 }
261 }
262 return os.str();
263}
264
265void DataSet::dump(std::ostream& os) { os << repr(); }
266
267void DataSet::load(std::istream& is) {
268 std::string key, typeflag;
269 int size;
270 while (is >> key >> typeflag >> size) {
271 if (typeflag == as_str<int>()) {
272 int* ptr = def<int>(key, size);
273 for (int i = 0; i < size; ++i) is >> ptr[i];
274 } else if (typeflag == as_str<kids_real>()) {
275 kids_real* ptr = def<kids_real>(key, size);
276 for (int i = 0; i < size; ++i) is >> ptr[i];
277 } else if (typeflag == as_str<kids_complex>()) {
278 kids_complex* ptr = def<kids_complex>(key, size);
279 for (int i = 0; i < size; ++i) is >> ptr[i];
280 }
281 }
282}
283
284
285}; // namespace PROJECT_NS
286
Declaration of the DataSet class and related classes.
provide Exception structs
Declaration of the Shape class.
definition of types in the project and some utiles for types
this file provide Variable class
std::vector< std::string > terms
Definition DataSet.h:295
DataSet class is a tree-structured container for storage of Tensor and other DataSet.
Definition DataSet.h:74
DataSet & _def_complex(const std::string &key, Shape S=1, const std::string &info="")
Define a complex variable with a specified key, shape, and info and update the dataset.
Definition DataSet.cpp:116
std::map< std::string, std::shared_ptr< Node > > DataType
Definition DataSet.h:79
DataSet & _def_real(const std::string &key, Shape S=1, const std::string &info="")
Define a real variable with a specified key, shape, and info and update the dataset.
Definition DataSet.cpp:95
virtual void dump(std::ostream &os)
Dump the DataSet to an output stream.
Definition DataSet.cpp:265
DataSet * at(const std::string &key)
Access the DataSet corresponding to a variable with a specified key.
Definition DataSet.cpp:203
virtual std::string repr()
Get a string representation of the DataSet.
Definition DataSet.cpp:240
DataSet & _undef(const std::string &key)
Undefine a variable with a specified key.
Definition DataSet.cpp:140
virtual void load(std::istream &is)
Load the DataSet from an input stream.
Definition DataSet.cpp:267
Node * node(const std::string &key)
Get the node corresponding to a variable with a specified key.
Definition DataSet.cpp:185
DataSet()
Constructor for DataSet.
Definition DataSet.cpp:22
kids_complex * def_complex(const std::string &key, Shape S=1, const std::string &info="")
Define a complex variable with a specified key, shape, and info.
Definition DataSet.cpp:100
std::tuple< kids_dtype, void *, Shape * > obtain(const std::string &key)
Obtain information about a variable with a specified key.
Definition DataSet.cpp:160
virtual std::string help(const std::string &name)
Get help information for a variable with a specified name.
Definition DataSet.cpp:213
DataSet & _def(const std::string &key, const std::string &key_in, const std::string &info="")
Define a variable with a specified key and reference key, and info.
Definition DataSet.cpp:122
kids_int * def_int(const std::string &key, Shape S=1, const std::string &info="")
Define an integer variable with a specified key, shape, and info.
Definition DataSet.cpp:58
kids_int * def(VARIABLE< kids_int > &var)
Define a variable of type kids_int.
Definition DataSet.cpp:53
std::shared_ptr< DataType > _data
Definition DataSet.h:80
kids_real * def_real(const std::string &key, Shape S=1, const std::string &info="")
Define a real variable with a specified key, shape, and info.
Definition DataSet.cpp:79
DataSet & _def_int(const std::string &key, Shape S=1, const std::string &info="")
Define an integer variable with a specified key, shape, and info and update the dataset.
Definition DataSet.cpp:74
Node class is an abstract interface.
Definition Node.h:39
virtual std::string help(const std::string &name)=0
kids_dtype type()
Definition Node.h:45
virtual std::string repr()=0
kids_dtype _type
Definition Node.h:50
Shape class provide information about of a Tensor's shape.
Definition Shape.h:50
Tensor class is the container for array-like data.
Definition Tensor.h:43
< http://warp.povusers.org/FunctionParser/fparser.html
Definition Context.h:39
@ kids_real_type
Represents real number type.
Definition Types.h:47
@ kids_complex_type
Represents complex number type.
Definition Types.h:48
@ kids_dataset_type
Represents DataSet type.
Definition Types.h:51
@ kids_int_type
Represents integer type.
Definition Types.h:46
double kids_real
Alias for real number type.
Definition Types.h:59
std::complex< double > kids_complex
Alias for complex number type.
Definition Types.h:60
int kids_int
Alias for integer type.
Definition Types.h:57