LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 
6 
7 namespace lsst { namespace afw { namespace table {
8 
9 namespace detail {
10 
12 struct FlagExtractor {
14  typedef bool result_type;
15 
16  result_type operator()(argument_type element) const { return element & _mask; }
17 
18  explicit FlagExtractor(Key<Flag> const & key) : _mask(argument_type(1) << key.getBit()) {}
19 
20 private:
22 };
23 
24 } // namespace detail
25 
26 class BaseTable;
27 
28 class BaseColumnView;
29 
38 class BitsColumn {
39 public:
40 
41  typedef boost::int64_t IntT;
42 
44 
45  IntT getBit(Key<Flag> const & key) const;
46  IntT getBit(std::string const & name) const;
47 
48  IntT getMask(Key<Flag> const & key) const { return IntT(1) << getBit(key); }
49  IntT getMask(std::string const & name) const { return IntT(1) << getBit(name); }
50 
51 #ifndef SWIG
52  std::vector< SchemaItem<Flag> > const & getSchemaItems() const { return _items; }
53 #endif
54 
55 private:
56 
57  friend class BaseColumnView;
58 
59  explicit BitsColumn(int size);
60 
62  std::vector< SchemaItem<Flag> > _items;
63 };
64 
84 public:
85 
87  PTR(BaseTable) getTable() const;
88 
90  Schema getSchema() const { return getTable()->getSchema(); }
91 
93  template <typename T>
94  ndarray::ArrayRef<T,1> const operator[](Key<T> const & key) const;
95 
97  template <typename T>
98  ndarray::ArrayRef<T,2,1> const operator[](Key< Array<T> > const & key) const;
99 
108  ndarray::Array< Field<Flag>::Element const,1> >::type
109  operator[](Key<Flag> const & key) const;
110 
119  BitsColumn getBits(std::vector< Key<Flag> > const & keys) const;
120 
129  BitsColumn getAllBits() const;
130 
137  template <typename InputIterator>
138  static BaseColumnView make(PTR(BaseTable) const & table, InputIterator first, InputIterator last);
139 
147  template <typename InputIterator>
148  static bool isRangeContiguous(PTR(BaseTable) const & table, InputIterator first, InputIterator last);
149 
150  ~BaseColumnView();
151 
152 protected:
153 
155  PTR(BaseTable) const & table, int recordCount, void * buf, ndarray::Manager::Ptr const & manager
156  );
157 
158 private:
159 
160  friend class BaseTable;
161 
162  struct Impl;
163 
164  boost::shared_ptr<Impl> _impl;
165 };
166 
167 template <typename RecordT>
168 class ColumnViewT : public BaseColumnView {
169 public:
170 
171  typedef RecordT Record;
172  typedef typename RecordT::Table Table;
173 
175  PTR(Table) getTable() const { return boost::static_pointer_cast<Table>(BaseColumnView::getTable()); }
176 
178  template <typename InputIterator>
179  static ColumnViewT make(PTR(Table) const & table, InputIterator first, InputIterator last) {
180  return ColumnViewT(BaseColumnView::make(table, first, last));
181  }
182 
183 protected:
184 
185  explicit ColumnViewT(BaseColumnView const & base) : BaseColumnView(base) {}
186 
187 };
188 
189 template <typename InputIterator>
190 BaseColumnView BaseColumnView::make(PTR(BaseTable) const & table, InputIterator first, InputIterator last) {
191  if (first == last) {
192  return BaseColumnView(table, 0, 0, ndarray::Manager::Ptr());
193  }
194  Schema schema = table->getSchema();
195  std::size_t recordSize = schema.getRecordSize();
196  std::size_t recordCount = 1;
197  void * buf = first->_data;
198  ndarray::Manager::Ptr manager = first->_manager;
199  char * expected = reinterpret_cast<char*>(buf) + recordSize;
200  for (++first; first != last; ++first, ++recordCount, expected += recordSize) {
201  if (first->_data != expected || first->_manager != manager) {
202  throw LSST_EXCEPT(
203  lsst::pex::exceptions::RuntimeError,
204  "Record data is not contiguous in memory."
205  );
206  }
207  }
208  return BaseColumnView(table, recordCount, buf, manager);
209 }
210 
211 template <typename InputIterator>
213  PTR(BaseTable) const & table, InputIterator first, InputIterator last
214 ) {
215  if (first == last) return true;
216  Schema schema = table->getSchema();
217  std::size_t recordSize = schema.getRecordSize();
218  std::size_t recordCount = 1;
219  void * buf = first->_data;
220  ndarray::Manager::Ptr manager = first->_manager;
221  char * expected = reinterpret_cast<char*>(buf) + recordSize;
222  for (++first; first != last; ++first, ++recordCount, expected += recordSize) {
223  if (first->_data != expected || first->_manager != manager) {
224  return false;
225  }
226  }
227  return true;
228 }
229 
230 }}} // namespace lsst::afw::table
231 
232 #endif // !AFW_TABLE_BaseColumnView_h_INCLUDED
A proxy class for Array with deep assignment operators.
Definition: ArrayRef.h:46
Defines the fields and offsets for a table.
Definition: Schema.h:46
Column-wise view into a sequence of records that have been allocated contiguously.
A packed representation of a collection of Flag field columns.
#define PTR(...)
Definition: base.h:41
static BaseColumnView make(boost::shared_ptr< BaseTable > const &table, InputIterator first, InputIterator last)
Construct a BaseColumnView from an iterator range.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
BitsColumn getBits(std::vector< Key< Flag > > const &keys) const
Return an integer array with the given Flag fields repacked into individual bits. ...
ndarray::ArrayRef< T, 1 > const operator[](Key< T > const &key) const
Return a 1-d array corresponding to a scalar field (or subfield).
BitsColumn getAllBits() const
Return an integer array with all Flag fields repacked into individual bits.
Field< Flag >::Element argument_type
IntT getBit(Key< Flag > const &key) const
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 ...
boost::shared_ptr< Impl > _impl
Functor to compute a flag bit, used to create an ndarray expression template for flag columns...
boost::intrusive_ptr< Manager > Ptr
Definition: Manager.h:42
ndarray::Array< IntT, 1, 1 > _array
ColumnViewT(BaseColumnView const &base)
A description of a field in a table.
Definition: Field.h:22
Tag types used to declare specialized field types.
Definition: misc.h:35
tbl::Schema schema
std::vector< SchemaItem< Flag > > _items
ndarray::Array< IntT, 1, 1 > getArray() const
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
IntT getMask(Key< Flag > const &key) const
Schema getSchema() const
Return the table&#39;s schema.
Definition: BaseTable.h:126
A class used as a handle to a particular field in a table.
Definition: fwd.h:44
result_type operator()(argument_type element) const
Key specialization for Flag.
Definition: Flag.h:82
int getRecordSize() const
Return the raw size of a record in bytes.
Definition: Schema.h:132
static ColumnViewT make(boost::shared_ptr< Table > const &table, InputIterator first, InputIterator last)
Construct a BaseColumnView from an iterator range.
FlagExtractor(Key< Flag > const &key)
boost::shared_ptr< BaseTable > getTable() const
Return the table that owns the records.
Schema getSchema() const
Return the schema that defines the fields.
BaseColumnView(boost::shared_ptr< BaseTable > const &table, int recordCount, void *buf, ndarray::Manager::Ptr const &manager)
std::vector< SchemaItem< Flag > > const & getSchemaItems() const
boost::shared_ptr< Table > getTable() const
Return the table that owns the records.
IntT getMask(std::string const &name) const
Base class for all tables.
Definition: BaseTable.h:44