- Integrated new Grid object into build - not yet integrated into Table.

This commit is contained in:
Paul Beckingham 2008-04-26 00:09:59 -04:00
parent ce52f88f4b
commit 62e6b31a17
15 changed files with 530 additions and 54 deletions

View file

@ -98,7 +98,7 @@ Config.o Config.o: Config.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_tree.h \
/usr/include/c++/4.0.0/bits/stl_map.h \
/usr/include/c++/4.0.0/bits/stl_multimap.h /usr/include/sys/types.h \
Config.h Table.h color.h TDB.h T.h stlmacros.h ../auto.h
Config.h Table.h color.h Grid.h color.h TDB.h T.h stlmacros.h ../auto.h
/usr/include/c++/4.0.0/iostream:
@ -392,6 +392,10 @@ Table.h:
color.h:
Grid.h:
color.h:
TDB.h:
T.h:

View file

@ -95,7 +95,8 @@ Date.o Date.o: Date.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_tree.h \
/usr/include/c++/4.0.0/bits/stl_map.h \
/usr/include/c++/4.0.0/bits/stl_multimap.h /usr/include/sys/types.h \
Config.h Table.h color.h TDB.h T.h stlmacros.h ../auto.h Date.h
Config.h Table.h color.h Grid.h color.h TDB.h T.h stlmacros.h ../auto.h \
Date.h
/usr/include/c++/4.0.0/iostream:
@ -379,6 +380,10 @@ Table.h:
color.h:
Grid.h:
color.h:
TDB.h:
T.h:

View file

@ -95,7 +95,7 @@ T.o T.o: T.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_tree.h \
/usr/include/c++/4.0.0/bits/stl_map.h \
/usr/include/c++/4.0.0/bits/stl_multimap.h /usr/include/sys/types.h \
Config.h Table.h color.h TDB.h T.h stlmacros.h ../auto.h
Config.h Table.h color.h Grid.h color.h TDB.h T.h stlmacros.h ../auto.h
/usr/include/c++/4.0.0/iostream:
@ -379,6 +379,10 @@ Table.h:
color.h:
Grid.h:
color.h:
TDB.h:
T.h:

View file

@ -99,7 +99,7 @@ TDB.o TDB.o: TDB.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_tree.h \
/usr/include/c++/4.0.0/bits/stl_map.h \
/usr/include/c++/4.0.0/bits/stl_multimap.h Config.h Table.h color.h \
TDB.h T.h stlmacros.h ../auto.h
Grid.h color.h TDB.h T.h stlmacros.h ../auto.h
/usr/include/c++/4.0.0/iostream:
@ -395,6 +395,10 @@ Table.h:
color.h:
Grid.h:
color.h:
TDB.h:
T.h:

View file

@ -94,8 +94,8 @@ Table.o Table.o: Table.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_multimap.h \
/usr/include/c++/4.0.0/vector /usr/include/c++/4.0.0/bits/stl_vector.h \
/usr/include/c++/4.0.0/bits/stl_bvector.h \
/usr/include/c++/4.0.0/bits/vector.tcc color.h Date.h stlmacros.h \
task.h /usr/include/sys/types.h Config.h TDB.h T.h ../auto.h
/usr/include/c++/4.0.0/bits/vector.tcc color.h Grid.h Date.h library.h \
/usr/include/sys/types.h stlmacros.h
/usr/include/c++/4.0.0/iostream:
@ -373,18 +373,12 @@ Table.h:
color.h:
Grid.h:
Date.h:
stlmacros.h:
task.h:
library.h:
/usr/include/sys/types.h:
Config.h:
TDB.h:
T.h:
../auto.h:
stlmacros.h:

View file

@ -95,8 +95,8 @@ parse.o parse.o: parse.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_tree.h \
/usr/include/c++/4.0.0/bits/stl_map.h \
/usr/include/c++/4.0.0/bits/stl_multimap.h Date.h task.h \
/usr/include/sys/types.h Config.h Table.h color.h TDB.h T.h stlmacros.h \
../auto.h
/usr/include/sys/types.h Config.h Table.h color.h Grid.h color.h TDB.h \
T.h stlmacros.h ../auto.h
/usr/include/c++/4.0.0/iostream:
@ -382,6 +382,10 @@ Table.h:
color.h:
Grid.h:
color.h:
TDB.h:
T.h:

View file

