-
Notifications
You must be signed in to change notification settings - Fork 1
/
DistanceMatrix.h
212 lines (171 loc) · 4.95 KB
/
DistanceMatrix.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
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
#ifndef DISTANCEMATRIX_H
#define DISTANCEMATRIX_H
#include <vector>
#include <algorithm>
#include <fstream>
#include "CrystalFpExceptions.h"
class DistanceMatrix
{
public:
DistanceMatrix(size_t aNumElements=0) : mNumElements(aNumElements), mDummy(0.0F)
{
if(aNumElements == 0) return;
mDistances.reserve((aNumElements*aNumElements-aNumElements)/2);
mDistances.resize((aNumElements*aNumElements-aNumElements)/2);
}
~DistanceMatrix()
{
mDistances.clear();
}
// Copy constructor and assignment
DistanceMatrix(const DistanceMatrix& aDistMat) : mDummy(0.0F)
{
mNumElements = aDistMat.size();
if(mNumElements == 0) return;
mDistances.reserve((mNumElements*mNumElements-mNumElements)/2);
mDistances = aDistMat.getVector();
}
DistanceMatrix& operator=(const DistanceMatrix& aDistMat)
{
// Make sure not same object
if(this != &aDistMat)
{
mNumElements = aDistMat.size();
if(mNumElements != 0)
{
mDistances.reserve((mNumElements*mNumElements-mNumElements)/2);
mDistances = aDistMat.getVector();
}
mDummy = 0.0F;
}
// Return ref for multiple assignment
return *this;
}
void setVector(const std::vector<float>& aDistVect, size_t aNumElements)
{
mNumElements = aNumElements;
if(mNumElements == 0) return;
mDistances = aDistVect;
}
void resize(size_t aNumElements)
{
mNumElements = aNumElements;
if(aNumElements == 0) return;
mDistances.reserve((aNumElements*aNumElements-aNumElements)/2);
mDistances.resize((aNumElements*aNumElements-aNumElements)/2);
}
const std::vector<float>& getVector(void) const
{
return mDistances;
}
size_t size(void) const
{
return mNumElements;
}
bool empty(void) const
{
return mNumElements == 0;
}
float min() const
{
return *std::min_element(mDistances.begin(), mDistances.end());
}
float max() const
{
return *std::max_element(mDistances.begin(), mDistances.end());
}
float& operator() (size_t aRow, size_t aCol)
{
if(aRow >= mNumElements) throw cfp::CrystalFpFatal("Invalid aRow in DistanceMatrix");
if(aCol >= mNumElements) throw cfp::CrystalFpFatal("Invalid aCol in DistanceMatrix");
if(aRow == aCol) {mDummy = 0.0F; return mDummy;}
size_t idx;
if(aRow < aCol)
{
idx = aRow*(2*mNumElements-aRow-1)/2 + aCol - aRow - 1;
}
else
{
idx = aCol*(2*mNumElements-aCol-1)/2 + aRow - aCol - 1;
}
return mDistances[idx];
}
float operator() (size_t aRow, size_t aCol) const
{
if(aRow >= mNumElements) throw cfp::CrystalFpFatal("Invalid aRow in DistanceMatrix");
if(aCol >= mNumElements) throw cfp::CrystalFpFatal("Invalid aCol in DistanceMatrix");
if(aRow == aCol) return 0.0F;
size_t idx;
if(aRow < aCol)
{
idx = aRow*(2*mNumElements-aRow-1)/2 + aCol - aRow - 1;
}
else
{
idx = aCol*(2*mNumElements-aCol-1)/2 + aRow - aCol - 1;
}
return mDistances[idx];
}
void resizeToIncluded(const std::vector<bool>& aIncluded)
{
size_t i;
unsigned int j;
// Obtain the new size
size_t new_size = 0;
for(i=0; i < aIncluded.size(); ++i) if(aIncluded[i]) ++new_size;
// Check
if(new_size == mNumElements) return;
if(new_size > mNumElements) throw cfp::CrystalFpFatal("Resize attempted to a bigger distance matrix");
// Map old row/col to new ones
std::vector<unsigned int> map;
map.resize(mNumElements);
for(i=j=0; i < aIncluded.size(); ++i) if(aIncluded[i]) map[i] = j++;
// Temporary distance matrix
std::vector<float> new_distances;
new_distances.resize((new_size*new_size-new_size)/2);
// Remove unselected row/columns
for(size_t row=0; row < mNumElements-1; ++row)
{
if(!aIncluded[row]) continue;
for(size_t col=row+1; col < mNumElements; ++col)
{
if(!aIncluded[col]) continue;
size_t idx = row*(2*mNumElements-row-1)/2 + col - row - 1;
unsigned int nrow = map[row];
unsigned int ncol = map[col];
size_t nidx = nrow*(2*new_size-nrow-1)/2 + ncol - nrow - 1;
new_distances[nidx] = mDistances[idx];
}
}
// Update
mDistances = new_distances;
mNumElements = new_size;
}
void clear(void)
{
mDistances.clear();
mNumElements = 0;
}
void serialize(std::ofstream& aStream) const
{
unsigned int x = static_cast<unsigned int>(mDistances.size());
aStream.write((char *)&x, sizeof(unsigned int));
if(x) aStream.write((char *)&mDistances[0], sizeof(float)*x);
x = static_cast<unsigned int>(mNumElements);
aStream.write((char *)&x, sizeof(unsigned int));
}
void unserialize(std::ifstream& aStream)
{
unsigned int x;
aStream.read((char *)&x, sizeof(unsigned int));
mDistances.resize(static_cast<size_t>(x));
if(x) aStream.read((char *)&mDistances[0], sizeof(float)*x);
aStream.read((char *)&x, sizeof(unsigned int));
mNumElements = x;
}
private:
std::vector<float> mDistances;
size_t mNumElements;
float mDummy;
};
#endif