Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:09

0001 #ifndef FWCore_SOA_TableView_h
0002 #define FWCore_SOA_TableView_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/SOA
0006 // Class  :     TableView
0007 //
0008 /**\class TableView TableView.h "TableView.h"
0009 
0010  Description: A view of certain columns of a edm::soa::Table
0011 
0012  Usage:
0013     A TableView<> type can be constructed from any edm::soa::Table<>
0014  that shares the same template arguments as the TableView. E.g.
0015  \code
0016  Table<Eta,Phi> epTable;
0017  TableView<Eta,Phi> epView{epTable}; //compiles
0018  TableView<Phi,Eta> peView{epTable}; //compiles and works properly
0019  TableView<Eta> eView{epTable}; //compiles
0020  
0021  //TableVew<Delta> dView{epTable}; //does not compile
0022  \endcode
0023 
0024  TableViews are particularly useful when defining functions intended to
0025  operate on Tables.
0026  \code
0027  edm::soa::Table<Eta,Phi> sphericalAngles(edm::soa::TableView<X,Y,Z>);
0028  \endcode
0029 
0030 */
0031 //
0032 // Original Author:  Chris Jones
0033 //         Created:  Fri, 25 Aug 2017 19:31:50 GMT
0034 //
0035 
0036 // system include files
0037 #include <tuple>
0038 #include <array>
0039 
0040 // user include files
0041 #include "FWCore/SOA/interface/Table.h"
0042 #include "FWCore/SOA/interface/ColumnValues.h"
0043 
0044 // forward declarations
0045 
0046 namespace edm {
0047   namespace soa {
0048     template <typename... Args>
0049     class TableView {
0050     public:
0051       using Layout = std::tuple<Args...>;
0052       static constexpr const size_t kNColumns = sizeof...(Args);
0053       using const_iterator = ConstTableItr<Args...>;
0054 
0055       template <typename... OArgs>
0056       TableView(Table<OArgs...> const& iTable) : m_size(iTable.size()) {
0057         fillArray(iTable, std::make_index_sequence<sizeof...(Args)>{});
0058       }
0059       TableView(unsigned int iSize, std::array<void*, sizeof...(Args)>& iArray) : m_size(iSize), m_values(iArray) {}
0060 
0061       TableView(unsigned int iSize, std::array<void const*, sizeof...(Args)>& iArray)
0062           : m_size(iSize), m_values(iArray) {}
0063 
0064       unsigned int size() const { return m_size; }
0065 
0066       template <typename U>
0067       typename U::type const& get(size_t iRow) const {
0068         return *(static_cast<typename U::type const*>(columnAddress<U>()) + iRow);
0069       }
0070 
0071       template <typename U>
0072       ColumnValues<typename U::type> column() const {
0073         return ColumnValues<typename U::type>{static_cast<typename U::type const*>(columnAddress<U>()), m_size};
0074       }
0075 
0076       const_iterator begin() const { return const_iterator{m_values}; }
0077       const_iterator end() const { return const_iterator{m_values, size()}; }
0078 
0079     private:
0080       std::array<void const*, sizeof...(Args)> m_values;
0081       unsigned int m_size;
0082 
0083       template <typename U>
0084       void const* columnAddress() const {
0085         return m_values[impl::GetIndex<0, U, Layout>::index];
0086       }
0087 
0088       template <typename T, size_t... I>
0089       void fillArray(T const& iTable, std::index_sequence<I...>) {
0090         ((m_values[I] = iTable.columnAddressWorkaround(
0091               static_cast<typename std::tuple_element<I, Layout>::type const*>(nullptr))),
0092          ...);
0093       }
0094     };
0095 
0096     template <typename T>
0097     struct ViewFromTable;
0098 
0099     template <typename... T>
0100     struct ViewFromTable<Table<T...>> {
0101       using type = TableView<T...>;
0102     };
0103     template <typename T>
0104     using ViewFromTable_t = typename ViewFromTable<T>::type;
0105 
0106   }  // namespace soa
0107 }  // namespace edm
0108 
0109 #endif