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