Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Acts/EventData/detail/DynamicColumn.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2022 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <any>
0012 #include <cassert>
0013 #include <memory>
0014 #include <vector>
0015 
0016 namespace Acts::detail {
0017 
0018 struct DynamicColumnBase {
0019   virtual ~DynamicColumnBase() = default;
0020 
0021   virtual std::any get(std::size_t i) = 0;
0022   virtual std::any get(std::size_t i) const = 0;
0023 
0024   virtual void add() = 0;
0025   virtual void clear() = 0;
0026   virtual void reserve(std::size_t size) = 0;
0027   virtual void erase(std::size_t i) = 0;
0028   virtual std::size_t size() const = 0;
0029   virtual void copyFrom(std::size_t dstIdx, const DynamicColumnBase& src,
0030                         std::size_t srcIdx) = 0;
0031   virtual void copyFrom(std::size_t dstIdx, const std::any& srcPtr) = 0;
0032 
0033   virtual std::unique_ptr<DynamicColumnBase> clone(
0034       bool empty = false) const = 0;
0035 };
0036 
0037 template <typename T>
0038 struct DynamicColumn : public DynamicColumnBase {
0039   std::any get(std::size_t i) override {
0040     assert(i < m_vector.size() && "DynamicColumn out of bounds");
0041     return &m_vector[i];
0042   }
0043 
0044   std::any get(std::size_t i) const override {
0045     assert(i < m_vector.size() && "DynamicColumn out of bounds");
0046     return &m_vector[i];
0047   }
0048 
0049   void add() override { m_vector.emplace_back(); }
0050   void clear() override { m_vector.clear(); }
0051   void reserve(std::size_t size) override { m_vector.reserve(size); }
0052   void erase(std::size_t i) override { m_vector.erase(m_vector.begin() + i); }
0053   std::size_t size() const override { return m_vector.size(); }
0054 
0055   std::unique_ptr<DynamicColumnBase> clone(bool empty) const override {
0056     if (empty) {
0057       return std::make_unique<DynamicColumn<T>>();
0058     }
0059     return std::make_unique<DynamicColumn<T>>(*this);
0060   }
0061 
0062   void copyFrom(std::size_t dstIdx, const DynamicColumnBase& src,
0063                 std::size_t srcIdx) override {
0064     const auto* other = dynamic_cast<const DynamicColumn<T>*>(&src);
0065     assert(other != nullptr &&
0066            "Source column is not of same type as destination");
0067     m_vector.at(dstIdx) = other->m_vector.at(srcIdx);
0068   }
0069 
0070   void copyFrom(std::size_t dstIdx, const std::any& srcPtr) override {
0071     const auto* other = std::any_cast<const T*>(srcPtr);
0072     assert(other != nullptr &&
0073            "Source column is not of same type as destination");
0074     m_vector.at(dstIdx) = *other;
0075   }
0076 
0077   std::vector<T> m_vector;
0078 };
0079 
0080 template <>
0081 struct DynamicColumn<bool> : public DynamicColumnBase {
0082   struct Wrapper {
0083     bool value;
0084   };
0085 
0086   std::any get(std::size_t i) override {
0087     assert(i < m_vector.size() && "DynamicColumn out of bounds");
0088     return &m_vector[i].value;
0089   }
0090 
0091   std::any get(std::size_t i) const override {
0092     assert(i < m_vector.size() && "DynamicColumn out of bounds");
0093     return &m_vector[i].value;
0094   }
0095 
0096   void add() override { m_vector.emplace_back(); }
0097   void reserve(std::size_t size) override { m_vector.reserve(size); }
0098   void clear() override { m_vector.clear(); }
0099   void erase(std::size_t i) override { m_vector.erase(m_vector.begin() + i); }
0100   std::size_t size() const override { return m_vector.size(); }
0101 
0102   std::unique_ptr<DynamicColumnBase> clone(bool empty) const override {
0103     if (empty) {
0104       return std::make_unique<DynamicColumn<bool>>();
0105     }
0106     return std::make_unique<DynamicColumn<bool>>(*this);
0107   }
0108 
0109   void copyFrom(std::size_t dstIdx, const DynamicColumnBase& src,
0110                 std::size_t srcIdx) override {
0111     const auto* other = dynamic_cast<const DynamicColumn<bool>*>(&src);
0112     assert(other != nullptr &&
0113            "Source column is not of same type as destination");
0114     m_vector.at(dstIdx) = other->m_vector.at(srcIdx);
0115   }
0116 
0117   void copyFrom(std::size_t dstIdx, const std::any& srcPtr) override {
0118     const auto* other = std::any_cast<const bool*>(srcPtr);
0119     assert(other != nullptr &&
0120            "Source column is not of same type as destination");
0121     m_vector.at(dstIdx).value = *other;
0122   }
0123 
0124   std::vector<Wrapper> m_vector;
0125 };
0126 
0127 }  // namespace Acts::detail