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
static size_t fill(std::array< void *, sizeof...(Args)> &oValues, T const &iContainer, U... iArgs)
Definition: Table.h:268
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
iterator begin()
Definition: Table.h:230
unsigned int size() const
Definition: Table.h:180
Table(Table< Args... > const &iOther)
Definition: Table.h:159
static void resetStartingAt(size_t iStartIndex, size_t iEndIndex, std::array< void *, sizeof...(Args)> &ioArray)
Definition: Table.h:395
typename RemoveColumn< TABLE, E >::type RemoveColumn_t
Definition: Table.h:441
Table(T const &iContainer, CArgs... iArgs)
Definition: Table.h:145
static void ctrFiller(std::array< void *, sizeof...(Args)> &oValues, size_t iSize, T const &iContainer, U... iU)
Definition: Table.h:276
static void fillElement(E const &iItem, size_t iIndex, std::array< void *, sizeof...(Args)> &oValues)
Definition: Table.h:330
typename AddColumns< T1, T2 >::type AddColumns_t
Definition: Table.h:416
assert(be >=bs)
void * columnAddress()
Definition: Table.h:251
typename impl::RemoveColumnCheck< Table<>, E, typename TABLE::Layout >::type type
Definition: Table.h:437
Table< Args... > & operator=(Table< Args... > &&iOther)
Definition: Table.h:172
MutableColumnValues< typename U::type > column()
Definition: Table.h:208
Table(T const &iContainer, ColumnFillers< CArgs... > iFiller)
Definition: Table.h:154
static void copyFromToWithResize(size_t iNElements, std::array< void *, sizeof...(Args)> const &iFrom, std::array< void *, sizeof...(Args)> &oTo)
Definition: Table.h:366
void resize(unsigned int iNewSize)
Definition: Table.h:182
ColumnValues< typename U::type > column() const
Definition: Table.h:204
ZVertexSoA * soa
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::array< void *, sizeof...(Args)> m_values
Definition: Table.h:243
const_iterator begin() const
Definition: Table.h:215
void const * columnAddressByIndex(unsigned int iIndex) const
Definition: Table.h:238
const std::complex< double > I
Definition: I.h:8
iterator end()
Definition: Table.h:231
void const * columnAddressWorkaround(U const *) const
Definition: Table.h:234
const_iterator end() const
Definition: Table.h:222
static size_t fill(std::array< void *, sizeof...(Args)> &oValues, T const &iContainer)
Definition: Table.h:297
unsigned int m_size
Definition: Table.h:242
RowView< Args... > row(size_t iRow) const
Definition: Table.h:212
static void dtr(std::array< void *, sizeof...(Args)> &iArray)
Definition: Table.h:257
Table< Args... > & operator=(Table< Args... > const &iOther)
Definition: Table.h:178
static void presize(std::array< void *, sizeof...(Args)> &oValues, size_t iSize)
Definition: Table.h:320
static void resizeFromTo(size_t iOldSize, size_t iNewSize, std::array< void *, sizeof...(Args)> &ioArray)
Definition: Table.h:379
Table(Table< Args... > &&iOther)
Definition: Table.h:163
static void ctrFiller(std::array< void *, sizeof...(Args)> &, size_t)
Definition: Table.h:291
void const * columnAddress() const
keep ROOT from trying to store this
Definition: Table.h:246
static size_t fillUsingFiller(F &iFiller, std::array< void *, sizeof...(Args)> &oValues, T const &iContainer)
Definition: Table.h:308
MutableRowView< Args... > row(size_t iRow)
Definition: Table.h:213
HLT enums.
static std::unique_ptr< TableExaminerBase > make(const Table< Args... > *iTable)
Definition: Table.h:449
col::Eta ::type value_for_column(Object const &x, col::Eta *)
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
long double T
static constexpr const unsigned int kNColumns
Definition: Table.h:139
static void fillElementUsingFiller(F &iFiller, E const &iItem, size_t iIndex, std::array< void *, sizeof...(Args)> &oValues)
Definition: Table.h:342
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
def move(src, dest)
Definition: eostools.py:511
def cp(fromDir, toDir, listOfFiles, overwrite=False, smallList=False)