[2688] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | // Class BaseDataTable
|
---|
| 3 | // R. Ansari - Avril 2005
|
---|
| 4 | // (C) LAL-IN2P3/CNRS CEA-DAPNIA
|
---|
| 5 |
|
---|
| 6 | #ifndef BASEDTABLE_H_SEEN
|
---|
| 7 | #define BASEDTABLE_H_SEEN
|
---|
| 8 |
|
---|
| 9 | #include "machdefs.h"
|
---|
| 10 |
|
---|
| 11 | #include <iostream>
|
---|
| 12 | #include <string>
|
---|
| 13 | #include <vector>
|
---|
[2849] | 14 | #include <map>
|
---|
[2688] | 15 |
|
---|
[2827] | 16 | #include <complex>
|
---|
| 17 |
|
---|
[2688] | 18 | #include "ntupintf.h"
|
---|
| 19 | #include "dvlist.h"
|
---|
| 20 | #include "segdatablock.h"
|
---|
[2831] | 21 | #include "tvector.h"
|
---|
[2688] | 22 |
|
---|
| 23 | namespace SOPHYA {
|
---|
[2822] | 24 |
|
---|
| 25 | // Forward class declaration for Fits handler
|
---|
| 26 | template <class T> class FitsHandler;
|
---|
| 27 |
|
---|
[2849] | 28 | //! Class for representing and manipulating DataTable rows.
|
---|
| 29 | class DataTableRow {
|
---|
| 30 | public:
|
---|
| 31 | //! Constructor - For use by BaseDataTable class
|
---|
[2850] | 32 | DataTableRow( vector<string> const & colnames );
|
---|
[2849] | 33 | //! Copy constructor
|
---|
| 34 | DataTableRow( DataTableRow const & a );
|
---|
| 35 | //! Returns the value for column \b k. No bound checking performed
|
---|
| 36 | inline MuTyV operator[] (sa_size_t k) const { return mtv_[k]; }
|
---|
| 37 | //! Returns a reference to the value of column \b k. No bound checking performed
|
---|
| 38 | inline MuTyV& operator[] (sa_size_t k) { return mtv_[k]; }
|
---|
| 39 | //! Returns the value for column \b colname.
|
---|
[2851] | 40 | inline MuTyV operator[] (string const colname) const { return get(colname); }
|
---|
[2849] | 41 | //! Returns a reference to the value of column \b k.
|
---|
[2851] | 42 | inline MuTyV& operator[] (string const colname) { return get(colname); }
|
---|
[2849] | 43 | //! Returns the value for column \b colname.
|
---|
| 44 | MuTyV get(string const& colname) const;
|
---|
| 45 | //! Returns a reference to the value of column \b k.
|
---|
| 46 | MuTyV& get(string const& colname);
|
---|
| 47 | //! Acces to the MuTyV pointer
|
---|
| 48 | inline sa_size_t Size() const { return size_; }
|
---|
| 49 | //! Acces to the MuTyV pointer
|
---|
| 50 | inline MuTyV const * MTVPtr() const { return mtv_; }
|
---|
| 51 | //! Acces to the MuTyV pointer
|
---|
| 52 | inline MuTyV* MTVPtr() { return mtv_; }
|
---|
| 53 | //! prints the object content on the output stream
|
---|
| 54 | ostream& Print(ostream& os) const;
|
---|
| 55 | protected:
|
---|
| 56 | MuTyV * mtv_;
|
---|
| 57 | sa_size_t size_;
|
---|
| 58 | map<string , sa_size_t> nm2idx_;
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | /*! Prints the DataTableRow object content on the output stream ( */
|
---|
| 62 | inline ostream& operator << (ostream& s, DataTableRow const & row)
|
---|
| 63 | { row.Print(s); return(s); }
|
---|
| 64 |
|
---|
[3392] | 65 | // Forward class declaration for Thread-safe operations
|
---|
| 66 | class ThSafeOp;
|
---|
| 67 |
|
---|
[2808] | 68 | //! Interface definition for classes handling data in a table.
|
---|
[2688] | 69 | class BaseDataTable : public AnyDataObj , public NTupleInterface {
|
---|
| 70 | public:
|
---|
| 71 | // ===> DO NOT CHANGE EXISTING enum type VALUES !!!
|
---|
| 72 | enum FieldType {IntegerField=1, LongField=2,
|
---|
| 73 | FloatField=3, DoubleField=4,
|
---|
| 74 | ComplexField=5, DoubleComplexField=6,
|
---|
[2831] | 75 | StringField=7, DateTimeField=8};
|
---|
[2688] | 76 | // <=====================================================>
|
---|
| 77 |
|
---|
[2831] | 78 | //! Return the column type as a string (long or short depending on fgl=true/false)
|
---|
[2688] | 79 | static string ColTypeToString(FieldType ft, bool fgl=false);
|
---|
[2831] | 80 | //! Return the column type corresponding to \b sctyp
|
---|
| 81 | static FieldType StringToColType(string const & sctyp);
|
---|
[2688] | 82 |
|
---|
| 83 | // constructeur , destructeur
|
---|
| 84 | BaseDataTable(sa_size_t segsz=512);
|
---|
| 85 | virtual ~BaseDataTable();
|
---|
[3392] | 86 |
|
---|
| 87 | // Activate/deactivate thread-safe AddRow()/GetRow() operation
|
---|
| 88 | virtual void SetThreadSafe(bool fg=true);
|
---|
| 89 | //! Return true if AddRow()/GetRow() operations are thread-safe
|
---|
| 90 | inline bool IsThreadSafe() const { return ((mThS)?true:false); }
|
---|
[2688] | 91 |
|
---|
[2808] | 92 | //! Adds a column holding integer, named \b cnom
|
---|
[2688] | 93 | inline sa_size_t AddIntegerColumn(const char * cnom)
|
---|
| 94 | { return AddColumn(IntegerField, cnom); }
|
---|
[2808] | 95 | //! Adds a column holding integer, named \b cnom
|
---|
[2688] | 96 | inline sa_size_t AddIntegerColumn(string const & cnom)
|
---|
| 97 | { return AddColumn(IntegerField, cnom); }
|
---|
[2808] | 98 | //! Adds a column holding long integer, named \b cnom
|
---|
[2732] | 99 | inline sa_size_t AddLongColumn(const char * cnom)
|
---|
| 100 | { return AddColumn(LongField, cnom); }
|
---|
[2808] | 101 | //! Adds a column holding long integer, named \b cnom
|
---|
[2732] | 102 | inline sa_size_t AddLongColumn(string const & cnom)
|
---|
| 103 | { return AddColumn(LongField, cnom); }
|
---|
[2808] | 104 | //! Adds a column holding floating values (r_4), named \b cnom
|
---|
[2688] | 105 | inline sa_size_t AddFloatColumn(const char * cnom)
|
---|
| 106 | { return AddColumn(FloatField, cnom); }
|
---|
[2808] | 107 | //! Adds a column holding floating values (r_4), named \b cnom
|
---|
[2688] | 108 | inline sa_size_t AddFloatColumn(string const & cnom)
|
---|
| 109 | { return AddColumn(FloatField, cnom); }
|
---|
[2808] | 110 | //! Adds a column holding double values (r_8), named \b cnom
|
---|
[2688] | 111 | inline sa_size_t AddDoubleColumn(const char * cnom)
|
---|
| 112 | { return AddColumn(DoubleField, cnom); }
|
---|
[2808] | 113 | //! Adds a column holding double values (r_8), named \b cnom
|
---|
[2688] | 114 | inline sa_size_t AddDoubleColumn(string const & cnom)
|
---|
| 115 | { return AddColumn(DoubleField, cnom); }
|
---|
[2827] | 116 | //! Adds a column holding single precision complex values (complex<r_4>), named \b cnom
|
---|
| 117 | inline sa_size_t AddComplexColumn(const char * cnom)
|
---|
| 118 | { return AddColumn(ComplexField, cnom); }
|
---|
| 119 | //! Adds a column holding single precision complex values (complex<r_4>), named \b cnom
|
---|
| 120 | inline sa_size_t AddComplexColumn(string const & cnom)
|
---|
| 121 | { return AddColumn(ComplexField, cnom); }
|
---|
| 122 | //! Adds a column holding double precision complex values (complex<r_8>), named \b cnom
|
---|
| 123 | inline sa_size_t AddDoubleComplexColumn(const char * cnom)
|
---|
| 124 | { return AddColumn(DoubleComplexField, cnom); }
|
---|
| 125 | //! Adds a column holding double precision complex values (complex<r_8>), named \b cnom
|
---|
| 126 | inline sa_size_t AddDoubleComplexColumn(string const & cnom)
|
---|
| 127 | { return AddColumn(DoubleComplexField, cnom); }
|
---|
[2808] | 128 | //! Adds a column holding character strings, named \b cnom
|
---|
[2732] | 129 | inline sa_size_t AddStringColumn(const char * cnom)
|
---|
| 130 | { return AddColumn(StringField, cnom); }
|
---|
[2808] | 131 | //! Adds a column holding character strings, named \b cnom
|
---|
[2732] | 132 | inline sa_size_t AddStringColumn(string const & cnom)
|
---|
| 133 | { return AddColumn(StringField, cnom); }
|
---|
[2831] | 134 | //! Adds a column holding Date/Time value, named \b cnom
|
---|
| 135 | inline sa_size_t AddDateTimeColumn(const char * cnom)
|
---|
| 136 | { return AddColumn(DateTimeField, cnom); }
|
---|
| 137 | //! Adds a column holding Date/Time value, named \b cnom
|
---|
| 138 | inline sa_size_t AddDateTimeColumn(string const & cnom)
|
---|
| 139 | { return AddColumn(DateTimeField, cnom); }
|
---|
[2688] | 140 |
|
---|
| 141 | inline sa_size_t AddColumn(FieldType ft, const char * cnom)
|
---|
| 142 | { string nom = cnom; return AddColumn(ft, nom); }
|
---|
| 143 |
|
---|
| 144 | //! Pure virtual method for adding a new column to the Data Table
|
---|
| 145 | virtual sa_size_t AddColumn(FieldType ft, string const & cnom) = 0;
|
---|
| 146 | //! Duplicate the data table structure from the source Table
|
---|
| 147 | virtual sa_size_t CopyStructure(BaseDataTable const & a);
|
---|
| 148 | //! Checks if the two table have the same column structure
|
---|
| 149 | virtual bool CompareStructure(BaseDataTable const & a);
|
---|
| 150 |
|
---|
| 151 | // Verifie la validite du nom de colonne:envoie une exception
|
---|
| 152 | // si nom en double ou non valide
|
---|
| 153 | virtual bool CheckColName(string const & cnom);
|
---|
[2962] | 154 | // Verifie et corrige si besoin un nom de colonne
|
---|
| 155 | virtual bool CheckCorrectColName(string & cnom);
|
---|
[2688] | 156 |
|
---|
| 157 | // Acces to various counts and parameters
|
---|
[2808] | 158 | //! Return the number of lines (rows) in the table)
|
---|
[2849] | 159 | inline sa_size_t NRows() const { return mNEnt ; }
|
---|
| 160 | //! Return the number of lines (rows) in the table)
|
---|
[2688] | 161 | inline sa_size_t NEntry() const { return mNEnt ; }
|
---|
[2808] | 162 | //! Return the number of columns in the tables (number of cells in a row)
|
---|
[2849] | 163 | inline sa_size_t NCols() const { return mNames.size() ; }
|
---|
| 164 | //! Return the number of columns in the tables (number of cells in a row)
|
---|
[2688] | 165 | inline sa_size_t NVar() const { return mNames.size() ; }
|
---|
[2808] | 166 | //! Return the number of columns in the tables (number of cells in a row)
|
---|
[2688] | 167 | inline sa_size_t NVars() const { return mNames.size() ; }
|
---|
[2808] | 168 | //! Return the segment size (SegDBInterface objects corresponding to columns)
|
---|
[2688] | 169 | inline sa_size_t SegmentSize() const { return mSegSz ; }
|
---|
[2808] | 170 | //! Return the number of segments (SegDBInterface objects corresponding to columns)
|
---|
[2688] | 171 | inline sa_size_t NbSegments() const { return mNSeg ; }
|
---|
| 172 |
|
---|
[2849] | 173 | //! Return a compatible DataTableRow object (with table column names)
|
---|
| 174 | virtual DataTableRow EmptyRow();
|
---|
[2808] | 175 |
|
---|
[2849] | 176 | // Filling data structures (adding lines/rows)
|
---|
| 177 | virtual sa_size_t AddRow(const r_8* data);
|
---|
| 178 | // Filling data structures (adding lines/rows)
|
---|
| 179 | virtual sa_size_t AddRow(const MuTyV * data);
|
---|
| 180 | // Filling data structures (adding lines/rows)
|
---|
| 181 | virtual sa_size_t AddRow(DataTableRow const & data);
|
---|
| 182 |
|
---|
| 183 | //! Alias for AddRow()
|
---|
| 184 | inline sa_size_t AddLine(const r_8* data)
|
---|
| 185 | { return AddRow(data); }
|
---|
| 186 | //! Alias for AddRow()
|
---|
| 187 | inline sa_size_t AddLine(const MuTyV * data)
|
---|
| 188 | { return AddRow(data); }
|
---|
[2808] | 189 | //! Alias for AddLine()
|
---|
[2849] | 190 | inline sa_size_t AddLine(DataTableRow const & data)
|
---|
| 191 | { return AddRow(data); }
|
---|
| 192 |
|
---|
| 193 | //! Alias for AddRow() - Kept for backward compatibility with NTuple class interface.
|
---|
[2688] | 194 | inline sa_size_t Fill(const r_8* data)
|
---|
[2849] | 195 | { return AddRow(data); }
|
---|
| 196 |
|
---|
| 197 | // Pour etendre la table -
|
---|
[2688] | 198 | virtual sa_size_t Extend();
|
---|
| 199 |
|
---|
[2849] | 200 | //! Return the information stored in row \b n of the table in \b row object
|
---|
| 201 | virtual DataTableRow& GetRow(sa_size_t n, DataTableRow& row) const ;
|
---|
| 202 | //! Return the information stored in line (row) \b n of the table
|
---|
[3392] | 203 | virtual MuTyV * GetRow(sa_size_t n, MuTyV* mtvp=NULL) const ;
|
---|
[2849] | 204 | //! Return the information stored in line \b n of the table. Alias of GetLine
|
---|
| 205 | inline MuTyV * GetLine(sa_size_t n) const
|
---|
| 206 | { return GetRow(n); }
|
---|
[2808] | 207 |
|
---|
[2831] | 208 | //! Return the information stored in column \b k of the table, converted to double
|
---|
| 209 | virtual TVector<r_8> GetColumnD(sa_size_t k) const ;
|
---|
| 210 |
|
---|
| 211 | //! Return the information stored in column named \b nom of the table, converted to double
|
---|
| 212 | inline TVector<r_8> GetColumnD(char const * nom) const
|
---|
| 213 | { return GetColumnD(IndexNom(nom)) ; }
|
---|
| 214 | //! Return the information stored in column named \b nom of the table, converted to double
|
---|
| 215 | inline TVector<r_8> GetColumnD(string const & nom) const
|
---|
| 216 | { return GetColumnD(IndexNom(nom)) ; }
|
---|
| 217 |
|
---|
[2808] | 218 | //! Return the index for column name \b nom
|
---|
[2688] | 219 | sa_size_t IndexNom(char const* nom) const ;
|
---|
[2808] | 220 | //! Return the index for column name \b nom
|
---|
[2688] | 221 | inline sa_size_t IndexNom(string const & nom) const
|
---|
| 222 | { return IndexNom(nom.c_str()); }
|
---|
[2808] | 223 | //! Return the column name for column index \b k
|
---|
[2688] | 224 | string NomIndex(sa_size_t k) const ;
|
---|
[2732] | 225 |
|
---|
| 226 | //! Return the column type for column \b k (no check on index range)
|
---|
| 227 | inline FieldType GetColumType(sa_size_t k) const
|
---|
| 228 | { return mNames[k].type; }
|
---|
| 229 | //! Return the column name for column \b k (no check on index range)
|
---|
| 230 | inline const string & GetColumName(sa_size_t k) const
|
---|
| 231 | { return mNames[k].nom; }
|
---|
[2688] | 232 |
|
---|
[2732] | 233 |
|
---|
[2688] | 234 | //! Copy or merges the data from \b a into the data table (cp=true -> copy)
|
---|
| 235 | virtual void CopyMerge(BaseDataTable const& a, bool cp=false) ;
|
---|
[2699] | 236 |
|
---|
| 237 | //! Clear/reset the table content and structure.
|
---|
| 238 | virtual void Clear() = 0;
|
---|
| 239 |
|
---|
[2831] | 240 | //! Prints the table content (ascii dump)
|
---|
| 241 | virtual ostream& Print(ostream& os, sa_size_t lstart, sa_size_t lend,
|
---|
| 242 | sa_size_t lstep=1) const ;
|
---|
| 243 | //! Prints the table content (ascii dump) on stream \b os
|
---|
| 244 | inline ostream& Print(ostream& os) const
|
---|
| 245 | { return Print(os, 0, NEntry(), 1); }
|
---|
| 246 | //! Prints the table content (ascii dump) on stream \b os
|
---|
| 247 | inline ostream& WriteASCII(ostream& os) const
|
---|
| 248 | { return Print(os, 0, NEntry(), 1); }
|
---|
| 249 |
|
---|
[2688] | 250 |
|
---|
[2831] | 251 | //! Prints table definition and number of entries
|
---|
[2688] | 252 | void Show(ostream& os) const ;
|
---|
[2808] | 253 | //! Prints table definition and number of entries on the standard output stream \b cout
|
---|
[2688] | 254 | inline void Show() const { Show(cout) ; }
|
---|
| 255 |
|
---|
[2831] | 256 | //! Return the associated DVList (info) object.
|
---|
[2688] | 257 | DVList& Info() const ;
|
---|
| 258 |
|
---|
| 259 | // Remplissage depuis fichier ASCII
|
---|
[2831] | 260 | sa_size_t FillFromASCIIFile(istream& is, char clm='#', const char* sep=" \t");
|
---|
[2688] | 261 |
|
---|
[2699] | 262 | //-------------------------------------------------------------
|
---|
| 263 | //----------- Declaration de l interface NTuple --------------
|
---|
| 264 |
|
---|
[2688] | 265 | virtual sa_size_t NbLines() const ;
|
---|
| 266 | virtual sa_size_t NbColumns() const ;
|
---|
| 267 | virtual r_8 * GetLineD(sa_size_t n) const ;
|
---|
| 268 | virtual r_8 GetCell(sa_size_t n, sa_size_t k) const ;
|
---|
| 269 | virtual r_8 GetCell(sa_size_t n, string const & nom) const ;
|
---|
| 270 | virtual string GetCelltoString(sa_size_t n, sa_size_t k) const ;
|
---|
| 271 | virtual void GetMinMax(sa_size_t k, double& min, double& max) const ;
|
---|
| 272 | virtual void GetMinMax(string const & nom, double& min, double& max) const ;
|
---|
| 273 | virtual sa_size_t ColumnIndex(string const & nom) const ;
|
---|
| 274 | virtual string ColumnName(sa_size_t k) const;
|
---|
| 275 | virtual string VarList_C(const char* nomx=NULL) const ;
|
---|
| 276 | virtual string LineHeaderToString() const;
|
---|
| 277 | virtual string LineToString(sa_size_t n) const;
|
---|
[2699] | 278 |
|
---|
[2831] | 279 | //! Return a table row (line), formatted and converted to a string
|
---|
| 280 | virtual string TableRowToString(sa_size_t n, bool qstr, const char* sep=" ",
|
---|
| 281 | int fw=12) const;
|
---|
| 282 |
|
---|
[2699] | 283 | // Pour la gestion de persistance PPF
|
---|
| 284 | friend class ObjFileIO<BaseDataTable> ;
|
---|
[2822] | 285 | // pour fichiers FITS
|
---|
| 286 | friend class FitsHandler<BaseDataTable>;
|
---|
[2688] | 287 |
|
---|
| 288 | protected:
|
---|
| 289 | uint_8 mNEnt ; // nb total d'entrees
|
---|
| 290 | uint_8 mSegSz ; // taille bloc/segment
|
---|
| 291 | uint_8 mNSeg; // Nb total de segments
|
---|
| 292 | mutable r_8 * mVarD; // Pour retourner une ligne de la table
|
---|
| 293 | mutable MuTyV * mVarMTV; // Pour retourner une ligne de la table en MuTyV
|
---|
[2808] | 294 |
|
---|
| 295 | //! \cond Pour pas inclure ds la documentation doxygen
|
---|
[2688] | 296 | typedef struct {
|
---|
| 297 | string nom;
|
---|
| 298 | FieldType type;
|
---|
| 299 | sa_size_t ser;
|
---|
| 300 | } colst;
|
---|
[2808] | 301 | //! \endcond
|
---|
[2688] | 302 | std::vector<colst> mNames;
|
---|
| 303 |
|
---|
| 304 | mutable DVList* mInfo; // Infos (variables) attachees au NTuple
|
---|
| 305 |
|
---|
| 306 | // Pour calculer et garder le min/max
|
---|
| 307 | mutable std::vector<r_8> mMin;
|
---|
| 308 | mutable std::vector<r_8> mMax;
|
---|
| 309 | mutable std::vector<uint_8> mMinMaxNEnt;
|
---|
| 310 |
|
---|
| 311 | // Les pointeurs des SegDataBlock ...
|
---|
| 312 | std::vector< SegDBInterface<int_4> * > mIColsP;
|
---|
| 313 | std::vector< sa_size_t > mIColIdx;
|
---|
| 314 | std::vector< SegDBInterface<int_8> * > mLColsP;
|
---|
| 315 | std::vector< sa_size_t > mLColIdx;
|
---|
| 316 | std::vector< SegDBInterface<r_4> * > mFColsP;
|
---|
| 317 | std::vector< sa_size_t > mFColIdx;
|
---|
| 318 | std::vector< SegDBInterface<r_8> * > mDColsP;
|
---|
| 319 | std::vector< sa_size_t > mDColIdx;
|
---|
[2827] | 320 | std::vector< SegDBInterface< complex<r_4> > * > mYColsP;
|
---|
| 321 | std::vector< sa_size_t > mYColIdx;
|
---|
| 322 | std::vector< SegDBInterface< complex<r_8> > * > mZColsP;
|
---|
| 323 | std::vector< sa_size_t > mZColIdx;
|
---|
[2688] | 324 | std::vector< SegDBInterface<string> * > mSColsP;
|
---|
| 325 | std::vector< sa_size_t > mSColIdx;
|
---|
[3392] | 326 |
|
---|
| 327 | ThSafeOp* mThS; // != NULL -> Thread safe operations
|
---|
[2688] | 328 |
|
---|
| 329 | };
|
---|
| 330 | /*! Prints table information (Column name and type, min/max) on stream \b s (bd.Show(s)) */
|
---|
| 331 | inline ostream& operator << (ostream& s, BaseDataTable const & bd)
|
---|
| 332 | { bd.Show(s); return(s); }
|
---|
| 333 |
|
---|
| 334 | } // namespace SOPHYA
|
---|
| 335 |
|
---|
| 336 | #endif
|
---|