00001 #ifndef Impala_Core_Table_QuidTable_h
00002 #define Impala_Core_Table_QuidTable_h
00003
00004 #include "Basis/Quid.h"
00005 #include "Util/QuickSort.h"
00006 #include "Core/Table/TableTem.h"
00007 #include "Core/Column/Find.h"
00008 #include "Core/Column/IsSorted.h"
00009 #include "Core/Database/RawDataSet.h"
00010
00011 namespace Impala
00012 {
00013 namespace Core
00014 {
00015 namespace Table
00016 {
00017
00018
00019 typedef TableTem<Column::ColumnTem<Quid> > QuidTableBaseType;
00020
00021
00022 class QuidTable : public QuidTableBaseType
00023 {
00024
00025 typedef TableTem<Column::ColumnTem<Int32>,
00026 Column::ColumnTem<Int32>,
00027 Column::ColumnTem<Int32> > ObjectGroupIndex;
00028
00029 public:
00030
00031 QuidTable() : QuidTableBaseType(0)
00032 {
00033 mObjectGroups = 0;
00034 SetInfo("QuidTable");
00035 SetColName(1, "quid");
00036 }
00037
00038 QuidTable(int size) : QuidTableBaseType(size)
00039 {
00040 mObjectGroups = 0;
00041 SetInfo("QuidTable");
00042 SetColName(1, "quid");
00043 }
00044
00045 int
00046 GetIndex(Quid q)
00047 {
00048 Column::ColumnTem<Quid>* column = GetColumn1();
00049 return Find(column, q, 0, Size());
00050 }
00051
00052 bool
00053 Contains(Quid q)
00054 {
00055 return (GetIndex(q) != Size());
00056 }
00057
00058 int
00059 GetFirstQuidObject(int obj)
00060 {
00061 for (int i=0 ; i<mObjectGroups->Size() ; i++)
00062 if (mObjectGroups->Get1(i) == obj)
00063 return mObjectGroups->Get2(i);
00064 return Size();
00065 }
00066
00067 int
00068 GetNrQuidsObject(int obj)
00069 {
00070 for (int i=0 ; i<mObjectGroups->Size() ; i++)
00071 if (mObjectGroups->Get1(i) == obj)
00072 return mObjectGroups->Get3(i);
00073 return 0;
00074 }
00075
00076 void
00077 UpdateGroups()
00078 {
00079 if (mObjectGroups == 0)
00080 {
00081 mObjectGroups = new ObjectGroupIndex(0);
00082 mObjectGroups->SetColName(1, "object");
00083 mObjectGroups->SetColName(2, "firstQuidIdx");
00084 mObjectGroups->SetColName(3, "nrQuids");
00085 }
00086
00087 if (! Core::Column::IsSortedAscending(GetColumn1(), Size()))
00088 Util::QuickSort(GetColumn1()->GetData(), 0, Size()-1);
00089 mObjectGroups->SetEmpty();
00090 int lastObj = QuidObject(Get1(0));
00091 int first = 0;
00092 int nr = 1;
00093 for (int i=1 ; i<Size() ; i++)
00094 {
00095 int obj = QuidObject(Get1(i));
00096 if (obj != lastObj)
00097 {
00098 mObjectGroups->Add(lastObj, first, nr);
00099 lastObj = obj;
00100 first = i;
00101 nr = 1;
00102 }
00103 else
00104 {
00105 nr++;
00106 }
00107 }
00108 mObjectGroups->Add(lastObj, first, nr);
00109 }
00110
00111 int
00112 Diff(QuidTable* arg) const
00113 {
00114 if (Size() != arg->Size())
00115 {
00116 ILOG_ERROR("Diff: Size differs: " << Size() << " vs " <<
00117 arg->Size());
00118
00119 }
00120 int nDiff = 0;
00121 for (int i=0 ; i<Size() ; i++)
00122 {
00123 if (Get1(i) != arg->Get1(i))
00124 {
00125 ILOG_INFO("Quid " << i << " differs " << Get1(i) <<
00126 " vs " << arg->Get1(i));
00127 nDiff++;
00128 }
00129 }
00130 if (nDiff > 0)
00131 ILOG_ERROR("Found " << nDiff << " differences");
00132 return nDiff;
00133 }
00134
00135 virtual void
00136 Dump(Database::RawDataSet* dataSet, int from = 0, int to = -1)
00137 {
00138 if (to == -1 || to > Size())
00139 to = Size();
00140 if (to < from)
00141 to = from;
00142 std::cout << "Dumping table from " << from << " to " << to
00143 << " (table size=" << Size() << ", capacity=" << Capacity()
00144 << ")" << std::endl;
00145 for (int i=0 ; i<1 ; i++)
00146 std::cout << i+1 << "=" << GetColName(i+1) << ", ";
00147 std::cout << std::endl;
00148 for (int i=from ; i<to ; i++)
00149 {
00150 Quid quid = Get1(i);
00151 if (dataSet)
00152 std::cout << dataSet->QuidToString(quid, true) << std::endl;
00153 else
00154 std::cout << QuidObj(quid) << std::endl;
00155 }
00156 std::cout << std::endl;
00157 }
00158
00159 void
00160 DumpGroup()
00161 {
00162 mObjectGroups->Dump();
00163 int maxNr = -1;
00164 for (int i=0 ; i<mObjectGroups->Size() ; i++)
00165 {
00166 int nr = mObjectGroups->Get3(i);
00167 if (nr > maxNr)
00168 maxNr = nr;
00169 }
00170 std::cout << "maxNr = " << maxNr << std::endl;
00171 }
00172
00179 void
00180 SetQuidSet(int setId)
00181 {
00182 for(int i=0 ; i<Size() ; ++i)
00183 {
00184 Quid q = Get1(i);
00185 Set1(i, MakeQuid(QuidClass(q), setId, QuidObject(q), QuidId(q)));
00186 }
00187 }
00188
00189 private:
00190
00191 ObjectGroupIndex* mObjectGroups;
00192
00193 ILOG_VAR_DEC;
00194 };
00195
00196 ILOG_VAR_INIT(QuidTable, Impala.Core.Table);
00197
00198 }
00199 }
00200 }
00201
00202 #endif