[computer-go] C++ pointer question
Berk Ozbozkurt
ozbozkurt at fisek.com.tr
Tue Aug 8 12:50:31 PDT 2006
Peter Drake wrote:
> Okay, I've got this method in my BitBoard class:
>
> void dilate() // Expand to include all adjacent points
> {
> BitBoard temp;
> temp.bits[0] = bits[0] | (bits[0] << 1) | (bits[0] >> 1) | bits[1];
> temp.bits[board_width - 1] = bits[board_width - 1] | (bits
> [board_width - 1] << 1)
> | (bits[board_width - 1] >> 1) | bits[board_width - 2];
> for (unsigned row = 1; row < board_width - 1; row++) {
> temp.bits[row] = bits[row] | (bits[row] << 1) | (bits[row] >>
> 1) | bits[row - 1] | bits[row + 1];
> }
> for (unsigned row = 0; row < board_width; row++) {
> bits[row] = temp.bits[row];
> }
> }
>
> Is there some way to avoid that second for loop? I tried "this =
> temp" and "*this = *temp", but the compiler isn't happy with either.
>
It has been quite a while since I used C++, but IIRC (and assuming you
have not overloaded [, ], << and >> operators) you can store your data
(bits) in a dynamic array. In that case a simple assignment will work:
class Bitboard{
bits_type *bits;
Bitboard()
{
bits=new bits_type[ARRAY_SIZE];
....
}
~Bitboard()
{
delete[] bits;
....
}
void dilate() // Expand to include all adjacent points
{
bits_type *temp=new bits_type[ARRAY_SIZE];
temp.bits[0] = bits[0] | (bits[0] << 1) | (bits[0] >> 1) | bits[1];
...
delete[] bits;
bits=temp;
}
}
}
More information about the computer-go
mailing list