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

Last change on this file since 3889 was 3712, checked in by ansari, 16 years ago

Correction bug trouve par cmv, ajout destructeur avec delete[] pour DataTableRow - Reza 10/12/2009

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