source: Sophya/trunk/SophyaLib/HiStats/basedtable.h@ 2928

Last change on this file since 2928 was 2851, checked in by ansari, 20 years ago

modif mineure de l'interface DataTableRow suite compil SGI-CC , Reza 21/11/2005

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