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

Last change on this file since 3540 was 3392, checked in by ansari, 18 years ago

Implementation NTuple::Fill(), BaseDataTable:;AddRow()/GetRow() thread-safe - Reza 22/11/2007

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//! 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.
40 inline MuTyV operator[] (string const colname) const { return get(colname); }
41//! Returns a reference to the value of column \b k.
42 inline MuTyV& operator[] (string const colname) { return get(colname); }
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
65// Forward class declaration for Thread-safe operations
66class ThSafeOp;
67
68//! Interface definition for classes handling data in a table.
69class BaseDataTable : public AnyDataObj , public NTupleInterface {
70public:
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,
75 StringField=7, DateTimeField=8};
76 // <=====================================================>
77
78 //! Return the column type as a string (long or short depending on fgl=true/false)
79 static string ColTypeToString(FieldType ft, bool fgl=false);
80 //! Return the column type corresponding to \b sctyp
81 static FieldType StringToColType(string const & sctyp);
82
83 // constructeur , destructeur
84 BaseDataTable(sa_size_t segsz=512);
85 virtual ~BaseDataTable();
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); }
91
92 //! Adds a column holding integer, named \b cnom
93 inline sa_size_t AddIntegerColumn(const char * cnom)
94 { return AddColumn(IntegerField, cnom); }
95 //! Adds a column holding integer, named \b cnom
96 inline sa_size_t AddIntegerColumn(string const & cnom)
97 { return AddColumn(IntegerField, cnom); }
98 //! Adds a column holding long integer, named \b cnom
99 inline sa_size_t AddLongColumn(const char * cnom)
100 { return AddColumn(LongField, cnom); }
101 //! Adds a column holding long integer, named \b cnom
102 inline sa_size_t AddLongColumn(string const & cnom)
103 { return AddColumn(LongField, cnom); }
104 //! Adds a column holding floating values (r_4), named \b cnom
105 inline sa_size_t AddFloatColumn(const char * cnom)
106 { return AddColumn(FloatField, cnom); }
107 //! Adds a column holding floating values (r_4), named \b cnom
108 inline sa_size_t AddFloatColumn(string const & cnom)
109 { return AddColumn(FloatField, cnom); }
110 //! Adds a column holding double values (r_8), named \b cnom
111 inline sa_size_t AddDoubleColumn(const char * cnom)
112 { return AddColumn(DoubleField, cnom); }
113 //! Adds a column holding double values (r_8), named \b cnom
114 inline sa_size_t AddDoubleColumn(string const & cnom)
115 { return AddColumn(DoubleField, cnom); }
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); }
128 //! Adds a column holding character strings, named \b cnom
129 inline sa_size_t AddStringColumn(const char * cnom)
130 { return AddColumn(StringField, cnom); }
131 //! Adds a column holding character strings, named \b cnom
132 inline sa_size_t AddStringColumn(string const & cnom)
133 { return AddColumn(StringField, cnom); }
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); }
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);
154 // Verifie et corrige si besoin un nom de colonne
155 virtual bool CheckCorrectColName(string & cnom);
156
157 // Acces to various counts and parameters
158 //! Return the number of lines (rows) in the table)
159 inline sa_size_t NRows() const { return mNEnt ; }
160 //! Return the number of lines (rows) in the table)
161 inline sa_size_t NEntry() const { return mNEnt ; }
162 //! Return the number of columns in the tables (number of cells in a row)
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)
165 inline sa_size_t NVar() const { return mNames.size() ; }
166 //! Return the number of columns in the tables (number of cells in a row)
167 inline sa_size_t NVars() const { return mNames.size() ; }
168 //! Return the segment size (SegDBInterface objects corresponding to columns)
169 inline sa_size_t SegmentSize() const { return mSegSz ; }
170 //! Return the number of segments (SegDBInterface objects corresponding to columns)
171 inline sa_size_t NbSegments() const { return mNSeg ; }
172
173 //! Return a compatible DataTableRow object (with table column names)
174 virtual DataTableRow EmptyRow();
175
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); }
189 //! Alias for AddLine()
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.
194 inline sa_size_t Fill(const r_8* data)
195 { return AddRow(data); }
196
197 // Pour etendre la table -
198 virtual sa_size_t Extend();
199
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
203 virtual MuTyV * GetRow(sa_size_t n, MuTyV* mtvp=NULL) const ;
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); }
207
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
218 //! Return the index for column name \b nom
219 sa_size_t IndexNom(char const* nom) const ;
220 //! Return the index for column name \b nom
221 inline sa_size_t IndexNom(string const & nom) const
222 { return IndexNom(nom.c_str()); }
223 //! Return the column name for column index \b k
224 string NomIndex(sa_size_t k) const ;
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; }
232
233
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) ;
236
237 //! Clear/reset the table content and structure.
238 virtual void Clear() = 0;
239
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
250
251 //! Prints table definition and number of entries
252 void Show(ostream& os) const ;
253 //! Prints table definition and number of entries on the standard output stream \b cout
254 inline void Show() const { Show(cout) ; }
255
256 //! Return the associated DVList (info) object.
257 DVList& Info() const ;
258
259 // Remplissage depuis fichier ASCII
260 sa_size_t FillFromASCIIFile(istream& is, char clm='#', const char* sep=" \t");
261
262 //-------------------------------------------------------------
263 //----------- Declaration de l interface NTuple --------------
264
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;
278
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
283 // Pour la gestion de persistance PPF
284 friend class ObjFileIO<BaseDataTable> ;
285 // pour fichiers FITS
286 friend class FitsHandler<BaseDataTable>;
287
288protected:
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
294
295 //! \cond Pour pas inclure ds la documentation doxygen
296 typedef struct {
297 string nom;
298 FieldType type;
299 sa_size_t ser;
300 } colst;
301 //! \endcond
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;
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;
324 std::vector< SegDBInterface<string> * > mSColsP;
325 std::vector< sa_size_t > mSColIdx;
326
327 ThSafeOp* mThS; // != NULL -> Thread safe operations
328
329};
330/*! Prints table information (Column name and type, min/max) on stream \b s (bd.Show(s)) */
331inline ostream& operator << (ostream& s, BaseDataTable const & bd)
332 { bd.Show(s); return(s); }
333
334} // namespace SOPHYA
335
336#endif
Note: See TracBrowser for help on using the repository browser.