Refactor LinearSpec a bit, make it more difficult to leak the objective function.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38888 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Clemens Zeidler
2010-10-04 22:27:02 +00:00
parent c26897652c
commit cad0c434c7
6 changed files with 50 additions and 41 deletions
+6 -3
View File
@@ -79,9 +79,12 @@ public:
PenaltyFunction* AddPenaltyFunction(Variable* var, BList* xs, PenaltyFunction* AddPenaltyFunction(Variable* var, BList* xs,
BList* gs); BList* gs);
SummandList* ObjFunction(); SummandList* ObjectiveFunction();
void SetObjFunction(SummandList* summands); //! Caller takes ownership of the Summand's and the SummandList.
void UpdateObjFunction(); SummandList* ReplaceObjectiveFunction(
SummandList* objFunction);
void SetObjectiveFunction(SummandList* objFunction);
void UpdateObjectiveFunction();
ResultType Presolve(); ResultType Presolve();
void RemovePresolved(); void RemovePresolved();
+6 -14
View File
@@ -798,17 +798,13 @@ BALMLayout::_CalculateMinSize()
{ {
_UpdateAreaConstraints(); _UpdateAreaConstraints();
SummandList* oldObjFunction = fSolver.ObjFunction();
SummandList* newObjFunction = new SummandList(2); SummandList* newObjFunction = new SummandList(2);
newObjFunction->AddItem(new Summand(1.0, fRight)); newObjFunction->AddItem(new Summand(1.0, fRight));
newObjFunction->AddItem(new Summand(1.0, fBottom)); newObjFunction->AddItem(new Summand(1.0, fBottom));
fSolver.SetObjFunction(newObjFunction); SummandList* oldObjFunction = fSolver.ReplaceObjectiveFunction(
newObjFunction);
_SolveLayout(); _SolveLayout();
fSolver.SetObjFunction(oldObjFunction); fSolver.SetObjectiveFunction(oldObjFunction);
fSolver.UpdateObjFunction();
delete newObjFunction->ItemAt(0);
delete newObjFunction->ItemAt(1);
delete newObjFunction;
if (fSolver.Result() == UNBOUNDED) if (fSolver.Result() == UNBOUNDED)
return kMinSize; return kMinSize;
@@ -831,17 +827,13 @@ BALMLayout::_CalculateMaxSize()
{ {
_UpdateAreaConstraints(); _UpdateAreaConstraints();
SummandList* oldObjFunction = fSolver.ObjFunction();
SummandList* newObjFunction = new SummandList(2); SummandList* newObjFunction = new SummandList(2);
newObjFunction->AddItem(new Summand(-1.0, fRight)); newObjFunction->AddItem(new Summand(-1.0, fRight));
newObjFunction->AddItem(new Summand(-1.0, fBottom)); newObjFunction->AddItem(new Summand(-1.0, fBottom));
fSolver.SetObjFunction(newObjFunction); SummandList* oldObjFunction = fSolver.ReplaceObjectiveFunction(
newObjFunction);
_SolveLayout(); _SolveLayout();
fSolver.SetObjFunction(oldObjFunction); fSolver.SetObjectiveFunction(oldObjFunction);
fSolver.UpdateObjFunction();
delete newObjFunction->ItemAt(0);
delete newObjFunction->ItemAt(1);
delete newObjFunction;
if (fSolver.Result() == UNBOUNDED) if (fSolver.Result() == UNBOUNDED)
return kMaxSize; return kMaxSize;
+10 -10
View File
@@ -97,7 +97,7 @@ Constraint::UpdateLeftSide()
if (!set_rowex(fLS->fLP, this->Index(), i, &coeffs[0], &varIndexes[0])) if (!set_rowex(fLS->fLP, this->Index(), i, &coeffs[0], &varIndexes[0]))
STRACE(("Error in set_rowex.")); STRACE(("Error in set_rowex."));
fLS->UpdateObjFunction(); fLS->UpdateObjectiveFunction();
fLS->RemovePresolved(); fLS->RemovePresolved();
} }
@@ -266,9 +266,9 @@ Constraint::SetPenaltyNeg(double value)
if (fDNegObjSummand == NULL) { if (fDNegObjSummand == NULL) {
fDNegObjSummand = new Summand(value, new Variable(fLS)); fDNegObjSummand = new Summand(value, new Variable(fLS));
fLS->ObjFunction()->AddItem(fDNegObjSummand); fLS->ObjectiveFunction()->AddItem(fDNegObjSummand);
UpdateLeftSide(); UpdateLeftSide();
fLS->UpdateObjFunction(); fLS->UpdateObjectiveFunction();
return; return;
} }
@@ -276,7 +276,7 @@ Constraint::SetPenaltyNeg(double value)
return; return;
fDNegObjSummand->SetCoeff(value); fDNegObjSummand->SetCoeff(value);
fLS->UpdateObjFunction(); fLS->UpdateObjectiveFunction();
} }
@@ -308,9 +308,9 @@ Constraint::SetPenaltyPos(double value)
if (fDPosObjSummand == NULL) { if (fDPosObjSummand == NULL) {
fDPosObjSummand = new Summand(value, new Variable(fLS)); fDPosObjSummand = new Summand(value, new Variable(fLS));
fLS->ObjFunction()->AddItem(fDPosObjSummand); fLS->ObjectiveFunction()->AddItem(fDPosObjSummand);
UpdateLeftSide(); UpdateLeftSide();
fLS->UpdateObjFunction(); fLS->UpdateObjectiveFunction();
return; return;
} }
@@ -318,7 +318,7 @@ Constraint::SetPenaltyPos(double value)
return; return;
fDPosObjSummand->SetCoeff(value); fDPosObjSummand->SetCoeff(value);
fLS->UpdateObjFunction(); fLS->UpdateObjectiveFunction();
} }
@@ -428,13 +428,13 @@ Constraint::Invalidate()
fLeftSide = NULL; fLeftSide = NULL;
if (fDNegObjSummand) { if (fDNegObjSummand) {
fLS->ObjFunction()->RemoveItem(fDNegObjSummand); fLS->ObjectiveFunction()->RemoveItem(fDNegObjSummand);
delete fDNegObjSummand->Var(); delete fDNegObjSummand->Var();
delete fDNegObjSummand; delete fDNegObjSummand;
fDNegObjSummand = NULL; fDNegObjSummand = NULL;
} }
if (fDPosObjSummand) { if (fDPosObjSummand) {
fLS->ObjFunction()->RemoveItem(fDPosObjSummand); fLS->ObjectiveFunction()->RemoveItem(fDPosObjSummand);
delete fDPosObjSummand->Var(); delete fDPosObjSummand->Var();
delete fDPosObjSummand; delete fDPosObjSummand;
fDPosObjSummand = NULL; fDPosObjSummand = NULL;
@@ -528,7 +528,7 @@ Constraint::Constraint(LinearSpec* ls, SummandList* summands, OperatorType op,
: LE), rightSide)) : LE), rightSide))
STRACE(("Error in add_constraintex.")); STRACE(("Error in add_constraintex."));
fLS->UpdateObjFunction(); fLS->UpdateObjectiveFunction();
fLS->Constraints()->AddItem(this); fLS->Constraints()->AddItem(this);
} }
+21 -8
View File
@@ -342,26 +342,39 @@ LinearSpec::AddPenaltyFunction(Variable* var, BList* xs, BList* gs)
/** /**
* Gets the objective function. * Gets the objective function.
* *
* @return BList containing the objective function's summands * @return SummandList containing the objective function's summands
*/ */
SummandList* SummandList*
LinearSpec::ObjFunction() LinearSpec::ObjectiveFunction()
{ {
return fObjFunction; return fObjFunction;
} }
SummandList*
LinearSpec::ReplaceObjectiveFunction(SummandList* objFunction)
{
SummandList* list = fObjFunction;
fObjFunction = objFunction;
UpdateObjectiveFunction();
return list;
}
/** /**
* Sets a new objective function. * Sets a new objective function.
* The old objective function summands are NOT deleted.
* *
* @param summands BList containing the objective function's summands * @param summands SummandList containing the objective function's summands
*/ */
void void
LinearSpec::SetObjFunction(SummandList* summands) LinearSpec::SetObjectiveFunction(SummandList* objFunction)
{ {
fObjFunction = summands; for (int32 i = 0; i < fObjFunction->CountItems(); i++)
UpdateObjFunction(); delete (Summand*)fObjFunction->ItemAt(i);
delete fObjFunction;
fObjFunction = objFunction;
UpdateObjectiveFunction();
} }
@@ -370,7 +383,7 @@ LinearSpec::SetObjFunction(SummandList* summands)
* Must be called whenever the summands of the objective function are changed. * Must be called whenever the summands of the objective function are changed.
*/ */
void void
LinearSpec::UpdateObjFunction() LinearSpec::UpdateObjectiveFunction()
{ {
int32 size = fObjFunction->CountItems(); int32 size = fObjFunction->CountItems();
double coeffs[size]; double coeffs[size];
+2 -2
View File
@@ -45,10 +45,10 @@ PenaltyFunction::PenaltyFunction(LinearSpec* ls, Variable* var, BList* xs, BList
*(double*)(xs->ItemAt(i)))); *(double*)(xs->ItemAt(i))));
Summand* objSummand = new Summand(*(double*)(gs->ItemAt(i + 1)) - *(double*)(gs->ItemAt(i)), dPos); Summand* objSummand = new Summand(*(double*)(gs->ItemAt(i + 1)) - *(double*)(gs->ItemAt(i)), dPos);
ls->ObjFunction()->AddItem(objSummand); ls->ObjectiveFunction()->AddItem(objSummand);
fObjFunctionSummands->AddItem(objSummand); fObjFunctionSummands->AddItem(objSummand);
} }
ls->UpdateObjFunction(); ls->UpdateObjectiveFunction();
} }
+5 -4
View File
@@ -28,13 +28,9 @@ public:
textView1 = new BTextView("textView1"); textView1 = new BTextView("textView1");
textView1->SetText("5"); textView1->SetText("5");
button1->SetExplicitMinSize(BSize(0, 0));
button1->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED)); button1->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
button2->SetExplicitMinSize(BSize(0, 0));
button2->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED)); button2->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
button3->SetExplicitMinSize(BSize(0, 0));
button3->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED)); button3->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
button4->SetExplicitMinSize(BSize(0, 0));
button4->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED)); button4->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED));
// create a new BALMLayout and use it for this window // create a new BALMLayout and use it for this window
@@ -76,6 +72,11 @@ public:
a1->SetWidthAs(a3); a1->SetWidthAs(a3);
a1->SetHeightAs(a3); a1->SetHeightAs(a3);
*/ */
// test size limits
BSize min = layout->MinSize();
BSize max = layout->MaxSize();
SetSizeLimits(min.Width(), max.Width(), min.Height(), max.Height());
} }
private: private: