- Integrated Grid object for data storage.

time ./task lo

			old	new
		real	0.262	0.018
		user	0.201	0.013
		sys	0.048	0.004

		~1200-1400% faster

	time ./task completed

			old	new
		real	3.991	4.014
		user	2.821	2.832
		sys	1.165	1.169

		~0.3-0.5% slower
This commit is contained in:
Paul Beckingham
2008-05-12 23:29:14 -04:00
parent 3dd45611ff
commit b63cf606f0
5 changed files with 90 additions and 97 deletions

View File

@@ -113,6 +113,15 @@ void Grid::add (
insertCell (row, col, new Cell (value)); insertCell (row, col, new Cell (value));
} }
void Grid::add (
const unsigned int row,
const unsigned int col,
const char* value)
{
expandGrid (row, col);
insertCell (row, col, new Cell (std::string (value)));
}
void Grid::add ( void Grid::add (
const unsigned int row, const unsigned int row,
const unsigned int col, const unsigned int col,

View File

@@ -55,6 +55,7 @@ public:
void add (const unsigned int, const unsigned int, const int); void add (const unsigned int, const unsigned int, const int);
void add (const unsigned int, const unsigned int, const float); void add (const unsigned int, const unsigned int, const float);
void add (const unsigned int, const unsigned int, const double); void add (const unsigned int, const unsigned int, const double);
void add (const unsigned int, const unsigned int, const char*);
void add (const unsigned int, const unsigned int, const std::string&); void add (const unsigned int, const unsigned int, const std::string&);
unsigned int width () const; unsigned int width () const;

View File

@@ -170,7 +170,7 @@ int Table::addRow ()
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::setRowColor (int row, Text::color fg, Text::color bg) void Table::setRowColor (const int row, const Text::color fg, const Text::color bg)
{ {
char id[12]; char id[12];
sprintf (id, "row:%d", row); sprintf (id, "row:%d", row);
@@ -179,7 +179,7 @@ void Table::setRowColor (int row, Text::color fg, Text::color bg)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::setRowFg (int row, Text::color c) void Table::setRowFg (const int row, const Text::color c)
{ {
char id[12]; char id[12];
sprintf (id, "row:%d", row); sprintf (id, "row:%d", row);
@@ -187,7 +187,7 @@ void Table::setRowFg (int row, Text::color c)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::setRowBg (int row, Text::color c) void Table::setRowBg (const int row, const Text::color c)
{ {
char id[12]; char id[12];
sprintf (id, "row:%d", row); sprintf (id, "row:%d", row);
@@ -195,11 +195,8 @@ void Table::setRowBg (int row, Text::color c)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::addCell (int row, int col, const std::string& data) void Table::addCell (const int row, const int col, const std::string& data)
{ {
char id[24];
sprintf (id, "cell:%d,%d", row, col);
int length = 0; int length = 0;
if (mSuppressWS) if (mSuppressWS)
@@ -212,14 +209,14 @@ void Table::addCell (int row, int col, const std::string& data)
clean (data2); clean (data2);
length = data2.length (); length = data2.length ();
mData[id] = data2; mData.add (row, col, data2);
} }
else else
{ {
if (mCommify.find (col) != mCommify.end ()) if (mCommify.find (col) != mCommify.end ())
mData[id] = commify (data); mData.add (row, col, commify (data));
else else
mData[id] = data; mData.add (row, col, data);
length = data.length (); length = data.length ();
} }
@@ -229,76 +226,61 @@ void Table::addCell (int row, int col, const std::string& data)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::addCell (int row, int col, char data) void Table::addCell (const int row, const int col, const char data)
{ {
char id[24]; mData.add (row, col, data);
sprintf (id, "cell:%d,%d", row, col);
char value[2];
sprintf (value, "%c", data);
mData[id] = value;
// Automatically maintain max width. // Automatically maintain max width.
mMaxDataWidth[col] = max (mMaxDataWidth[col], (signed) ::strlen (value)); mMaxDataWidth[col] = max (mMaxDataWidth[col], 1);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::addCell (int row, int col, int data) void Table::addCell (const int row, const int col, const int data)
{ {
char id[24];
sprintf (id, "cell:%d,%d", row, col);
char value[12]; char value[12];
sprintf (value, "%d", data); sprintf (value, "%d", data);
if (mCommify.find (col) != mCommify.end ()) if (mCommify.find (col) != mCommify.end ())
mData[id] = commify (value); mData.add (row, col, commify (value));
else else
mData[id] = value; mData.add (row, col, value);
// Automatically maintain max width. // Automatically maintain max width.
mMaxDataWidth[col] = max (mMaxDataWidth[col], (signed) ::strlen (value)); mMaxDataWidth[col] = max (mMaxDataWidth[col], (signed) ::strlen (value));
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::addCell (int row, int col, float data) void Table::addCell (const int row, const int col, const float data)
{ {
char id[24];
sprintf (id, "cell:%d,%d", row, col);
char value[24]; char value[24];
sprintf (value, "%.2f", data); sprintf (value, "%.2f", data);
if (mCommify.find (col) != mCommify.end ()) if (mCommify.find (col) != mCommify.end ())
mData[id] = commify (value); mData.add (row, col, commify (value));
else else
mData[id] = value; mData.add (row, col, value);
// Automatically maintain max width. // Automatically maintain max width.
mMaxDataWidth[col] = max (mMaxDataWidth[col], (signed) ::strlen (value)); mMaxDataWidth[col] = max (mMaxDataWidth[col], (signed) ::strlen (value));
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::addCell (int row, int col, double data) void Table::addCell (const int row, const int col, const double data)
{ {
char id[24];
sprintf (id, "cell:%d,%d", row, col);
char value[24]; char value[24];
sprintf (value, "%.6f", data); sprintf (value, "%.6f", data);
if (mCommify.find (col) != mCommify.end ()) if (mCommify.find (col) != mCommify.end ())
mData[id] = commify (value); mData.add (row, col, commify (value));
else else
mData[id] = value; mData.add (row, col, value);
// Automatically maintain max width. // Automatically maintain max width.
mMaxDataWidth[col] = max (mMaxDataWidth[col], (signed) ::strlen (value)); mMaxDataWidth[col] = max (mMaxDataWidth[col], (signed) ::strlen (value));
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::setCellColor (int row, int col, Text::color fg, Text::color bg) void Table::setCellColor (const int row, const int col, const Text::color fg, const Text::color bg)
{ {
char id[24]; char id[24];
sprintf (id, "cell:%d,%d", row, col); sprintf (id, "cell:%d,%d", row, col);
@@ -307,7 +289,7 @@ void Table::setCellColor (int row, int col, Text::color fg, Text::color bg)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::setCellFg (int row, int col, Text::color c) void Table::setCellFg (const int row, const int col, const Text::color c)
{ {
char id[24]; char id[24];
sprintf (id, "cell:%d,%d", row, col); sprintf (id, "cell:%d,%d", row, col);
@@ -315,7 +297,7 @@ void Table::setCellFg (int row, int col, Text::color c)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::setCellBg (int row, int col, Text::color c) void Table::setCellBg (const int row, const int col, const Text::color c)
{ {
char id[24]; char id[24];
sprintf (id, "cell:%d,%d", row, col); sprintf (id, "cell:%d,%d", row, col);
@@ -323,15 +305,17 @@ void Table::setCellBg (int row, int col, Text::color c)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string Table::getCell (int row, int col) std::string Table::getCell (const int row, const int col)
{ {
char id[24]; Grid::Cell* c = mData.byRow (row, col);
sprintf (id, "cell:%d,%d", row, col); if (c)
return mData[id]; return (std::string) *c;
return "";
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Text::color Table::getFg (int row, int col) Text::color Table::getFg (const int row, const int col)
{ {
char idCell[24]; char idCell[24];
sprintf (idCell, "cell:%d,%d", row, col); sprintf (idCell, "cell:%d,%d", row, col);
@@ -366,7 +350,7 @@ Text::color Table::getHeaderFg (int col)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Text::color Table::getBg (int row, int col) Text::color Table::getBg (const int row, const int col)
{ {
char idCell[24]; char idCell[24];
sprintf (idCell, "cell:%d,%d", row, col); sprintf (idCell, "cell:%d,%d", row, col);
@@ -501,7 +485,7 @@ void Table::calculateColumnWidths ()
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Table::just Table::getJustification (int row, int col) Table::just Table::getJustification (const int row, const int col)
{ {
if (mJustification.find (col) != mJustification.end ()) if (mJustification.find (col) != mJustification.end ())
return mJustification[col]; return mJustification[col];
@@ -602,10 +586,10 @@ const std::string Table::formatHeader (
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
void Table::formatCell ( void Table::formatCell (
int row, const int row,
int col, const int col,
int width, const int width,
int padding, const int padding,
std::vector <std::string>& lines, std::vector <std::string>& lines,
std::string& blank) std::string& blank)
{ {
@@ -700,10 +684,10 @@ void Table::formatCell (
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
const std::string Table::formatCell ( const std::string Table::formatCell (
int row, const int row,
int col, const int col,
int width, const int width,
int padding) const int padding)
{ {
assert (width > 0); assert (width > 0);
@@ -855,48 +839,47 @@ void Table::sort (std::vector <int>& order)
{ {
keepScanning = false; keepScanning = false;
char left[16]; Grid::Cell* left = mData.byRow (order[r], mSortColumns[c]);
sprintf (left, "cell:%d,%d", order[r], mSortColumns[c]); Grid::Cell* right = mData.byRow (order[r + gap], mSortColumns[c]);
if (left == NULL && right != NULL)
SWAP
char right[16]; if (left && right && *left != *right)
sprintf (right, "cell:%d,%d", order[r + gap], mSortColumns[c]);
if (mData[left] != mData[right])
{ {
switch (mSortOrder[mSortColumns[c]]) switch (mSortOrder[mSortColumns[c]])
{ {
case ascendingNumeric: case ascendingNumeric:
if (::atoi (mData[left].c_str ()) > ::atoi (mData[right].c_str ())) if ((float)*left > (float)*right)
SWAP SWAP
break; break;
case descendingNumeric: case descendingNumeric:
if (::atoi (mData[left].c_str ()) < ::atoi (mData[right].c_str ())) if ((float)*left < (float)*right)
SWAP SWAP
break; break;
case ascendingCharacter: case ascendingCharacter:
if (mData[left] > mData[right]) if ((char)*left > (char)*right)
SWAP SWAP
break; break;
case descendingCharacter: case descendingCharacter:
if (mData[left] < mData[right]) if ((char)*left < (char)*right)
SWAP SWAP
break; break;
case ascendingDate: case ascendingDate:
{ {
if (mData[left] != "" && mData[right] == "") if ((std::string)*left != "" && (std::string)*right == "")
break; break;
else if (mData[left] == "" && mData[right] != "") else if ((std::string)*left == "" && (std::string)*right != "")
SWAP SWAP
else else
{ {
Date dl (mData[left]); Date dl ((std::string)*left);
Date dr (mData[right]); Date dr ((std::string)*right);
if (dl > dr) if (dl > dr)
SWAP SWAP
} }
@@ -905,16 +888,16 @@ void Table::sort (std::vector <int>& order)
case descendingDate: case descendingDate:
{ {
if (mData[left] != "" && mData[right] == "") if ((std::string)*left != "" && (std::string)*right == "")
break; break;
else if (mData[left] == "" && mData[right] != "") else if ((std::string)*left == "" && (std::string)*right != "")
SWAP SWAP
else else
{ {
Date dl (mData[left]); Date dl ((std::string)*left);
Date dr (mData[right]); Date dr ((std::string)*right);
if (dl < dr) if (dl < dr)
SWAP SWAP
} }
@@ -922,16 +905,16 @@ void Table::sort (std::vector <int>& order)
break; break;
case ascendingPriority: case ascendingPriority:
if ((mData[left] == "" && mData[right] != "") || if (((std::string)*left == "" && (std::string)*right != "") ||
(mData[left] == "M" && mData[right] == "L") || ((std::string)*left == "M" && (std::string)*right == "L") ||
(mData[left] == "H" && (mData[right] == "L" || mData[right] == "M"))) ((std::string)*left == "H" && ((std::string)*right == "L" || (std::string)*right == "M")))
SWAP SWAP
break; break;
case descendingPriority: case descendingPriority:
if ((mData[left] == "" && mData[right] != "") || if (((std::string)*left == "" && (std::string)*right != "") ||
(mData[left] == "L" && (mData[right] == "M" || mData[right] == "H")) || ((std::string)*left == "L" && ((std::string)*right == "M" || (std::string)*right == "H")) ||
(mData[left] == "M" && mData[right] == "H")) ((std::string)*left == "M" && (std::string)*right == "H"))
SWAP SWAP
break; break;
} }

View File

@@ -65,20 +65,20 @@ public:
const std::string render (); const std::string render ();
private: private:
std::string getCell (int, int); std::string getCell (const int, const int);
Text::color getFg (int, int); Text::color getFg (const int, const int);
Text::color getHeaderFg (int); Text::color getHeaderFg (const int);
Text::color getBg (int, int); Text::color getBg (const int, const int);
Text::color getHeaderBg (int); Text::color getHeaderBg (const int);
Text::attr getHeaderUnderline (int); Text::attr getHeaderUnderline (const int);
int getPadding (int); int getPadding (const int);
int getIntraPadding (); int getIntraPadding ();
void calculateColumnWidths (); void calculateColumnWidths ();
just getJustification (int, int); just getJustification (const int, const int);
just getHeaderJustification (int); just getHeaderJustification (const int);
const std::string formatHeader (int, int, int); const std::string formatHeader (const int, const int, const int);
const std::string formatCell (int, int, int, int); const std::string formatCell (const int, const int, const int, const int);
void formatCell (int, int, int, int, std::vector <std::string>&, std::string&); void formatCell (const int, const int, const int, const int, std::vector <std::string>&, std::string&);
void optimize (std::string&); void optimize (std::string&);
void sort (std::vector <int>&); void sort (std::vector <int>&);
void clean (std::string&); void clean (std::string&);
@@ -103,7 +103,7 @@ private:
std::map <int, just> mJustification; std::map <int, just> mJustification;
std::map <int, bool> mCommify; std::map <int, bool> mCommify;
std::map <std::string, std::string> mData; Grid mData;
std::vector <int> mSortColumns; std::vector <int> mSortColumns;
std::map <int, order> mSortOrder; std::map <int, order> mSortOrder;

View File

@@ -492,7 +492,7 @@ void handleList (const TDB& tdb, T& task, Config& conf)
// All criteria match, so add refTask to the output table. // All criteria match, so add refTask to the output table.
int row = table.addRow (); int row = table.addRow ();
table.addCell (row, 0, (int) i + 1); table.addCell (row, 0, refTask.getId ());
table.addCell (row, 1, refTask.getAttribute ("project")); table.addCell (row, 1, refTask.getAttribute ("project"));
table.addCell (row, 2, refTask.getAttribute ("priority")); table.addCell (row, 2, refTask.getAttribute ("priority"));
table.addCell (row, 3, due); table.addCell (row, 3, due);
@@ -665,7 +665,7 @@ void handleSmallList (const TDB& tdb, T& task, Config& conf)
// All criteria match, so add refTask to the output table. // All criteria match, so add refTask to the output table.
int row = table.addRow (); int row = table.addRow ();
table.addCell (row, 0, (int) i + 1); table.addCell (row, 0, refTask.getId ());
table.addCell (row, 1, refTask.getAttribute ("project")); table.addCell (row, 1, refTask.getAttribute ("project"));
table.addCell (row, 2, refTask.getAttribute ("priority")); table.addCell (row, 2, refTask.getAttribute ("priority"));
table.addCell (row, 3, refTask.getDescription ()); table.addCell (row, 3, refTask.getDescription ());
@@ -1159,7 +1159,7 @@ void handleLongList (const TDB& tdb, T& task, Config& conf)
// All criteria match, so add refTask to the output table. // All criteria match, so add refTask to the output table.
int row = table.addRow (); int row = table.addRow ();
table.addCell (row, 0, (int) i + 1); table.addCell (row, 0, refTask.getId ());
table.addCell (row, 1, refTask.getAttribute ("project")); table.addCell (row, 1, refTask.getAttribute ("project"));
table.addCell (row, 2, refTask.getAttribute ("priority")); table.addCell (row, 2, refTask.getAttribute ("priority"));
table.addCell (row, 3, entered); table.addCell (row, 3, entered);
@@ -1980,7 +1980,7 @@ void handleReportActive (const TDB& tdb, T& task, Config& conf)
// All criteria match, so add refTask to the output table. // All criteria match, so add refTask to the output table.
int row = table.addRow (); int row = table.addRow ();
table.addCell (row, 0, (int) i + 1); table.addCell (row, 0, refTask.getId ());
table.addCell (row, 1, refTask.getAttribute ("project")); table.addCell (row, 1, refTask.getAttribute ("project"));
table.addCell (row, 2, refTask.getAttribute ("priority")); table.addCell (row, 2, refTask.getAttribute ("priority"));
table.addCell (row, 3, due); table.addCell (row, 3, due);
@@ -2099,7 +2099,7 @@ void handleReportOverdue (const TDB& tdb, T& task, Config& conf)
{ {
// All criteria match, so add refTask to the output table. // All criteria match, so add refTask to the output table.
int row = table.addRow (); int row = table.addRow ();
table.addCell (row, 0, (int) i + 1); table.addCell (row, 0, refTask.getId ());
table.addCell (row, 1, refTask.getAttribute ("project")); table.addCell (row, 1, refTask.getAttribute ("project"));
table.addCell (row, 2, refTask.getAttribute ("priority")); table.addCell (row, 2, refTask.getAttribute ("priority"));
table.addCell (row, 3, due); table.addCell (row, 3, due);