@ -94,8 +94,8 @@ rules.o rules.o: rules.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_multimap.h \
/usr/include/c++/4.0.0/vector /usr/include/c++/4.0.0/bits/stl_vector.h \
/usr/include/c++/4.0.0/bits/stl_bvector.h \
/usr/include/c++/4.0.0/bits/vector.tcc Table.h color.h Date.h T.h \
task.h /usr/include/sys/types.h TDB.h stlmacros.h ../auto.h
/usr/include/c++/4.0.0/bits/vector.tcc Table.h color.h Grid.h Date.h \
T.h task.h /usr/include/sys/types.h color.h TDB.h stlmacros.h ../auto.h
/usr/include/c++/4.0.0/iostream:
@ -375,6 +375,8 @@ Table.h:
color.h:
Grid.h:
Date.h:
T.h:
@ -383,6 +385,8 @@ task.h:
/usr/include/sys/types.h:
color.h:
TDB.h:
stlmacros.h:

View file

@ -98,8 +98,8 @@ task.o task.o: task.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/bits/stl_multimap.h \
/usr/include/c++/4.0.0/vector /usr/include/c++/4.0.0/bits/stl_vector.h \
/usr/include/c++/4.0.0/bits/stl_bvector.h \
/usr/include/c++/4.0.0/bits/vector.tcc Date.h Table.h color.h \
stlmacros.h TDB.h T.h task.h ../auto.h /usr/include/ncurses.h \
/usr/include/c++/4.0.0/bits/vector.tcc Date.h Table.h color.h Grid.h \
stlmacros.h TDB.h T.h task.h color.h ../auto.h /usr/include/ncurses.h \
/usr/include/ncurses_dll.h /usr/include/unctrl.h /usr/include/curses.h
/usr/include/c++/4.0.0/iostream:
@ -396,6 +396,8 @@ Table.h:
color.h:
Grid.h:
stlmacros.h:
TDB.h:
@ -404,6 +406,8 @@ T.h:
task.h:
color.h:
../auto.h:
/usr/include/ncurses.h:

View file

@ -95,7 +95,7 @@ text.o text.o: text.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/c++/4.0.0/map /usr/include/c++/4.0.0/bits/stl_tree.h \
/usr/include/c++/4.0.0/bits/stl_map.h \
/usr/include/c++/4.0.0/bits/stl_multimap.h /usr/include/sys/types.h \
Config.h Table.h color.h TDB.h T.h stlmacros.h ../auto.h
Config.h Table.h color.h Grid.h color.h TDB.h T.h stlmacros.h ../auto.h
/usr/include/c++/4.0.0/iostream:
@ -379,6 +379,10 @@ Table.h:
color.h:
Grid.h:
color.h:
TDB.h:
T.h:

View file

@ -95,8 +95,9 @@ util.o util.o: util.cpp /usr/include/c++/4.0.0/iostream \
/usr/include/sys/time.h Table.h /usr/include/c++/4.0.0/map \
/usr/include/c++/4.0.0/bits/stl_tree.h \
/usr/include/c++/4.0.0/bits/stl_map.h \
/usr/include/c++/4.0.0/bits/stl_multimap.h color.h task.h Config.h \
TDB.h T.h stlmacros.h ../auto.h /usr/include/uuid/uuid.h
/usr/include/c++/4.0.0/bits/stl_multimap.h color.h Grid.h task.h \
Config.h color.h TDB.h T.h stlmacros.h ../auto.h \
/usr/include/uuid/uuid.h
/usr/include/c++/4.0.0/iostream:
@ -378,10 +379,14 @@ Table.h:
color.h:
Grid.h:
task.h:
Config.h:
color.h:
TDB.h:
T.h:

386
src/Grid.cpp Normal file
View file

