librcsb-core-wrapper  1.000
ITTable.h
Go to the documentation of this file.
1 //$$FILE$$
2 //$$VERSION$$
3 //$$DATE$$
4 //$$LICENSE$$
5 
6 
14 #ifndef ITTABLE_H
15 #define ITTABLE_H
16 
17 
18 #include <float.h>
19 
20 #include <string>
21 #include <vector>
22 #include <map>
23 
24 #include <rcsb/GenString.h>
25 #include <rcsb/TTable.h>
26 #include <rcsb/Serializer.h>
27 
28 
29 typedef std::multimap<std::string, unsigned int, StringLess> tIndex;
30 
31 
51 class ITTable
52 {
53  public:
55  {
58  };
59 
61  {
62  eEQUAL = 0,
67  };
68 
70  {
71  eFORWARD = 0,
73  };
74 
75  static const unsigned char DT_STRING_VAL = 1;
76  static const unsigned char DT_INTEGER_VAL = 2;
77  // static const unsigned char DT_DOUBLE_VAL = 3;
78 
79  // Sets string comparison case sensitive
80  static const unsigned char CASE_SENSE = 0x00;
81  // Sets string comparison case insensitive
82  static const unsigned char CASE_INSENSE = 0x01;
83  // Sets string comparison to be sensitive to whitespace
84  static const unsigned char W_SPACE_SENSE = 0x00;
85  // Sets string comparison to ignore repeating whitspace.
86  // Also ignores leading and trailing whitespace
87  static const unsigned char W_SPACE_INSENSE = 0x02;
88  // string datatype
89  static const unsigned char DT_STRING = DT_STRING_VAL << 4;
90  // integer datatype
91  static const unsigned char DT_INTEGER = DT_INTEGER_VAL << 4;
92  // VLAD FEATURE NOT WORKING double is not working, maybe integer. check it // double datatype
93  // static const unsigned char DT_DOUBLE = DT_DOUBLE_VAL << 4;
94 
107  ITTable();
108 
125  ITTable(eOrientation orient);
126 
143  ITTable(const ITTable& inTable);
144 
158  ~ITTable();
159 
175  ITTable& operator=(const ITTable& inTable);
176 
190  inline unsigned int GetNumColumns() const;
191 
220  void AddColumn(const std::string& colName,
221  const std::vector<std::string>& col = std::vector<std::string>());
222 
265  void InsertColumn(const std::string& colName,
266  const std::string& atColName, const std::vector<std::string>& col =
267  std::vector<std::string>());
268 
297  void FillColumn(const std::string& colName,
298  const std::vector<std::string>& col);
299 
325  void AppendToColumn(const std::string& colName, const std::string& cell);
326 
352  void AppendToColumn(const std::string& colName,
353  const std::vector<std::string>& col);
354 
373  void GetColumn(std::vector<std::string>& col, const std::string& colName);
374 
406  void GetColumn(std::vector<std::string>& col, const std::string& colName,
407  const unsigned int fromRowIndex, unsigned int toRowIndex);
408 
432  void GetColumn(std::vector<std::string>& col, const std::string& colName,
433  const std::vector<unsigned int>& rowIndex);
434 
451  void ClearColumn(const std::string& colName);
452 
469  void DeleteColumn(const std::string& colName);
470 
484  inline unsigned int GetNumRows() const;
485 
522  unsigned int AddRow(const std::vector<std::string>& row =
523  std::vector<std::string>());
524 
571  unsigned int InsertRow(const unsigned int atRowIndex,
572  const std::vector<std::string>& row = std::vector<std::string>());
573 
598  void FillRow(const unsigned int rowIndex,
599  const std::vector<std::string>& row);
600 
640  void GetRow(std::vector<std::string>& row, const unsigned int rowIndex,
641  const std::string& fromColName = std::string(),
642  const std::string& toColName = std::string());
643 
659  void ClearRow(const unsigned int rowIndex);
660 
679  void DeleteRow(const unsigned int rowIndex);
680 
698  void DeleteRows(const std::vector<unsigned int>& rows);
699 
713  inline unsigned int GetLastRowIndex();
714 
738  void UpdateCell(const unsigned int rowIndex, const std::string& colName,
739  const std::string& value);
740 
762  const std::string& operator()(const unsigned int rowIndex,
763  const std::string& colName) const;
764 
789  void SetFlags(const std::string& colName, const unsigned char flags);
790 
808  unsigned char GetDataType(const std::string& colName);
809 
838 #ifdef VLAD_SECOND_ITTABLE
839  unsigned int FindFirst(const std::vector<std::string>& targets,
840  const std::vector<std::string>& colNames,
841  const std::string& indexName = std::string());
842 #endif
843 
867 #ifdef VLAD_SECOND_ITTABLE
868  void Search(std::vector<unsigned int>& res, const std::string& target,
869  const std::string& colName, const eSearchType searchType = eEQUAL);
870 #endif
871 
901 #ifdef VLAD_SECOND_ITTABLE
902  void Search(std::vector<unsigned int>& res,
903  const std::vector<std::string>& targets,
904  const std::vector<std::string>& colNames,
905  const eSearchType searchType = eEQUAL,
906  const std::string& indexName = std::string());
907 #endif
908 
939  void FindDuplicateRows(std::vector<std::pair<unsigned int,
940  unsigned int> >& duplRows,
941  const std::vector<std::string>& colNames, const bool keepDuplRows,
942  const eSearchDir searchDir = eFORWARD);
943 
944  /* \todo Figure this out. */
945  void ValidateOptions(unsigned int colIndex);
946  void UpdateIndex(const unsigned int indexIndex,
947  const unsigned int rowIndex);
948  void InsertIndexEntry(const unsigned int indexIndex,
949  const unsigned int rowIndex);
950  void DeleteIndexEntry(const unsigned int indexIndex,
951  const unsigned int rowIndex);
952  void VerifyColumnsIndices(const std::vector<unsigned int>& colIndices);
953  int FindIndex(const std::vector<unsigned int>& colIndices);
954  int SetFlags(const unsigned char newOpts, const unsigned int colIndex);
955  unsigned int FindFirst(const std::vector<std::string>& targets,
956  const std::vector<unsigned int>& colIndices, const unsigned int indexIndex);
957 
958  void Search(std::vector<unsigned int>& res,
959  const std::vector<std::string>& targets,
960  const std::vector<unsigned int>& colIndices,
961  const unsigned int indexIndex,
962  const eSearchType searchType = eEQUAL);
963  void DeleteIndex(const unsigned int indexIndex);
964  void Search(std::vector<unsigned int>& res, const std::string& target,
965  const unsigned int colIndex, const eSearchType searchType = eEQUAL);
966  void FindDuplicateRows(const std::vector<unsigned int>& colIndices,
967  std::vector<std::pair<unsigned int, unsigned int> >& duplRows,
968  const bool keep, const eSearchDir searchDir = eFORWARD);
969  void RebuildIndex(const unsigned int indexIndex);
970  void InsertColumn(const unsigned int colIndex,
971  const std::vector<std::string>& col = std::vector<std::string>());
972  void InsertColumn(const unsigned int colIndex,
973  std::vector<std::string>::const_iterator colBeg,
974  std::vector<std::string>::const_iterator colEnd);
975  void Clear();
976  const std::string& operator()(const unsigned int rowIndex,
977  const unsigned int colIndex) const;
978  int UpdateCell(const std::string& cell, const unsigned int colIndex,
979  const unsigned int rowIndex);
980  void FillColumn(const std::vector<std::string>& col,
981  const unsigned int colIndex);
982  void FillColumn(const unsigned int colIndex,
983  std::vector<std::string>::const_iterator colBeg,
984  std::vector<std::string>::const_iterator colEnd);
985  void AppendToColumn(const unsigned int colIndex,
986  const std::vector<std::string>& col);
987  void AppendToColumn(const unsigned int colIndex, const std::string& cell);
988  void CreateColumn(const unsigned int atColIndex,
989  const std::vector<std::string>& col = std::vector<std::string>());
990  void CreateColumn(const unsigned int atColIndex,
991  std::vector<std::string>::const_iterator colBeg,
992  std::vector<std::string>::const_iterator colEnd);
993  void GetColumn(std::vector<std::string>& col, const unsigned int colIndex,
994  const unsigned int fromRowIndex, unsigned int toRowIndex);
995  void GetColumn(std::vector<std::string>& col, const unsigned int colIndex,
996  const std::vector<unsigned int>& rowIndex);
997  void ClearColumn(const unsigned int colIndex);
998  void DeleteColumn(const unsigned int colIndex);
999  void GetColumn(std::vector<std::string>& col, const unsigned int colIndex);
1000  void GetRow(std::vector<std::string>& row, const unsigned int rowIndex,
1001  const unsigned int fromColIndex, unsigned int toColIndex);
1002  const std::vector<std::string>& GetRow(const unsigned int rowIndex);
1004  void CreateIndex(const std::vector<unsigned int>& colIndices,
1005  const unsigned int unique = 0);
1006 
1010  void SetSerializer(Serializer* ser);
1011 
1015  int WriteObject(Serializer* ser, int& size);
1016 
1020  int GetObject(UInt32 index, Serializer* ser);
1021 
1025  int Read(unsigned int indexInFile, Serializer* ser);
1026 
1030  int Write(Serializer* ser, int& size);
1031 
1035  void RebuildIndices();
1036 
1037  void InsertEntry(const unsigned int rowIndex);
1038  void DeleteEntry(const unsigned int rowIndex);
1039 
1043  inline unsigned int GetNumIndices();
1044 
1048  void GetColumn(std::vector<std::string>& col, const unsigned int colIndex,
1049  const unsigned int indexIndex);
1050 
1051  private:
1052  // number of digit DBL_MIN_10_EXP, letter e is not included in size
1053  static const unsigned int EXPONENT = 4;
1054  static const unsigned int MAX_PRECISION = DBL_DIG;
1055  //???DBL_MANT_DIG;
1056  static const unsigned int MANTISSA = MAX_PRECISION + 2;
1057  static const unsigned int INT_LIMIT = 11;
1058 
1059  // datatype mask
1060  static const unsigned char DT_MASK = 15 << 4;
1061  // string comparison sensitivity mask
1062  static const unsigned char SC_MASK = 0x01;
1063  // white space sensitivity mask
1064  static const unsigned char WS_MASK = 0x02;
1065  static const unsigned char LAST_DT_VALUE = 3;
1066  static const unsigned int DEFAULT_PRECISION = MAX_PRECISION;
1067  static const unsigned char DEFAULT_OPTIONS;
1068 
1069  // static const string _version;
1070 
1071  TTable _ttable;
1072 
1073  eOrientation _orient;
1074 
1075  Serializer* _ser;
1076 
1077  std::vector<unsigned char> _compare_opts;
1078 
1079  std::vector<std::vector<unsigned int> > _listsOfColumns;
1080  std::vector<unsigned int> _unique;
1081  std::vector<tIndex> _indices;
1082 
1083  bool AreListsOfColumnsValid(const std::vector<unsigned int>& colIndices);
1084  void CreateKey(const std::vector<unsigned int>& colIndices);
1085 
1086  void Init();
1087 
1089  GetCompareType(const std::vector<unsigned int>& colIndices);
1090 
1091  std::string CellValue(const unsigned int colIndex,
1092  const unsigned int rowIndex);
1093  std::string ConvertString(const std::string& value,
1094  const unsigned int colIndex);
1095  std::string MultiStringsValue(const std::vector<std::string>& values,
1096  const std::vector<unsigned int>& colIndices);
1097  std::string SubRowValue(const std::vector<unsigned int>& colIndices,
1098  const unsigned int rowIndex);
1099  std::string AggregateRow(const std::vector<unsigned int>& colIndices,
1100  const unsigned int rowIndex);
1101 
1102  inline void AppendToAndDelimit(std::string& to,
1103  const std::string& appending);
1104 
1105  std::string CreateInternalIndexName(const unsigned int indexIndex);
1106  void ClearIndex(const unsigned int indexIndex);
1107 
1108  void UpdateIndices(const unsigned int rowIndex);
1109  void ClearIndices();
1110 
1111  bool IsColumnInIndex(const unsigned int indexIndex,
1112  const unsigned int colIndex);
1113 
1114  int FindKeyIndex();
1115 
1116  void UpdateColListOnColInsert(const unsigned int colIndex);
1117  void UpdateColListOnColDelete(const unsigned int colIndex);
1118  void UpdateIndicesOnCellUpdate(const unsigned int rowIndex,
1119  const unsigned int colIndex);
1120 
1121  void ConvertToInt(const std::string& a, std::string& ret);
1122  void ConvertDouble(const std::string& a, std::string& ret);
1123  void ConvertToLowerNoWhiteSpace(const std::string& a, std::string& ret);
1124 
1125  void Print(unsigned int indexIndex);
1126 };
1127 
1128 
1129 std::ostream& operator<<(std::ostream& out, const ITTable& isTable);
1130 
1131 
1132 inline unsigned int ITTable::GetLastRowIndex()
1133 {
1134 
1135  return(GetNumRows() - 1);
1136 
1137 }
1138 
1139 
1140 inline unsigned int ITTable::GetNumIndices()
1141 {
1142 
1143  return(_listsOfColumns.size());
1144 
1145 }
1146 
1147 
1148 inline void ITTable::AppendToAndDelimit(std::string& to,
1149  const std::string& appending)
1150 {
1151 
1152  to += appending;
1153  // VLAD HARDCODED CONST
1154  to += " ";
1155 
1156 }
1157 
1158 
1159 inline unsigned int ITTable::GetNumColumns() const
1160 {
1161  if (_orient == eCOLUMN_WISE)
1162  return(_ttable.GetNumTuples());
1163  else
1164  return(_ttable.GetNumColumns());
1165 }
1166 
1167 inline unsigned int ITTable::GetNumRows() const
1168 {
1169  if (_orient == eCOLUMN_WISE)
1170  return(_ttable.GetNumColumns());
1171  else
1172  return(_ttable.GetNumTuples());
1173 }
1174 
1175 #endif // ITTABLE_H