lsst.afw  16.0-76-g802307b6d+1
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 
7 namespace lsst {
8 namespace afw {
9 namespace table {
10 
11 // =============== BitsColumn implementation ================================================================
12 
13 namespace {
14 
15 struct 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 
23 struct 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  IntT 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  IntT 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 
51 BitsColumn::BitsColumn(int size) : _array(ndarray::allocate(size)) { _array.deep() = IntT(0); }
52 
53 // =============== BaseColumnView private Impl object =======================================================
54 
56  int 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 
61  Impl(std::shared_ptr<BaseTable> const &table_, int recordCount_, void *buf_,
62  ndarray::Manager::Ptr const &manager_)
63  : recordCount(recordCount_), buf(buf_), table(table_), manager(manager_) {}
64 };
65 
66 // =============== BaseColumnView member function implementations ===========================================
67 
68 std::shared_ptr<BaseTable> BaseColumnView::getTable() const { return _impl->table; }
69 
70 template <typename T>
71 typename ndarray::ArrayRef<T, 1> const BaseColumnView::operator[](Key<T> const &key) const {
72  return ndarray::external(reinterpret_cast<T *>(reinterpret_cast<char *>(_impl->buf) + key.getOffset()),
73  ndarray::makeVector(_impl->recordCount),
74  ndarray::makeVector(int(_impl->table->getSchema().getRecordSize() / sizeof(T))),
75  _impl->manager);
76 }
77 
78 template <typename T>
79 typename ndarray::ArrayRef<T, 2, 1> const BaseColumnView::operator[](Key<Array<T> > const &key) const {
80  if (key.isVariableLength()) {
81  throw LSST_EXCEPT(pex::exceptions::LogicError, "Cannot get columns for variable-length array fields");
82  }
83  return ndarray::external(
84  reinterpret_cast<T *>(reinterpret_cast<char *>(_impl->buf) + key.getOffset()),
85  ndarray::makeVector(_impl->recordCount, key.getSize()),
86  ndarray::makeVector(int(_impl->table->getSchema().getRecordSize() / sizeof(T)), 1),
87  _impl->manager);
88 }
89 
90 ndarray::result_of::vectorize<detail::FlagExtractor, ndarray::Array<Field<Flag>::Element const, 1> >::type
92  return ndarray::vectorize(detail::FlagExtractor(key),
93  ndarray::Array<Field<Flag>::Element const, 1>(ndarray::external(
94  reinterpret_cast<Field<Flag>::Element *>(
95  reinterpret_cast<char *>(_impl->buf) + key.getOffset()),
96  ndarray::makeVector(_impl->recordCount),
97  ndarray::makeVector(int(_impl->table->getSchema().getRecordSize() /
98  sizeof(Field<Flag>::Element))),
99  _impl->manager)));
100 }
101 
103  BitsColumn result(_impl->recordCount);
104  ndarray::ArrayRef<BitsColumn::IntT, 1, 1> array = result._array.deep();
105  if (keys.size() > sizeof(BitsColumn::IntT)) {
107  (boost::format("Too many keys passed to getBits(); %d > %d.") % keys.size() %
108  sizeof(BitsColumn::IntT))
109  .str());
110  }
111  BitsColumn::IntT const size = keys.size(); // just for unsigned/signed comparisons
112  for (BitsColumn::IntT i = 0; i < size; ++i) {
113  array |= (BitsColumn::IntT(1) << i) * (*this)[keys[i]];
114  result._items.push_back(getSchema().find(keys[i]));
115  }
116  return result;
117 }
118 
119 namespace {
120 
121 struct ExtractFlagItems {
122  template <typename T>
123  void operator()(SchemaItem<T> const &) const {}
124 
125  void operator()(SchemaItem<Flag> const &item) const { items->push_back(item); }
126 
128 };
129 
130 } // namespace
131 
133  BitsColumn result(_impl->recordCount);
134  ExtractFlagItems func = {&result._items};
135  getSchema().forEach(func);
136  if (result._items.size() > sizeof(BitsColumn::IntT)) {
138  (boost::format("Too many Flag keys in schema; %d > %d.") % result._items.size() %
139  sizeof(BitsColumn::IntT))
140  .str());
141  }
142  ndarray::ArrayRef<BitsColumn::IntT, 1, 1> array = result._array.deep();
143  BitsColumn::IntT const size = result._items.size(); // just for unsigned/signed comparisons
144  for (BitsColumn::IntT i = 0; i < size; ++i) {
145  array |= (BitsColumn::IntT(1) << i) * (*this)[result._items[i].key];
146  }
147  return result;
148 }
149 
154 
155 // needs to be in source file so it can (implicitly) call Impl's (implicit) dtor
157 
158 BaseColumnView::BaseColumnView(std::shared_ptr<BaseTable> const &table, int recordCount, void *buf,
159  ndarray::Manager::Ptr const &manager)
160  : _impl(std::make_shared<Impl>(table, recordCount, buf, manager)) {}
161 
162 // =============== Explicit instantiations ==================================================================
163 
164 #define INSTANTIATE_COLUMNVIEW_SCALAR(r, data, elem) \
165  template ndarray::ArrayRef<elem, 1> const BaseColumnView::operator[](Key<elem> const &) const;
166 
169 
170 #define INSTANTIATE_COLUMNVIEW_ARRAY(r, data, elem) \
171  template ndarray::ArrayRef<elem, 2, 1> const BaseColumnView::operator[](Key<Array<elem> > const &) const;
172 
175 } // namespace table
176 } // namespace afw
177 } // namespace lsst
#define INSTANTIATE_COLUMNVIEW_ARRAY(r, data, elem)
#define AFW_TABLE_SCALAR_FIELD_TYPE_N
Definition: types.h:28
Column-wise view into a sequence of records that have been allocated contiguously.
A packed representation of a collection of Flag field columns.
def keys(self)
int getOffset() const noexcept
Return the offset (in bytes) of this field within a record.
Definition: Key.h:87
BaseColumnView & operator=(BaseColumnView const &)
Key< Flag > const & target
BitsColumn getAllBits() const
Return an integer array with all Flag fields repacked into individual bits.
std::shared_ptr< BaseTable > getTable() const
Return the table that owns the records.
std::vector< SchemaItem< Flag > > * items
IntT getBit(Key< Flag > const &key) const
BitsColumn getBits(std::vector< Key< Flag > > const &keys) const
Return an integer array with the given Flag fields repacked into individual bits. ...
STL namespace.
#define INSTANTIATE_COLUMNVIEW_SCALAR(r, data, elem)
std::shared_ptr< BaseTable > table
STL class.
Functor to compute a flag bit, used to create an ndarray expression template for flag columns...
A base class for image defects.
#define AFW_TABLE_SCALAR_FIELD_TYPE_TUPLE
Definition: types.h:31
#define AFW_TABLE_ARRAY_FIELD_TYPE_N
Definition: types.h:34
Impl(std::shared_ptr< BaseTable > const &table_, int recordCount_, void *buf_, ndarray::Manager::Ptr const &manager_)
A description of a field in a table.
Definition: Field.h:24
Key< U > key
Definition: Schema.cc:281
Tag types used to declare specialized field types.
Definition: misc.h:32
BaseColumnView(BaseColumnView const &)
table::Key< int > type
Definition: Detector.cc:164
std::unique_ptr< SchemaItem< U > > result
Definition: Schema.cc:195
ndarray::ArrayRef< T, 1 > const operator[](Key< T > const &key) const
Return a 1-d array corresponding to a scalar field (or subfield).
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
T find_if(T... args)
#define LSST_EXCEPT(type,...)
STL class.
A class used as a handle to a particular field in a table.
Definition: fwd.h:45
Key specialization for Flag.
Definition: Flag.h:94
int getOffset() const
Return the offset in bytes of the integer element that holds this field&#39;s bit.
Definition: Flag.h:126
#define AFW_TABLE_ARRAY_FIELD_TYPE_TUPLE
Definition: types.h:36
A simple pair-like struct for mapping a Field (name and description) with a Key (used for actual data...
Definition: SchemaImpl.h:27