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
|
size_t index_slice( size_t a, size_t b = 0 /* no 'c', since that specifies a single element */ ) {
/*
The result is:
(size of first dimention slice) * (index into first dimension)
plus
(size of second dimention slice) * (index into second dimension)
Remember that the total size of a dimension's elements is the product of the
sizes of all lower dimensions.
*/
return (L*L*a) + (L*b);
}
void swap_a( bool* bit, size_t a, size_t b ) {
/*
Notice how, as argument, we take a simple pointer to the first element:
bit[0][0][0]
The neat thing here is that, with the downgraded pointer, we can refer to it as
bit[0]
or even more simply, just
*bit
Keep in mind that this time, the arguments 'a' and 'b' are both indices into the first
dimension. (Unlike in the above function, where 'a' was the index of the first and 'b'
was the index of the second.)
*/
// Another way to get the length of a dimension
size_t len = index_slice( a + 1 ) - index_slice( a );
// Now for our swaps
// First, we'll get pointers to the first element in each slice we wish to swap
bool* bita = bit + index_slice( a ); // same as "&(bit[a][0][0])"
bool* bitb = bit + index_slice( b );
// Now, we swap each element in the slices
while (len--) {
bool temp = *bita;
*bita++ = *bitb;
*bitb++ = temp;
}
}
void swap_b( bool* bit, size_t a1, size_t a2, size_t b1, size_t b2 ) {
/*
Here it is again, but swaping the second dimension instead of the first.
*/
size_t len = index_slice( a1, a2 + 1 ) - index_slice( a1, a2 );
bool* bita = bit + index_slice( a1, a2 );
bool* bitb = bit + index_slice( b1, b2 );
while (len--) {
bool temp = *bita;
*bita++ = *bitb;
*bitb++ = temp;
}
}
|