forked from schrodinger/maeparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaeBlock.hpp
554 lines (451 loc) · 14.5 KB
/
MaeBlock.hpp
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#pragma once
#include <boost/dynamic_bitset.hpp>
#include <cassert>
#include <cstring>
#include <map>
#include <stdexcept>
#include <string>
#include <utility>
#include "MaeParserConfig.hpp"
namespace schrodinger
{
namespace mae
{
using BoolProperty = uint8_t;
template <typename T>
inline const T& get_property(const std::map<std::string, T>& map,
const std::string& name)
{
auto iter = map.find(name);
if (iter == map.end()) {
throw std::out_of_range("Key not found: " + name);
} else {
return iter->second;
}
}
// Forward declaration.
class IndexedBlockBuffer;
class IndexedBlock;
class EXPORT_MAEPARSER IndexedBlockMapI
{
public:
virtual ~IndexedBlockMapI() = default;
virtual bool hasIndexedBlock(const std::string& name) const = 0;
virtual std::shared_ptr<const IndexedBlock>
getIndexedBlock(const std::string& name) const = 0;
virtual std::vector<std::string> getBlockNames() const = 0;
bool operator==(const IndexedBlockMapI& rhs) const;
};
class EXPORT_MAEPARSER IndexedBlockMap : public IndexedBlockMapI
{
std::map<std::string, std::shared_ptr<IndexedBlock>> m_indexed_block;
public:
bool hasIndexedBlock(const std::string& name) const override;
std::shared_ptr<const IndexedBlock>
getIndexedBlock(const std::string& name) const override;
std::vector<std::string> getBlockNames() const override
{
std::vector<std::string> rval;
for (const auto& p : m_indexed_block) {
rval.push_back(p.first);
}
return rval;
}
/**
* Add an IndexedBlock to the map.
*/
void addIndexedBlock(const std::string& name,
std::shared_ptr<IndexedBlock> indexed_block)
{
m_indexed_block[name] = std::move(indexed_block);
}
};
class EXPORT_MAEPARSER BufferedIndexedBlockMap : public IndexedBlockMapI
{
private:
std::map<std::string, std::shared_ptr<IndexedBlock>> m_indexed_block;
std::map<std::string, std::shared_ptr<IndexedBlockBuffer>> m_indexed_buffer;
public:
bool hasIndexedBlock(const std::string& name) const override;
std::shared_ptr<const IndexedBlock>
getIndexedBlock(const std::string& name) const override;
std::vector<std::string> getBlockNames() const override
{
std::vector<std::string> rval;
for (const auto& p : m_indexed_buffer) {
rval.push_back(p.first);
}
return rval;
}
/**
* Add an IndexedBlockBuffer to the map, which can be used to retrieve an
* IndexedBlock.
*/
void
addIndexedBlockBuffer(const std::string& name,
std::shared_ptr<IndexedBlockBuffer> block_buffer)
{
m_indexed_buffer[name] = std::move(block_buffer);
}
};
class EXPORT_MAEPARSER Block
{
private:
const std::string m_name;
std::map<std::string, BoolProperty> m_bmap;
std::map<std::string, double> m_rmap;
std::map<std::string, int> m_imap;
std::map<std::string, std::string> m_smap;
std::map<std::string, std::shared_ptr<Block>> m_sub_block;
std::shared_ptr<IndexedBlockMapI> m_indexed_block_map;
public:
// Prevent copying.
Block(const Block&) = delete;
Block& operator=(const Block&) = delete;
Block(std::string name)
: m_name(std::move(name)), m_bmap(), m_rmap(), m_imap(), m_smap(),
m_indexed_block_map(nullptr)
{
}
const std::string& getName() const { return m_name; }
std::string toString() const;
void write(std::ostream& out, unsigned int current_indentation = 0) const;
void setIndexedBlockMap(std::shared_ptr<IndexedBlockMapI> indexed_block_map)
{
m_indexed_block_map = std::move(indexed_block_map);
}
bool hasIndexedBlockData() const { return m_indexed_block_map != nullptr; }
bool hasIndexedBlock(const std::string& name)
{
return hasIndexedBlockData() &&
m_indexed_block_map->hasIndexedBlock(name);
}
std::shared_ptr<const IndexedBlock>
getIndexedBlock(const std::string& name) const;
void addBlock(std::shared_ptr<Block> b) { m_sub_block[b->getName()] = std::move(b); }
/**
* Check whether this block has a sub-block of the provided name.
*/
bool hasBlock(const std::string& name)
{
std::map<std::string, std::shared_ptr<Block>>::const_iterator iter =
m_sub_block.find(name);
return (iter != m_sub_block.end());
}
/**
* Retrieve a shared pointer to the named sub-block.
*/
std::shared_ptr<Block> getBlock(const std::string& name)
{
std::map<std::string, std::shared_ptr<Block>>::const_iterator iter =
m_sub_block.find(name);
if (iter == m_sub_block.end()) {
throw std::out_of_range("Sub-block not found: " + name);
} else {
return iter->second;
}
}
bool operator==(const Block& rhs) const;
bool hasRealProperty(const std::string& name) const
{
return (m_rmap.find(name) != m_rmap.end());
}
double getRealProperty(const std::string& name) const
{
return get_property<double>(m_rmap, name);
}
void setRealProperty(const std::string& name, double value)
{
m_rmap[name] = value;
}
bool hasIntProperty(const std::string& name) const
{
return (m_imap.find(name) != m_imap.end());
}
int getIntProperty(const std::string& name) const
{
return get_property<int>(m_imap, name);
}
void setIntProperty(const std::string& name, int value)
{
m_imap[name] = value;
}
bool hasBoolProperty(const std::string& name) const
{
return (m_bmap.find(name) != m_bmap.end());
}
bool getBoolProperty(const std::string& name) const
{
return 1u == get_property<BoolProperty>(m_bmap, name);
}
void setBoolProperty(const std::string& name, bool value)
{
m_bmap[name] = static_cast<BoolProperty>(value);
}
bool hasStringProperty(const std::string& name) const
{
return (m_smap.find(name) != m_smap.end());
}
const std::string& getStringProperty(const std::string& name) const
{
return get_property<std::string>(m_smap, name);
}
void setStringProperty(const std::string& name, std::string value)
{
m_smap[name] = std::move(value);
}
template <typename T> const std::map<std::string, T>& getProperties() const;
};
template <typename T> class IndexedProperty
{
private:
std::vector<T> m_data;
boost::dynamic_bitset<>* m_is_null;
public:
// Prevent copying.
IndexedProperty<T>(const IndexedProperty<T>&) = delete;
IndexedProperty<T>& operator=(const IndexedProperty<T>&) = delete;
using size_type = typename std::vector<T>::size_type;
/**
* Construct an IndexedProperty from a reference to a vector of data.
* This swaps out the data of the input vector.
*
* The optional boost::dynamic_bitset is owned by the created object.
*/
explicit IndexedProperty<T>(std::vector<T>& data,
boost::dynamic_bitset<>* is_null = nullptr)
: m_data(), m_is_null(is_null)
{
m_data.swap(data);
}
~IndexedProperty<T>()
{
if (m_is_null != nullptr) {
delete m_is_null;
}
}
bool operator==(const IndexedProperty<T>& rhs) const;
size_type size() const { return m_data.size(); }
bool hasUndefinedValues() const
{
return (m_is_null != NULL && m_is_null->any());
}
bool isDefined(size_type index) const
{
if (m_is_null == nullptr) {
// Use of assert matches out-of-bounds behavior for dynamic_bitset.
assert(index < m_data.size());
return true;
} else {
return !m_is_null->test(index);
}
}
void undefine(size_type index)
{
if (m_is_null == NULL) {
m_is_null = new boost::dynamic_bitset<>(m_data.size());
}
m_is_null->set(index);
}
inline T& operator[](size_type index)
{
if (m_is_null && m_is_null->test(index)) {
throw std::runtime_error("Indexed property value undefined.");
}
return m_data[index];
}
inline const T& operator[](size_type index) const
{
if (m_is_null && m_is_null->test(index)) {
throw std::runtime_error("Indexed property value undefined.");
}
return m_data[index];
}
inline T& at(size_type index) { return operator[](index); }
inline const T& at(size_type index) const { return operator[](index); }
inline const T& at(size_type index, const T& default_) const
{
if (m_is_null && m_is_null->test(index)) {
return default_;
}
return m_data[index];
}
void set(size_type index, const T& value)
{
m_data[index] = value;
if (m_is_null != NULL && m_is_null->test(index)) {
m_is_null->reset(index);
}
}
const std::vector<T>& data() const { return m_data; }
const boost::dynamic_bitset<>* nullIndices() const { return m_is_null; }
};
using IndexedRealProperty = IndexedProperty<double>;
using IndexedIntProperty = IndexedProperty<int>;
using IndexedBoolProperty = IndexedProperty<BoolProperty>;
using IndexedStringProperty = IndexedProperty<std::string>;
template <typename T>
inline std::shared_ptr<T>
get_indexed_property(const std::map<std::string, std::shared_ptr<T>>& map,
const std::string& name)
{
auto iter = map.find(name);
if (iter == map.end()) {
return std::shared_ptr<T>(nullptr);
} else {
return iter->second;
}
}
template <typename T>
inline void set_indexed_property(std::map<std::string, std::shared_ptr<T>>& map,
const std::string& name,
std::shared_ptr<T> value)
{
map[name] = std::move(value);
}
class EXPORT_MAEPARSER IndexedBlock
{
private:
const std::string m_name;
std::map<std::string, std::shared_ptr<IndexedBoolProperty>> m_bmap;
std::map<std::string, std::shared_ptr<IndexedIntProperty>> m_imap;
std::map<std::string, std::shared_ptr<IndexedRealProperty>> m_rmap;
std::map<std::string, std::shared_ptr<IndexedStringProperty>> m_smap;
public:
// Prevent copying.
IndexedBlock(const IndexedBlock&) = delete;
IndexedBlock& operator=(const IndexedBlock&) = delete;
/**
* Create an indexed block.
*/
IndexedBlock(std::string name)
: m_name(std::move(name)), m_bmap(), m_imap(), m_rmap(), m_smap()
{
}
size_t size() const;
const std::string& getName() const { return m_name; }
std::string toString() const;
void write(std::ostream& out, unsigned int current_indentation = 0) const;
bool operator==(const IndexedBlock& rhs) const;
bool operator!=(const IndexedBlock& rhs) const
{
return !(operator==(rhs));
}
template <typename T>
void setProperty(const std::string& name,
std::shared_ptr<IndexedProperty<T>> value);
bool hasBoolProperty(const std::string& name) const
{
return (m_bmap.find(name) != m_bmap.end());
}
std::shared_ptr<IndexedBoolProperty>
getBoolProperty(const std::string& name) const
{
return get_indexed_property<IndexedBoolProperty>(m_bmap, name);
}
void setBoolProperty(const std::string& name,
std::shared_ptr<IndexedBoolProperty> value)
{
set_indexed_property<IndexedBoolProperty>(m_bmap, name,
std::move(value));
}
bool hasIntProperty(const std::string& name) const
{
return (m_imap.find(name) != m_imap.end());
}
std::shared_ptr<IndexedIntProperty>
getIntProperty(const std::string& name) const
{
return get_indexed_property<IndexedIntProperty>(m_imap, name);
}
void setIntProperty(const std::string& name,
std::shared_ptr<IndexedIntProperty> value)
{
set_indexed_property<IndexedIntProperty>(m_imap, name,
std::move(value));
}
bool hasRealProperty(const std::string& name) const
{
return (m_rmap.find(name) != m_rmap.end());
}
std::shared_ptr<IndexedRealProperty>
getRealProperty(const std::string& name) const
{
return get_indexed_property<IndexedRealProperty>(m_rmap, name);
}
void setRealProperty(const std::string& name,
std::shared_ptr<IndexedRealProperty> value)
{
set_indexed_property<IndexedRealProperty>(m_rmap, name,
std::move(value));
}
bool hasStringProperty(const std::string& name) const
{
return (m_smap.find(name) != m_smap.end());
}
std::shared_ptr<IndexedStringProperty>
getStringProperty(const std::string& name) const
{
return get_indexed_property<IndexedStringProperty>(m_smap, name);
}
void setStringProperty(const std::string& name,
std::shared_ptr<IndexedStringProperty> value)
{
set_indexed_property<IndexedStringProperty>(m_smap, name,
std::move(value));
}
template <typename T>
const std::map<std::string, std::shared_ptr<IndexedProperty<T>>>&
getProperties() const;
};
// Template specializations
template <>
inline const std::map<std::string, BoolProperty>&
Block::getProperties<BoolProperty>() const
{
return m_bmap;
}
template <>
inline const std::map<std::string, int>& Block::getProperties<int>() const
{
return m_imap;
}
template <>
inline const std::map<std::string, double>& Block::getProperties<double>() const
{
return m_rmap;
}
template <>
inline const std::map<std::string, std::string>&
Block::getProperties<std::string>() const
{
return m_smap;
}
template <>
inline const std::map<std::string,
std::shared_ptr<IndexedProperty<BoolProperty>>>&
IndexedBlock::getProperties() const
{
return m_bmap;
}
template <>
inline const std::map<std::string, std::shared_ptr<IndexedProperty<int>>>&
IndexedBlock::getProperties() const
{
return m_imap;
}
template <>
inline const std::map<std::string, std::shared_ptr<IndexedProperty<double>>>&
IndexedBlock::getProperties() const
{
return m_rmap;
}
template <>
inline const std::map<std::string,
std::shared_ptr<IndexedProperty<std::string>>>&
IndexedBlock::getProperties() const
{
return m_smap;
}
} // namespace mae
} // namespace schrodinger