[2913] | 1 | #include <assert.h>
|
---|
[2519] | 2 | #include <iostream>
|
---|
| 3 | #include <casa/Utilities/GenSort.h>
|
---|
| 4 | #include <casa/Arrays/Vector.h>
|
---|
| 5 | #include <casa/Arrays/Matrix.h>
|
---|
| 6 | #include <casa/Arrays/ArrayMath.h>
|
---|
| 7 | #include <casa/Arrays/ArrayIO.h>
|
---|
[2911] | 8 | #include <casa/Utilities/DataType.h>
|
---|
[2519] | 9 | #include <tables/Tables/ScalarColumn.h>
|
---|
[2911] | 10 | #include <tables/Tables/TableRow.h>
|
---|
| 11 | #include <tables/Tables/TableRecord.h>
|
---|
[2519] | 12 | #include "STIdxIter.h"
|
---|
| 13 |
|
---|
| 14 | namespace asap {
|
---|
[2911] | 15 | STIdxIter2::STIdxIter2()
|
---|
| 16 | : cols_(),
|
---|
| 17 | table_(),
|
---|
| 18 | counter_(0),
|
---|
| 19 | num_iter_(0),
|
---|
| 20 | num_row_(0),
|
---|
| 21 | sorter_(),
|
---|
| 22 | index_(),
|
---|
| 23 | unique_(),
|
---|
[2918] | 24 | pointer_(),
|
---|
| 25 | string_storage_()
|
---|
[2911] | 26 | {
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | STIdxIter2::STIdxIter2( const string &name,
|
---|
| 30 | const vector<string> &cols )
|
---|
| 31 | : cols_(cols),
|
---|
| 32 | table_(name, Table::Old),
|
---|
| 33 | counter_(0),
|
---|
| 34 | num_iter_(0),
|
---|
| 35 | num_row_(0),
|
---|
| 36 | sorter_(),
|
---|
| 37 | index_(),
|
---|
| 38 | unique_(),
|
---|
[2918] | 39 | pointer_(),
|
---|
| 40 | string_storage_()
|
---|
[2911] | 41 | {
|
---|
| 42 | init();
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | STIdxIter2::STIdxIter2( const CountedPtr<Scantable> &s,
|
---|
| 46 | const vector<string> &cols )
|
---|
| 47 | : cols_(cols),
|
---|
| 48 | table_(s->table()),
|
---|
| 49 | counter_(0),
|
---|
| 50 | num_iter_(0),
|
---|
| 51 | num_row_(0),
|
---|
| 52 | sorter_(),
|
---|
| 53 | index_(),
|
---|
| 54 | unique_(),
|
---|
[2918] | 55 | pointer_(),
|
---|
| 56 | string_storage_()
|
---|
[2911] | 57 | {
|
---|
| 58 | init();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | STIdxIter2::~STIdxIter2()
|
---|
| 62 | {
|
---|
| 63 | deallocate();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void STIdxIter2::deallocate()
|
---|
| 67 | {
|
---|
| 68 | for (vector<void*>::iterator i = pointer_.begin(); i != pointer_.end(); ++i) {
|
---|
| 69 | free(*i);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | Record STIdxIter2::currentValue() {
|
---|
| 74 | assert(counter_ < num_iter_);
|
---|
| 75 | Vector<String> cols(cols_.size());
|
---|
| 76 | for (uInt i = 0; i < cols.nelements(); ++i) {
|
---|
| 77 | cols[i] = cols_[i];
|
---|
| 78 | }
|
---|
| 79 | const ROTableRow row(table_, cols);
|
---|
| 80 | const TableRecord rec = row.get(index_[unique_[counter_]]);
|
---|
| 81 | return Record(rec);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | Bool STIdxIter2::pastEnd() {
|
---|
| 85 | return counter_ >= num_iter_;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void STIdxIter2::next() {
|
---|
| 89 | counter_++;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | vector<uInt> STIdxIter2::tovector( Vector<uInt> v )
|
---|
| 93 | {
|
---|
| 94 | vector<uInt> ret ;
|
---|
| 95 | v.tovector( ret ) ;
|
---|
| 96 | return ret ;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | Vector<uInt> STIdxIter2::getRows( StorageInitPolicy policy )
|
---|
| 100 | {
|
---|
[2914] | 101 | assert(num_iter_ >= 1);
|
---|
[2911] | 102 | assert(counter_ < num_iter_);
|
---|
| 103 | if (counter_ == num_iter_ - 1) {
|
---|
| 104 | uInt start = unique_[counter_];
|
---|
| 105 | uInt num_row = num_row_ - start;
|
---|
| 106 | Vector<uInt> rows(IPosition(1, num_row), &(index_.data()[start]), policy);
|
---|
| 107 | return rows;
|
---|
| 108 | }
|
---|
| 109 | else {
|
---|
| 110 | uInt start = unique_[counter_];
|
---|
| 111 | uInt end = unique_[counter_ + 1];
|
---|
| 112 | uInt num_row = end - start;
|
---|
| 113 | Vector<uInt> rows(IPosition(1, num_row), &(index_.data()[start]), policy);
|
---|
| 114 | return rows;
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void STIdxIter2::init()
|
---|
| 119 | {
|
---|
[2912] | 120 | num_row_ = table_.nrow();
|
---|
[2911] | 121 | for (uInt i = 0; i < cols_.size(); ++i) {
|
---|
| 122 | addSortKey(cols_[i]);
|
---|
| 123 | }
|
---|
| 124 | sorter_.sort(index_, num_row_);
|
---|
| 125 | num_iter_ = sorter_.unique(unique_, index_);
|
---|
| 126 | // cout << "num_row_ = " << num_row_ << endl
|
---|
| 127 | // << "num_iter_ = " << num_iter_ << endl;
|
---|
| 128 | // cout << "unique_ = " << unique_ << endl;
|
---|
| 129 | // cout << "index_ = " << index_ << endl;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void STIdxIter2::addSortKey(const string &name)
|
---|
| 133 | {
|
---|
| 134 | const ColumnDesc &desc = table_.tableDesc().columnDesc(name);
|
---|
| 135 | const DataType dtype = desc.trueDataType();
|
---|
| 136 | switch (dtype) {
|
---|
| 137 | case TpUInt:
|
---|
| 138 | addColumnToKey<uInt, TpUInt>(name);
|
---|
| 139 | break;
|
---|
| 140 | case TpInt:
|
---|
| 141 | addColumnToKey<Int, TpInt>(name);
|
---|
| 142 | break;
|
---|
| 143 | case TpFloat:
|
---|
| 144 | addColumnToKey<Float, TpFloat>(name);
|
---|
| 145 | break;
|
---|
| 146 | case TpDouble:
|
---|
| 147 | addColumnToKey<Double, TpDouble>(name);
|
---|
| 148 | break;
|
---|
| 149 | case TpComplex:
|
---|
| 150 | addColumnToKey<Complex, TpComplex>(name);
|
---|
| 151 | break;
|
---|
[2918] | 152 | case TpString:
|
---|
| 153 | addColumnToKeyTpString(name);
|
---|
| 154 | break;
|
---|
[2911] | 155 | default:
|
---|
| 156 | deallocate();
|
---|
| 157 | stringstream oss;
|
---|
| 158 | oss << name << ": data type is not supported" << endl;
|
---|
| 159 | throw(AipsError(oss.str()));
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | template<class T, DataType U>
|
---|
| 164 | void STIdxIter2::addColumnToKey(const string &name)
|
---|
| 165 | {
|
---|
[2912] | 166 | void *raw_storage = malloc(sizeof(T) * num_row_);
|
---|
[2911] | 167 | T *storage = reinterpret_cast<T*>(raw_storage);
|
---|
[2912] | 168 | Vector<T> array(IPosition(1, num_row_), storage, SHARE);
|
---|
[2911] | 169 | ROScalarColumn<T> col(table_, name);
|
---|
| 170 | col.getColumn(array);
|
---|
| 171 | sorter_.sortKey(storage, U, 0, Sort::Ascending);
|
---|
| 172 | pointer_.push_back(raw_storage);
|
---|
| 173 | }
|
---|
[2918] | 174 |
|
---|
| 175 | void STIdxIter2::addColumnToKeyTpString(const string &name)
|
---|
| 176 | {
|
---|
| 177 | ROScalarColumn<String> col(table_, name);
|
---|
| 178 | String *storage = new String[num_row_];
|
---|
| 179 | Vector<String> array(IPosition(1, num_row_), storage, TAKE_OVER);
|
---|
| 180 | col.getColumn(array);
|
---|
| 181 | sorter_.sortKey(storage, TpString, 0, Sort::Ascending);
|
---|
| 182 | string_storage_.push_back(array);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[2519] | 185 | } // namespace
|
---|
[2911] | 186 |
|
---|