ITTable.h

Go to the documentation of this file.
00001 //$$FILE$$
00002 //$$VERSION$$
00003 //$$DATE$$
00004 //$$LICENSE$$
00005 
00006 
00014 #ifndef ITTABLE_H
00015 #define ITTABLE_H
00016 
00017 
00018 #include <float.h>
00019 
00020 #include <string>
00021 #include <vector>
00022 #include <map>
00023 
00024 #include "GenString.h"
00025 #include "TTable.h"
00026 #include "Serializer.h"
00027 
00028 
00029 using namespace std;
00030 
00031 
00032 typedef multimap<string, unsigned int, StringCompare> tIndex;
00033 
00034 
00054 class ITTable
00055 {
00056   public:
00057     enum eOrientation
00058     {
00059         eCOLUMN_WISE = 0,
00060         eROW_WISE
00061     };
00062 
00063     enum eSearchType
00064     {
00065         eEQUAL = 0,
00066         eLESS_THAN,
00067         eLESS_THAN_OR_EQUAL,
00068         eGREATER_THAN,
00069         eGREATER_THAN_OR_EQUAL
00070     };
00071 
00072     enum eSearchDir
00073     {
00074         eFORWARD = 0,
00075         eBACKWARD
00076     };
00077 
00078     static const unsigned char DT_STRING_VAL = 1; 
00079     static const unsigned char DT_INTEGER_VAL = 2;
00080     // static const unsigned char DT_DOUBLE_VAL = 3;
00081 
00082     // Sets string comparison case sensitive
00083     static const unsigned char CASE_SENSE = 0x00;
00084     // Sets string comparison case insensitive
00085     static const unsigned char CASE_INSENSE = 0x01;
00086     // Sets string comparison to be sensitive to whitespace
00087     static const unsigned char W_SPACE_SENSE = 0x00;
00088     // Sets string comparison to ignore repeating whitspace.  
00089     // Also ignores leading and trailing whitespace
00090     static const unsigned char W_SPACE_INSENSE = 0x02;
00091     // string datatype
00092     static const unsigned char DT_STRING  = DT_STRING_VAL  << 4;
00093     // integer datatype
00094     static const unsigned char DT_INTEGER = DT_INTEGER_VAL << 4;
00095     // VLAD FEATURE NOT WORKING double is not working, maybe integer. check it      // double datatype
00096     // static const unsigned char DT_DOUBLE  = DT_DOUBLE_VAL  << 4;
00097 
00110     ITTable();
00111 
00128     ITTable(eOrientation orient);
00129 
00146     ITTable(const ITTable& inTable);
00147 
00161     ~ITTable();
00162  
00178     ITTable& operator=(const ITTable& inTable);
00179 
00193     inline unsigned int GetNumColumns() const;
00194 
00223     void AddColumn(const string& colName, const vector<string>& col =
00224       vector<string> (0));
00225 
00268     void InsertColumn(const string& colName,
00269       const string& atColName, const vector<string>& col =
00270       vector<string> (0));
00271 
00300     void FillColumn(const string& colName, const vector<string>& col);
00301 
00327     void AppendToColumn(const string& colName, const string& cell);
00328 
00354     void AppendToColumn(const string& colName, const vector<string>& col);
00355 
00374     void GetColumn(vector<string>& col, const string& colName);
00375 
00407     void GetColumn(vector<string>& col, const string& colName,
00408       const unsigned int fromRowIndex, unsigned int toRowIndex);
00409 
00433     void GetColumn(vector<string>& col, const string& colName,
00434       const vector<unsigned int>& rowIndex);
00435 
00452     void ClearColumn(const string& colName);
00453 
00470     void DeleteColumn(const string& colName);
00471 
00485     inline unsigned int GetNumRows() const;
00486 
00523     unsigned int AddRow(const vector<string>& row = vector<string> (0));
00524 
00571     unsigned int InsertRow(const unsigned int atRowIndex,
00572       const vector<string>& row = vector<string> (0));
00573 
00598     void FillRow(const unsigned int rowIndex, const vector<string>& row);
00599 
00639     void GetRow(vector<string>& row, const unsigned int rowIndex,
00640       const string& fromColName = String::Empty, const string& toColName =
00641       String::Empty);
00642 
00658     void ClearRow(const unsigned int rowIndex);
00659 
00678     void DeleteRow(const unsigned int rowIndex);
00679 
00697     void DeleteRows(const vector<unsigned int>& rows);
00698 
00712     inline unsigned int GetLastRowIndex();
00713 
00737     void UpdateCell(const unsigned int rowIndex, const string& colName,
00738       const string& value);
00739 
00761     const string& operator()(const unsigned int rowIndex,
00762       const string& colName) const;
00763 
00788     void SetFlags(const string& colName, const unsigned char flags);
00789 
00807     unsigned char GetDataType(const string& colName);
00808 
00837 #ifdef VLAD_SECOND_ITTABLE
00838     unsigned int FindFirst(const vector<string>& targets,
00839       const vector<string>& colNames,
00840       const string& indexName = String::Empty);
00841 #endif
00842 
00866 #ifdef VLAD_SECOND_ITTABLE
00867     void Search(vector<unsigned int>& res, const string& target,
00868       const string& colName, const eSearchType searchType = eEQUAL);
00869 #endif
00870 
00900 #ifdef VLAD_SECOND_ITTABLE
00901     void Search(vector<unsigned int>& res, const vector<string>& targets,
00902       const vector<string>& colNames, const eSearchType searchType = eEQUAL,
00903       const string& indexName = String::Empty);
00904 #endif
00905 
00936     void FindDuplicateRows(vector<pair<unsigned int, unsigned int> >& duplRows,
00937       const vector<string>& colNames, const bool keepDuplRows,
00938       const eSearchDir searchDir = eFORWARD);
00939 
00940     /* \todo Figure this out. */
00941     void ValidateOptions(unsigned int colIndex);
00942     void UpdateIndex(const unsigned int indexIndex,
00943       const unsigned int rowIndex);
00944     void InsertIndexEntry(const unsigned int indexIndex,
00945       const unsigned int rowIndex);
00946     void DeleteIndexEntry(const unsigned int indexIndex,
00947       const unsigned int rowIndex);
00948     void VerifyColumnsIndices(const vector<unsigned int>& colIndices);
00949     int FindIndex(const vector<unsigned int>& colIndices);
00950     int SetFlags(const unsigned char newOpts, const unsigned int colIndex);
00951     unsigned int FindFirst(const vector<string>& targets,
00952       const vector<unsigned int>& colIndices, const unsigned int indexIndex);
00953 
00954     void Search(vector<unsigned int>& res, const vector<string>& targets,
00955       const vector<unsigned int>& colIndices, const unsigned int indexIndex,
00956       const eSearchType searchType = eEQUAL);
00957     void DeleteIndex(const unsigned int indexIndex);
00958     void Search(vector<unsigned int>& res, const string& target,
00959       const unsigned int colIndex, const eSearchType searchType = eEQUAL);
00960     void FindDuplicateRows(const vector<unsigned int>& colIndices,
00961       vector<pair<unsigned int, unsigned int> >& duplRows,
00962       const bool keep, const eSearchDir searchDir = eFORWARD);
00963     void RebuildIndex(const unsigned int indexIndex);
00964     void InsertColumn(const unsigned int colIndex,
00965       const vector<string>& col = vector<string> (0));
00966     void InsertColumn(const unsigned int colIndex,
00967       vector<string>::const_iterator colBeg,
00968       vector<string>::const_iterator colEnd);
00969     void Clear();
00970     const string& operator()(const unsigned int rowIndex,
00971       const unsigned int colIndex) const;
00972     int UpdateCell(const string& cell, const unsigned int colIndex,
00973       const unsigned int rowIndex);
00974     void FillColumn(const vector<string>& col, const unsigned int colIndex);
00975     void FillColumn(const unsigned int colIndex,
00976       vector<string>::const_iterator colBeg,
00977       vector<string>::const_iterator colEnd);
00978     void AppendToColumn(const unsigned int colIndex, const vector<string>& col);
00979     void AppendToColumn(const unsigned int colIndex, const string& cell);
00980     void CreateColumn(const unsigned int atColIndex,
00981       const vector<string>& col = vector<string> (0));
00982     void CreateColumn(const unsigned int atColIndex,
00983       vector<string>::const_iterator colBeg,
00984       vector<string>::const_iterator colEnd);
00985     void GetColumn(vector<string>& col, const unsigned int colIndex,
00986       const unsigned int fromRowIndex, unsigned int toRowIndex);
00987     void GetColumn(vector<string>& col, const unsigned int colIndex,
00988       const vector<unsigned int>& rowIndex);
00989     void ClearColumn(const unsigned int colIndex);
00990     void DeleteColumn(const unsigned int colIndex);
00991     void GetColumn(vector<string>& col, const unsigned int colIndex);
00992     void GetRow(vector<string>& row, const unsigned int rowIndex,
00993       const unsigned int fromColIndex, unsigned int toColIndex);
00994     const vector<string>& GetRow(const unsigned int rowIndex);
00995     eOrientation GetOrientation();
00996     void CreateIndex(const vector<unsigned int>& colIndices,
00997       const unsigned int unique = 0);
00998     
01002     void SetSerializer(Serializer* ser);
01003 
01007     int WriteObject(Serializer* ser, int& size);
01008 
01012     int GetObject(UInt32 index, Serializer* ser);
01013 
01017     int Read(unsigned int indexInFile, Serializer* ser);
01018 
01022     int Write(Serializer* ser, int& size);
01023 
01027     void RebuildIndices();
01028 
01029     void InsertEntry(const unsigned int rowIndex);
01030     void DeleteEntry(const unsigned int rowIndex);
01031 
01035     inline unsigned int GetNumIndices();
01036 
01040     void GetColumn(vector<string>& col, const unsigned int colIndex,
01041       const unsigned int indexIndex);
01042 
01043   private:
01044     // number of digit DBL_MIN_10_EXP, letter e is not included in size
01045     static const unsigned int EXPONENT      =  4;
01046     static const unsigned int MAX_PRECISION = DBL_DIG;
01047     //???DBL_MANT_DIG;
01048     static const unsigned int MANTISSA       =  MAX_PRECISION + 2;
01049     static const unsigned int INT_LIMIT      = 11;
01050 
01051     // datatype mask
01052     static const unsigned char DT_MASK        = 15 << 4;
01053     // string comparison sensitivity mask
01054     static const unsigned char SC_MASK        = 0x01;
01055     // white space sensitivity mask
01056     static const unsigned char WS_MASK        = 0x02;
01057     static const unsigned char LAST_DT_VALUE  = 3;
01058     static const unsigned int  DEFAULT_PRECISION = MAX_PRECISION;
01059     static const unsigned char DEFAULT_OPTIONS;
01060 
01061     // static const string _version;
01062 
01063     TTable _ttable;
01064 
01065     eOrientation _orient;
01066 
01067     Serializer* _ser;
01068 
01069     vector<unsigned char> _compare_opts;
01070 
01071     vector<vector<unsigned int> > _listsOfColumns;
01072     vector<unsigned int> _unique;
01073     vector<tIndex> _indices;
01074 
01075     bool AreListsOfColumnsValid(const vector<unsigned int>& colIndices);
01076     void CreateKey(const vector<unsigned int>& colIndices);
01077 
01078     void Init();
01079 
01080     StringCompare::eCompareType
01081       GetCompareType(const vector<unsigned int>& colIndices);
01082 
01083     string CellValue(const unsigned int colIndex,
01084       const unsigned int rowIndex);
01085     string ConvertString(const string& value, const unsigned int colIndex);
01086     string MultiStringsValue(const vector<string>& values,
01087       const vector<unsigned int>& colIndices);
01088     string SubRowValue(const vector<unsigned int>& colIndices,
01089       const unsigned int rowIndex);
01090     string AggregateRow(const vector<unsigned int>& colIndices,
01091       const unsigned int rowIndex);
01092 
01093     inline void AppendToAndDelimit(string& to, const string& appending);
01094 
01095     string CreateInternalIndexName(const unsigned int indexIndex);
01096     void ClearIndex(const unsigned int indexIndex);
01097 
01098     void UpdateIndices(const unsigned int rowIndex);
01099     void ClearIndices();
01100 
01101     bool IsColumnInIndex(const unsigned int indexIndex,
01102       const unsigned int colIndex);
01103 
01104     int FindKeyIndex();
01105 
01106     void UpdateColListOnColInsert(const unsigned int colIndex);
01107     void UpdateColListOnColDelete(const unsigned int colIndex);
01108     void UpdateIndicesOnCellUpdate(const unsigned int rowIndex,
01109       const unsigned int colIndex);
01110 
01111     void ConvertToInt(const string& a, string& ret);
01112     void ConvertDouble(const string& a, string& ret);
01113     void ConvertToLowerNoWhiteSpace(const string& a, string& ret);
01114 
01115     void Print(unsigned int indexIndex);
01116 };
01117 
01118 
01119 ostream& operator<<(ostream& out, const ITTable& isTable);
01120 
01121 
01122 inline unsigned int ITTable::GetLastRowIndex()
01123 {
01124 
01125     return(GetNumRows() - 1);
01126 
01127 }
01128 
01129 
01130 inline unsigned int ITTable::GetNumIndices()
01131 {
01132 
01133     return(_listsOfColumns.size());
01134 
01135 }
01136 
01137 
01138 inline void ITTable::AppendToAndDelimit(string& to, const string& appending)
01139 {
01140 
01141     to += appending;
01142     // VLAD HARDCODED CONST
01143     to += " ";
01144 
01145 }
01146 
01147 
01148 inline unsigned int ITTable::GetNumColumns() const
01149 {
01150     if (_orient == eCOLUMN_WISE)
01151         return(_ttable.GetNumTuples());
01152     else
01153         return(_ttable.GetNumColumns());
01154 }
01155 
01156 inline unsigned int ITTable::GetNumRows() const
01157 {
01158     if (_orient == eCOLUMN_WISE)
01159         return(_ttable.GetNumColumns());
01160     else
01161         return(_ttable.GetNumTuples());
01162 }
01163 
01164 #endif // ITTABLE_H

Generated on Tue Feb 5 09:01:26 2008 for tables-v8.0 by  doxygen 1.5.1