-
Notifications
You must be signed in to change notification settings - Fork 24
/
MaeBlock.cpp
408 lines (345 loc) · 11.2 KB
/
MaeBlock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include "MaeBlock.hpp"
#include <cmath>
#include <utility>
#include "MaeParser.hpp"
using namespace std;
namespace schrodinger
{
namespace mae
{
namespace
{
const double tolerance = 0.00001; // Tolerance to match string cutoff
// Wrap to-string to allow it to take strings and be a no-op
template <typename T> inline string local_to_string(T val)
{
return to_string(val);
}
inline bool char_requires_escaping(char c)
{
return c == '"' || c == '\\';
}
string local_to_string(const string& val)
{
if (val.empty()) {
return R"("")";
}
// Quotes are required if any character needs escaping, or there are
// spaces in the string (spaces do not require escaping)
bool quotes_required = false;
for (const char& c : val) {
if (char_requires_escaping(c) || c == ' ') {
quotes_required = true;
break;
}
}
if (!quotes_required) {
return val;
}
std::stringstream new_string;
new_string << '\"';
for (const char& c : val) {
if (char_requires_escaping(c)) {
new_string << '\\';
}
new_string << c;
}
new_string << '\"';
return new_string.str();
}
template <typename T>
inline void output_property_names(ostream& out, const string& indentation,
const map<string, T>& properties)
{
for (const auto& p : properties) {
out << indentation << p.first << "\n";
}
}
template <typename T>
inline void output_property_values(ostream& out, const string& indentation,
const map<string, T>& properties)
{
for (const auto& p : properties) {
out << indentation << local_to_string(p.second) << "\n";
}
}
template <typename T>
void output_indexed_property_values(ostream& out,
const map<string, T>& properties,
unsigned int index)
{
for (const auto& p : properties) {
const auto& property = p.second;
if (property->isDefined(index)) {
out << ' ' << local_to_string(property->at(index));
} else {
out << " <>";
}
}
}
template <typename T>
bool maps_indexed_props_equal(const T& lmap, const T& rmap)
{
if (rmap.size() != lmap.size())
return false;
auto diff = std::mismatch(
lmap.begin(), lmap.end(), rmap.begin(),
[](decltype(*begin(lmap)) l, decltype(*begin(lmap)) r) {
return l.first == r.first && *(l.second) == *(r.second);
});
if (diff.first != lmap.end())
return false;
return true;
}
} // namespace
void Block::write(ostream& out, unsigned int current_indentation) const
{
string root_indentation = string(current_indentation, ' ');
current_indentation += 2;
string indentation = string(current_indentation, ' ');
const bool has_data = !m_bmap.empty() || !m_rmap.empty() ||
!m_imap.empty() || !m_smap.empty();
out << root_indentation << getName() << " {\n";
if (has_data) {
output_property_names(out, indentation, m_bmap);
output_property_names(out, indentation, m_rmap);
output_property_names(out, indentation, m_imap);
output_property_names(out, indentation, m_smap);
out << indentation + ":::\n";
output_property_values(out, indentation, m_bmap);
output_property_values(out, indentation, m_rmap);
output_property_values(out, indentation, m_imap);
output_property_values(out, indentation, m_smap);
}
if (hasIndexedBlockData()) {
const auto block_names = m_indexed_block_map->getBlockNames();
for (const auto& name : block_names) {
const auto& indexed_block =
m_indexed_block_map->getIndexedBlock(name);
indexed_block->write(out, current_indentation);
}
}
for (const auto& p : m_sub_block) {
const auto& sub_block = p.second;
sub_block->write(out, current_indentation);
}
out << root_indentation << "}\n\n";
return;
}
string Block::toString() const
{
ostringstream stream;
write(stream);
return stream.str();
}
shared_ptr<const IndexedBlock> Block::getIndexedBlock(const string& name) const
{
if (!hasIndexedBlockData()) {
throw out_of_range("Indexed block not found: " + name);
}
return const_pointer_cast<const IndexedBlock>(
m_indexed_block_map->getIndexedBlock(name));
}
bool real_map_equal(const map<string, double>& rmap1,
const map<string, double>& rmap2)
{
if (rmap1.size() != rmap2.size())
return false;
for (const auto& p : rmap1) {
if (rmap2.count(p.first) != 1)
return false;
if ((float) abs(p.second - rmap2.at(p.first)) > tolerance)
return false;
}
return true;
}
bool Block::operator==(const Block& rhs) const
{
if (m_bmap != rhs.m_bmap)
return false;
if (!real_map_equal(m_rmap, rhs.m_rmap))
return false;
if (m_imap != rhs.m_imap)
return false;
if (m_smap != rhs.m_smap)
return false;
if (m_sub_block != rhs.m_sub_block)
return false;
if (!(*m_indexed_block_map == *(rhs.m_indexed_block_map)))
return false;
return true;
}
bool IndexedBlockMapI::operator==(const IndexedBlockMapI& rhs) const
{
const auto& block_names = getBlockNames();
for (const auto& name : block_names) {
if (!rhs.hasIndexedBlock(name))
return false;
const auto& block1 = rhs.getIndexedBlock(name);
const auto& block2 = getIndexedBlock(name);
if (*block1 != *block2)
return false;
}
return true;
}
bool IndexedBlockMap::hasIndexedBlock(const string& name) const
{
return m_indexed_block.find(name) != m_indexed_block.end();
}
shared_ptr<const IndexedBlock>
IndexedBlockMap::getIndexedBlock(const string& name) const
{
auto block_iter = m_indexed_block.find(name);
if (block_iter != m_indexed_block.end()) {
return const_pointer_cast<const IndexedBlock>(block_iter->second);
} else {
throw out_of_range("Indexed block not found: " + name);
}
}
bool BufferedIndexedBlockMap::hasIndexedBlock(const string& name) const
{
if (m_indexed_buffer.find(name) != m_indexed_buffer.end()) {
return true;
} else if (m_indexed_block.find(name) != m_indexed_block.end()) {
return true;
} else {
return false;
}
}
shared_ptr<const IndexedBlock>
BufferedIndexedBlockMap::getIndexedBlock(const string& name) const
{
auto itb = m_indexed_block.find(name);
if (itb != m_indexed_block.end()) {
return itb->second;
}
auto itbb = m_indexed_buffer.find(name);
if (itbb == m_indexed_buffer.end()) {
throw out_of_range("Indexed block not found: " + name);
} else {
shared_ptr<const IndexedBlock> ib(itbb->second->getIndexedBlock());
return ib;
}
}
template <>
EXPORT_MAEPARSER void
IndexedBlock::setProperty<BoolProperty>(const string& name,
shared_ptr<IndexedBoolProperty> value)
{
set_indexed_property<IndexedBoolProperty>(m_bmap, name, std::move(value));
}
template <>
EXPORT_MAEPARSER void
IndexedBlock::setProperty<double>(const string& name,
shared_ptr<IndexedProperty<double>> value)
{
set_indexed_property<IndexedProperty<double>>(m_rmap, name,
std::move(value));
}
template <>
EXPORT_MAEPARSER void
IndexedBlock::setProperty<int>(const string& name,
shared_ptr<IndexedProperty<int>> value)
{
set_indexed_property<IndexedProperty<int>>(m_imap, name, std::move(value));
}
template <>
EXPORT_MAEPARSER void
IndexedBlock::setProperty<string>(const string& name,
shared_ptr<IndexedProperty<string>> value)
{
set_indexed_property<IndexedProperty<string>>(m_smap, name,
std::move(value));
}
size_t IndexedBlock::size() const
{
size_t count = 0;
// To save memory, not all maps will have max index count for the block,
// so we must find the max size of all maps in the block.
for (const auto& p : m_bmap)
count = max(p.second->size(), count);
for (const auto& p : m_imap)
count = max(p.second->size(), count);
for (const auto& p : m_rmap)
count = max(p.second->size(), count);
for (const auto& p : m_smap)
count = max(p.second->size(), count);
return count;
}
void IndexedBlock::write(ostream& out, unsigned int current_indentation) const
{
string root_indentation = string(current_indentation, ' ');
string indentation = string(current_indentation + 2, ' ');
const bool has_data = !m_bmap.empty() || !m_rmap.empty() ||
!m_imap.empty() || !m_smap.empty();
out << root_indentation << getName() << "[" << to_string((int) size())
<< "] {\n";
if (has_data) {
out << indentation + "# First column is Index #\n";
output_property_names(out, indentation, m_bmap);
output_property_names(out, indentation, m_rmap);
output_property_names(out, indentation, m_imap);
output_property_names(out, indentation, m_smap);
out << indentation + ":::\n";
for (unsigned int i = 0; i < size(); ++i) {
out << indentation << i + 1;
output_indexed_property_values(out, m_bmap, i);
output_indexed_property_values(out, m_rmap, i);
output_indexed_property_values(out, m_imap, i);
output_indexed_property_values(out, m_smap, i);
out << endl;
}
out << indentation + ":::\n";
}
out << root_indentation << "}\n";
return;
}
string IndexedBlock::toString() const
{
ostringstream stream;
write(stream);
return stream.str();
}
template <typename T>
bool IndexedProperty<T>::operator==(const IndexedProperty<T>& rhs) const
{
if (m_is_null == nullptr || rhs.m_is_null == nullptr) {
if ((m_is_null == nullptr) != (rhs.m_is_null == nullptr))
return false;
} else if (*m_is_null != *(rhs.m_is_null)) {
return false;
}
if (m_data != rhs.m_data)
return false;
return true;
}
// For doubles we need to implement our own comparator for the vectors to
// take precision into account
template <>
bool IndexedProperty<double>::operator==(
const IndexedProperty<double>& rhs) const
{
if (m_is_null == nullptr || rhs.m_is_null == nullptr) {
if ((m_is_null == nullptr) != (rhs.m_is_null == nullptr))
return false;
} else if (*m_is_null != *(rhs.m_is_null))
return false;
for (size_t i = 0; i < m_data.size(); ++i)
if ((float) abs(m_data[i] - rhs.m_data[i]) > tolerance)
return false;
return true;
}
bool IndexedBlock::operator==(const IndexedBlock& rhs) const
{
if (!maps_indexed_props_equal(m_bmap, rhs.m_bmap))
return false;
if (!maps_indexed_props_equal(m_imap, rhs.m_imap))
return false;
if (!maps_indexed_props_equal(m_rmap, rhs.m_rmap))
return false;
if (!maps_indexed_props_equal(m_smap, rhs.m_smap))
return false;
return true;
}
} // namespace mae
} // namespace schrodinger