There must be a better way

Emp empty at nomail.com
Thu Aug 3 17:40:44 PDT 2006


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