source: branches/casa-release-4_3/src/STIdxIter.cpp@ 3113

Last change on this file since 3113 was 2921, checked in by Takeshi Nakazato, 11 years ago

New Development: No

JIRA Issue: No

Ready for Test: Yes

Interface Changes: Yes

What Interface Changed: Replaced C++ implementation of python iterator with

STIdxIter2 (was STIdxIterAcc)

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Removed STIdxIter classes except STIdxIter2. Implementation of Python
iterator is replaced with STIdxITer2.


File size: 4.2 KB
Line 
1#include <assert.h>
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>
8#include <casa/Utilities/DataType.h>
9#include <tables/Tables/ScalarColumn.h>
10#include <tables/Tables/TableRow.h>
11#include <tables/Tables/TableRecord.h>
12#include "STIdxIter.h"
13
14namespace asap {
15STIdxIter2::STIdxIter2()
16 : cols_(),
17 table_(),
18 counter_(0),
19 num_iter_(0),
20 num_row_(0),
21 sorter_(),
22 index_(),
23 unique_(),
24 pointer_(),
25 string_storage_()
26{
27}
28
29STIdxIter2::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_(),
39 pointer_(),
40 string_storage_()
41{
42 init();
43}
44
45STIdxIter2::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_(),
55 pointer_(),
56 string_storage_()
57{
58 init();
59}
60
61STIdxIter2::~STIdxIter2()
62{
63 deallocate();
64}
65
66void STIdxIter2::deallocate()
67{
68 for (vector<void*>::iterator i = pointer_.begin(); i != pointer_.end(); ++i) {
69 free(*i);
70 }
71}
72
73Record 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
84Bool STIdxIter2::pastEnd() {
85 return counter_ >= num_iter_;
86}
87
88void STIdxIter2::next() {
89 counter_++;
90}
91
92vector<uInt> STIdxIter2::tovector( Vector<uInt> v )
93{
94 vector<uInt> ret ;
95 v.tovector( ret ) ;
96 return ret ;
97}
98
99Vector<uInt> STIdxIter2::getRows( StorageInitPolicy policy )
100{
101 assert(num_iter_ >= 1);
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
118void STIdxIter2::init()
119{
120 num_row_ = table_.nrow();
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
132void 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;
152 case TpString:
153 addColumnToKeyTpString(name);
154 break;
155 default:
156 deallocate();
157 stringstream oss;
158 oss << name << ": data type is not supported" << endl;
159 throw(AipsError(oss.str()));
160 }
161}
162
163template<class T, DataType U>
164void STIdxIter2::addColumnToKey(const string &name)
165{
166 void *raw_storage = malloc(sizeof(T) * num_row_);
167 T *storage = reinterpret_cast<T*>(raw_storage);
168 Vector<T> array(IPosition(1, num_row_), storage, SHARE);
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}
174
175void 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
185} // namespace
186
Note: See TracBrowser for help on using the repository browser.