LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
BaseColumnView.cc
Go to the documentation of this file.
1#include "boost/preprocessor/seq/for_each.hpp"
2#include "boost/preprocessor/tuple/to_seq.hpp"
3
6
7namespace lsst {
8namespace afw {
9namespace table {
10
11// =============== BitsColumn implementation ================================================================
12
13namespace {
14
15struct MatchKey {
16 bool operator()(SchemaItem<Flag> const &item) const { return item.key == target; }
17
18 explicit MatchKey(Key<Flag> const &t) : target(t) {}
19
20 Key<Flag> const &target;
21};
22
23struct MatchName {
24 bool operator()(SchemaItem<Flag> const &item) const { return item.field.getName() == target; }
25
26 explicit MatchName(std::string const &t) : target(t) {}
27
28 std::string const &target;
29};
30
31} // namespace
32
34 SizeT r = std::find_if(_items.begin(), _items.end(), MatchKey(key)) - _items.begin();
35 if (std::size_t(r) == _items.size()) {
37 (boost::format("'%s' not found in BitsColumn") % key).str());
38 }
39 return r;
40}
41
43 SizeT r = std::find_if(_items.begin(), _items.end(), MatchName(name)) - _items.begin();
44 if (std::size_t(r) == _items.size()) {
46 (boost::format("'%s' not found in BitsColumn") % name).str());
47 }
48 return r;
49}
50
51BitsColumn::BitsColumn(std::size_t size) : _array(ndarray::allocate(size)) { _array.deep() = SizeT(0); }
52
53// =============== BaseColumnView private Impl object =======================================================
54
56 std::size_t recordCount; // number of records
57 void *buf; // pointer to the beginning of the first record's data
58 std::shared_ptr<BaseTable> table; // table that owns the records
59 ndarray::Manager::Ptr manager; // manages lifetime of 'buf'
60
62 ndarray::Manager::Ptr const &manager_)
64};
65
66// =============== BaseColumnView member function implementations ===========================================
67
69
70template <typename T>
71typename ndarray::ArrayRef<T, 1> const BaseColumnView::operator[](Key<T> const &key) const {
72 if (!key.isValid()) {
73 throw LSST_EXCEPT(
75 "Key is not valid (if this is a SourceCatalog, make sure slot aliases have been set up).");
76 }
77 return ndarray::external(reinterpret_cast<T *>(reinterpret_cast<char *>(_impl->buf) + key.getOffset()),
78 ndarray::makeVector(_impl->recordCount),
79 ndarray::makeVector(_impl->table->getSchema().getRecordSize() / sizeof(T)),
80 _impl->manager);
81}
82
83template <typename T>
84typename ndarray::ArrayRef<T, 2, 1> const BaseColumnView::operator[](Key<Array<T> > const &key) const {
85 if (!key.isValid()) {
86 throw LSST_EXCEPT(
88 "Key is not valid (if this is a SourceCatalog, make sure slot aliases have been set up).");
89 }
90 if (key.isVariableLength()) {
91 throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot get columns for variable-length array fields");
92 }
93 return ndarray::external(
94 reinterpret_cast<T *>(reinterpret_cast<char *>(_impl->buf) + key.getOffset()),
95 ndarray::makeVector(_impl->recordCount, key.getSize()),
96 ndarray::makeVector(_impl->table->getSchema().getRecordSize() / sizeof(T), std::size_t(1)),
97 _impl->manager);
98}
99
100ndarray::result_of::vectorize<detail::FlagExtractor, ndarray::Array<Field<Flag>::Element const, 1> >::type
102 if (!key.isValid()) {
103 throw LSST_EXCEPT(
105 "Key is not valid (if this is a SourceCatalog, make sure slot aliases have been set up).");
106 }
107 return ndarray::vectorize(detail::FlagExtractor(key),
108 ndarray::Array<Field<Flag>::Element const, 1>(ndarray::external(
109 reinterpret_cast<Field<Flag>::Element *>(
110 reinterpret_cast<char *>(_impl->buf) + key.getOffset()),
111 ndarray::makeVector(_impl->recordCount),
112 ndarray::makeVector(_impl->table->getSchema().getRecordSize() /
113 sizeof(Field<Flag>::Element)),
114 _impl->manager)));
115}
116
117ndarray::Array<double, 1> const BaseColumnView::get_radians_array(Key<Angle> const & key) const {
118 using DoubleArray = ndarray::Array<double, 1>;
119 using AngleArray = ndarray::Array<lsst::geom::Angle, 1>;
120 AngleArray angle_array = (*this)[key];
121 return ndarray::detail::ArrayAccess<DoubleArray>::construct(
122 reinterpret_cast<double *>(angle_array.getData()),
123 ndarray::detail::ArrayAccess<AngleArray>::getCore(angle_array)
124 );
125}
126
129 ndarray::ArrayRef<BitsColumn::SizeT, 1, 1> array = result._array.deep();
130 if (keys.size() > sizeof(BitsColumn::SizeT)) {
132 (boost::format("Too many keys passed to getBits(); %d > %d.") % keys.size() %
133 sizeof(BitsColumn::SizeT))
134 .str());
135 }
136 BitsColumn::SizeT const size = keys.size(); // just for unsigned/signed comparisons
137 for (BitsColumn::SizeT i = 0; i < size; ++i) {
138 array |= (BitsColumn::SizeT(1) << i) * (*this)[keys[i]];
139 result._items.push_back(getSchema().find(keys[i]));
140 }
141 return result;
142}
143
144namespace {
145
146struct ExtractFlagItems {
147 template <typename T>
148 void operator()(SchemaItem<T> const &) const {}
149
150 void operator()(SchemaItem<Flag> const &item) const { items->push_back(item); }
151
153};
154
155} // namespace
156
159 ExtractFlagItems func = {&result._items};
160 getSchema().forEach(func);
161 if (result._items.size() > sizeof(BitsColumn::SizeT)) {
163 (boost::format("Too many Flag keys in schema; %d > %d.") % result._items.size() %
164 sizeof(BitsColumn::SizeT))
165 .str());
166 }
167 ndarray::ArrayRef<BitsColumn::SizeT, 1, 1> array = result._array.deep();
168 BitsColumn::SizeT const size = result._items.size(); // just for unsigned/signed comparisons
169 for (BitsColumn::SizeT i = 0; i < size; ++i) {
170 array |= (BitsColumn::SizeT(1) << i) * (*this)[result._items[i].key];
171 }
172 return result;
173}
174
179
180// needs to be in source file so it can (implicitly) call Impl's (implicit) dtor
182
184 ndarray::Manager::Ptr const &manager)
185 : _impl(std::make_shared<Impl>(table, recordCount, buf, manager)) {}
186
187// =============== Explicit instantiations ==================================================================
188
189#define INSTANTIATE_COLUMNVIEW_SCALAR(r, data, elem) \
190 template ndarray::ArrayRef<elem, 1> const BaseColumnView::operator[](Key<elem> const &) const;
191
194
195#define INSTANTIATE_COLUMNVIEW_ARRAY(r, data, elem) \
196 template ndarray::ArrayRef<elem, 2, 1> const BaseColumnView::operator[](Key<Array<elem> > const &) const;
197
200} // namespace table
201} // namespace afw
202} // namespace lsst
py::object result
Definition _schema.cc:429
#define INSTANTIATE_COLUMNVIEW_SCALAR(r, data, elem)
Key< Flag > const & target
#define INSTANTIATE_COLUMNVIEW_ARRAY(r, data, elem)
std::vector< SchemaItem< Flag > > * items
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition Exception.h:48
Tag types used to declare specialized field types.
Definition misc.h:31
Column-wise view into a sequence of records that have been allocated contiguously.
BaseColumnView & operator=(BaseColumnView const &)
BitsColumn getBits(std::vector< Key< Flag > > const &keys) const
Return an integer array with the given Flag fields repacked into individual bits.
Schema getSchema() const
Return the schema that defines the fields.
BaseColumnView(BaseColumnView const &)
BitsColumn getAllBits() const
Return an integer array with all Flag fields repacked into individual bits.
ndarray::Array< double, 1 > const get_radians_array(Key< Angle > const &key) const
Return an array column as a double array in radians.
std::shared_ptr< BaseTable > getTable() const
Return the table that owns the records.
ndarray::ArrayRef< T, 1 > const operator[](Key< T > const &key) const
Return a 1-d array corresponding to a scalar field (or subfield).
A packed representation of a collection of Flag field columns.
SizeT getBit(Key< Flag > const &key) const
A class used as a handle to a particular field in a table.
Definition Key.h:53
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
Definition Schema.h:214
SchemaItem< T > find(std::string const &name) const
Find a SchemaItem in the Schema by name.
Definition Schema.cc:467
Reports attempts to exceed implementation-defined length limits for some classes.
Definition Runtime.h:76
Reports errors in the logical structure of the program.
Definition Runtime.h:46
Reports attempts to access elements using an invalid key.
Definition Runtime.h:151
T find_if(T... args)
BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_SCALAR, _, BOOST_PP_TUPLE_TO_SEQ(AFW_TABLE_SCALAR_FIELD_TYPE_N, AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE)) BOOST_PP_SEQ_FOR_EACH(INSTANTIATE_COLUMNVIEW_ARRAY
STL namespace.
std::shared_ptr< BaseTable > table
Impl(std::shared_ptr< BaseTable > const &table_, size_t recordCount_, void *buf_, ndarray::Manager::Ptr const &manager_)
typename FieldBase< T >::Element Element
Type used to store field data in the table (a field may have multiple elements).
Definition Field.h:26
Functor to compute a flag bit, used to create an ndarray expression template for flag columns.
#define AFW_TABLE_ARRAY_FIELD_TYPE_TUPLE
Definition types.h:35
#define AFW_TABLE_ARRAY_FIELD_TYPE_N
Definition types.h:33
#define AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE
Definition types.h:30
#define AFW_TABLE_SCALAR_FIELD_TYPE_N
Definition types.h:27