00001
00002
00003
00004
00005
00006
00014 #ifndef ISTABLE_H
00015 #define ISTABLE_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 "FileNavigator.h"
00027
00028
00029 using namespace std;
00030
00031
00032 typedef multimap<string, unsigned int, StringCompare> tIndex;
00033
00034
00054 class ISTable
00055 {
00056 public:
00057 enum eOrientation
00058 {
00059 eCOLUMN_WISE = 0,
00060 eROW_WISE
00061 };
00062
00063 enum eTableDiff
00064 {
00065 eNONE = 0,
00066 eCASE_SENSE,
00067 eMORE_COLS,
00068 eLESS_COLS,
00069 eCOL_NAMES,
00070 eMORE_ROWS,
00071 eLESS_ROWS,
00072 eCELLS,
00073
00074 eMISSING,
00075
00076 eEXTRA
00077 };
00078
00079 enum eSearchType
00080 {
00081 eEQUAL = 0,
00082 eLESS_THAN,
00083 eLESS_THAN_OR_EQUAL,
00084 eGREATER_THAN,
00085 eGREATER_THAN_OR_EQUAL
00086 };
00087
00088 enum eSearchDir
00089 {
00090 eFORWARD = 0,
00091 eBACKWARD
00092 };
00093
00094 static const unsigned char DT_STRING_VAL = 1;
00095 static const unsigned char DT_INTEGER_VAL = 2;
00096
00097
00098
00099 static const unsigned char CASE_SENSE = 0x00;
00100
00101 static const unsigned char CASE_INSENSE = 0x01;
00102
00103 static const unsigned char W_SPACE_SENSE = 0x00;
00104
00105
00106 static const unsigned char W_SPACE_INSENSE = 0x02;
00107
00108 static const unsigned char DT_STRING = DT_STRING_VAL << 4;
00109
00110 static const unsigned char DT_INTEGER = DT_INTEGER_VAL << 4;
00111
00112
00113
00131 ISTable(const StringCompare::eCompareType colCaseSense =
00132 StringCompare::eCASE_SENSITIVE);
00133
00154 ISTable(eOrientation orient, const StringCompare::eCompareType
00155 colCaseSense = StringCompare::eCASE_SENSITIVE);
00156
00174 ISTable(const string& name,
00175 const StringCompare::eCompareType colCaseSense =
00176 StringCompare::eCASE_SENSITIVE);
00177
00198 ISTable(const string& name, eOrientation orient,
00199 const StringCompare::eCompareType colCaseSense =
00200 StringCompare::eCASE_SENSITIVE);
00201
00218 ISTable(const ISTable& inTable);
00219
00233 ~ISTable();
00234
00250 ISTable& operator=(const ISTable& inTable);
00251
00273 eTableDiff operator==(ISTable& inTable);
00274
00288 inline const string GetName() const;
00289
00303 void Rename(const string& name);
00304
00318 inline unsigned int GetNumColumns() const;
00319
00333 void GetColumnNames(vector<string>& colNames) const;
00334
00349 bool IsColumnPresent(const string& colName);
00350
00379 void AddColumn(const string& colName, const vector<string>& col =
00380 vector<string> (0));
00381
00424 void InsertColumn(const string& colName,
00425 const string& afColName, const vector<string>& col =
00426 vector<string> (0));
00427
00456 void FillColumn(const string& colName, const vector<string>& col);
00457
00483 void AppendToColumn(const string& colName, const string& cell);
00484
00510 void AppendToColumn(const string& colName, const vector<string>& col);
00511
00530 void GetColumn(vector<string>& col, const string& colName);
00531
00563 void GetColumn(vector<string>& col, const string& colName,
00564 const unsigned int fromRowIndex, unsigned int toRowIndex);
00565
00589 void GetColumn(vector<string>& col, const string& colName,
00590 const vector<unsigned int>& rowIndex);
00591
00614 void RenameColumn(const string& oldColName, const string& newColName);
00615
00632 void ClearColumn(const string& colName);
00633
00650 void DeleteColumn(const string& colName);
00651
00665 inline unsigned int GetNumRows() const;
00666
00703 unsigned int AddRow(const vector<string>& row = vector<string> (0));
00704
00751 unsigned int InsertRow(const unsigned int atRowIndex,
00752 const vector<string>& row = vector<string> (0));
00753
00778 void FillRow(const unsigned int rowIndex, const vector<string>& row);
00779
00819 void GetRow(vector<string>& row, const unsigned int rowIndex,
00820 const string& fromColName = String::Empty, const string& toColName =
00821 String::Empty);
00822
00838 void ClearRow(const unsigned int rowIndex);
00839
00858 void DeleteRow(const unsigned int rowIndex);
00859
00877 void DeleteRows(const vector<unsigned int>& rows);
00878
00892 inline unsigned int GetLastRowIndex();
00893
00917 void UpdateCell(const unsigned int rowIndex, const string& colName,
00918 const string& value);
00919
00941 const string& operator()(const unsigned int rowIndex,
00942 const string& colName) const;
00943
00968 void SetFlags(const string& colName, const unsigned char flags);
00969
00987 unsigned char GetDataType(const string& colName);
00988
01017 unsigned int FindFirst(const vector<string>& targets,
01018 const vector<string>& colNames,
01019 const string& indexName = String::Empty);
01020
01044 void Search(vector<unsigned int>& res, const string& target,
01045 const string& colName, const eSearchType searchType = eEQUAL);
01046
01076 void Search(vector<unsigned int>& res, const vector<string>& targets,
01077 const vector<string>& colNames, const eSearchType searchType = eEQUAL,
01078 const string& indexName = String::Empty);
01079
01111 void FindDuplicateRows(vector<pair<unsigned int, unsigned int> >& duplRows,
01112 const vector<string>& colNames, const bool keepDuplRows,
01113 const eSearchDir searchDir = eFORWARD);
01114
01118 inline void SetModified(const bool modified);
01119
01123 inline bool GetModified();
01124
01128 void SetFileNavigator(FileNavigator* fileNavigator);
01129
01133 int WriteObject(FileNavigator* fileNav, int& size);
01134
01138 int GetObject(Word index, FileNavigator* fileNav);
01139
01143 void Read(unsigned int indexInFile);
01144
01148 int Write();
01149
01153
01154 void Merge(ISTable& inTable, unsigned int typeOfMerge = 0);
01155
01159 bool PrintDiff(ISTable& inTable);
01160
01164 void Print(const string& indexName);
01165
01169 inline bool IndexExists(const string& indexName);
01170
01174 void CreateIndex(const string& indexName, const vector<string>& colNames,
01175 const unsigned int unique = 0);
01176
01180 void UpdateIndex(const string& indexName, const unsigned int rowIndex);
01181
01185 void RebuildIndex(const string& indexName);
01186
01190 void RebuildIndices();
01191
01195 void DeleteIndex(const string& indexName);
01196
01200 inline unsigned int GetNumIndices();
01201
01205 void CreateKey(const vector<string>& colNames);
01206
01210 static void SetUnion(const vector<unsigned int>& a,
01211 const vector<unsigned int>& b, vector<unsigned int>& ret);
01212
01216 static void SetIntersect(const vector<unsigned int>& a,
01217 const vector<unsigned int>& b, vector<unsigned int>& ret);
01218
01222 unsigned int GetColumnIndex(const string& colName) const;
01223
01227 void GetColumnsIndices(vector<unsigned int>& colIndices,
01228 const vector<string>& colNames = vector<string> (0));
01229
01233 void _GetColumnsIndices(vector<unsigned int>& colIndices,
01234 const vector<string>& colNames);
01235
01239 void GetColumn(vector<string>& col, const string& colName,
01240 const string& indexName);
01241
01242 private:
01243
01244 static const unsigned int EXPONENT = 4;
01245 static const unsigned int MAX_PRECISION = DBL_DIG;
01246
01247 static const unsigned int MANTISSA = MAX_PRECISION + 2;
01248 static const unsigned int INT_LIMIT = 11;
01249
01250
01251 static const unsigned char DT_MASK = 15 << 4;
01252
01253 static const unsigned char SC_MASK = 0x01;
01254
01255 static const unsigned char WS_MASK = 0x02;
01256 static const unsigned char LAST_DT_VALUE = 3;
01257 static const unsigned int DEFAULT_PRECISION = MAX_PRECISION;
01258 static const unsigned char DEFAULT_OPTIONS;
01259
01260 static const string _version;
01261
01262 string _name;
01263
01264 TTable _ttable;
01265
01266 eOrientation _orient;
01267
01268 StringCompare::eCompareType _colCaseSense;
01269
01270 vector<string> _colNames;
01271
01272 vector<unsigned int> _precision;
01273 vector<unsigned char> _compare_opts;
01274
01275 vector<string> _indexNames;
01276 vector<vector<unsigned int> > _listsOfColumns;
01277 vector<unsigned int> _unique;
01278
01279 FileNavigator* _fnav;
01280
01281 bool _modified;
01282
01283 vector<tIndex> _indices;
01284
01285 void InsertColumn(const string& colName, const unsigned int colIndex,
01286 const vector<string>& col = vector<string> (0));
01287 void CreateColumn(const string& colName, const unsigned int colIndex,
01288 const vector<string>& col = vector<string> (0));
01289 void FillColumn(const vector<string>& col, const unsigned int colIndex);
01290 void AppendToColumn(const unsigned int colIndex, const vector<string>& col);
01291 int UpdateCell(const string& cell, const unsigned int colIndex,
01292 const unsigned int rowIndex);
01293 const string& operator()(const unsigned int rowIndex,
01294 const unsigned int colIndex) const;
01295 int SetFlags(const unsigned char newOpts, const unsigned int colIndex);
01296 void FindDuplicateRows(const vector<unsigned int>& colIndices,
01297 vector<pair<unsigned int, unsigned int> >& duplRows,
01298 const unsigned int keep, const eSearchDir searchDir = eFORWARD);
01299 void VerifyColumnsIndices(const vector<unsigned int>& colIndices);
01300 bool AreListsOfColumnsValid(const vector<unsigned int>& colIndices);
01301 void CreateIndex(const string& indexName,
01302 const vector<unsigned int>& colIndices, const unsigned int unique = 0);
01303 void CreateKey(const vector<unsigned int>& colIndices);
01304 unsigned int FindFirst(const vector<string>& targets,
01305 const vector<unsigned int>& colIndices,
01306 const string& indexName = String::Empty);
01307 void Search(vector<unsigned int>& res, const string& target,
01308 const unsigned int colIndex, const eSearchType searchType = eEQUAL);
01309 void Search(vector<unsigned int>& res, const vector<string>& targets,
01310 const vector<unsigned int>& colIndices,
01311 const eSearchType searchType = eEQUAL,
01312 const string& indexName = String::Empty);
01313
01314 void Init();
01315 void Clear();
01316
01317 StringCompare::eCompareType
01318 GetCompareType(const vector<unsigned int>& colIndices);
01319
01320 string CellValue(const unsigned int colIndex,
01321 const unsigned int rowIndex);
01322 string ConvertString(const string& value, const unsigned int colIndex);
01323 string MultiStringsValue(const vector<string>& values,
01324 const vector<unsigned int>& colIndices);
01325 string SubRowValue(const vector<unsigned int>& colIndices,
01326 const unsigned int rowIndex);
01327 string AggregateRow(const vector<unsigned int>& colIndices,
01328 const unsigned int rowIndex);
01329
01330 inline void AppendToAndDelimit(string& to, const string& appending);
01331
01332 void ValidateOptions(unsigned int colIndex);
01333
01334 string CreateInternalIndexName(const unsigned int indexIndex);
01335 void UpdateIndex(const unsigned int indexIndex,
01336 const unsigned int rowIndex);
01337 void RebuildIndex(const unsigned int indexIndex);
01338 void ClearIndex(const unsigned int indexIndex);
01339 void DeleteIndex(const unsigned int indexIndex);
01340
01341 int FindIndex(const string& indexName);
01342 int FindIndex(const vector<unsigned int>& colIndices);
01343
01344 void UpdateIndices(const unsigned int rowIndex);
01345 void ClearIndices();
01346
01347 bool IsColumnInIndex(const unsigned int indexIndex,
01348 const unsigned int colIndex);
01349
01350 int FindKeyIndex();
01351
01352 void UpdateColListOnColInsert(const unsigned int colIndex);
01353 void UpdateColListOnColDelete(const unsigned int colIndex);
01354 void UpdateColListOnCellUpdate(const unsigned int rowIndex,
01355 const unsigned int colIndex);
01356
01357 unsigned int FindFirst(const vector<string>& targets,
01358 const vector<unsigned int>& colIndices, const unsigned int indexIndex);
01359
01360 void Search(vector<unsigned int>& res, const vector<string>& targets,
01361 const vector<unsigned int>& colIndices, const unsigned int indexIndex,
01362 const eSearchType searchType = eEQUAL);
01363 void Search(vector<unsigned int>& res, const vector<string>& targets,
01364 const vector<string>& colNames, const unsigned int indexIndex,
01365 const eSearchType searchType = eEQUAL);
01366
01367 int WriteObjectV7(FileNavigator*, int& size);
01368
01369 int GetObjectV7(Word index, FileNavigator*);
01370 int GetObjectV6(Word index, FileNavigator*);
01371 int GetObjectV3(Word index, FileNavigator*);
01372 int GetObjectV2(Word index, FileNavigator*);
01373 int GetObjectV1(Word index, FileNavigator*);
01374 int GetObjectV1_1(Word index, FileNavigator*);
01375
01376 void Print(unsigned int indexIndex);
01377
01378 void ConvertToInt(const string& a, string& ret);
01379 void ConvertDouble(const string& a, string& ret);
01380 void ConvertToLowerNoWhiteSpace(const string& a, string& ret);
01381 };
01382
01383
01384 ostream& operator<<(ostream& out, const ISTable& isTable);
01385
01386
01387 inline unsigned int ISTable::GetLastRowIndex()
01388 {
01389
01390 return(GetNumRows() - 1);
01391
01392 }
01393
01394
01395 inline unsigned int ISTable::GetNumIndices()
01396 {
01397
01398 return(_indexNames.size());
01399
01400 }
01401
01402
01403 inline bool ISTable::IndexExists(const string& indexName)
01404 {
01405
01406 int ret = FindIndex(indexName);
01407
01408 if (ret == -1)
01409 {
01410 return(false);
01411 }
01412 else
01413 {
01414 return(true);
01415 }
01416
01417 }
01418
01419
01420 inline void ISTable::AppendToAndDelimit(string& to, const string& appending)
01421 {
01422
01423 to += appending;
01424
01425 to += " ";
01426
01427 }
01428
01429
01430 inline void ISTable::SetModified(const bool modified)
01431 {
01432 _modified = modified;
01433 }
01434
01435
01436 inline bool ISTable::GetModified()
01437 {
01438 return _modified;
01439 }
01440
01441 inline const string ISTable::GetName() const
01442 {
01443 return(_name);
01444 }
01445
01446 inline unsigned int ISTable::GetNumColumns() const
01447 {
01448 return(_colNames.size());
01449 }
01450
01451 inline unsigned int ISTable::GetNumRows() const
01452 {
01453 if (_orient == eCOLUMN_WISE)
01454 return(_ttable.GetNumRows());
01455 else
01456 return(_ttable.GetNumColumns());
01457 }
01458
01459 #endif // ISTABLE_H