PLearn 0.1
|
#include <StringTable.h>
Public Member Functions | |
StringTable (const string &filename) | |
StringTable () | |
int | length () const |
int | width () const |
string & | operator() (int i, int j) |
TVec< string > | operator() (int i) const |
void | appendRow (const list< pair< string, string > > &row) |
void | declareFields (const list< pair< string, string > > &row) |
string | getFieldName (int i) const |
Private Attributes | |
TMat< string > | data |
vector< string > | fieldnames |
map< string, int > | rev_fn |
Friends | |
ostream & | operator<< (ostream &out, const StringTable &st) |
Definition at line 49 of file StringTable.h.
PLearn::StringTable::StringTable | ( | const string & | filename | ) |
Definition at line 118 of file StringTable.cc.
References PLearn::countNonBlankLinesOfFile(), i, in, PLERROR, PLearn::removeblanks(), and PLearn::split().
{ int nrows= countNonBlankLinesOfFile(filename); string str; ifstream in(filename.c_str()); in.ignore(2); if(in.peek()==' ') in.ignore(1); getline(in,str); fieldnames=split(str); data.resize(nrows, (int)fieldnames.size()); int rnum=0; getline(in,str); while(removeblanks(str)!="") { /* vector<string> line; size_t pos,lpos=0; while((pos=str.find(";",lpos))!=string::npos) { line.push_back(str.substr(lpos,pos-lpos)); lpos=pos+1; } line.push_back(str.substr(lpos,str.size()-lpos)); if(line.size()!=fieldnames.size()) PLERROR("in row %i : elements (%i) mismatch number of fields (%i)",rnum,line.size(),fieldnames.size()); */ vector<string> line=split(str,";"); //line.pop_back(); // last string found is garbage *** NO, not true...! if(line.size()!=fieldnames.size()) PLERROR("in row %i : elements (%i) mismatch number of fields (%i)",rnum,line.size(),fieldnames.size()); for(unsigned int i= 0; i < line.size(); ++i) data(rnum,i)= line[i]; ++rnum; getline(in,str); } data.resize(rnum, (int)fieldnames.size()); }
PLearn::StringTable::StringTable | ( | ) |
Definition at line 116 of file StringTable.cc.
{}
void PLearn::StringTable::appendRow | ( | const list< pair< string, string > > & | row | ) |
Definition at line 75 of file StringTable.cc.
References PLearn::TMat< T >::subMatColumns().
{ vector<string> vec; data.resize(data.length()+1,data.width()); int rownum=data.length()-1; for(list<pair<string,string> >::const_iterator it=row.begin();it!=row.end();++it) { int colnum; map<string,int>::iterator revit=rev_fn.find(it->first); if(revit==rev_fn.end()) { colnum=data.width(); TMat<string> tmp(data.length(),colnum+1); tmp.subMatColumns(0,colnum)<<data; data=tmp; rev_fn[it->first]=colnum; fieldnames.push_back(it->first);; } else colnum=rev_fn[it->first]; data(rownum,colnum)=it->second; } }
void PLearn::StringTable::declareFields | ( | const list< pair< string, string > > & | row | ) |
Definition at line 102 of file StringTable.cc.
string PLearn::StringTable::getFieldName | ( | int | i | ) | const [inline] |
Definition at line 84 of file StringTable.h.
References i.
Referenced by PLearn::StrTableVMatrix::StrTableVMatrix().
{return fieldnames[i];}
int PLearn::StringTable::length | ( | ) | const [inline] |
Definition at line 75 of file StringTable.h.
Referenced by PLearn::operator<<(), and PLearn::StrTableVMatrix::StrTableVMatrix().
{return data.length();}
Definition at line 78 of file StringTable.h.
{return data(i);}
Definition at line 77 of file StringTable.h.
{return data(i,j);}
int PLearn::StringTable::width | ( | ) | const [inline] |
Definition at line 76 of file StringTable.h.
Referenced by PLearn::operator<<(), and PLearn::StrTableVMatrix::StrTableVMatrix().
{return data.width();}
ostream& operator<< | ( | ostream & | out, |
const StringTable & | st | ||
) | [friend] |
Definition at line 42 of file StringTable.cc.
{ // find out width of each columns TVec<size_t> colsiz(st.width(),(size_t)0); for(int j=0;j<st.length();j++) { TVec<string> row=st.data(j); for(int i=0;i<st.width();i++) if((size_t)row[i].length() > colsiz[i]) colsiz[i]=(size_t)row[i].length(); } for(int i=0;i<st.width();i++) if(st.fieldnames[i].length() > colsiz[i]) colsiz[i]=(size_t)st.fieldnames[i].length(); out<<"#: "; for(int i=0;i<st.width();i++) out<<left(st.fieldnames[i],colsiz[i]+3); out<<"\n"; for(int j=0;j<st.length();j++) { TVec<string> row=st.data(j); out<<" "; for(int i=0;i<st.width();i++) out<<left(row[i],colsiz[i])<<";"; out<<"\n"; } return out; }
TMat<string> PLearn::StringTable::data [private] |
Definition at line 51 of file StringTable.h.
Referenced by PLearn::operator<<().
vector<string> PLearn::StringTable::fieldnames [private] |
Definition at line 52 of file StringTable.h.
Referenced by PLearn::operator<<().
map<string, int> PLearn::StringTable::rev_fn [private] |
Definition at line 53 of file StringTable.h.