LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
LSSTDataManagementBasePackage
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 { namespace afw { namespace table {
10 
11 namespace detail {
12 
14 struct FlagExtractor {
16  typedef bool result_type;
17 
18  result_type operator()(argument_type element) const { return element & _mask; }
19 
20  explicit FlagExtractor(Key<Flag> const & key) : _mask(argument_type(1) << key.getBit()) {}
21 
22 private:
24 };
25 
26 } // namespace detail
27 
28 class BaseTable;
29 
30 class BaseColumnView;
31 
40 class BitsColumn {
41 public:
42 
43  typedef std::int64_t IntT;
44 
45  ndarray::Array<IntT,1,1> getArray() const { return _array; }
46 
47  IntT getBit(Key<Flag> const & key) const;
48  IntT getBit(std::string const & name) const;
49 
50  IntT getMask(Key<Flag> const & key) const { return IntT(1) << getBit(key); }
51  IntT getMask(std::string const & name) const { return IntT(1) << getBit(name); }
52 
53 #ifndef SWIG
54  std::vector< SchemaItem<Flag> > const & getSchemaItems() const { return _items; }
55 #endif
56 
57 private:
58 
59  friend class BaseColumnView;
60 
61  explicit BitsColumn(int size);
62 
63  ndarray::Array<IntT,1,1> _array;
64  std::vector< SchemaItem<Flag> > _items;
65 };
66 
86 public:
87 
89  PTR(BaseTable) getTable() const;
90 
92  Schema getSchema() const { return getTable()->getSchema(); }
93 
95  template <typename T>
96  ndarray::ArrayRef<T,1> const operator[](Key<T> const & key) const;
97 
99  template <typename T>
100  ndarray::ArrayRef<T,2,1> const operator[](Key< Array<T> > const & key) const;
101 
109  ndarray::result_of::vectorize< detail::FlagExtractor,
110  ndarray::Array< Field<Flag>::Element const,1> >::type
111  operator[](Key<Flag> const & key) const;
112 
121  BitsColumn getBits(std::vector< Key<Flag> > const & keys) const;
122 
131  BitsColumn getAllBits() const;
132 
139  template <typename InputIterator>
140  static BaseColumnView make(PTR(BaseTable) const & table, InputIterator first, InputIterator last);
141 
149  template <typename InputIterator>
150  static bool isRangeContiguous(PTR(BaseTable) const & table, InputIterator first, InputIterator last);
151 
152  ~BaseColumnView();
153 
154 protected:
155 
157  PTR(BaseTable) const & table, int recordCount, void * buf, ndarray::Manager::Ptr const & manager
158  );
159 
160 private:
161 
162  friend class BaseTable;
163 
164  struct Impl;
165 
166  std::shared_ptr<Impl> _impl;
167 };
168 
169 template <typename RecordT>
170 class ColumnViewT : public BaseColumnView {
171 public:
172 
173  typedef RecordT Record;
174  typedef typename RecordT::Table Table;
175 
177  PTR(Table) getTable() const { return std::static_pointer_cast<Table>(BaseColumnView::getTable()); }
178 
180  template <typename InputIterator>
181  static ColumnViewT make(PTR(Table) const & table, InputIterator first, InputIterator last) {
182  return ColumnViewT(BaseColumnView::make(table, first, last));
183  }
184 
185 protected:
186 
187  explicit ColumnViewT(BaseColumnView const & base) : BaseColumnView(base) {}
188 
189 };
190 
191 template <typename InputIterator>
192 BaseColumnView BaseColumnView::make(PTR(BaseTable) const & table, InputIterator first, InputIterator last) {
193  if (first == last) {
194  return BaseColumnView(table, 0, 0, ndarray::Manager::Ptr());
195  }
196  Schema schema = table->getSchema();
197  std::size_t recordSize = schema.getRecordSize();
198  std::size_t recordCount = 1;
199  void * buf = first->_data;
200  ndarray::Manager::Ptr manager = first->_manager;
201  char * expected = reinterpret_cast<char*>(buf) + recordSize;
202  for (++first; first != last; ++first, ++recordCount, expected += recordSize) {
203  if (first->_data != expected || first->_manager != manager) {
204  throw LSST_EXCEPT(
205  lsst::pex::exceptions::RuntimeError,
206  "Record data is not contiguous in memory."
207  );
208  }
209  }
210  return BaseColumnView(table, recordCount, buf, manager);
211 }
212 
213 template <typename InputIterator>
215  PTR(BaseTable) const & table, InputIterator first, InputIterator last
216 ) {
217  if (first == last) return true;
218  Schema schema = table->getSchema();
219  std::size_t recordSize = schema.getRecordSize();
220  std::size_t recordCount = 1;
221  void * buf = first->_data;
222  ndarray::Manager::Ptr manager = first->_manager;
223  char * expected = reinterpret_cast<char*>(buf) + recordSize;
224  for (++first; first != last; ++first, ++recordCount, expected += recordSize) {
225  if (first->_data != expected || first->_manager != manager) {
226  return false;
227  }
228  }
229  return true;
230 }
231 
232 }}} // namespace lsst::afw::table
233 
234 #endif // !AFW_TABLE_BaseColumnView_h_INCLUDED
Defines the fields and offsets for a table.
Definition: Schema.h:44
ndarray::Array< IntT, 1, 1 > _array
Schema getSchema() const
Return the schema that defines the fields.
BitsColumn getAllBits() const
Return an integer array with all Flag fields repacked into individual bits.
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
afw::table::Schema schema
Definition: GaussianPsf.cc:41
IntT getMask(Key< Flag > const &key) const
ColumnViewT(BaseColumnView const &base)
boost::shared_ptr< Table > getTable() const
Return the table that owns the records.
static ColumnViewT make(boost::shared_ptr< Table > const &table, InputIterator first, InputIterator last)
Construct a BaseColumnView from an iterator range.
BaseColumnView(boost::shared_ptr< BaseTable > const &table, int recordCount, void *buf, ndarray::Manager::Ptr const &manager)
BitsColumn getBits(std::vector< Key< Flag > > const &keys) const
Return an integer array with the given Flag fields repacked into individual bits. ...
Functor to compute a flag bit, used to create an ndarray expression template for flag columns...
std::shared_ptr< Impl > _impl
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:130
A description of a field in a table.
Definition: Field.h:22
Tag types used to declare specialized field types.
Definition: misc.h:34
static BaseColumnView make(boost::shared_ptr< BaseTable > const &table, InputIterator first, InputIterator last)
Construct a BaseColumnView from an iterator range.
std::vector< SchemaItem< Flag > > const & getSchemaItems() const
ndarray::ArrayRef< T, 1 > const operator[](Key< T > const &key) const
Return a 1-d array corresponding to a scalar field (or subfield).
boost::shared_ptr< BaseTable > getTable() const
Return the table that owns the records.
IntT getBit(Key< Flag > const &key) const
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
Key specialization for Flag.
Definition: Flag.h:84
#define PTR(...)
Definition: base.h:41
Schema getSchema() const
Return the table&#39;s schema.
Definition: BaseTable.h:125
static bool isRangeContiguous(boost::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 > > _items
result_type operator()(argument_type element) const
ndarray::Array< IntT, 1, 1 > getArray() const
FlagExtractor(Key< Flag > const &key)
Base class for all tables.
Definition: BaseTable.h:43
IntT getMask(std::string const &name) const