CMS 3D CMS Logo

Table.h
Go to the documentation of this file.
1 #ifndef FWCore_SOA_Table_h
2 #define FWCore_SOA_Table_h
3 // -*- C++ -*-
4 //
5 // Package: FWCore/SOA
6 // Class : Table
7 //
111 //
112 // Original Author: Chris Jones
113 // Created: Thu, 24 Aug 2017 16:18:05 GMT
114 //
115 
116 // system include files
117 #include <memory>
118 #include <tuple>
119 #include <array>
120 
121 // user include files
127 
128 //The following is needed for edm::Wrapper
130 
131 // forward declarations
132 
133 namespace edm {
134  namespace soa {
135 
136  template <typename... Args>
137  class Table {
138  public:
139  static constexpr const unsigned int kNColumns = sizeof...(Args);
140  using Layout = std::tuple<Args...>;
141  using const_iterator = ConstTableItr<Args...>;
142  using iterator = TableItr<Args...>;
143 
144  template <typename T, typename... CArgs>
145  Table(T const& iContainer, CArgs... iArgs) : m_size(iContainer.size()) {
146  if constexpr (sizeof...(CArgs) == 0) {
147  CtrFillerFromAOS::fill(m_values, iContainer);
148  } else {
149  CtrFillerFromContainers::fill(m_values, iContainer, std::forward<CArgs>(iArgs)...);
150  }
151  }
152 
153  template <typename T, typename... CArgs>
154  Table(T const& iContainer, ColumnFillers<CArgs...> iFiller) {
155  m_size = iContainer.size();
156  CtrFillerFromAOS::fillUsingFiller(iFiller, m_values, iContainer);
157  }
158 
159  Table(Table<Args...> const& iOther) : m_size(iOther.m_size), m_values{{nullptr}} {
160  copyFromToWithResizeAll(m_size, iOther.m_values, m_values, std::make_index_sequence<sizeof...(Args)>{});
161  }
162 
163  Table(Table<Args...>&& iOther) : m_size(0), m_values{{nullptr}} {
164  std::swap(m_size, iOther.m_size);
165  std::swap(m_values, iOther.m_values);
166  }
167 
168  Table() : m_size(0) {}
169 
170  ~Table() { dtr<0>(m_values); }
171 
172  Table<Args...>& operator=(Table<Args...>&& iOther) {
173  Table<Args...> cp(std::move(iOther));
174  std::swap(m_size, cp.m_size);
175  std::swap(m_values, cp.m_values);
176  return *this;
177  }
178  Table<Args...>& operator=(Table<Args...> const& iOther) { return operator=(Table<Args...>(iOther)); }
179 
180  unsigned int size() const { return m_size; }
181 
182  void resize(unsigned int iNewSize) {
183  if (m_size == iNewSize) {
184  return;
185  }
186  resizeFromTo<0>(m_size, iNewSize, m_values);
187  if (m_size < iNewSize) {
188  //initialize the extra values
189  resetStartingAt<0>(m_size, iNewSize, m_values);
190  }
191  m_size = iNewSize;
192  }
193 
194  template <typename U>
195  typename U::type const& get(size_t iRow) const {
196  return *(static_cast<typename U::type const*>(columnAddress<U>()) + iRow);
197  }
198  template <typename U>
199  typename U::type& get(size_t iRow) {
200  return *(static_cast<typename U::type*>(columnAddress<U>()) + iRow);
201  }
202 
203  template <typename U>
205  return ColumnValues<typename U::type>{static_cast<typename U::type*>(columnAddress<U>()), m_size};
206  }
207  template <typename U>
209  return MutableColumnValues<typename U::type>{static_cast<typename U::type*>(columnAddress<U>()), m_size};
210  }
211 
212  RowView<Args...> row(size_t iRow) const { return *(begin() + iRow); }
213  MutableRowView<Args...> row(size_t iRow) { return *(begin() + iRow); }
214 
216  std::array<void const*, sizeof...(Args)> t;
217  for (size_t i = 0; i < t.size(); ++i) {
218  t[i] = m_values[i];
219  }
220  return const_iterator{t};
221  }
222  const_iterator end() const {
223  std::array<void const*, sizeof...(Args)> t;
224  for (size_t i = 0; i < t.size(); ++i) {
225  t[i] = m_values[i];
226  }
227  return const_iterator{t, size()};
228  }
229 
231  iterator end() { return iterator{m_values, size()}; }
232 
233  template <typename U>
234  void const* columnAddressWorkaround(U const*) const {
235  return columnAddress<U>();
236  }
237 
238  void const* columnAddressByIndex(unsigned int iIndex) const { return m_values[iIndex]; }
239 
240  private:
241  // Member data
242  unsigned int m_size = 0;
243  std::array<void*, sizeof...(Args)> m_values = {{nullptr}};
244 
245  template <typename U>
246  void const* columnAddress() const {
248  }
249 
250  template <typename U>
251  void* columnAddress() {
253  }
254 
255  //Recursive destructor handling
256  template <int I>
257  static void dtr(std::array<void*, sizeof...(Args)>& iArray) {
258  if constexpr (I < sizeof...(Args)) {
260  delete[] static_cast<Type*>(iArray[I]);
261  dtr<I + 1>(iArray);
262  }
263  }
264 
265  //Construct the Table using a container per column
267  template <typename T, typename... U>
268  static size_t fill(std::array<void*, sizeof...(Args)>& oValues, T const& iContainer, U... iArgs) {
269  static_assert(sizeof...(Args) == sizeof...(U) + 1, "Wrong number of arguments passed to Table constructor");
270  ctrFiller<0>(oValues, iContainer.size(), iContainer, std::forward<U>(iArgs)...);
271  return iContainer.size();
272  }
273 
274  private:
275  template <int I, typename T, typename... U>
276  static void ctrFiller(std::array<void*, sizeof...(Args)>& oValues, size_t iSize, T const& iContainer, U... iU) {
277  assert(iContainer.size() == iSize);
279  Type* temp = new Type[iSize];
280  unsigned int index = 0;
281  for (auto const& v : iContainer) {
282  temp[index] = v;
283  ++index;
284  }
285  oValues[I] = temp;
286 
287  ctrFiller<I + 1>(oValues, iSize, std::forward<U>(iU)...);
288  }
289 
290  template <int I>
291  static void ctrFiller(std::array<void*, sizeof...(Args)>&, size_t) {}
292  };
293 
294  //Construct the Table using one container with each entry representing a row
296  template <typename T>
297  static size_t fill(std::array<void*, sizeof...(Args)>& oValues, T const& iContainer) {
298  presize<0>(oValues, iContainer.size());
299  unsigned index = 0;
300  for (auto&& item : iContainer) {
301  fillElement<0>(item, index, oValues);
302  ++index;
303  }
304  return iContainer.size();
305  }
306 
307  template <typename T, typename F>
308  static size_t fillUsingFiller(F& iFiller, std::array<void*, sizeof...(Args)>& oValues, T const& iContainer) {
309  presize<0>(oValues, iContainer.size());
310  unsigned index = 0;
311  for (auto&& item : iContainer) {
312  fillElementUsingFiller<0>(iFiller, item, index, oValues);
313  ++index;
314  }
315  return iContainer.size();
316  }
317 
318  private:
319  template <int I>
320  static void presize(std::array<void*, sizeof...(Args)>& oValues, size_t iSize) {
321  if constexpr (I < sizeof...(Args)) {
322  using Layout = std::tuple<Args...>;
324  oValues[I] = new Type[iSize];
325  presize<I + 1>(oValues, iSize);
326  }
327  }
328 
329  template <int I, typename E>
330  static void fillElement(E const& iItem, size_t iIndex, std::array<void*, sizeof...(Args)>& oValues) {
331  if constexpr (I < sizeof...(Args)) {
332  using Layout = std::tuple<Args...>;
333  using ColumnType = typename std::tuple_element<I, Layout>::type;
334  using Type = typename ColumnType::type;
335  Type* pElement = static_cast<Type*>(oValues[I]) + iIndex;
336  *pElement = value_for_column(iItem, static_cast<ColumnType*>(nullptr));
337  fillElement<I + 1>(iItem, iIndex, oValues);
338  }
339  }
340 
341  template <int I, typename E, typename F>
342  static void fillElementUsingFiller(F& iFiller,
343  E const& iItem,
344  size_t iIndex,
345  std::array<void*, sizeof...(Args)>& oValues) {
346  if constexpr (I < sizeof...(Args)) {
347  using Layout = std::tuple<Args...>;
348  using ColumnType = typename std::tuple_element<I, Layout>::type;
349  using Type = typename ColumnType::type;
350  Type* pElement = static_cast<Type*>(oValues[I]) + iIndex;
351  *pElement = iFiller.value(iItem, static_cast<ColumnType*>(nullptr));
352  fillElementUsingFiller<I + 1>(iFiller, iItem, iIndex, oValues);
353  }
354  }
355  };
356 
357  template <size_t... I>
358  static void copyFromToWithResizeAll(size_t iNElements,
359  std::array<void*, sizeof...(Args)> const& iFrom,
360  std::array<void*, sizeof...(Args)>& oTo,
361  std::index_sequence<I...>) {
362  (copyFromToWithResize<I>(iNElements, iFrom, oTo), ...);
363  }
364 
365  template <int I>
366  static void copyFromToWithResize(size_t iNElements,
367  std::array<void*, sizeof...(Args)> const& iFrom,
368  std::array<void*, sizeof...(Args)>& oTo) {
369  using Layout = std::tuple<Args...>;
371  Type* oldPtr = static_cast<Type*>(oTo[I]);
372  Type* ptr = new Type[iNElements];
373  oTo[I] = ptr;
374  std::copy(static_cast<Type const*>(iFrom[I]), static_cast<Type const*>(iFrom[I]) + iNElements, ptr);
375  delete[] oldPtr;
376  }
377 
378  template <int I>
379  static void resizeFromTo(size_t iOldSize, size_t iNewSize, std::array<void*, sizeof...(Args)>& ioArray) {
380  if constexpr (I < sizeof...(Args)) {
381  using Layout = std::tuple<Args...>;
383  Type* oldPtr = static_cast<Type*>(ioArray[I]);
384  auto ptr = new Type[iNewSize];
385  auto nToCopy = std::min(iOldSize, iNewSize);
386  std::copy(static_cast<Type const*>(ioArray[I]), static_cast<Type const*>(ioArray[I]) + nToCopy, ptr);
387  resizeFromTo<I + 1>(iOldSize, iNewSize, ioArray);
388 
389  delete[] oldPtr;
390  ioArray[I] = ptr;
391  }
392  }
393 
394  template <int I>
395  static void resetStartingAt(size_t iStartIndex, size_t iEndIndex, std::array<void*, sizeof...(Args)>& ioArray) {
396  if constexpr (I < sizeof...(Args)) {
397  using Layout = std::tuple<Args...>;
399  auto ptr = static_cast<Type*>(ioArray[I]);
400  auto temp = Type{};
401  std::fill(ptr + iStartIndex, ptr + iEndIndex, temp);
402  resetStartingAt<I + 1>(iStartIndex, iEndIndex, ioArray);
403  }
404  }
405  };
406 
407  /* Table Type Manipulation */
408  template <typename T1, typename T2>
409  struct AddColumns;
410  template <typename... T1, typename... T2>
411  struct AddColumns<Table<T1...>, std::tuple<T2...>> {
412  using type = Table<T1..., T2...>;
413  };
414 
415  template <typename T1, typename T2>
417 
418  namespace impl {
419  template <typename LHS, typename E, typename RHS>
421  template <typename LHS, typename E, typename T, typename... U>
422  struct RemoveColumnCheck<LHS, E, std::tuple<T, U...>> {
423  using type = typename std::conditional<
425  typename AddColumns<LHS, std::tuple<U...>>::type,
427  };
428 
429  template <typename LHS, typename E>
430  struct RemoveColumnCheck<LHS, E, std::tuple<>> {
431  using type = LHS;
432  };
433  } // namespace impl
434 
435  template <typename TABLE, typename E>
436  struct RemoveColumn {
437  using type = typename impl::RemoveColumnCheck<Table<>, E, typename TABLE::Layout>::type;
438  };
439 
440  template <typename TABLE, typename E>
442 
443  //This is used by edm::Wrapper
444  template <typename T>
445  struct MakeTableExaminer;
446 
447  template <typename... Args>
448  struct MakeTableExaminer<Table<Args...>> {
449  static std::unique_ptr<TableExaminerBase> make(const Table<Args...>* iTable) {
450  return std::make_unique<TableExaminer<Table<Args...>>>(iTable);
451  }
452  };
453  } // namespace soa
454 } // namespace edm
455 #endif
edm::soa::Table::operator=
Table< Args... > & operator=(Table< Args... > const &iOther)
Definition: Table.h:178
edm::soa::impl::RemoveColumnCheck
Definition: Table.h:420
edm::soa::Table::row
MutableRowView< Args... > row(size_t iRow)
Definition: Table.h:213
mps_fire.i
i
Definition: mps_fire.py:428
edm::soa::Table::columnAddressByIndex
void const * columnAddressByIndex(unsigned int iIndex) const
Definition: Table.h:238
edm::soa::Table::get
U::type const & get(size_t iRow) const
Definition: Table.h:195
edm::soa::Table::CtrFillerFromContainers::ctrFiller
static void ctrFiller(std::array< void *, sizeof...(Args)> &oValues, size_t iSize, T const &iContainer, U... iU)
Definition: Table.h:276
TableExaminer.h
filterCSVwithJSON.copy
copy
Definition: filterCSVwithJSON.py:36
edm::soa::Table::column
ColumnValues< typename U::type > column() const
Definition: Table.h:204
edm::soa::RowView
Definition: RowView.h:57
min
T min(T a, T b)
Definition: MathUtil.h:58
edm::soa::Table::row
RowView< Args... > row(size_t iRow) const
Definition: Table.h:212
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::soa::Table::resize
void resize(unsigned int iNewSize)
Definition: Table.h:182
edm::soa::TableItr
Definition: TableItr.h:79
edm::soa::Table::resizeFromTo
static void resizeFromTo(size_t iOldSize, size_t iNewSize, std::array< void *, sizeof...(Args)> &ioArray)
Definition: Table.h:379
edm::soa::Table::columnAddress
void const * columnAddress() const
keep ROOT from trying to store this
Definition: Table.h:246
cms::cuda::assert
assert(be >=bs)
gpuVertexFinder::soa
ZVertexSoA * soa
Definition: gpuVertexFinder.cc:21
mps_check.array
array
Definition: mps_check.py:216
hgcal_conditions::parameters
Definition: HGCConditions.h:86
edm::soa::MutableColumnValues
Definition: ColumnValues.h:43
findQualityFiles.v
v
Definition: findQualityFiles.py:179
edm::soa::Table::Table
Table(Table< Args... > const &iOther)
Definition: Table.h:159
groupFilesInBlocks.temp
list temp
Definition: groupFilesInBlocks.py:142
edm::soa::Table::Table
Table(T const &iContainer, CArgs... iArgs)
Definition: Table.h:145
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
edm::soa::Table::copyFromToWithResize
static void copyFromToWithResize(size_t iNElements, std::array< void *, sizeof...(Args)> const &iFrom, std::array< void *, sizeof...(Args)> &oTo)
Definition: Table.h:366
edm::soa::Table::CtrFillerFromAOS::fillElement
static void fillElement(E const &iItem, size_t iIndex, std::array< void *, sizeof...(Args)> &oValues)
Definition: Table.h:330
Exhume::I
const std::complex< double > I
Definition: I.h:8
susybsm::HSCParticleType::Type
Type
Definition: HSCParticle.h:20
edm::soa::AddColumns
Definition: Table.h:409
edm::soa::Table::Table
Table()
Definition: Table.h:168
edm::soa::Table::CtrFillerFromContainers::ctrFiller
static void ctrFiller(std::array< void *, sizeof...(Args)> &, size_t)
Definition: Table.h:291
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
edm::soa::Table::columnAddress
void * columnAddress()
Definition: Table.h:251
edm::soa::TableExaminer
Definition: TableExaminer.h:31
edm::soa::Table::Table
Table(Table< Args... > &&iOther)
Definition: Table.h:163
edm::soa::Table::size
unsigned int size() const
Definition: Table.h:180
edm::soa::Table::operator=
Table< Args... > & operator=(Table< Args... > &&iOther)
Definition: Table.h:172
edm::soa::ConstTableItr
Definition: TableItr.h:116
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
ntuplemaker.fill
fill
Definition: ntuplemaker.py:304
ColumnFillers.h
tablehelpers.h
edm::soa::value_for_column
col::Eta ::type value_for_column(Object const &x, col::Eta *)
Definition: KinematicColumns.h:24
edm::soa::Table::~Table
~Table()
Definition: Table.h:170
edm::soa::Table::CtrFillerFromAOS
Definition: Table.h:295
edm::soa::impl::RemoveColumnCheck< LHS, E, std::tuple<> >::type
LHS type
Definition: Table.h:431
gainCalibHelper::gainCalibPI::type
type
Definition: SiPixelGainCalibHelper.h:40
edm::soa::Table::CtrFillerFromAOS::fillUsingFiller
static size_t fillUsingFiller(F &iFiller, std::array< void *, sizeof...(Args)> &oValues, T const &iContainer)
Definition: Table.h:308
edm::soa::Table::m_values
std::array< void *, sizeof...(Args)> m_values
Definition: Table.h:243
edm::soa::ColumnFillers
Definition: ColumnFillers.h:45
edm::soa::RemoveColumn
Definition: Table.h:436
edm::soa::Table::copyFromToWithResizeAll
static void copyFromToWithResizeAll(size_t iNElements, std::array< void *, sizeof...(Args)> const &iFrom, std::array< void *, sizeof...(Args)> &oTo, std::index_sequence< I... >)
Definition: Table.h:358
edm::soa::MutableRowView
Definition: RowView.h:76
edm::soa::Table::kNColumns
static constexpr const unsigned int kNColumns
Definition: Table.h:139
RowView.h
edm::soa::Table::end
const_iterator end() const
Definition: Table.h:222
edm::soa::Table::CtrFillerFromAOS::fill
static size_t fill(std::array< void *, sizeof...(Args)> &oValues, T const &iContainer)
Definition: Table.h:297
B2GTnPMonitor_cfi.item
item
Definition: B2GTnPMonitor_cfi.py:147
edm::soa::Table::end
iterator end()
Definition: Table.h:231
edm::soa::Table::Table
Table(T const &iContainer, ColumnFillers< CArgs... > iFiller)
Definition: Table.h:154
edm::soa::Table::CtrFillerFromAOS::presize
static void presize(std::array< void *, sizeof...(Args)> &oValues, size_t iSize)
Definition: Table.h:320
edm::soa::Table::m_size
unsigned int m_size
Definition: Table.h:242
edm::soa::Table::get
U::type & get(size_t iRow)
Definition: Table.h:199
edm::soa::Table< edm::soa::col::Pt, edm::soa::col::Eta, edm::soa::col::Phi, edm::soa::col::Vz >::Layout
std::tuple< Args... > Layout
Definition: Table.h:140
impl
Definition: trackAlgoPriorityOrder.h:18
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
edm::soa::ColumnValues
Definition: ColumnValues.h:30
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
TableItr.h
edm::soa::MakeTableExaminer< Table< Args... > >::make
static std::unique_ptr< TableExaminerBase > make(const Table< Args... > *iTable)
Definition: Table.h:449
edm::soa::Table
Definition: Table.h:137
edm::soa::Table::begin
iterator begin()
Definition: Table.h:230
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
edm::soa::impl::GetIndex
Definition: tablehelpers.h:38
edm::soa::Table::column
MutableColumnValues< typename U::type > column()
Definition: Table.h:208
ColumnValues.h
edm::soa::Table::CtrFillerFromContainers
Definition: Table.h:266
edm::soa::RemoveColumn_t
typename RemoveColumn< TABLE, E >::type RemoveColumn_t
Definition: Table.h:441
edm::soa::Table::columnAddressWorkaround
void const * columnAddressWorkaround(U const *) const
Definition: Table.h:234
edm::soa::Table::resetStartingAt
static void resetStartingAt(size_t iStartIndex, size_t iEndIndex, std::array< void *, sizeof...(Args)> &ioArray)
Definition: Table.h:395
edm::soa::Table::CtrFillerFromContainers::fill
static size_t fill(std::array< void *, sizeof...(Args)> &oValues, T const &iContainer, U... iArgs)
Definition: Table.h:268
edm::soa::AddColumns_t
typename AddColumns< T1, T2 >::type AddColumns_t
Definition: Table.h:416
CommonMethods.cp
def cp(fromDir, toDir, listOfFiles, overwrite=False, smallList=False)
Definition: CommonMethods.py:191
edm::soa::Table::dtr
static void dtr(std::array< void *, sizeof...(Args)> &iArray)
Definition: Table.h:257
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
edm::soa::Table::CtrFillerFromAOS::fillElementUsingFiller
static void fillElementUsingFiller(F &iFiller, E const &iItem, size_t iIndex, std::array< void *, sizeof...(Args)> &oValues)
Definition: Table.h:342
edm::soa::Table::begin
const_iterator begin() const
Definition: Table.h:215
edm::soa::MakeTableExaminer
Definition: Wrapper.h:147
edm::soa::RemoveColumn::type
typename impl::RemoveColumnCheck< Table<>, E, typename TABLE::Layout >::type type
Definition: Table.h:437
edm::soa::impl::RemoveColumnCheck< LHS, E, std::tuple< T, U... > >::type
typename std::conditional< std::is_same< E, T >::value, typename AddColumns< LHS, std::tuple< U... > >::type, typename RemoveColumnCheck< typename AddColumns< LHS, std::tuple< T > >::type, E, std::tuple< U... > >::type >::type type
Definition: Table.h:426