* Simplified and optimized a lot the "ToString()" debugging facilities.

* *::Index() is now const, thanks to the BList improvements.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34521 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-12-06 13:14:45 +00:00
parent b0850e9ba1
commit b8ec67f491
9 changed files with 404 additions and 423 deletions
+2 -2
View File
@@ -83,8 +83,8 @@ public:
bool AutoPreferredContentSize() const; bool AutoPreferredContentSize() const;
void SetAutoPreferredContentSize(bool value); void SetAutoPreferredContentSize(bool value);
BString* ToBString(); operator BString() const;
const char* ToString(); void GetString(BString& string) const;
Constraint* HasSameWidthAs(Area* area); Constraint* HasSameWidthAs(Area* area);
Constraint* HasSameHeightAs(Area* area); Constraint* HasSameHeightAs(Area* area);
+3 -3
View File
@@ -29,7 +29,7 @@ class LinearSpec;
class Constraint { class Constraint {
public: public:
int32 Index(); int32 Index() const;
BList* LeftSide(); BList* LeftSide();
void SetLeftSide(BList* summands); void SetLeftSide(BList* summands);
@@ -68,8 +68,8 @@ public:
bool IsValid(); bool IsValid();
void Invalidate(); void Invalidate();
BString* ToBString(); operator BString() const;
const char* ToString(); void GetString(BString& string) const;
~Constraint(); ~Constraint();
+3 -3
View File
@@ -90,7 +90,7 @@ public:
ResultType Presolve(); ResultType Presolve();
void RemovePresolved(); void RemovePresolved();
ResultType Solve(); ResultType Solve();
void Save(char* fname); void Save(const char* fileName);
int32 CountColumns() const; int32 CountColumns() const;
OptimizationType Optimization() const; OptimizationType Optimization() const;
@@ -101,8 +101,8 @@ public:
double ObjectiveValue() const; double ObjectiveValue() const;
double SolvingTime() const; double SolvingTime() const;
BString* ToBString(); operator BString() const;
const char* ToString(); void GetString(BString& string) const;
protected: protected:
int32 fCountColumns; int32 fCountColumns;
+3 -3
View File
@@ -24,7 +24,7 @@ class Summand;
class Variable { class Variable {
public: public:
int32 Index(); int32 Index() const;
LinearSpec* LS() const; LinearSpec* LS() const;
double Value() const; double Value() const;
void SetValue(double value); void SetValue(double value);
@@ -37,8 +37,8 @@ public:
const char* Label(); const char* Label();
void SetLabel(const char* label); void SetLabel(const char* label);
BString* ToBString(); operator BString() const;
const char* ToString(); void GetString(BString& string) const;
Constraint* IsEqual(Variable* var); Constraint* IsEqual(Variable* var);
Constraint* IsSmallerOrEqual(Variable* var); Constraint* IsSmallerOrEqual(Variable* var);
+15 -23
View File
@@ -649,34 +649,26 @@ Area::SetDefaultBehavior()
} }
BString* Area::operator BString() const
Area::ToBString()
{ {
BString* str = new BString(); BString string;
BString* leftStr = fLeft->ToBString(); GetString(string);
BString* topStr = fTop->ToBString(); return string;
BString* rightStr = fRight->ToBString();
BString* bottomStr = fBottom->ToBString();
*str << "Area(" << *leftStr << ", "
<< *topStr << ", "
<< *rightStr << ", "
<< *bottomStr << ")";
delete leftStr;
delete topStr;
delete rightStr;
delete bottomStr;
return str;
} }
const char* void
Area::ToString() Area::GetString(BString& string) const
{ {
BString* str = new BString(); string << "Area(";
char* result = (char*) malloc(str->Length() + 1); fLeft->GetString(string);
str->CopyInto(result, 0, str->Length()); string << ", ";
delete str; fTop->GetString(string);
return result; string << ", ";
fRight->GetString(string);
string << ", ";
fBottom->GetString(string);
string << ")";
} }
+18 -11
View File
@@ -62,22 +62,27 @@ BALMLayout::SolveLayout()
currentArea->SetDefaultBehavior(); currentArea->SetDefaultBehavior();
} }
// try to solve the layout until the result is OPTIMAL or INFEASIBLE, maximally // Try to solve the layout until the result is OPTIMAL or INFEASIBLE,
// 15 tries sometimes the solving algorithm encounters numerical problems // maximally 15 tries sometimes the solving algorithm encounters numerical
// (NUMFAILURE), and repeating the solving often helps to overcome them // problems (NUMFAILURE), and repeating the solving often helps to overcome
BFile* file = new BFile(fPerformancePath, // them.
B_READ_WRITE | B_CREATE_FILE | B_OPEN_AT_END); BFile* file = NULL;
if (fPerformancePath != NULL) {
file = new BFile(fPerformancePath,
B_READ_WRITE | B_CREATE_FILE | B_OPEN_AT_END);
}
ResultType result; ResultType result;
for (int32 tries = 0; tries < 15; tries++) { for (int32 tries = 0; tries < 15; tries++) {
result = Solve(); result = Solve();
if (fPerformancePath != NULL) { if (fPerformancePath != NULL) {
char buffer [100]; char buffer [100];
file->Write(buffer, sprintf(buffer, "%d\t%fms\t#vars=%ld\t#constraints=%ld\n", file->Write(buffer, sprintf(buffer, "%d\t%fms\t#vars=%ld\t"
result, SolvingTime(), Variables()->CountItems(), "#constraints=%ld\n", result, SolvingTime(),
Constraints()->CountItems())); Variables()->CountItems(), Constraints()->CountItems()));
} }
if (result == OPTIMAL || result == INFEASIBLE) break; if (result == OPTIMAL || result == INFEASIBLE)
break;
} }
delete file; delete file;
} }
@@ -130,8 +135,10 @@ Row*
BALMLayout::AddRow(YTab* top, YTab* bottom) BALMLayout::AddRow(YTab* top, YTab* bottom)
{ {
Row* row = new Row(this); Row* row = new Row(this);
if (top != NULL) row->Constraints()->AddItem(row->Top()->IsEqual(top)); if (top != NULL)
if (bottom != NULL) row->Constraints()->AddItem(row->Bottom()->IsEqual(bottom)); row->Constraints()->AddItem(row->Top()->IsEqual(top));
if (bottom != NULL)
row->Constraints()->AddItem(row->Bottom()->IsEqual(bottom));
return row; return row;
} }
+25 -32
View File
@@ -27,7 +27,7 @@
* @return the index of the constraint * @return the index of the constraint
*/ */
int32 int32
Constraint::Index() Constraint::Index() const
{ {
int32 i = fLS->Constraints()->IndexOf(this); int32 i = fLS->Constraints()->IndexOf(this);
if (i == -1) { if (i == -1) {
@@ -349,16 +349,15 @@ Constraint::WriteXML(BFile* file)
file->Write(buffer, sprintf(buffer, "\t\t\t<summand>\n")); file->Write(buffer, sprintf(buffer, "\t\t\t<summand>\n"));
file->Write(buffer, sprintf(buffer, "\t\t\t\t<coeff>%f</coeff>\n", file->Write(buffer, sprintf(buffer, "\t\t\t\t<coeff>%f</coeff>\n",
summand->Coeff())); summand->Coeff()));
BString* varStr = summand->Var()->ToBString(); BString varStr = *(summand->Var());
file->Write(buffer, sprintf(buffer, "\t\t\t\t<var>%s</var>\n", file->Write(buffer, sprintf(buffer, "\t\t\t\t<var>%s</var>\n",
varStr->String())); varStr.String()));
delete varStr;
file->Write(buffer, sprintf(buffer, "\t\t\t</summand>\n")); file->Write(buffer, sprintf(buffer, "\t\t\t</summand>\n"));
} }
file->Write(buffer, sprintf(buffer, "\t\t</leftside>\n")); file->Write(buffer, sprintf(buffer, "\t\t</leftside>\n"));
char* op; const char* op = "??";
if (fOp == OperatorType(EQ)) if (fOp == OperatorType(EQ))
op = "EQ"; op = "EQ";
else if (fOp == OperatorType(LE)) else if (fOp == OperatorType(LE))
@@ -457,44 +456,38 @@ Constraint::Invalidate()
} }
BString* Constraint::operator BString() const
Constraint::ToBString()
{ {
BString* str = new BString(); BString string;
*str << "Constraint "; GetString(string);
return string;
}
void
Constraint::GetString(BString& string) const
{
string << "Constraint ";
if (fLabel) if (fLabel)
*str << fLabel; string << fLabel;
*str << "(" << (int32)this << "): "; string << "(" << (int32)this << "): ";
if (fIsValid) { if (fIsValid) {
for (int i = 0; i < fLeftSide->CountItems(); i++) { for (int i = 0; i < fLeftSide->CountItems(); i++) {
Summand* s = static_cast<Summand*>(fLeftSide->ItemAt(i)); Summand* s = static_cast<Summand*>(fLeftSide->ItemAt(i));
*str << (float)s->Coeff() << "*"; string << (float)s->Coeff() << "*";
BString* varString = s->Var()->ToBString(); s->Var()->GetString(string);
*str << *varString << " "; string << " ";
delete varString;
} }
*str << ((fOp == OperatorType(EQ)) ? "== " string << ((fOp == OperatorType(EQ)) ? "== "
: (fOp == OperatorType(GE)) ? ">= " : (fOp == OperatorType(GE)) ? ">= "
: (fOp == OperatorType(LE)) ? "<= " : (fOp == OperatorType(LE)) ? "<= "
: "?? "); : "?? ");
*str << (float)fRightSide; string << (float)fRightSide;
*str << " PenaltyPos=" << (float)PenaltyPos(); string << " PenaltyPos=" << (float)PenaltyPos();
*str << " PenaltyNeg=" << (float)PenaltyNeg(); string << " PenaltyNeg=" << (float)PenaltyNeg();
} else } else
*str << "invalid"; string << "invalid";
return str;
}
const char*
Constraint::ToString()
{
BString* str = ToBString();
char* result = (char*) malloc(str->Length() + 1);
str->CopyInto(result, 0, str->Length());
delete str;
return result;
} }
+30 -36
View File
@@ -486,9 +486,10 @@ LinearSpec::Solve()
* @param fname the file name * @param fname the file name
*/ */
void void
LinearSpec::Save(char* fname) LinearSpec::Save(const char* fileName)
{ {
write_lp(fLP, fname); // TODO: Constness should be fixed in liblpsolve API.
write_lp(fLP, const_cast<char*>(fileName));
} }
@@ -594,54 +595,47 @@ LinearSpec::SolvingTime() const
} }
BString* LinearSpec::operator BString() const
LinearSpec::ToBString()
{ {
BString* str = new BString(); BString string;
*str << "LinearSpec " << (int32)this << ":\n"; GetString(string);
return string;
}
void
LinearSpec::GetString(BString& string) const
{
string << "LinearSpec " << (int32)this << ":\n";
for (int i = 0; i < fVariables->CountItems(); i++) { for (int i = 0; i < fVariables->CountItems(); i++) {
Variable* variable = static_cast<Variable*>(fVariables->ItemAt(i)); Variable* variable = static_cast<Variable*>(fVariables->ItemAt(i));
BString* vStr = variable->ToBString(); variable->GetString(string);
*str << *vStr << "=" << (float)variable->Value() << " "; string << "=" << (float)variable->Value() << " ";
delete vStr;
} }
*str << "\n"; string << "\n";
for (int i = 0; i < fConstraints->CountItems(); i++) { for (int i = 0; i < fConstraints->CountItems(); i++) {
Constraint* c = static_cast<Constraint*>(fConstraints->ItemAt(i)); Constraint* c = static_cast<Constraint*>(fConstraints->ItemAt(i));
BString* cStr = c->ToBString(); string << i << ": ";
*str << i << ": " << *cStr; c->GetString(string);
delete cStr; string << "\n";
*str << "\n";
} }
*str << "Result="; string << "Result=";
if (fResult==-1) if (fResult==-1)
*str << "ERROR"; string << "ERROR";
else if (fResult==0) else if (fResult==0)
*str << "OPTIMAL"; string << "OPTIMAL";
else if (fResult==1) else if (fResult==1)
*str << "SUBOPTIMAL"; string << "SUBOPTIMAL";
else if (fResult==2) else if (fResult==2)
*str << "INFEASIBLE"; string << "INFEASIBLE";
else if (fResult==3) else if (fResult==3)
*str << "UNBOUNDED"; string << "UNBOUNDED";
else if (fResult==4) else if (fResult==4)
*str << "DEGENERATE"; string << "DEGENERATE";
else if (fResult==5) else if (fResult==5)
*str << "NUMFAILURE"; string << "NUMFAILURE";
else else
*str << fResult; string << fResult;
*str << " SolvingTime=" << (float)fSolvingTime << "ms"; string << " SolvingTime=" << (float)fSolvingTime << "ms";
return str;
}
const char*
LinearSpec::ToString()
{
BString* str = ToBString();
char* result = (char*) malloc(str->Length() + 1);
str->CopyInto(result, 0, str->Length());
delete str;
return result;
} }
+16 -21
View File
@@ -30,7 +30,7 @@
* @return the index of the variable * @return the index of the variable
*/ */
int32 int32
Variable::Index() Variable::Index() const
{ {
int32 i = fLS->Variables()->IndexOf(this); int32 i = fLS->Variables()->IndexOf(this);
if (i == -1) { if (i == -1) {
@@ -166,39 +166,34 @@ Variable::SetLabel(const char* label)
} }
Variable::operator BString() const
{
BString string;
GetString(string);
return string;
}
/** /**
* Returns index of the variable as String. * Returns index of the variable as String.
* E.g. "Var2" * E.g. "Var2"
* *
* @return the <code>String</code> index of the variable * @return the <code>String</code> index of the variable
*/ */
BString* void
Variable::ToBString() Variable::GetString(BString& string) const
{ {
BString* str = new BString();
if (fLabel) { if (fLabel) {
*str << fLabel; string << fLabel;
if (!fIsValid) if (!fIsValid)
*str << "(invalid)"; string << "(invalid)";
} else { } else {
*str << "Var"; string << "Var";
if (!fIsValid) if (!fIsValid)
*str << "(invalid," << (int32)this << ")"; string << "(invalid," << (int32)this << ")";
else else
*str << Index(); string << Index();
} }
return str;
}
const char*
Variable::ToString()
{
BString* str = ToBString();
char* result = (char*) malloc(str->Length() + 1);
str->CopyInto(result, 0, str->Length());
delete str;
return result;
} }