-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.h
95 lines (80 loc) · 2.61 KB
/
storage.h
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
#ifndef STORAGE
#define STORAGE
#include <map>
#include <iostream>
#include <cassert>
#include "structure.h"
/*
This class (Storage) implements a wrapper for a map container, that can be used
to store and compare optimised structures. The comparator function is the
template parameter and can be redefined by the user.
Implementations can be found in "storage.cc".
Parameters
----------
storageMap :
map container that stores structures. Third parameter is the comparator
functor that can be redefined. Currently, "compareEnergy" and
"compareInterPartDist" are implemented. Comparator functors need to typedef
a key type for the map container as "keyType".
_mapping :
mapping container.
Methods
-------
bool addCluster (structure) :
Checks if a cluster is already stored in the map container. If not it will
be added. Returns true if cluster was added and false otherwise.
int getSize() :
returns number of stored structures.
void printKeys (std::ostream) :
print stored keys to ostream.
void printStructures() :
uses xyzoutall function from "iop.h" to print all structures to output folder.
typename storageMap<T>::iterator begin()
typename storageMap<T>::iterator end() :
wrapper for begin() and end() methods of map container.
*/
struct compareEnergy
{
compareEnergy(double eps = 1e-4) : _eps(eps) {}
using keyType = double;
double _eps;
bool operator() (const keyType a, const keyType b) const
{
return (b - a > _eps);
}
};
struct compareInterPartDist
{
compareInterPartDist (double eps = 1e-4) : _eps(eps) {}
using keyType = std::vector<double>;
double _eps;
bool operator() (const keyType a, const keyType b) const
{
assert(a.size() == b.size());
for (int i = 0; i < a.size(); i++)
{
if (fabs(b[i] - a[i]) < _eps) continue;
if (b[i] - a[i] > _eps) return true;
if (b[i] - a[i] < - _eps) return false;
}
return false;
}
};
template <typename T>
class Storage
{
private:
template <typename Q> using storageMap = std::map<typename Q::keyType, structure, Q>;
storageMap<T> _mapping;
public:
Storage() : _mapping() {}
bool addCluster (structure &S);
int getSize() {return _mapping.size();}
void printKeys(std::ostream& out);
void printStructures();
typename storageMap<T>::iterator begin() {return _mapping.begin();}
typename storageMap<T>::iterator end() {return _mapping.end();}
};
using StorageByEnergy = Storage<compareEnergy>;
using StorageByInterPartDist = Storage<compareInterPartDist>;
#endif