lsst.afw  14.0-40-g0271be365
BaseColumnView.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 #ifndef AFW_TABLE_BaseColumnView_h_INCLUDED
3 #define AFW_TABLE_BaseColumnView_h_INCLUDED
4 
5 #include <cstdint>
6 
8 
9 namespace lsst {
10 namespace afw {
11 namespace table {
12 
13 namespace detail {
14 
16 struct FlagExtractor {
18  typedef bool result_type;
19 
20  result_type operator()(argument_type element) const { return element & _mask; }
21 
22  explicit FlagExtractor(Key<Flag> const& key) : _mask(argument_type(1) << key.getBit()) {}
23 
24 private:
25  argument_type _mask;
26 };
27 
28 } // namespace detail
29 
30 class BaseTable;
31 
32 class BaseColumnView;
33 
42 class BitsColumn {
43 public:
44  typedef std::int64_t IntT;
45 
46  ndarray::Array<IntT, 1, 1> getArray() const { return _array; }
47 
48  IntT getBit(Key<Flag> const& key) const;
49  IntT getBit(std::string const& name) const;
50 
51  IntT getMask(Key<Flag> const& key) const { return IntT(1) << getBit(key); }
52  IntT getMask(std::string const& name) const { return IntT(1) << getBit(name); }
53 
54  std::vector<SchemaItem<Flag> > const& getSchemaItems() const { return _items; }
55 
56 private:
57  friend class BaseColumnView;
58 
59  explicit BitsColumn(int size);
60 
61  ndarray::Array<IntT, 1, 1> _array;
63 };
64 
84 public:
86  std::shared_ptr<BaseTable> getTable() const;
87 
89  Schema getSchema() const { return getTable()->getSchema(); }
90 
92  template <typename T>
93  ndarray::ArrayRef<T, 1> const operator[](Key<T> const& key) const;
94 
96  template <typename T>
97  ndarray::ArrayRef<T, 2, 1> const operator[](Key<Array<T> > const& key) const;
98 
106  ndarray::result_of::vectorize<detail::FlagExtractor, ndarray::Array<Field<Flag>::Element const, 1> >::type
107  operator[](Key<Flag> const& key) const;
108 
117  BitsColumn getBits(std::vector<Key<Flag> > const& keys) const;
118 
127  BitsColumn getAllBits() const;
128 
135  template <typename InputIterator>
136  static BaseColumnView make(std::shared_ptr<BaseTable> const& table, InputIterator first,
137  InputIterator last);
138 
146  template <typename InputIterator>
147  static bool isRangeContiguous(std::shared_ptr<BaseTable> const& table, InputIterator first,
148  InputIterator last);
149 
150  ~BaseColumnView();
151 
152 protected:
153  BaseColumnView(std::shared_ptr<BaseTable> const& table, int recordCount, void* buf,
154  ndarray::Manager::Ptr const& manager);
155 
156 private:
157  friend class BaseTable;
158 
159  struct Impl;
160 
161  std::shared_ptr<Impl> _impl;
162 };
163 
164 template <typename RecordT>
165 class ColumnViewT : public BaseColumnView {
166 public:
167  typedef RecordT Record;
168  typedef typename RecordT::Table Table;
169 
173  }
174 
176  template <typename InputIterator>
177  static ColumnViewT make(std::shared_ptr<Table> const& table, InputIterator first, InputIterator last) {
178  return ColumnViewT(BaseColumnView::make(table, first, last));
179  }
180 
181 protected:
182  explicit ColumnViewT(BaseColumnView const& base) : BaseColumnView(base) {}
183 };
184 
185 template <typename InputIterator>
187  InputIterator last) {
188  if (first == last) {
189  return BaseColumnView(table, 0, 0, ndarray::Manager::Ptr());
190  }
191  Schema schema = table->getSchema();
192  std::size_t recordSize = schema.getRecordSize();
193  std::size_t recordCount = 1;
194  void* buf = first->_data;
195  ndarray::Manager::Ptr manager = first->_manager;
196  char* expected = reinterpret_cast<char*>(buf) + recordSize;
197  for (++first; first != last; ++first, ++recordCount, expected += recordSize) {
198  if (first->_data != expected || first->_manager != manager) {
200  "Record data is not contiguous in memory.");
201  }
202  }
203  return BaseColumnView(table, recordCount, buf, manager);
204 }
205 
206 template <typename InputIterator>
208  InputIterator last) {
209  if (first == last) return true;
210  Schema schema = table->getSchema();
211  std::size_t recordSize = schema.getRecordSize();
212  std::size_t recordCount = 1;
213  void* buf = first->_data;
214  ndarray::Manager::Ptr manager = first->_manager;
215  char* expected = reinterpret_cast<char*>(buf) + recordSize;
216  for (++first; first != last; ++first, ++recordCount, expected += recordSize) {
217  if (first->_data != expected || first->_manager != manager) {
218  return false;
219  }
220  }
221  return true;
222 }
223 }
224 }
225 } // namespace lsst::afw::table
226 
227 #endif // !AFW_TABLE_BaseColumnView_h_INCLUDED
Defines the fields and offsets for a table.
Definition: Schema.h:47
ndarray::Array< IntT, 1, 1 > getArray() const
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:145
Column-wise view into a sequence of records that have been allocated contiguously.
A packed representation of a collection of Flag field columns.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
IntT getMask(std::string const &name) const
afw::table::Schema schema
Definition: GaussianPsf.cc:43
static ColumnViewT make(std::shared_ptr< Table > const &table, InputIterator first, InputIterator last)
Construct a BaseColumnView from an iterator range.
ColumnViewT(BaseColumnView const &base)
Schema getSchema() const
Return the schema that defines the fields.
STL class.
Functor to compute a flag bit, used to create an ndarray expression template for flag columns...
A base class for image defects.
static BaseColumnView make(std::shared_ptr< BaseTable > const &table, InputIterator first, InputIterator last)
Construct a BaseColumnView from an iterator range.
std::shared_ptr< Table > getTable() const
Return the table that owns the records.
A description of a field in a table.
Definition: Field.h:24
Tag types used to declare specialized field types.
Definition: misc.h:32
std::shared_ptr< BaseTable > getTable() const
Return the table that owns the records.
T static_pointer_cast(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:79
result_type operator()(argument_type element) const
IntT getMask(Key< Flag > const &key) const
static bool isRangeContiguous(std::shared_ptr< BaseTable > const &table, InputIterator first, InputIterator last)
Return true if the given record iterator range is continuous and the records all belong to the given ...
std::vector< SchemaItem< Flag > > const & getSchemaItems() const
FlagExtractor(Key< Flag > const &key)
Base class for all tables.
Definition: BaseTable.h:44