/
Cluster.cpp
429 lines (350 loc) · 9.12 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
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
#include "Cluster.h"
namespace Clustering {
Cluster::Cluster(const Cluster &cluster)
{
size = cluster.size;
//case cluster is empty
if (cluster.points == NULL) {
points = NULL;
return;
}
LNodePtr node;
LNodePtr cPtr = cluster.points;
LNodePtr lastPtr = nullptr;
for (cPtr; cPtr != NULL; cPtr = cPtr->next) {
node = new LNode;
if (lastPtr != NULL)
lastPtr->next = node;
else if (cPtr == cluster.points) { //first run only
points = node;
}
node->p = cPtr->p; //copy point pointer
lastPtr = node;
}
}
Cluster & Cluster::operator=(const Cluster &cluster)
{
if (cluster == *this)
return *this;
clear(); //free up memory
size = cluster.size;
//case: cluster is empty
if (cluster.points == NULL) {
points = NULL;
return *this;
}
LNodePtr node;
LNodePtr cPtr = cluster.points;
LNodePtr lastPtr = nullptr;
for (int i = 0; i < cluster.size; i++) {
node = new LNode;
if (lastPtr != NULL)
lastPtr->next = node;
else if (cPtr == cluster.points) { //first run only
points = node;
}
node->p = cPtr->p; //copy point pointer
cPtr = cPtr->next;
lastPtr = node;
}
return *this;
}
Cluster::~Cluster()
{
if (points == NULL) return; //empty cluster = nothing to delete
LNodePtr current = points; //the current node to delete
LNodePtr next = nullptr; //the address of the next node
//delete nodes
for (current; current != NULL; current = next) {
next = current->next;
delete current;
}
}
void Cluster::add(const PointPtr &p)
{
if (points != NULL)
assert(p->getDims() == points->p->getDims()); //make sure dimensions match
if (find(p) != NULL) { //make sure p is not already in cluster
return;
}
size++;
LNodePtr node = new LNode; // Dynamically allocate new node
LNodePtr ptr = points;
node->p = p;
if (points == NULL) { //case: empty cluster
points = node;
node->next = nullptr;
return;
}
if (*node->p < *points->p) { //case: new point is smallest point
node->next = points;
points = node;
return;
}
LNodePtr last = nullptr;
while ( *p > *ptr->p) { //put in lexographic order
last = ptr;
if (ptr->next == NULL) {//if end of list
node->next = nullptr;
last->next = node;
return;
}
ptr = ptr->next;
}
while (*p == *ptr->p && p > ptr->p) { //if points are equal sort by memory address
last = ptr; //for reasons of comparison
if (ptr->next == NULL) {
node->next = nullptr;
last->next = node;
return;
}
ptr = ptr->next;
}
node->next = ptr;
if (last != nullptr)
last->next = node;
else
points = node;
}
const PointPtr & Cluster::remove(const PointPtr &point)
{
LNodePtr lastNode = find(point); //node before one to be deleted
if (lastNode != NULL) { //if point was found
size--;
LNodePtr xNode = lastNode->next; //node to be removed
if (lastNode->next == points) {
points = xNode->next;
delete lastNode; //delete node created by find
}
else if (xNode->next == NULL)
lastNode->next = nullptr;
else
lastNode->next = xNode->next; //remove node from list
PointPtr thePoint = xNode->p;
delete xNode;
return thePoint;
}
else
std::cout << "The point was not found in the cluster" << std::endl;
}
void Cluster::removeAfter(LNodePtr lastNode)
{
if (lastNode == NULL)
return;
size--;
LNodePtr xNode = lastNode->next; // node to be removed
LNodePtr nextNode = lastNode->next->next;
if (lastNode->next == points)
points = nextNode;
else
lastNode->next = nextNode;
delete xNode;
}
void Cluster::clear()
{
LNodePtr node = points;
LNodePtr nextNode = nullptr;
for (node; node != NULL; node = nextNode) {
nextNode = node->next;
delete node;
}
size = 0;
points = nullptr;
}
int Cluster::numLines(std::istream & in)
{
//save position of file pointer
int pos = in.tellg();
// account for possible eof bit
in.clear();
// move file pointer to beginning of file
in.seekg(0);
std::string aString; //holds unused information
int lines = 0; //counts lines
while (getline(in, aString))
lines++;
// clear eof bit
in.clear();
// recover previous position in file
in.seekg(pos);
return lines;
}
const LNodePtr Cluster::find(PointPtr point) const
{
if (points == NULL)
return NULL;
LNodePtr node = points;
if (node->p == point) { // case: point is found in first node
LNodePtr newNode = new LNode; // create new node to point to it
newNode->next = node;
return newNode;
}
int count = 0;
while(true) {
if (node->next == NULL) // case: point not found
return NULL;
if (node->next->p == point) // case: point found
return node;
node = node->next;
}
}
const LNodePtr Cluster::find(Point point) const
{
if (points == NULL)
return NULL;
LNodePtr node = points;
if (*node->p == point) { // case: point is found in first node
LNodePtr newNode = new LNode; // create new node to point to it
newNode->next = node;
return newNode;
}
while (true) {
if (node->next == NULL) // case: point not found
return NULL;
if (*node->next->p == point) // case: point found
return node;
node = node->next;
}
}
std::ostream & operator<<(std::ostream &output, const Cluster &cluster) {
LNodePtr ptr = cluster.points;
if (cluster.points == NULL)
output << "The cluster is empty" << std::endl;
else {
int count = 1;
for (ptr; ptr != NULL; ptr = ptr->next) {
output << *ptr->p;
count++;
}
}
return output;
}
std::istream & operator>>(std::istream &in, Cluster &cluster)
{
// get number of points coming in
int numPoints = Cluster::numLines(in);
PointPtr point = nullptr; //points to a point
std::string lineString; // holds line from input file
//convert to stringStream
for (int i = 0; i < numPoints; i++) {
getline(in, lineString);
std::stringstream line(lineString);
point = new Point(Point::getInFileDim(line));
line >> *point;
cluster.add(point);
}
return in;
}
const Cluster operator+(const Cluster &LHS, const Cluster &RHS)
{
Cluster cluster; //cluster to be returned
Cluster difference1 = LHS - RHS; //LHS unshared points
Cluster difference2 = RHS - LHS; //RHS unshared Points
Cluster shared = LHS - difference1; //points shared by LHS and RHS
// add LHS unshared points
LNodePtr node = difference1.points;
for (node; node != NULL; node = node->next)
cluster.add(node->p);
//add RHS unshared points
node = difference2.points;
for (node; node != NULL; node = node->next)
cluster.add(node->p);
//add shared points
node = shared.points;
for (node; node != NULL; node = node->next)
cluster.add(node->p);
return cluster;
}
const Cluster operator-(const Cluster &LHS, const Cluster &RHS)
{
Cluster cluster(LHS);
LNodePtr leftNode = LHS.points;
for (int i = 0; i < LHS.size; i++) {
if(RHS.find(leftNode->p) != NULL)
cluster.remove(leftNode->p);
leftNode = leftNode->next;
}
return cluster;
}
bool operator==(const Cluster &LHS, const Cluster &RHS)
{
if (LHS.getSize() != RHS.getSize())
return false;
LNodePtr lNode = LHS.points;
LNodePtr rNode = RHS.points;
for (lNode; lNode != NULL; lNode = lNode->next) {
if (lNode->p != rNode->p)
return false;
rNode = rNode->next;
}
return true;
}
Cluster & operator+=(Cluster &LHS, const Cluster &RHS)
{
LHS = LHS + RHS;
return LHS;
}
Cluster & operator-=(Cluster &LHS, const Cluster &RHS)
{
LHS = LHS - RHS;
return LHS;
}
const Cluster operator+(const Cluster &cluster, const PointPtr Point)
{
Cluster c(cluster);
c.add(Point);
return c;
}
const Cluster operator+(const Cluster &cluster, const Point &point)
{
//dynamically allocate point object
PointPtr p = new Point(point);
Cluster c(cluster);
c.add(p);
return c;
}
const Cluster operator-(const Cluster &cluster, const Point &point)
{
Cluster newCluster(cluster);
LNodePtr node = cluster.find(point);
if (node == NULL)
std::cout << "Can't Remove: The point was not found" << std::endl;
while (node != NULL){
node = newCluster.find(point);
newCluster.removeAfter(node);
}
return newCluster;
}
const Cluster operator-(const Cluster &cluster, const PointPtr point)
{
Cluster newCluster(cluster);
LNodePtr node = cluster.find(point);
if (node == NULL)
std::cout << "Can't Remove: The point was not found" << std::endl;
while (node != NULL) {
node = newCluster.find(point);
newCluster.removeAfter(node);
}
return newCluster;
}
Cluster & operator+=(Cluster &cluster, const Point &point)
{
cluster = cluster + point;
return cluster;
}
Cluster & operator+=(Cluster &cluster, const PointPtr point)
{
cluster = cluster + point;
return cluster;
}
Cluster & operator-=(Cluster &cluster, const Point &point)
{
cluster = cluster - point;
return cluster;
}
Cluster & operator-=(Cluster &cluster, const PointPtr point)
{
cluster = cluster - *point;
return cluster;
}
}