Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DynamicColumn.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DynamicColumn.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <any>
12 #include <cassert>
13 #include <memory>
14 #include <vector>
15 
16 namespace Acts::detail {
17 
19  virtual ~DynamicColumnBase() = default;
20 
21  virtual std::any get(size_t i) = 0;
22  virtual std::any get(size_t i) const = 0;
23 
24  virtual void add() = 0;
25  virtual void clear() = 0;
26  virtual void reserve(size_t size) = 0;
27  virtual void erase(size_t i) = 0;
28  virtual size_t size() const = 0;
29  virtual void copyFrom(size_t dstIdx, const DynamicColumnBase& src,
30  size_t srcIdx) = 0;
31 
32  virtual std::unique_ptr<DynamicColumnBase> clone(
33  bool empty = false) const = 0;
34 };
35 
36 template <typename T>
38  std::any get(size_t i) override {
39  assert(i < m_vector.size() && "DynamicColumn out of bounds");
40  return &m_vector[i];
41  }
42 
43  std::any get(size_t i) const override {
44  assert(i < m_vector.size() && "DynamicColumn out of bounds");
45  return &m_vector[i];
46  }
47 
48  void add() override { m_vector.emplace_back(); }
49  void clear() override { m_vector.clear(); }
50  void reserve(size_t size) override { m_vector.reserve(size); }
51  void erase(size_t i) override { m_vector.erase(m_vector.begin() + i); }
52  size_t size() const override { return m_vector.size(); }
53 
54  std::unique_ptr<DynamicColumnBase> clone(bool empty) const override {
55  if (empty) {
56  return std::make_unique<DynamicColumn<T>>();
57  }
58  return std::make_unique<DynamicColumn<T>>(*this);
59  }
60 
61  void copyFrom(size_t dstIdx, const DynamicColumnBase& src,
62  size_t srcIdx) override {
63  const auto* other = dynamic_cast<const DynamicColumn<T>*>(&src);
64  assert(other != nullptr &&
65  "Source column is not of same type as destination");
66  m_vector.at(dstIdx) = other->m_vector.at(srcIdx);
67  }
68 
69  std::vector<T> m_vector;
70 };
71 
72 template <>
73 struct DynamicColumn<bool> : public DynamicColumnBase {
74  struct Wrapper {
75  bool value;
76  };
77 
78  std::any get(size_t i) override {
79  assert(i < m_vector.size() && "DynamicColumn out of bounds");
80  return &m_vector[i].value;
81  }
82 
83  std::any get(size_t i) const override {
84  assert(i < m_vector.size() && "DynamicColumn out of bounds");
85  return &m_vector[i].value;
86  }
87 
88  void add() override { m_vector.emplace_back(); }
89  void reserve(size_t size) override { m_vector.reserve(size); }
90  void clear() override { m_vector.clear(); }
91  void erase(size_t i) override { m_vector.erase(m_vector.begin() + i); }
92  size_t size() const override { return m_vector.size(); }
93 
94  std::unique_ptr<DynamicColumnBase> clone(bool empty) const override {
95  if (empty) {
96  return std::make_unique<DynamicColumn<bool>>();
97  }
98  return std::make_unique<DynamicColumn<bool>>(*this);
99  }
100 
101  void copyFrom(size_t dstIdx, const DynamicColumnBase& src,
102  size_t srcIdx) override {
103  const auto* other = dynamic_cast<const DynamicColumn<bool>*>(&src);
104  assert(other != nullptr &&
105  "Source column is not of same type as destination");
106  m_vector.at(dstIdx) = other->m_vector.at(srcIdx);
107  }
108 
109  std::vector<Wrapper> m_vector;
110 };
111 
112 } // namespace Acts::detail