There must be a better way
Unknown W. Brackets
unknown at simplemachines.org
Thu Aug 3 22:04:17 PDT 2006
I guess, then, you'd have block with data inside it, as you do now, but
data would have to be a class. It would have to know about its parent,
block.
Then you would be able to do something like that.
However, if you can settle for:
type = block.data(wrap(0, x), wrap(1, y)).type;
Then you don't need a class to proxy things. But, is the call to wrap
mandatory? It looks like you really want something like below...
Let me note that D, when it sees this:
x.y(z);
Will try:
y(x, z);
If the function is available. That's what I'm using (abusing?) here.
data_t data(whatever_block_is_t block, uint x, uint y)
{
return return block.realData[wrap(x, block.maxX)][wrap(y, block.maxY)];
}
uint wrap(uint value, uint max)
{
if (value >= max)
return value % max;
else if (value < 0)
return max + (value % max);
else
return value;
}
So then you'd do:
type = block.data(x, y).type;
Assuming you always want the x and why wrapped. If you didn't, you
could still do:
type = block.realData[x][y].type;
-[Unknown]
> I've been looking through your post but kind quite grasp how I should prog
> like that... :/
> I want to do stuff like this:
> (I hope it is a bit more clear)
>
> type=block.data[wrap(0,x)][wrap(1,y)].type;
>
> with:
> uint wrap(uint axis, int value)
> {
> int max=1;
> if (axis==1) max=block.maxX; //variable
> if (axis==0) max=block.maxY; //same :)
>
> if(value >= max){
> value %= max;
> }else if (value < 0){
> value = max + (value % max);
> if (value == max) value = 0;
> }
>
> return value;
> }
>
>> I'm not clear on where your going, but I like to keep things simpler and
>> thus more maintainable. Instead of:
>>
>> array[wrap(something[var].maxX,currentX)][wrap(something[var].maxY,currentY)];
>>
>> I would probably prefer...
>>
>> whatever_t getWrapped(whatever_t[][] array, int x, int y, int var)
>> {
>> return array[wrap(something[var].maxX, x)][wrap(something[var].maxY, y)];
>> }
>>
>> Then you'd do:
>>
>> array.getWrapped(currentX, currentY, var);
>>
>> Which would seem much easier, and should be optimized out the same with
>> inlining. But this might not be practical depending on what "something"
>> is (I'm guessing here it's a lookup or something.)
>>
>> Also, fwiw, I use inout all the time. I think there are specific design
>> patterns and code paths with which it makes complete sense. Example:
>>
>> // Attempt to bring item to the top/head of the linked list.
>> if (!bringToTop(linked_list, item))
>> writefln("Uh oh, %s was not found!", item.toString());
>>
>> I don't think it's ambiguous that linked_list might be modified. Just my
>> opinion. I might prefer "linked_list.bringToTop(item)" if it made sense,
>> though (since that's even harder to misunderstand.)
>>
>> -[Unknown]
>>
>>
>>> Thanks for the maths :)
>>> The maximum are not constant and writing out the place they live would
>>> yield to something like:
>>>
>>> array[wrap(something[var].maxX,currentX)][wrap(something[var].maxY,currentY)];
>>>
>>> So the int was a bit of a hack... sorry :)
>>>
>>> About the inout:
>>> How would you do something like this?
>>>
>>> bool something(inout structure struc ,int var){
>>> for (int i=0; i < struc.data[].length; i++){
>>> if(struc.data[i].count==0){
>>> struc.data[i].type=var;
>>> struc.data[i].count=30;
>>> return true;
>>> }
>>> }
>>> return false;
>>> }
>>>
>>>
>>>
>>>
>>>
>>>
>
More information about the Digitalmars-d-learn
mailing list