@ -0,0 +1,386 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 - 2008, Paul Beckingham. All rights reserved.
//
// Grid implements a sparse 2D array of Cell objects. Grid makes every effort
// to perform well on cell insertion and retrieval. A Cell is a variant type,
// capable of storing char, bool, int, float, double and std::string types.
//
// Every cell is accessible from both mColumns and mRows. This allows the
// client code to specify which method is used, because there will be a
// performance penalty with one of the methods, depending on the layout of
// cells within the grid.
//
// mColumns, like mRows, is a vector of a vector of Cell*.
//
// mColumns
// [0..n]
// +---+---+-----------+---+
// | 0 | 1 | | n |
// +---+---+-----------+---+
// | |
// v |
// +---+ . +---+ . | .
// mRows | 0 | -------> | x | v
// [0..1] +---+ . +---+ +---+
// | 1 | -------> | y | --------> | z |
// +---+ . +---+ +---+
// | |
// | |
// | |
// +---+ . . . . .
// | n |
// +---+ . . . . .
//
//
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <Grid.h>
////////////////////////////////////////////////////////////////////////////////
Grid::Grid ()
{
}
////////////////////////////////////////////////////////////////////////////////
// The cells are deleted via their mRows reference, not by their mColumns
// reference. This is because the cells are doubly-linked, and so the
// convention is that rows own cells, columns merely own pointers.
Grid::~Grid ()
{
std::vector < std::vector <Cell*>* >::iterator row;
std::vector <Cell*>::iterator col;
for (row = mRows.begin (); row != mRows.end (); ++row)
if (*row)
for (col = (*row)->begin (); col != (*row)->end (); ++col)
if (*col)
delete *col;
std::vector < std::vector <Cell*>* >::iterator it;
for (it = mRows.begin (); it != mRows.end (); ++it)
if (*it)
delete *it;
for (it = mColumns.begin (); it != mColumns.end (); ++it)
if (*it)
delete *it;
}
////////////////////////////////////////////////////////////////////////////////
void Grid::add (
const unsigned int row,
const unsigned int col,
const bool value)
{
expandGrid (row, col);
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 (value));
}
void Grid::add (
const unsigned int row,
const unsigned int col,
const int value)
{
expandGrid (row, col);
insertCell (row, col, new Cell (value));
}
void Grid::add (
const unsigned int row,
const unsigned int col,
const float value)
{
expandGrid (row, col);
insertCell (row, col, new Cell (value));
}
void Grid::add (
const unsigned int row,
const unsigned int col,
const double value)
{
expandGrid (row, col);
insertCell (row, col, new Cell (value));
}
void Grid::add (
const unsigned int row,
const unsigned int col,
const std::string& value)
{
expandGrid (row, col);
insertCell (row, col, new Cell (value));
}
////////////////////////////////////////////////////////////////////////////////
unsigned int Grid::width () const
{
return mColumns.size ();
}
unsigned int Grid::height () const
{
return mRows.size ();
}
////////////////////////////////////////////////////////////////////////////////
Grid::Cell* Grid::byRow (const unsigned int row, const unsigned int col) const
{
if (row <= mRows.size () &&
mRows[row] != NULL &&
col <= mRows[row]->size ())
return (*mRows[row])[col];
return NULL;
}
Grid::Cell* Grid::byColumn (const unsigned int row, const unsigned int col) const
{
if (col <= mColumns.size () &&
mColumns[col] != NULL &&
row <= mColumns[col]->size ())
return (*mColumns[col])[row];
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
void Grid::expandGrid (const unsigned int row, const unsigned int col)
{
// If the new row is outside the bounds of the current grid, add blank rows to
// pad, then a new row vector.
if (row >= mRows.size ())
{
for (unsigned int r = mRows.size (); r <= row; ++r)
if (r < row)
// {
// std::cout << "additional mRows[" << r << "] = NULL" << std::endl;
mRows.push_back (NULL);
// }
else
// {
// std::cout << "additional mRows[" << r << "] = new std::vector <Cell*>" << std::endl;
mRows.push_back (new std::vector <Cell*>);
// }
}
// If the new row is within the bounds of the current grid, ensure that the
// row points to a vector of cells.
else if (mRows[row] == NULL)
// {
// std::cout << "existing mRows[" << row << "] = new std::vector <Cell*>" << std::endl;
mRows[row] = new std::vector <Cell*>;
// }
if (col >= mRows[row]->size ())
for (unsigned int c = mRows[row]->size (); c <= col; ++c)
// {
// std::cout << "additional mRows[" << row << "][" << c << "] = NULL" << std::endl;
mRows[row]->push_back (NULL);
// }
// If the new col is outside the bounds of the current grid, add blank cols to
// pad, then a new col vector.
if (col >= mColumns.size ())
{
for (unsigned int c = mColumns.size (); c <= col; ++c)
if (c < col)
// {
// std::cout << "additional mColumns[" << c << "] = NULL" << std::endl;
mColumns.push_back (NULL);
// }
else
// {
// std::cout << "additional mColumns[" << c << "] = new std::vector <Cell*>" << std::endl;
mColumns.push_back (new std::vector <Cell*>);
// }
}
// If the new col is within the bounds of the current grid, ensure that the
// col points to a vector of cells.
else if (mColumns[col] == NULL)
// {
// std::cout << "existing mColumns[" << col << "] = new std::vector <Cell*>" << std::endl;
mColumns[col] = new std::vector <Cell*>;
// }
if (row >= mColumns[col]->size ())
for (unsigned int r = mColumns[col]->size (); r <= row; ++r)
// {
// std::cout << "additional mColumns[" << col << "][" << r << "] = NULL" << std::endl;
mColumns[col]->push_back (NULL);
// }
}
////////////////////////////////////////////////////////////////////////////////
void Grid::insertCell (
const unsigned int row,
const unsigned int col,
Cell* cell)
{
// Delete any existing cell, because cells are owned by rows, not columns.
if ((*mRows[row])[col] != NULL)
// {
// std::cout << "deleted old cell mRows[" << row << "][" << col << "]" << std::endl;
delete (*mRows[row])[col];
// }
(*mRows[row])[col] = cell;
(*mColumns[col])[row] = cell;
// std::cout << "assigned new cell mRows[" << row << "][" << col << "]" << std::endl;
// std::cout << "assigned new cell mColumns[" << col << "][" << row << "]" << std::endl;
}
////////////////////////////////////////////////////////////////////////////////
Grid::Cell::Cell (const bool value)
: mType (CELL_BOOL)
, mBool (value)
{
}
Grid::Cell::Cell (const char value)
: mType (CELL_CHAR)
, mChar (value)
{
}
Grid::Cell::Cell (const int value)
: mType (CELL_INT)
, mInt (value)
{
}
Grid::Cell::Cell (const float value)
: mType (CELL_FLOAT)
, mFloat (value)
{
}
Grid::Cell::Cell (const double value)
: mType (CELL_DOUBLE)
, mDouble (value)
{
}
Grid::Cell::Cell (const std::string& value)
: mType (CELL_STRING)
, mString (value)
{
}
////////////////////////////////////////////////////////////////////////////////
// These cast operators make a best approximation to an appropriate rendering,
// given the format change.
Grid::Cell::operator bool () const
{
switch (mType)
{
case CELL_BOOL: return mBool;
case CELL_CHAR: return mChar != '\0' &&
mChar != ' ' &&
mChar != '0';
case CELL_INT: return mInt != 0;
case CELL_FLOAT: return mFloat != 0.0;
case CELL_DOUBLE: return mDouble != 0.0;
case CELL_STRING: return mString.length () > 0;
}
return false;
}
Grid::Cell::operator char () const
{
switch (mType)
{
case CELL_BOOL: return mBool ? 'Y' : 'N';
case CELL_CHAR: return mChar;
case CELL_INT: return (char) mInt;
case CELL_FLOAT: return (char) (int) mFloat;
case CELL_DOUBLE: return (char) (int) mDouble;
case CELL_STRING: return mString[0];
}
return '\0';
}
Grid::Cell::operator int () const
{
switch (mType)
{
case CELL_BOOL: return mBool ? 1 : 0;
case CELL_CHAR: return (int) mChar;
case CELL_INT: return mInt;
case CELL_FLOAT: return (int) mFloat;
case CELL_DOUBLE: return (int) mDouble;
case CELL_STRING: return mString.length ();
}
return 0;
}
Grid::Cell::operator float () const
{
switch (mType)
{
case CELL_BOOL: return mBool ? 1.0 : 0.0;
case CELL_CHAR: return (float) (int) mChar;
case CELL_INT: return (float) mInt;
case CELL_FLOAT: return mFloat;
case CELL_DOUBLE: return (float) mDouble;
case CELL_STRING: return (float) mString.length ();
}
return 0.0;
}
Grid::Cell::operator double () const
{
switch (mType)
{
case CELL_BOOL: return mBool ? 1.0 : 0.0;
case CELL_CHAR: return (double) (int) mChar;
case CELL_INT: return (double) mInt;
case CELL_FLOAT: return (double) mFloat;
case CELL_DOUBLE: return mDouble;
case CELL_STRING: return (double) mString.length ();
}
return 0.0;
}
Grid::Cell::operator std::string () const
{
char s[64] = {0};
switch (mType)
{
case CELL_BOOL: return mBool ? "true" : "false";
case CELL_CHAR: sprintf (s, "%c", mChar);
return std::string (s);
case CELL_INT: sprintf (s, "%d", mInt);
return std::string (s);
case CELL_FLOAT: sprintf (s, "%f", mFloat);
return std::string (s);
case CELL_DOUBLE: sprintf (s, "%f", mDouble);
return std::string (s);
case CELL_STRING: return mString;
}
return std::string ("");
}
////////////////////////////////////////////////////////////////////////////////
Grid::Cell::cellType Grid::Cell::type () const
{
return mType;
}
////////////////////////////////////////////////////////////////////////////////

75
src/Grid.h Normal file
View file

@ -0,0 +1,75 @@
////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 - 2008, Paul Beckingham. All rights reserved.
//
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_GRID
#define INCLUDED_GRID
#include <string>
#include <vector>
////////////////////////////////////////////////////////////////////////////////
class Grid
{
public:
class Cell
{
public:
Cell (const bool);
Cell (const char);
Cell (const int);
Cell (const float);
Cell (const double);
Cell (const std::string&);
operator bool () const;
operator char () const;
operator int () const;
operator float () const;
operator double () const;
operator std::string () const;
enum cellType {CELL_BOOL, CELL_CHAR, CELL_INT, CELL_FLOAT, CELL_DOUBLE, CELL_STRING};
cellType type () const;
private:
cellType mType;
bool mBool;
char mChar;
int mInt;
float mFloat;
double mDouble;
std::string mString;
};
public:
Grid ();
~Grid ();
void add (const unsigned int, const unsigned int, const bool);
void add (const unsigned int, const unsigned int, const char);
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 double);
void add (const unsigned int, const unsigned int, const std::string&);
unsigned int width () const;
unsigned int height () const;
Cell* byRow (const unsigned int, const unsigned int) const;
Cell* byColumn (const unsigned int, const unsigned int) const;
private:
void expandGrid (const unsigned int, const unsigned int);
void insertCell (const unsigned int, const unsigned int, Cell*);
private:
std::vector < std::vector <Cell*>* > mRows;
std::vector < std::vector <Cell*>* > mColumns;
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -1,2 +1,2 @@
bin_PROGRAMS = task
task_SOURCES = Config.cpp Date.cpp T.cpp TDB.cpp Table.cpp color.cpp parse.cpp task.cpp util.cpp text.cpp rules.cpp Config.h Date.h T.h TDB.h Table.h color.h stlmacros.h task.h
task_SOURCES = Config.cpp Date.cpp T.cpp TDB.cpp Table.cpp Grid.cpp color.cpp parse.cpp task.cpp util.cpp text.cpp rules.cpp Config.h Date.h T.h TDB.h Table.h Grid.h color.h stlmacros.h task.h

View file

@ -22,10 +22,10 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "Table.h"
#include "Date.h"
#include "stlmacros.h"
#include "task.h"
#include <Table.h>
#include <Date.h>
#include <library.h>
#include <stlmacros.h>
////////////////////////////////////////////////////////////////////////////////
Table::Table ()
@ -153,9 +153,7 @@ void Table::setColumnCommify (int column)
////////////////////////////////////////////////////////////////////////////////
void Table::setColumnJustification (int column, just j)
{
char id[12];
sprintf (id, "col:%d", column);
mJustification[id] = j;
mJustification[column] = j;
}
////////////////////////////////////////////////////////////////////////////////
@ -505,20 +503,8 @@ void Table::calculateColumnWidths ()
////////////////////////////////////////////////////////////////////////////////
Table::just Table::getJustification (int row, int col)
{
char idCell[24];
sprintf (idCell, "cell:%d,%d", row, col);
if (mJustification.find (idCell) != mJustification.end ())
return mJustification[idCell];
char idRow[12];
sprintf (idRow, "row:%d", row);
if (mJustification.find (idRow) != mJustification.end ())
return mJustification[idRow];
char idCol[12];
sprintf (idCol, "col:%d", col);
if (mJustification.find (idCol) != mJustification.end ())
return mJustification[idCol];
if (mJustification.find (col) != mJustification.end ())
return mJustification[col];
return left;
}
@ -526,11 +512,7 @@ Table::just Table::getJustification (int row, int col)
////////////////////////////////////////////////////////////////////////////////
Table::just Table::getHeaderJustification (int col)
{
char idCol[12];
sprintf (idCol, "col:%d", col);
return mJustification.find (idCol) != mJustification.end () ? mJustification[idCol]
: mJustification.find ("table") != mJustification.end () ? mJustification["table"]
return mJustification.find (col) != mJustification.end () ? mJustification[col]
: left;
}

View file

@ -10,7 +10,8 @@
#include <map>
#include <vector>
#include <string>
#include "color.h"
#include <color.h>
#include <Grid.h>
class Table
{
@ -100,7 +101,7 @@ private:
std::vector <int> mMaxDataWidth;
std::vector <int> mCalculatedWidth;
std::map <std::string, just> mJustification;
std::map <int, just> mJustification;
std::map <int, bool> mCommify;
std::map <std::string, std::string> mData;
std::vector <int> mSortColumns;