Changeset 2849 in Sophya for trunk


Ignore:
Timestamp:
Nov 21, 2005, 12:08:55 PM (20 years ago)
Author:
ansari
Message:

Ajout de la classe DataTableRow , modifs/ajout interface BaseDataTable - Reza 21/11/2005

Location:
trunk/SophyaLib/HiStats
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaLib/HiStats/basedtable.cc

    r2831 r2849  
    33#include "sopnamsp.h"
    44#include "pexceptions.h"
     5
     6/*!
     7   \class SOPHYA::DataTableRow
     8   \ingroup HiStats
     9   This class is intented to be used with datatable classes
     10   (inheriting from BaseDataTable) for representing a row (line)
     11   of the table.
     12*/
     13DataTableRow::DataTableRow( vector<string>& colnames )
     14{
     15  if (colnames.size() < 1)
     16    throw ParmError("DataTableRow::DataTableRow(vector<string>& cn) cn.size()==0 ");
     17  size_ = colnames.size();
     18  mtv_ = new MuTyV[ size_ ];
     19  for(sa_size_t k=0; k<size_; k++)
     20    nm2idx_[colnames[k]] = k;
     21}
     22
     23DataTableRow::DataTableRow(DataTableRow const & a )
     24{
     25  size_ = a.size_;
     26  mtv_ = new MuTyV[ size_ ];
     27  nm2idx_ = a.nm2idx_;
     28}
     29
     30MuTyV DataTableRow::get(string const& colname) const
     31{
     32  map<string, sa_size_t>::const_iterator it = nm2idx_.find(colname);
     33  if (it == nm2idx_.end()) {
     34    string msg = "DataTableRow::get( " ;
     35    msg += colname;    msg += " ) Not found column name";
     36    throw NotFoundExc(msg);
     37  }
     38  return mtv_[(*it).second];
     39}
     40
     41MuTyV& DataTableRow::get(string const& colname)
     42{
     43  map<string, sa_size_t>::const_iterator it = nm2idx_.find(colname);
     44  if (it == nm2idx_.end()) {
     45    string msg = "DataTableRow::get( " ;
     46    msg += colname;    msg += " ) const - Not found column name";
     47    throw NotFoundExc(msg);
     48  }
     49  return mtv_[(*it).second];
     50}
     51
     52ostream&   DataTableRow::Print(ostream& os) const
     53{
     54  for(sa_size_t k=0; k<size_; k++)
     55    os << (string)mtv_[k] << " ";
     56  return os;
     57}
    558
    659/*!
     
    143196}
    144197
     198// Retourne une structure
     199/*!
     200 The returned BaseDataTable object can be used for subsequent call to
     201 AddRow() or GetRow() methods.
     202 Generate an exception if called for a table with no columns
     203*/
     204DataTableRow BaseDataTable::EmptyRow()
     205{
     206  if (NCols == 0)
     207    throw ParmError("BaseDataTable::EmptyRow() Table has no column !");
     208  vector<string> nms;
     209  for(sa_size_t k=0; k<NVar(); k++)  nms.push_back(mNames[k].nom);
     210  return DataTableRow(nms);
     211}
     212
    145213//
    146214// A quel index correspond mon nom ?
     
    162230}
    163231
    164 //! Adds a line (or row to the table) with r_8* inout data
     232//! Adds a row (or line) to the table with r_8* inout data
    165233/*!
    166234  The data to be added is provided as an array (vector) of double (r_8).
     
    171239  (data[k] k=0..NbColumns())
    172240*/
    173 sa_size_t BaseDataTable::AddLine(const r_8* data)
     241sa_size_t BaseDataTable::AddRow(const r_8* data)
    174242{
    175243  if (NVar() == 0)
    176     throw ParmError("BaseDataTable::AddLine(const r_8*) Table has no column !");
     244    throw ParmError("BaseDataTable::AddRow(const r_8*) Table has no column !");
    177245  if (NEntry() == SegmentSize()*NbSegments())  Extend();
    178246  sa_size_t n = NEntry();
     
    198266}
    199267
    200 //! Adds a line (or row to the table) with input data as an array of MuTyV
     268//! Adds a row (or line) to the table with input data as an array of MuTyV
    201269/*!
    202270  The data to be added is provided as an array (vector) of MuTyV.
     
    207275  (data[k] k=0..NbColumns())
    208276*/
    209 sa_size_t BaseDataTable::AddLine(const MuTyV* data)
     277sa_size_t BaseDataTable::AddRow(const MuTyV* data)
    210278{
    211279  if (NVar() == 0)
    212     throw ParmError("BaseDataTable::AddLine(const MuTyV*) Table has no column !");
     280    throw ParmError("BaseDataTable::AddRow(const MuTyV*) Table has no column !");
    213281  if (NEntry() == SegmentSize()*NbSegments())  Extend();
    214282  sa_size_t n = NEntry();
     
    235303  return mNEnt;
    236304}
    237 
     305//! Adds a row (or line) to the table with input data as DataTableRow object
     306/*!
     307  The internal MuTyV array of the object contains the date and the
     308  MuTyV class conversion operators are used to match against each
     309  cell data type.
     310  Only the size of the input data object is checked.
     311  Return the new number of table rows (lines / entries)
     312  \param data : Data  for each cell of the row to be appended
     313  (data[k] k=0..NbColumns())
     314*/
     315sa_size_t BaseDataTable::AddRow(DataTableRow const& data)
     316{
     317  if ( data.Size() != NCols() )
     318    throw SzMismatchError(" BaseDataTable::AddRow() - data.Size() != NCols() ");
     319  return AddRow(data.MTVPtr());
     320}
     321
     322/*!
     323  Extends the table (in the row direction). This method is called automatically when needed.
     324*/
    238325sa_size_t BaseDataTable::Extend()
    239326{
     
    256343}
    257344
    258 
    259 MuTyV* BaseDataTable::GetLine(sa_size_t n) const
     345/*!
     346  Fills the input \b row object with the content of row \b n.
     347  Return a reference to the input \b row object.
     348  Generate an exception if the input \b row object has the wrong size.
     349  This method is slower(less efficient) than the GetRow(n) method.
     350*/
     351DataTableRow& BaseDataTable::GetRow(sa_size_t n, DataTableRow& row) const
     352{
     353  if ( row.Size() != NCols() )
     354    throw SzMismatchError(" BaseDataTable::GetRow(n, row) - row.Size() != NCols() ");
     355  MuTyV* rmtv = GetRow(n);
     356  for(sa_size_t k=0; k<NCols(); k++)
     357    row[k] = rmtv[k];
     358  return row;
     359}
     360
     361MuTyV* BaseDataTable::GetRow(sa_size_t n) const
    260362{
    261363  if ((n < 0) || (n >= NEntry()))
    262     throw RangeCheckError("BaseDataTable::GetLine() out of range line index n");
     364    throw RangeCheckError("BaseDataTable::GetRow() out of range line index n");
    263365  if (mVarMTV == NULL) mVarMTV = new MuTyV[NVar()];
    264366
     
    345447  }
    346448  for(sa_size_t kk=0; kk<a.NEntry(); kk++)
    347     AddLine(a.GetLine(kk));
     449    AddRow(a.GetLine(kk));
    348450}
    349451
     
    456558        }
    457559      }
    458       AddLine(mVarMTV);
     560      AddRow(mVarMTV);
    459561      nl++;
    460562    }
  • trunk/SophyaLib/HiStats/basedtable.h

    r2831 r2849  
    1212#include <string>
    1313#include <vector>
     14#include <map>
    1415
    1516#include <complex>
     
    2425//  Forward class declaration for Fits handler
    2526template <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>& 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  inline MuTyV      operator[]  (const char* colname)  const
     45  { string sc(colname); return get(sc); }
     46//! Returns a reference to the value of column \b k.
     47  inline MuTyV&     operator[]  (const char* colname)   
     48  { string sc(colname); return get(sc); }
     49//! Returns the value for column \b colname.
     50  MuTyV      get(string const& colname) const;
     51//! Returns a reference to the value of column \b k.
     52  MuTyV&     get(string const& colname);
     53//! Acces to the MuTyV pointer
     54  inline sa_size_t  Size() const { return size_; }
     55//! Acces to the MuTyV pointer
     56  inline MuTyV const *     MTVPtr() const { return mtv_; }
     57//! Acces to the MuTyV pointer
     58  inline MuTyV*     MTVPtr() { return mtv_; }
     59//! prints the object content on the output stream
     60  ostream&   Print(ostream& os) const;
     61protected:
     62  MuTyV * mtv_;
     63  sa_size_t size_;
     64  map<string , sa_size_t> nm2idx_;
     65};
     66
     67/*! Prints the DataTableRow object content on the output stream ( */
     68inline ostream& operator << (ostream& s, DataTableRow const & row)
     69  {  row.Print(s);  return(s);  }
    2670
    2771//! Interface definition for classes handling data in a table.
     
    109153  // Acces to various counts and parameters
    110154  //! Return the number of lines (rows) in the table)
     155  inline sa_size_t  NRows() const { return mNEnt ; }
     156  //! Return the number of lines (rows) in the table)
    111157  inline sa_size_t  NEntry() const { return mNEnt ; }
     158  //! Return the number of columns in the tables (number of cells in a row) 
     159  inline sa_size_t  NCols() const   { return mNames.size() ; }
    112160  //! Return the number of columns in the tables (number of cells in a row) 
    113161  inline sa_size_t  NVar() const   { return mNames.size() ; }
     
    119167  inline sa_size_t  NbSegments() const   { return mNSeg ; }
    120168
    121   // Filling data structures (adding lines)
    122   virtual sa_size_t AddLine(const r_8* data);
    123   virtual sa_size_t AddLine(const MuTyV * data);
    124 
     169  //! Return a compatible DataTableRow object (with table column names)
     170  virtual DataTableRow  EmptyRow();
     171
     172  // Filling data structures (adding lines/rows)
     173  virtual sa_size_t AddRow(const r_8* data);
     174  // Filling data structures (adding lines/rows)
     175  virtual sa_size_t AddRow(const MuTyV * data);
     176  // Filling data structures (adding lines/rows)
     177  virtual sa_size_t AddRow(DataTableRow const & data);
     178
     179  //! Alias for AddRow()
     180  inline sa_size_t  AddLine(const r_8* data)
     181  { return AddRow(data); }
     182  //! Alias for AddRow()
     183  inline sa_size_t  AddLine(const MuTyV * data)
     184  { return AddRow(data); }
    125185  //! Alias for AddLine()
     186  inline sa_size_t  AddLine(DataTableRow const & data)
     187  { return AddRow(data); }
     188
     189  //! Alias for AddRow() - Kept for backward compatibility with NTuple class interface.
    126190  inline sa_size_t  Fill(const r_8* data)
    127   { return AddLine(data); }
    128   //! Alias for AddLine()
    129   inline sa_size_t  Fill(const MuTyV * data);
    130      
     191  { return AddRow(data); }
     192
     193  // Pour etendre la table -     
    131194  virtual sa_size_t Extend();
    132195
    133   //! Return the information stored in line \b n of the table
    134   virtual MuTyV *   GetLine(sa_size_t n) const ;
     196  //! Return the information stored in row \b n of the table in \b row object
     197  virtual DataTableRow&   GetRow(sa_size_t n, DataTableRow& row) const ;
     198  //! Return the information stored in line (row) \b n of the table
     199  virtual MuTyV *   GetRow(sa_size_t n) const ;
     200  //! Return the information stored in line \b n of the table. Alias of GetLine
     201  inline  MuTyV *   GetLine(sa_size_t n) const
     202  { return GetRow(n); }
    135203
    136204  //! Return the information stored in column \b k of the table, converted to double
  • trunk/SophyaLib/HiStats/datatable.cc

    r2847 r2849  
    1515   The whole data set is kept in memory.
    1616   \sa SOPHYA::MuTyV
     17   \sa SOPHYA::DataTableRow
    1718   \sa SOPHYA::BaseDataTable
    1819   \sa SOPHYA::SegDataBlock
     
    2829   for(int i=0; i<63; i++) {
    2930     x[0] = (i%9)-4.;  x[1] = (i/9)-3.;  x[2] = x[0]*x[0]+x[1]*x[1];
    30      dt.AddLine(x);
     31     dt.AddRow(x);
    3132   }
    3233   // Printing table info
     
    3637   po << dt ;
    3738   \endcode
     39
     40  DataTableRow objects can also be used to fill the table, or access the data, as
     41  shown in the example below:
     42  \code
     43  // We use the same table as in the example above:
     44  DataTableRow row = dt.EmptyRow();
     45  row[0] = 83.3; row[1] = 20.;   row[2] = 83.3*83.3+20.*20.;
     46  dt.AddRow(row);
     47  row["X0_f"] = 7.5; row["X1_f"] = 6.;  row["X0X0pX1X1_d"] = 7.5*7.5+6.*6.;
     48  dt.AddRow(row); 
     49  \endcode
    3850*/
    3951
  • trunk/SophyaLib/HiStats/swppfdtable.cc

    r2831 r2849  
    2525
    2626   \sa SOPHYA::MuTyV
     27   \sa SOPHYA::DataTableRow
    2728   \sa SOPHYA::DataTable
    2829   \sa SOPHYA::SwSegDataBlock  SOPHYA::PPFDataSwapper
Note: See TracChangeset for help on using the changeset viewer.