forked from ivogeorg/ucd-csci2312-pa3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cluster.cpp
342 lines (335 loc) · 10 KB
/
Cluster.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
//
// Created by Mario on 3/28/2016.
//
#include <limits>
#include <assert.h>
#include <sstream>
#include <algorithm>
#include "Cluster.h"
#include "Point.h"
#include "Exceptions.h"
namespace Clustering {
unsigned int Cluster::__idGenerator = 0;
const char Cluster::POINT_CLUSTER_ID_DELIM = ':';
LNode::LNode(const Point &p, LNodePtr n) : point(p) {
next = n;
}
Cluster::Centroid::Centroid(unsigned int d, const Cluster &c) : __p(d), __c(c) {
__dimensions = d;
__valid = false;
}
/* Cluster::Centroid::Centroid(const Centroid ¢) : __p(cent.__p), __c(cent.__c){
__dimensions = cent.__dimensions;
__valid = cent.__dimensions;
}*/
const Point Cluster::Centroid::get() const {
return __p;
}
void Cluster::Centroid::set(const Point &p) {
__p = p;
__valid = true;
}
bool Cluster::Centroid::isValid() const {
return __valid;
}
void Cluster::Centroid::setValid(bool valid) {
__valid = valid;
}
void Cluster::Centroid::compute() {
int size = __c.__size, dim = __dimensions;
double average, total;
if(size == 1) {
__p = __c[0];
}
else if (size > 0) {
for(int i = 0; i < dim; i++) {
for (int k = 0; k < size; k++) {
Point p(__c[k]);
total += p[i];
}
average = total / size;
__p.setValue(i, average);
total = 0;
}
}
else {
toInfinity();
}
setValid(true);
//std::cout << isValid() << " should be 1" << std::endl;
}
bool Cluster::Centroid::equal(const Point &p) const {
return __p == p;
}
void Cluster::Centroid::toInfinity() {
for(int i = 0; i < __p.getDims(); i++) {
__p[i] = std::numeric_limits<double>::max();
}
}
//constructor
Cluster::Cluster(unsigned int d) : centroid(d, *this) {
__id = __idGenerator;
__idGenerator++;
__size = 0;
__points = nullptr;
__dimensionality = d;
}
//copy constructor
Cluster::Cluster(const Cluster &c) : centroid(c.__dimensionality, c) {
__id = c.__id;
__points = c.__points;
__dimensionality = c.__dimensionality;
__size = c.__size;
}
Cluster& Cluster::operator=(const Cluster &c) {
if(__dimensionality != c.__dimensionality) {
throw DimensionalityMismatchEx(__dimensionality, c.__dimensionality);
}
__id = c.__id;
if(c.__points != nullptr) {
delete c.__points;
}
__points = c.__points;
__dimensionality = c.__dimensionality;
__size = c.__size;
return *this;
}
//destructor
Cluster::~Cluster() {
delete __points;
}
unsigned int Cluster::getSize() const {
return __size;
}
unsigned int Cluster::getId() const {
return __id;
}
unsigned int Cluster::getDimensionality() const {
return __dimensionality;
}
void Cluster::add(const Point &p) {
if(p.getDims() != __dimensionality) {
throw DimensionalityMismatchEx(p.getDims(), __dimensionality);
}
int iter = 0;
LNodePtr cursor = __points;
LNodePtr pCursor = nullptr;
LNodePtr point = new LNode(p, nullptr);
if(__points == nullptr) {
assert(__size == 0);
__points = point;
__points->next = nullptr;
__size++;
return;
}
else if(__points->next == nullptr) {
__points->next = point;
__size++;
return;
}
while(cursor != nullptr) {
pCursor = cursor;
cursor = cursor->next;
}
point->next = cursor;
pCursor->next = point;
centroid.setValid(false);
//std::cout << centroid.isValid() << " should be 0" << std::endl;
__size++;
}
const Point& Cluster::remove(const Point &p) {
if(p.getDims() != __dimensionality) {
throw DimensionalityMismatchEx(p.getDims(), __dimensionality);
}
LNodePtr cursor = __points;
LNodePtr pCursor = nullptr;
int i;
if(__points == nullptr) {
return p;
}
if(__points->point == p) {
cursor = __points->next;
pCursor = __points;
delete __points;
__points = cursor;
__size--;
return p;
}
else {
while (cursor->point != p) {
cursor = cursor->next;
i++;
}
cursor = cursor->next;
__points = cursor;
centroid.setValid(false);
//std::cout << centroid.isValid() << " should be 0" << std::endl;
__size--;
}
return p;
}
bool Cluster::contains(const Point &p) const {
if(p.getDims() != __dimensionality) {
throw DimensionalityMismatchEx(p.getDims(), __dimensionality);
}
LNodePtr cursor = __points;
if(__size == 0) {
assert(__points == nullptr);
return false;
}
if(__points->point == p) {
return true;
}
else {
for(cursor; cursor->next != nullptr; cursor = cursor->next) {
if(cursor->point == p) {
return true;
}
}
}
return false;
}
void Cluster::pickCentroids(unsigned int k, Point **pointArray) {
try {
if (k >= __size) {
for (int i = 0; i < k; i++) {
if (i < __size) {
*pointArray[i] = (*this)[i];
}
else {
for (int j = 0; j < pointArray[i]->getDims(); j++) {
pointArray[i][j] = std::numeric_limits<double>::max();
}
}
}
}
else {
double x = static_cast<double>(__size) / k;
for (int i = 0; i < k; i++) {
*pointArray[i] = (*this)[static_cast<int>(i * x)];
}
}
}
catch(ZeroDimensionsEx) {
std::cout << ZeroDimensionsEx();
}
}
const Point& Cluster::operator[](unsigned int u) const {
if(__size == 0) {
throw EmptyClusterEx();
}
if(u >= __size) {
throw OutOfBoundsEx(u, __size);
}
LNodePtr cursor = __points;
for(int i = 0; i < u; i++) {
cursor = cursor->next;
}
return cursor->point;
}
std::ostream &operator<<(std::ostream &os, const Cluster &c) {
for(int i = 0; i < c.__size; i++) {
os << c[i] << Cluster::POINT_CLUSTER_ID_DELIM << c.__id << std::endl;
}
return os;
}
std::istream &operator>>(std::istream &is, Cluster &c) {
unsigned int size;
std::string line;
while (std::getline(is, line)) {
if (line == "") {
break;
}
size = std::count(line.begin(), line.end(), Point::POINT_VALUE_DELIM);
std::stringstream ss(line);
Point p(size + 1);
ss >> p;
c.add(p);
}
return is;
}
bool operator==(const Cluster &lhs, const Cluster &rhs) {
if(lhs.__dimensionality != rhs.__dimensionality) {
throw DimensionalityMismatchEx(lhs.__dimensionality, rhs.__dimensionality);
}
if(lhs.__id != rhs.__id) {
return false;
}
if(lhs.__size != rhs.__size) {
return false;
}
for(int i = 0; i < lhs.__size; i++) {
if(lhs[i] == rhs[i]) {
break;
}
else {
return false;
}
}
return true;
}
bool operator!=(const Cluster &lhs, const Cluster &rhs) {
return !(lhs == rhs);
}
Cluster& Cluster::operator+=(const Point &rhs) {
if(this->contains(rhs)) {
return *this;
}
add(rhs);
return *this;
}
Cluster& Cluster::operator-=(const Point &rhs) {
if(!this->contains(rhs)) {
return *this;
}
remove(rhs);
return *this;
}
Cluster& Cluster::operator+=(const Cluster &rhs) {
for(LNodePtr cursor = rhs.__points; cursor != nullptr; cursor = cursor->next) {
if(!this->contains(cursor->point)) {
this->add(cursor->point);
}
}
return *this;
}
Cluster& Cluster::operator-=(const Cluster &rhs) {
for(LNodePtr cursor = rhs.__points; cursor != nullptr; cursor = cursor->next) {
if(this->contains(cursor->point)) {
this->remove(cursor->point);
}
}
return *this;
}
const Cluster operator+(const Cluster &lhs, const Cluster &rhs) {
Cluster sum(lhs);
sum += rhs;
return sum;
}
const Cluster operator-(const Cluster &lhs, const Cluster &rhs) {
Cluster difference(rhs);
difference -= lhs;
return difference;
}
const Cluster operator+(const Cluster &lhs, const Point &rhs) {
if(lhs.__dimensionality != rhs.getDims()) {
throw DimensionalityMismatchEx(lhs.__dimensionality, rhs.getDims());
}
Cluster sum(lhs);
sum += rhs;
return sum;
}
const Cluster operator-(const Cluster &lhs, const Point &rhs) {
if(lhs.__dimensionality != rhs.getDims()) {
throw DimensionalityMismatchEx(lhs.__dimensionality, rhs.getDims());
}
Cluster difference(lhs);
difference -= rhs;
return difference;
}
Cluster::Move::Move(const Point &p, Cluster &from, Cluster &to) : __p(p), __from(from), __to(to) {}
void Cluster::Move::perform() {
__from.remove(__p);
__to.add(__p);
}
}