Changeset 2911 for trunk/src


Ignore:
Timestamp:
04/01/14 13:05:18 (11 years ago)
Author:
Takeshi Nakazato
Message:

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: test_sdcal

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Defined new index iterator class, STIdxIter2.
STIdxIter2 has almost same functionality as STIdxIter and support
various kind of data.


Location:
trunk/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/STIdxIter.cpp

    r2580 r2911  
    55#include <casa/Arrays/ArrayMath.h>
    66#include <casa/Arrays/ArrayIO.h>
     7#include <casa/Utilities/DataType.h>
    78#include <tables/Tables/ScalarColumn.h>
     9#include <tables/Tables/TableRow.h>
     10#include <tables/Tables/TableRecord.h>
    811#include "STIdxIter.h"
    912
     
    502505}
    503506
     507STIdxIter2::STIdxIter2()
     508  : cols_(),
     509    table_(),
     510    counter_(0),
     511    num_iter_(0),
     512    num_row_(0),
     513    sorter_(),
     514    index_(),
     515    unique_(),
     516    pointer_()
     517{
     518}
     519
     520STIdxIter2::STIdxIter2( const string &name,
     521                      const vector<string> &cols )
     522  : cols_(cols),
     523    table_(name, Table::Old),
     524    counter_(0),
     525    num_iter_(0),
     526    num_row_(0),
     527    sorter_(),
     528    index_(),
     529    unique_(),
     530    pointer_()
     531{
     532  init();
     533}
     534
     535STIdxIter2::STIdxIter2( const CountedPtr<Scantable> &s,
     536                      const vector<string> &cols )
     537  : cols_(cols),
     538    table_(s->table()),
     539    counter_(0),
     540    num_iter_(0),
     541    num_row_(0),
     542    sorter_(),
     543    index_(),
     544    unique_(),
     545    pointer_()
     546{
     547  init();
     548}
     549
     550STIdxIter2::~STIdxIter2()
     551{
     552  deallocate();
     553}
     554
     555void STIdxIter2::deallocate()
     556{
     557  for (vector<void*>::iterator i = pointer_.begin(); i != pointer_.end(); ++i) {
     558    free(*i);
     559  }
     560}
     561
     562Record STIdxIter2::currentValue() {
     563  assert(counter_ < num_iter_);
     564  Vector<String> cols(cols_.size());
     565  for (uInt i = 0; i < cols.nelements(); ++i) {
     566    cols[i] = cols_[i];
     567  }
     568  const ROTableRow row(table_, cols);
     569  const TableRecord rec = row.get(index_[unique_[counter_]]);
     570  return Record(rec);
     571}
     572
     573Bool STIdxIter2::pastEnd() {
     574  return counter_ >= num_iter_;
     575}
     576
     577void STIdxIter2::next() {
     578  counter_++;
     579}
     580
     581vector<uInt> STIdxIter2::tovector( Vector<uInt> v )
     582{
     583  vector<uInt> ret ;
     584  v.tovector( ret ) ;
     585  return ret ;
     586}
     587
     588Vector<uInt> STIdxIter2::getRows( StorageInitPolicy policy )
     589{
     590  assert(num_iter_ > 1);
     591  assert(counter_ < num_iter_);
     592  if (counter_ == num_iter_ - 1) {
     593    uInt start = unique_[counter_];
     594    uInt num_row = num_row_ - start;
     595    Vector<uInt> rows(IPosition(1, num_row), &(index_.data()[start]), policy);
     596    return rows;
     597  }
     598  else {
     599    uInt start = unique_[counter_];
     600    uInt end = unique_[counter_ + 1];
     601    uInt num_row = end - start;
     602    Vector<uInt> rows(IPosition(1, num_row), &(index_.data()[start]), policy);
     603    return rows;
     604  }
     605}
     606
     607void STIdxIter2::init()
     608{
     609  for (uInt i = 0; i < cols_.size(); ++i) {
     610    addSortKey(cols_[i]);
     611  }
     612  num_row_ = table_.nrow();
     613  sorter_.sort(index_, num_row_);
     614  num_iter_ = sorter_.unique(unique_, index_);
     615  // cout << "num_row_ = " << num_row_ << endl
     616  //      << "num_iter_ = " << num_iter_ << endl;
     617  // cout << "unique_ = " << unique_ << endl;
     618  // cout << "index_ = " << index_ << endl;
     619}
     620
     621void STIdxIter2::addSortKey(const string &name)
     622{
     623  const ColumnDesc &desc = table_.tableDesc().columnDesc(name);
     624  const DataType dtype = desc.trueDataType();
     625  switch (dtype) {
     626  case TpUInt:
     627    addColumnToKey<uInt, TpUInt>(name);
     628    break;
     629  case TpInt:
     630    addColumnToKey<Int, TpInt>(name);
     631    break;
     632  case TpFloat:
     633    addColumnToKey<Float, TpFloat>(name);
     634    break;
     635  case TpDouble:
     636    addColumnToKey<Double, TpDouble>(name);
     637    break;
     638  case TpComplex:
     639    addColumnToKey<Complex, TpComplex>(name);
     640    break;
     641  // case TpString:
     642  //   addColumnToKey<String, TpString>(name);
     643  //   break;
     644  default:
     645    deallocate();
     646    stringstream oss;
     647    oss << name << ": data type is not supported" << endl;
     648    throw(AipsError(oss.str()));
     649  }
     650}
     651
     652template<class T, DataType U>
     653void STIdxIter2::addColumnToKey(const string &name)
     654{
     655  uInt nrow = table_.nrow();
     656  void *raw_storage = malloc(sizeof(T) * nrow);
     657  T *storage = reinterpret_cast<T*>(raw_storage);
     658  Vector<T> array(IPosition(1, nrow), storage, SHARE);
     659  ROScalarColumn<T> col(table_, name);
     660  col.getColumn(array);
     661  sorter_.sortKey(storage, U, 0, Sort::Ascending);
     662  pointer_.push_back(raw_storage);
     663}
    504664} // namespace
     665
  • trunk/src/STIdxIter.h

    r2580 r2911  
    88#include <casa/Arrays/IPosition.h>
    99#include <casa/BasicSL/String.h>
     10
     11#include <casa/Utilities/Sort.h>
    1012
    1113#include "Scantable.h"
     
    155157} ;
    156158
     159class STIdxIter2
     160{
     161public:
     162  STIdxIter2() ;
     163  STIdxIter2( const string &name,
     164                             const vector<string> &cols ) ;
     165  STIdxIter2( const CountedPtr<Scantable> &s,
     166                          const vector<string> &cols ) ;
     167  virtual ~STIdxIter2() ;
     168  Record currentValue();
     169  Bool pastEnd() ;
     170  void next() ;
     171  Vector<uInt> getRows(StorageInitPolicy policy=COPY) ;
     172  vector<uInt> getRowsSTL() { return tovector( getRows() ) ; } ;
     173  virtual void init();
     174private:
     175  vector<uInt> tovector(Vector<uInt> v);
     176  void addSortKey(const string &name);
     177  template<class T, DataType U> void addColumnToKey(const string &name);
     178  void deallocate();
     179  vector<string> cols_;
     180  Table table_;
     181  uInt counter_;
     182  uInt num_iter_;
     183  uInt num_row_;
     184  Sort sorter_;
     185  Vector<uInt> index_;
     186  Vector<uInt> unique_;
     187  vector<void*> pointer_;
     188} ;
     189
    157190} // namespace
    158191#endif /* _ASAP_INDEX_ITERATOR_H_ */
  • trunk/src/STMath.cpp

    r2905 r2911  
    36213621    cols[1] = "POLNO" ;
    36223622    cols[2] = "IFNO" ;
    3623     STIdxIter *iter = new STIdxIterAcc( out, cols ) ;
     3623    STIdxIter2 iter( out, cols ) ;
    36243624    STSelector sel ;
    3625     while ( !iter->pastEnd() ) {
    3626       Vector<uInt> ids = iter->current() ;
     3625    while ( !iter.pastEnd() ) {
     3626      Record ids = iter.currentValue() ;
    36273627      stringstream ss ;
    36283628      ss << "SELECT FROM $1 WHERE "
    3629          << "BEAMNO==" << ids[0] << "&&"
    3630          << "POLNO==" << ids[1] << "&&"
    3631          << "IFNO==" << ids[2] ;
     3629         << "BEAMNO==" << ids.asuInt(cols[0]) << "&&"
     3630         << "POLNO==" << ids.asuInt(cols[1]) << "&&"
     3631         << "IFNO==" << ids.asuInt(cols[2]) ;
    36323632      //cout << "TaQL string: " << ss.str() << endl ;
    36333633      sel.setTaQL( ss.str() ) ;
    36343634      aoff->setSelection( sel ) ;
    3635       Vector<uInt> rows = iter->getRows( SHARE ) ;
     3635      Vector<uInt> rows = iter.getRows( SHARE ) ;
    36363636      // out should be an exact copy of s except that SPECTRA column is empty
    36373637      calibrateALMA( out, s, aoff, rows ) ;
    36383638      aoff->unsetSelection() ;
    36393639      sel.reset() ;
    3640       iter->next() ;
    3641     }
    3642     delete iter ;
     3640      iter.next() ;
     3641    }
    36433642    s->table_ = torg ;
    36443643    s->attach() ;
Note: See TracChangeset for help on using the changeset viewer.