Range returning an array
Steven Schveighoffer
schveiguy at yahoo.com
Tue Apr 9 15:33:31 PDT 2013
On Tue, 09 Apr 2013 18:09:07 -0400, Joseph Rushton Wakeling
<joseph.wakeling at webdrake.net> wrote:
> On 04/09/2013 11:02 PM, Steven Schveighoffer wrote:
>> 3. For your specific situation, add lastFront():
>
> It's an interesting thought. I don't think it's ultimately the right
> way to go
> -- yes, my application rests strongly on finding the last value, but the
> problem
> is very simply that popFront kills the value _before_ finding out if the
> range
> is now empty.
Well here is another solution:
struct MySimulation(T)
{
T[] var;
T[] tmpvar;
T diff, convergence;
auto front() @property
{
return var;
}
bool empty() @property
{
return (diff < convergence);
}
void popFront()
{
tmpvar[] = var[];
// update values in var
// and calculate diff
if(empty)
{
var[] = tmpvar[]; // revert
var = null; // optional, detach from original array
}
}
}
You could also use alloca to avoid storing tmpvar as a struct member and
also avoid allocation.
-Steve
More information about the Digitalmars-d-learn
mailing list