[computer-go] C++ pointer question
Peter Drake
drake at lclark.edu
Tue Aug 8 13:14:50 PDT 2006
This is an even better solution -- thanks!
Peter Drake
Assistant Professor of Computer Science
Lewis & Clark College
http://www.lclark.edu/~drake/
On Aug 8, 2006, at 12:52 PM, Dave Denholm wrote:
> Peter Drake <drake at lclark.edu> writes:
>
>
> (not cc-ing the list, but feel free to quote anything in a followup to
> the list if you want)
>
>
>> 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.
>>
>
> If I understand your code correctly, I think you can eliminate the
> temporary array entirely... you just need to preserve one value in a
> temporary scalar across the loop.
>
> unsigned prev = 0; // within loop, holds old value of bits[row-1]
> for (unsigned row = 0; row < board_width - 1; row++) {
> unsigned new_value =
> bits[row] |
> (bits[row] << 1) |
> (bits[row] >> 1) |
> prev | // preserved value of bits[row - 1], or 0 for row
> == 0
> bits[row + 1];
> prev = bits[row];
> bits[row] = new_value;
> }
>
> bits[board_width-1] = ...
>
> (loop handles [0] for free by initialising prev to 0)
>
> bear in mind I'm a c programmer, not a c++ programmer, so I may have
> missed some subtlety. But I assume bits[] is just a scalar array
>
>
> dd
> --
> Dave Denholm <ddenholm at esmertec.com> http://
> www.esmertec.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://computer-go.org/pipermail/computer-go/attachments/20060808/3cfaa86e/attachment.htm
More information about the computer-go
mailing list