-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.h
executable file
·325 lines (281 loc) · 9.33 KB
/
Block.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
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
/*!
\file Block.h
\brief Implementazione della classe gameoflife::Block
\author Andrea Zanelli
\date 09-03-2010
*/
#ifndef _BLOCK_H
#define _BLOCK_H 1
#include <iostream>
#include <cstring>
#include "Muesli.h"
#include "Vector.h"
namespace gameoflife {
/*!
\class Block
\brief Un blocco in cui suddividere la matrice del gioco della vita.
Rappresenta un blocco della matrice del gioco della vita, utilizzato per
l'esecuzione in parallelo. Implementa l'interfaccia MSL_Serializable per
poter essere utilizzato come input e output negli skeleton della libreria
Muesli.
*/
class Block : public MSL_Serializable {
// PRIVATE MEMBERS
private:
unsigned int _n; // numero del blocco
unsigned int _pos; // posizione del blocco nella matrice
unsigned int _rows; // numero di righe
unsigned int _cols; // numero di colone
bool** _slice; // _slice[_rows][_cols]
Vector* _leftv; // _leftv[_rows]
Vector* _rightv; // _rightv[_rows]
// PRIVATE METHODS
private:
/*
Elimina la matrice _slice
*/
void deleteSlice() {
for(int i=0; i < _rows; ++i)
delete[] _slice[i];
delete[] _slice;
return;
} // end of method deleteBlock
/*
Restituisce il numero di vicini vivi della cella con indice i,j
*/
int getNeighborsCount(unsigned int i, unsigned int j) const {
unsigned int count = 0;
unsigned int row;
// Vicini nella riga sopra (row := i-1)
row = (i == 0 ? _rows-1 : i-1);
if(j == 0) { if(_leftv->get(row)) count++; }
else { if(_slice[row][j-1]) count++; }
if(_slice[row][j]) count++;
if(j == _cols-1) { if(_rightv->get(row)) count++; }
else { if(_slice[row][j+1]) count++; }
// Vicini sulla stessa riga (row := i)
if(j == 0) { if(_leftv->get(i)) count++; }
else { if(_slice[i][j-1]) count++; }
if(j == _cols-1) { if(_rightv->get(i)) count++; }
else { if(_slice[i][j+1]) count++; }
// Vicini nella riga sotto (row := i+1)
row = (i == _rows-1 ? 0 : i+1);
if(j == 0) { if(_leftv->get(row)) count++; }
else { if(_slice[row][j-1]) count++; }
if(_slice[row][j]) count++;
if(j == _cols-1) { if(_rightv->get(row)) count++; }
else { if(_slice[row][j+1]) count++; }
return count;
} // end of method getNeighborsCount
/*
Restituisce il valore della generazione successiva della cella i,j.
*/
bool getNextValue(unsigned int i, unsigned int j) const {
int neighbors = getNeighborsCount(i,j);
return _slice[i][j] ?
(neighbors == 2 || neighbors == 3) :
(neighbors == 3) ;
} // end of method getNextValue
// PUBLIC METHODS
public:
/**
* Costruttore di default: costruisce un blocco vuoto.
*/
Block() : _n(0), _pos(0), _rows(0), _cols(0) { }
/**
* Costruisce un blocco a partire dalla matrice "matrix". Il blocco
* costruito ha come numero "n", dimensione "dim" (numero di colonne) e
* viene costruito a partire dalla colonna "pos" della matrice.
*/
Block(unsigned int n, unsigned int dim, unsigned int pos,
bool** matrix, unsigned int rows, unsigned int cols) :
_n(n), _pos(pos), _rows(rows), _cols(dim) {
_slice = new bool*[_rows];
_leftv = new Vector(_rows);
_rightv = new Vector(_rows);
unsigned int jleft = (pos == 0 ? cols-1 : pos-1);
unsigned int jright = (pos+dim == cols ? 0 : pos+dim);
for(unsigned int i=0; i < _rows; ++i) {
_leftv->set(i,matrix[i][jleft]);
_rightv->set(i,matrix[i][jright]);
_slice[i] = new bool[dim];
for(unsigned int j=0; j < dim; ++j)
_slice[i][j] = matrix[i][pos+j];
} // end for i
return;
} // end of constructor
/**
* Distruttore
*/
virtual ~Block() {
deleteSlice();
delete _leftv;
delete _rightv;
} // end of distructor
/**
* Restituisce il numero del blocco (n).
*/
unsigned int getN() const {
return _n;
} // end of method get
/**
* Restituisce la posizione del blocco nella matrice.
*/
unsigned int getPosition() const {
return _pos;
} // end of method get
/**
* Restituisce il numero di righe del blocco.
*/
unsigned int getRows() const {
return _rows;
} // end of method getRows();
/**
* Restituisce il numero di colonne del blocco.
*/
unsigned int getColumns() const {
return _cols;
} // end of method getColumns();
/**
* Restituisce l'elemento nella riga i - colonna j.
*/
bool& get(unsigned int i, unsigned int j) {
return _slice[i][j];
} // end of method get
/**
* Restituisce l'elemento nella riga i - colonna j.
*/
const bool& get(unsigned int i, unsigned int j) const {
return _slice[i][j];
} // end of method get
/**
* Restituisce un puntatore ad un nuovo oggetto di tipo Vector che contiene
* una copia degli elementi della prima colonna del blocco (il suo bordo
* sinistro).
*/
Vector* getLeftBoundary() const {
Vector* leftb = new Vector(_rows);
for(int i=0; i < _rows; ++i)
leftb->set(i, _slice[i][0]);
return leftb;
} // end of method getLeftBoundary
/**
* Restituisce un puntatore ad un nuovo oggetto di tipo Vector che contiene
* una copia degli elementi dell'ultima colonna del blocco (il suo bordo
* destro).
*/
Vector* getRightBoundary() const {
Vector* rightb = new Vector(_rows);
for(int i=0; i < _rows; ++i)
rightb->set(i, _slice[i][_cols-1]);
return rightb;
} // end of method getRightBoundary
/**
* Restituisce il vettore sinistro.
*/
const Vector& getLeftVector() const {
return (*_leftv);
} // end of method getLeftVector
/**
* Restituisce il vettore destro.
*/
const Vector& getRightVector() const {
return (*_rightv);
} // end of method getRightVector
/**
* Cambia il vettore sinistro con quello passato come parametro.
*/
void setLeftVector(Vector* leftv) {
delete _leftv;
_leftv = leftv;
} // end of method setLeftVector
/**
* Cambia il vettore destro con quello passato come parametro.
*/
void setRightVector(Vector* rightv) {
delete _rightv;
_rightv = rightv;
} // end of method setRightVector
/**
* Esegue un'iterazione del gioco della vita sugli elementi del blocco.
*/
void compute() {
bool** newslice = new bool*[_rows];
for(int i=0; i < _rows; ++i) {
newslice[i] = new bool[_cols];
for(int j=0; j < _cols; ++j)
newslice[i][j] = getNextValue(i,j);
} // end for i
deleteSlice();
_slice = newslice;
return;
} // end of method compute
/** Override */
virtual inline int getSize() {
return sizeof(unsigned int) + // _n
sizeof(unsigned int) + // _pos
sizeof(unsigned int) + // _rows
sizeof(unsigned int) + // _cols
sizeof(bool)*_rows*_cols + // _slice
_leftv->getSize() + // _leftv
_rightv->getSize(); // _rightv
} // end of method getSize
/** Override */
virtual void reduce(void* pBuffer, int bufferSize) {
typedef unsigned int uint;
uint* adr1 = (uint*) memcpy(pBuffer, &(_n), sizeof(uint));
adr1++;
memcpy(adr1, &(_pos), sizeof(uint));
adr1++;
memcpy(adr1, &(_rows), sizeof(uint));
adr1++;
memcpy(adr1, &(_cols), sizeof(uint));
adr1++;
bool* adr2 = (bool*) adr1;
for(int i = 0; i < _rows; ++i) {
memcpy(adr2, _slice[i], sizeof(bool)*_cols);
adr2 += _cols;
}
_leftv->reduce((void*)adr2, bufferSize);
unsigned char* adr3 = ((unsigned char*)adr2 + _leftv->getSize());
_rightv->reduce((void*)adr3, bufferSize);
return;
} // end of method reduce
/** Override */
virtual void expand(void* pBuffer, int bufferSize) {
unsigned int* adr1 = (unsigned int*) pBuffer;
_n = *(adr1++);
_pos = *(adr1++);
_rows = *(adr1++);
_cols = *(adr1++);
bool* adr2 = (bool*) adr1;
_slice = new bool*[_rows];
for(int i=0; i < _rows; ++i) {
_slice[i] = new bool[_cols];
for(int j=0; j < _cols; ++j)
_slice[i][j] = *(adr2++);
}
_leftv = new Vector();
_leftv->expand((void*)adr2, bufferSize);
unsigned char* adr3 = ((unsigned char*)adr2 + _leftv->getSize());
_rightv = new Vector();
_rightv->expand((void*)adr3, bufferSize);
return;
} // end of method expand
}; // end of class Block
/**
* Operatore di output <<: stampa il blocco rappresentando con "[ ]" le celle
* morte e con "[*]" le celle vive, separando i vettori con uno spazio rispetto
* alle celle del blocco.
*/
std::ostream& operator<<(std::ostream& out, const Block& b) {
for(unsigned int i = 0; i < b.getRows(); ++i) {
out <<(b.getLeftVector().get(i) ? "[*] " : "[ ] ");
for(unsigned int j = 0; j < b.getColumns(); ++j)
out <<(b.get(i,j) ? "[*]" : "[ ]");
out <<(b.getRightVector().get(i) ? " [*]" : " [ ]") <<std::endl;
}
return out;
} // end of function operator<<
} // end of namespace gameoflife
#endif // _BLOCK_H