Changeset 3392 in Sophya for trunk/SophyaLib/HiStats/basedtable.cc
- Timestamp:
- Nov 22, 2007, 7:25:40 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/HiStats/basedtable.cc
r3181 r3392 1 1 #include "basedtable.h" 2 2 #include <ctype.h> 3 #include "sopnamsp.h"4 3 #include "pexceptions.h" 4 #include "thsafeop.h" 5 6 namespace SOPHYA { 5 7 6 8 /*! 7 \class SOPHYA::DataTableRow9 \class DataTableRow 8 10 \ingroup HiStats 9 11 This class is intented to be used with datatable classes … … 58 60 59 61 /*! 60 \class SOPHYA::BaseDataTable62 \class BaseDataTable 61 63 \ingroup HiStats 62 64 Base class for data tables. Each line represent a record 63 65 and the column contains a given data type. 66 \warning 67 Thread safe fill operation can be activated using the method SetThreadSafe() 68 Default mode is NON thread-safe fill. 64 69 */ 65 70 … … 147 152 mVarMTV = NULL; 148 153 mInfo = NULL; 154 mThS = NULL; 149 155 } 150 156 … … 154 160 if (mVarMTV) delete[] mVarMTV; 155 161 if (mInfo) delete mInfo; 156 } 162 if (mThS) delete mThS; 163 } 164 165 /*! 166 \brief Activate or deactivate thread-safe \b AddRow() operations 167 168 If fg==true, create an ThSafeOp object in order to insure atomic AddRow() 169 and GetRow()/GetColumn() operations. if fg==false, the ThSafeOp object (mThS) 170 of the target DataTable is destroyed. 171 172 When activated, the following operations are thread-safe : 173 174 - AddRow(const r_8* data) 175 - AddRow(const MuTyV* data) 176 - AddRow(DataTableRow const& data) 177 - GetRow(sa_size_t n, DataTableRow& row) 178 - GetRow(sa_size_t n, MuTyV* mtvp) 179 - GetColumnD(sa_size_t k) 180 181 \warning The default AddRow() operation mode for DataTables is NOT thread-safe. 182 Please note also that the thread-safety state is NOt saved to PPF (or FITS) streams. 183 */ 184 void BaseDataTable::SetThreadSafe(bool fg) 185 { 186 if (fg) { 187 if (mThS) return; 188 else mThS = new ThSafeOp(); 189 } 190 else { 191 if (mThS) delete mThS; 192 mThS = NULL; 193 } 194 } 195 196 197 /*! \cond 198 \class DT_TSOP_SYNC 199 \ingroup HiStats 200 Classe utilitaire pour faciliter la gestion de lock pour 201 operations thread-safe BaseDataTable 202 */ 203 class DT_TSOP_SYNC { 204 public: 205 explicit DT_TSOP_SYNC(ThSafeOp* ths) 206 { 207 ths_ = ths; 208 if (ths_) ths_->lock(); 209 } 210 ~DT_TSOP_SYNC() 211 { 212 if (ths_) ths_->unlock(); 213 } 214 inline ThSafeOp* NOp() { return ths_; } 215 protected: 216 ThSafeOp* ths_; 217 }; 218 /*! \endcond */ 157 219 158 220 sa_size_t BaseDataTable::CopyStructure(BaseDataTable const & a) … … 273 335 sa_size_t BaseDataTable::AddRow(const r_8* data) 274 336 { 337 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 338 275 339 if (NVar() == 0) 276 340 throw ParmError("BaseDataTable::AddRow(const r_8*) Table has no column !"); … … 309 373 sa_size_t BaseDataTable::AddRow(const MuTyV* data) 310 374 { 375 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 376 311 377 if (NVar() == 0) 312 378 throw ParmError("BaseDataTable::AddRow(const MuTyV*) Table has no column !"); … … 379 445 Return a reference to the input \b row object. 380 446 Generate an exception if the input \b row object has the wrong size. 381 This method is slower(less efficient) than the GetRow(n) method.382 447 */ 383 448 DataTableRow& BaseDataTable::GetRow(sa_size_t n, DataTableRow& row) const … … 385 450 if ( row.Size() != NCols() ) 386 451 throw SzMismatchError(" BaseDataTable::GetRow(n, row) - row.Size() != NCols() "); 387 MuTyV* rmtv = GetRow(n); 388 for(sa_size_t k=0; k<NCols(); k++) 389 row[k] = rmtv[k]; 452 GetRow(n, row.MTVPtr()); 390 453 return row; 391 454 } 392 455 393 MuTyV* BaseDataTable::GetRow(sa_size_t n) const 394 { 456 /*! 457 For thread-safe operation, specify a valid \b mtvp pointer (!= NULL) 458 */ 459 MuTyV* BaseDataTable::GetRow(sa_size_t n, MuTyV* mtvp) const 460 { 461 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 462 395 463 if ((n < 0) || (n >= NEntry())) 396 464 throw RangeCheckError("BaseDataTable::GetRow() out of range line index n"); 397 if (mVarMTV == NULL) mVarMTV = new MuTyV[NVar()]; 398 465 if (mtvp == NULL) { 466 if (mVarMTV == NULL) mVarMTV = new MuTyV[NVar()]; 467 mtvp = mVarMTV; 468 } 399 469 sa_size_t bid = n/mSegSz; 400 470 sa_size_t off = n%mSegSz; 401 471 for(sa_size_t k=0; k<mIColsP.size(); k++) 402 m VarMTV[mIColIdx[k]] = mIColsP[k]->GetCstSegment(bid)[off];472 mtvp[mIColIdx[k]] = mIColsP[k]->GetCstSegment(bid)[off]; 403 473 for(sa_size_t k=0; k<mLColsP.size(); k++) 404 m VarMTV[mLColIdx[k]] = mLColsP[k]->GetCstSegment(bid)[off];474 mtvp[mLColIdx[k]] = mLColsP[k]->GetCstSegment(bid)[off]; 405 475 for(sa_size_t k=0; k<mFColsP.size(); k++) 406 m VarMTV[mFColIdx[k]] = mFColsP[k]->GetCstSegment(bid)[off];476 mtvp[mFColIdx[k]] = mFColsP[k]->GetCstSegment(bid)[off]; 407 477 for(sa_size_t k=0; k<mDColsP.size(); k++) { 408 478 if (GetColumType(mDColIdx[k]) == DateTimeField) 409 m VarMTV[mDColIdx[k]] = TimeStamp(mDColsP[k]->GetCstSegment(bid)[off]);410 else m VarMTV[mDColIdx[k]] = mDColsP[k]->GetCstSegment(bid)[off];479 mtvp[mDColIdx[k]] = TimeStamp(mDColsP[k]->GetCstSegment(bid)[off]); 480 else mtvp[mDColIdx[k]] = mDColsP[k]->GetCstSegment(bid)[off]; 411 481 } 412 482 for(sa_size_t k=0; k<mYColsP.size(); k++) 413 m VarMTV[mYColIdx[k]] = mYColsP[k]->GetCstSegment(bid)[off];483 mtvp[mYColIdx[k]] = mYColsP[k]->GetCstSegment(bid)[off]; 414 484 for(sa_size_t k=0; k<mZColsP.size(); k++) 415 m VarMTV[mZColIdx[k]] = mZColsP[k]->GetCstSegment(bid)[off];485 mtvp[mZColIdx[k]] = mZColsP[k]->GetCstSegment(bid)[off]; 416 486 for(sa_size_t k=0; k<mSColsP.size(); k++) 417 m VarMTV[mSColIdx[k]] = mSColsP[k]->GetCstSegment(bid)[off];418 419 return m VarMTV;487 mtvp[mSColIdx[k]] = mSColsP[k]->GetCstSegment(bid)[off]; 488 489 return mtvp; 420 490 } 421 491 … … 424 494 TVector<r_8> BaseDataTable::GetColumnD(sa_size_t k) const 425 495 { 496 DT_TSOP_SYNC dttss(mThS); dttss.NOp(); // Thread-safe operation 497 426 498 if ((k < 0) || (k >= NVar())) 427 499 throw RangeCheckError("BaseDataTable::GetColumnD() out of range column index k"); … … 968 1040 } 969 1041 1042 } // FIN namespace SOPHYA 1043
Note:
See TracChangeset
for help on using the changeset viewer.