-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryVector.cpp
234 lines (202 loc) · 6.38 KB
/
BinaryVector.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
/*
* File: BinaryVector.cpp
* Author: honza
*
* Created on 7. říjen 2012, 15:54
*/
#include "BinaryVector.h"
//helping free vector - used as a nullObject
BinaryVector::BinaryVector() {
this->actualArray = new bool[1];
this->newArray = new bool[1];
this->startConfiguration = new bool[1];
}
BinaryVector::BinaryVector(int width, int height) {
this->width = width;
this->height = height;
this->actualArray = new bool[width * height];
this->newArray = new bool[width * height];
this->startConfiguration = new bool[width * height];
this->size = width*height;
}
BinaryVector::BinaryVector(BinaryVector* orig) {
this->width = orig->width;
this->height = orig->height;
this->size = orig->size;
this->actualArray = BinaryVector::copyArray(orig->actualArray,size);
this->newArray = BinaryVector::copyArray(orig->newArray,size);
this->startConfiguration = BinaryVector::copyArray(orig->startConfiguration,size);
}
/**
* Constructor that will copy the given array as a startConfiguration
* @param array
* @param width
* @param height
*/
BinaryVector::BinaryVector(bool* array, int width, int height) {
//TODO osetreni nespravneho pole, nesouhlasi vyska sirka
this->width = width;
this->height = height;
this->size = width*height;
this->startConfiguration = BinaryVector::copyArray(array,size);
this->actualArray = BinaryVector::copyArray(array,size);
this->newArray = new bool[width * height];
}
BinaryVector::~BinaryVector() {
delete actualArray;
delete newArray;
delete startConfiguration;
}
void BinaryVector::printVector() {
cout << "Vector width:" << width << ", height:" << height
<< ", hamWeight:" << BinaryVector::hammingStartArrayWeight(this) << endl;
int index = 0, index2 = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cout << startConfiguration[index] << "|";
index++;
}
cout << " => ";
for (int j = 0; j < width; j++) {
cout << actualArray[index2] << "|";
index2++;
}
cout << endl;
}
}
int BinaryVector::getCountOfAliveNeighbours(int index) {
int count = 0;
int leftMiddleIndex = index - 1;
int leftTopIndex = index - width - 1;
int rightTopIndex = index - width + 1;
int rightMiddleIndex = index + 1;
int leftBottomIndex = index + width - 1;
int rightBottomIndex = index + width + 1;
int topMiddleIndex = index - width;
int bottomMiddleIndex = index + width;
bool hasTop = true, hasBottom = true;
//first line
if (index - width < 0) {
hasTop = false;
}
//last line
if (index + width >= (width * height)) {
hasBottom = false;
}
//has a left neigbhours
if (index % width != 0) {
if (hasTop) {
count += actualArray[leftTopIndex] ? 1 : 0;
};
count += actualArray[leftMiddleIndex] ? 1 : 0;
if (hasBottom) {
count += actualArray[leftBottomIndex] ? 1 : 0;
};
}
//has right neigbhours
if ((index + 1) % width != 0) {
if (hasTop) {
count += actualArray[rightTopIndex] ? 1 : 0;
};
count += actualArray[rightMiddleIndex] ? 1 : 0;
if (hasBottom) {
count += actualArray[rightBottomIndex] ? 1 : 0;
};
}
if (hasTop) {
count += actualArray[topMiddleIndex] ? 1 : 0;
}
if (hasBottom) {
count += actualArray[bottomMiddleIndex] ? 1 : 0;
}
return count;
}
void BinaryVector::writeNewValueOfCellToNextIteration(int index, int liveNeigbhours) {
if (actualArray[index] == true) {//if is alive then stay alive with 3 or 2 neibhours
newArray[index] = (liveNeigbhours == 2 || liveNeigbhours == 3);
} else {//if is dead, then come back to live with 3 neigbhours
newArray[index] = liveNeigbhours == 3;
}
}
void BinaryVector::nextIteration() {
for (int i = 0; i < size; i++) {
writeNewValueOfCellToNextIteration(i, getCountOfAliveNeighbours(i));
}
bool* oldreference = actualArray;
actualArray = newArray;
newArray = oldreference;
}
void BinaryVector::iterateForQTimes(int q) {
for (int i = 0; i < q; i++) {
nextIteration();
}
}
//STATIC-----------------------------------------------------------------------
bool* BinaryVector::copyArray(bool* b,int size) {
bool* newb = new bool[size];
for (int i = 0; i < size; i++) {
newb[i] = b[i];
}
return newb;
}
void BinaryVector::printArray(bool* b,int size){
for (int i = 0; i < size; i++) {
cout<< b[i]<<",";
}
}
bool* BinaryVector::getEmptyArray(int size) {
bool* newb = new bool[size];
for (int i = 0; i < size; i++) {
newb[i] = false;
}
return newb;
}
int BinaryVector::hammingActualArrayDistance(BinaryVector* first, BinaryVector* second) {
//TODO osetrit rozdilnost velikosti vektoru - vyhozeni vyjimky
int distance = 0;
for (int i = 0; i < first->getSize(); i++) {
if (first->getActualArray()[i] != second->getActualArray()[i]) {
distance++;
}
}
return distance;
}
int BinaryVector::hammingStartArrayWeight(BinaryVector* vector) {
int weight = 0;
for (int i = 0; i < vector->getSize(); i++) {
if (vector->getStartConfiguration()[i] == true) {
weight++;
}
}
return weight;
}
bool BinaryVector::equals(BinaryVector* first, BinaryVector* second) {
for (int i = 0; i < first->getSize(); i++) {
if (first->getActualArray()[i] != second->getActualArray()[i]) {
return false;
}
}
return true;
}
int BinaryVector::hammingDistance(bool* array, bool* config,int size){
int weight = 0;
for (int i = 0; i < size; i++) {
if (array[i]!=config[i]) {
weight++;
}
}
return weight;
}
//test purposes****************************************************************
bool* BinaryVector::getActualArray() {
return actualArray;
}
bool* BinaryVector::getNewArray() {
return newArray;
}
int BinaryVector::getSize() {
return size;
}
bool* BinaryVector::getStartConfiguration() {
return startConfiguration;
}