Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:15

0001 #ifndef FWCore_SOA_TableItr_h
0002 #define FWCore_SOA_TableItr_h
0003 // -*- C++ -*-
0004 //
0005 // Package:     FWCore/SOA
0006 // Class  :     TableItr
0007 //
0008 /**\class TableItr TableItr.h "TableItr.h"
0009 
0010  Description: An iterator for edm::soa::Table like classes
0011 
0012  Usage:
0013     Can be used like any standard library iterator.
0014 
0015 */
0016 //
0017 // Original Author:  Chris Jones
0018 //         Created:  Thu, 24 Aug 2017 19:49:27 GMT
0019 //
0020 
0021 // system include files
0022 #include <tuple>
0023 #include <array>
0024 #include <type_traits>
0025 
0026 // user include files
0027 #include "RowView.h"
0028 
0029 // forward declarations
0030 
0031 namespace edm {
0032   namespace soa {
0033     namespace impl {
0034 
0035       template <int I, typename... Args>
0036       struct TableItrAdvance {
0037         using Layout = std::tuple<Args...>;
0038         static void advance(std::array<void*, sizeof...(Args)>& iArray, long iStep) {
0039           using Type = typename std::tuple_element<I, Layout>::type::type;
0040           TableItrAdvance<I - 1, Args...>::advance(iArray, iStep);
0041           auto p = static_cast<Type*>(iArray[I]);
0042           iArray[I] = p + iStep;
0043         }
0044       };
0045 
0046       template <typename... Args>
0047       struct TableItrAdvance<0, Args...> {
0048         using Layout = std::tuple<Args...>;
0049         static void advance(std::array<void*, sizeof...(Args)>& iArray, long iStep) {
0050           using Type = typename std::tuple_element<0, Layout>::type::type;
0051           auto p = static_cast<Type*>(iArray[0]);
0052           iArray[0] = p + iStep;
0053         }
0054       };
0055 
0056       template <int I, typename... Args>
0057       struct ConstTableItrAdvance {
0058         using Layout = std::tuple<Args...>;
0059         static void advance(std::array<void const*, sizeof...(Args)>& iArray, long iStep) {
0060           using Type = typename std::tuple_element<I, Layout>::type::type;
0061           ConstTableItrAdvance<I - 1, Args...>::advance(iArray, iStep);
0062           auto p = static_cast<Type const*>(iArray[I]);
0063           iArray[I] = p + iStep;
0064         }
0065       };
0066 
0067       template <typename... Args>
0068       struct ConstTableItrAdvance<0, Args...> {
0069         using Layout = std::tuple<Args...>;
0070         static void advance(std::array<void const*, sizeof...(Args)>& iArray, long iStep) {
0071           using Type = typename std::tuple_element<0, Layout>::type::type;
0072           auto p = static_cast<Type const*>(iArray[0]);
0073           iArray[0] = p + iStep;
0074         }
0075       };
0076     }  // namespace impl
0077 
0078     template <typename... Args>
0079     class TableItr {
0080       using Layout = std::tuple<Args...>;
0081       std::array<void*, sizeof...(Args)> m_values;
0082 
0083     public:
0084       using value_type = MutableRowView<Args...>;
0085 
0086       explicit TableItr(std::array<void*, sizeof...(Args)> const& iValues) : m_values{iValues} {}
0087 
0088       explicit TableItr(std::array<void*, sizeof...(Args)> const& iValues, long iOffset) : m_values{iValues} {
0089         impl::TableItrAdvance<sizeof...(Args) - 1, Args...>::advance(m_values, iOffset);
0090       }
0091 
0092       explicit TableItr(std::array<void*, sizeof...(Args)> const& iValues, unsigned int iOffset) : m_values{iValues} {
0093         impl::TableItrAdvance<sizeof...(Args) - 1, Args...>::advance(m_values, static_cast<long>(iOffset));
0094       }
0095 
0096       value_type operator*() { return value_type{m_values}; }
0097 
0098       TableItr& operator++() {
0099         impl::TableItrAdvance<sizeof...(Args) - 1, Args...>::advance(m_values, 1);
0100         return *this;
0101       }
0102 
0103       bool operator==(const TableItr<Args...>& iOther) { return m_values[0] == iOther.m_values[0]; }
0104 
0105       bool operator!=(const TableItr<Args...>& iOther) { return m_values[0] != iOther.m_values[0]; }
0106 
0107       TableItr<Args...> makeOffset(long iDiff) const { return TableItr<Args...>{m_values, iDiff}; }
0108     };
0109 
0110     template <typename... Args>
0111     TableItr<Args...> operator+(TableItr<Args...> const& iLHS, long iDiff) {
0112       return iLHS.makeOffset(iDiff);
0113     }
0114 
0115     template <typename... Args>
0116     class ConstTableItr {
0117       using Layout = std::tuple<Args...>;
0118       std::array<void const*, sizeof...(Args)> m_values;
0119 
0120     public:
0121       using value_type = RowView<Args...>;
0122 
0123       explicit ConstTableItr(std::array<void const*, sizeof...(Args)> const& iValues) : m_values{iValues} {}
0124 
0125       ConstTableItr(std::array<void const*, sizeof...(Args)> const& iValues, long iOffset) : m_values{iValues} {
0126         impl::ConstTableItrAdvance<sizeof...(Args) - 1, Args...>::advance(m_values, iOffset);
0127       }
0128 
0129       ConstTableItr(std::array<void const*, sizeof...(Args)> const& iValues, unsigned int iOffset) : m_values{iValues} {
0130         impl::ConstTableItrAdvance<sizeof...(Args) - 1, Args...>::advance(m_values, static_cast<long>(iOffset));
0131       }
0132 
0133       value_type operator*() const { return value_type{m_values}; }
0134 
0135       ConstTableItr& operator++() {
0136         impl::ConstTableItrAdvance<sizeof...(Args) - 1, Args...>::advance(m_values, 1);
0137         return *this;
0138       }
0139 
0140       bool operator==(const ConstTableItr<Args...>& iOther) { return m_values[0] == iOther.m_values[0]; }
0141 
0142       bool operator!=(const ConstTableItr<Args...>& iOther) { return m_values[0] != iOther.m_values[0]; }
0143 
0144       TableItr<Args...> makeOffset(long iDiff) const { return TableItr<Args...>{m_values, iDiff}; }
0145     };
0146 
0147     template <typename... Args>
0148     ConstTableItr<Args...> operator+(ConstTableItr<Args...> const& iLHS, long iDiff) {
0149       return iLHS.makeOffset(iDiff);
0150     }
0151 
0152   }  // namespace soa
0153 }  // namespace edm
0154 
0155 #endif