struct AdjustableVal ( T = double )
{
this ( string name, T initVal, T maxVal, T minVal, T step, bool goUpForSafety )
{
this.name = name;
this.initVal = initVal;
this.curVal = initVal;
this.maxVal = maxVal;
this.minVal = minVal;
this.step = step;
this.goUpForSafety = goUpForSafety;
}
void GoSafer()
{
if (goUpForSafety)
{
curVal += step;
curVal = min( curVal, maxVal);
}
else
{
curVal -= step;
curVal = max( curVal, minVal);
}
writeln("Adjusted the value ", name, " : ", curVal, " : ", initVal );
}
void GoGreedy()
{
bool goUpForGreed = !goUpForSafety;
if ( goUpForGreed )
{
curVal += step;
curVal = min( curVal, maxVal);
}
else
{
curVal -= step;
curVal = max( curVal, minVal);
}
}
int opCmp(T s) const {
return s < curVal;
}
T opBinary(string op)(T rhs)
{
return mixin("rhs"~op~"curVal");
}
string name;
T initVal;
T curVal;
T maxVal;
T minVal;
T step;
bool goUpForSafety;
}
Yukardaki gibi bir yapıyı
AdjustableVal[] adjustableList; gibi bir dizi içinde nasıl üye eleman olarak kullanabilirim.
Şimdilik AdjustableVal!double[] olarak idare edeceğim. Ama integer olarak kullanma durumumda oluştu.
Interface olur, inheritance olur bir fikriniz varsa sevinirim.
Erdemdem
--
[ Bu gönderi, http://ddili.org/forum'dan dönüştürülmüştür. ]