ATLAS Offline Software
Loading...
Searching...
No Matches
ColumnAccessorArray.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5/// @author Nils Krumnack
6
7
8#include <ColumnarCore/ColumnAccessorDataArray.h>
9#include <ColumnarCore/VectorConvertView.h>
10#include <stdexcept>
11#include <type_traits>
12
13namespace columnar
14{
15 namespace detail
16 {
17 struct ColumnAccessorOptionsArray final
18 {
19 std::string_view offsetName;
20 std::string baseName;
21 std::string dataSuffix = "";
22 bool isOffset = false;
23 unsigned numOffsets = 0u;
24 };
25
26 // the Array mode specialization for native types
27 template<typename CT,ColumnAccessMode CAM,ColumnarArrayModeConcept CM>
28 requires (MemoryAccessor<CT,CM>::isDefined)
29 class ContainerFreeAccessor<CT,CAM,CM> final
30 {
31 /// Common Public Members
32 /// =====================
33 public:
34
35 static_assert (CAM == ColumnAccessMode::input || MemoryAccessor<CT,CM>::viewIsReference || MemoryAccessor<CT,CM>::hasSetter, "output access not supported for this type");
36
37 using ColumnType = typename MemoryAccessor<CT,CM>::MemoryType;
38
39 static constexpr bool isDefined = true;
40 static constexpr bool viewIsReference = MemoryAccessor<CT,CM>::viewIsReference;
41 static constexpr bool useSetter = !viewIsReference && CAM == ColumnAccessMode::output && MemoryAccessor<CT,CM>::hasSetter;
42 static constexpr unsigned internalOffsetColumns = 0;
43
44 ContainerFreeAccessor () noexcept = default;
45
46 ContainerFreeAccessor (ColumnarTool<CM>& columnarTool, ColumnAccessorOptions&& options, ColumnAccessorOptionsArray&& optionsArray)
47 {
48 auto info = options.makeColumnInfo();
49 info.offsetName = optionsArray.offsetName;
50 info.isOffset = optionsArray.isOffset;
51 setColumnType<ColumnType> (info);
52 if (options.isSignAgnostic)
53 {
54 if (info.typeCategory != ColumnTypeCategory::signedInteger && info.typeCategory != ColumnTypeCategory::unsignedInteger && info.typeCategory != ColumnTypeCategory::signAgnosticInteger)
55 throw std::logic_error ("isSignAgnostic set on a column that is not an integer: " + optionsArray.baseName + optionsArray.dataSuffix);
56 info.typeCategory = ColumnTypeCategory::signAgnosticInteger;
57 }
58 if constexpr (requires { requires MemoryAccessor<CT,CM>::isBooleanValued; })
59 info.typeCategory = ColumnTypeCategory::boolean;
60 m_accessorData = std::make_unique<ColumnAccessorDataArray> (&m_dataIndex, &m_accessorData, &typeid (typename MemoryAccessor<CT,CM>::MemoryType), CAM);
61 columnarTool.addColumn (optionsArray.baseName + optionsArray.dataSuffix, m_accessorData.get(), std::move (info));
62 }
63
64 ContainerFreeAccessor (ContainerFreeAccessor&& that)
65 {
66 moveAccessor (m_dataIndex, m_accessorData, that.m_dataIndex, that.m_accessorData);
67 }
68
69 ContainerFreeAccessor& operator = (ContainerFreeAccessor&& that)
70 {
71 if (this != &that)
72 moveAccessor (m_dataIndex, m_accessorData, that.m_dataIndex, that.m_accessorData);
73 return *this;
74 }
75
76 ContainerFreeAccessor (const ContainerFreeAccessor&) = delete;
77 ContainerFreeAccessor& operator = (const ContainerFreeAccessor&) = delete;
78
79 [[nodiscard]] ColumnType& operator () (void** dataArea, std::size_t index) const noexcept
80 requires (viewIsReference)
81 {
82 auto *data = static_cast<ColumnType*>(dataArea[m_dataIndex]);
83 return data[index];
84 }
85
86 [[nodiscard]] decltype(auto) operator () (void** dataArea, std::size_t index) const noexcept
87 requires (!viewIsReference && CAM == ColumnAccessMode::input)
88 {
89 auto *data = static_cast<ColumnType*>(dataArea[m_dataIndex]);
90 return MemoryAccessor<CT,CM>::makeViewer (dataArea)(data[index]);
91 }
92
93 [[nodiscard]] std::span<ColumnType> operator () (void** dataArea, std::size_t beginIndex, std::size_t endIndex) const noexcept
94 requires (viewIsReference)
95 {
96 auto *data = static_cast<ColumnType*>(dataArea[m_dataIndex]);
97 return std::span<ColumnType> (data+beginIndex, endIndex-beginIndex);
98 }
99
100 [[nodiscard]] auto operator () (void** dataArea, std::size_t beginIndex, std::size_t endIndex) const noexcept
101 requires (!viewIsReference && CAM == ColumnAccessMode::input)
102 {
103 auto *data = static_cast<ColumnType*>(dataArea[m_dataIndex]);
104 std::span<ColumnType> mySpan (data+beginIndex, endIndex-beginIndex);
105 return VectorConvertView (MemoryAccessor<CT,CM>::makeViewer (dataArea), std::span (mySpan));
106 }
107
108 [[nodiscard]] auto operator () (void** dataArea, std::size_t index) const noexcept
109 requires (useSetter)
110 {
111 auto *data = static_cast<ColumnType*>(dataArea[m_dataIndex]);
112 return OutputProxy (data[index], MemoryAccessor<CT,CM>::makeSetter (dataArea));
113 }
114
115 template<typename T>
116 requires (useSetter)
117 void set (void** dataArea, std::size_t index, T&& value) const
118 {
119 auto *data = static_cast<ColumnType*>(dataArea[m_dataIndex]);
120 MemoryAccessor<CT,CM>::makeSetter (dataArea)(data[index], std::forward<T>(value));
121 }
122
123 [[nodiscard]] bool isAvailable (void** dataArea) const noexcept
124 {
125 auto *data = static_cast<ColumnType*>(dataArea[m_dataIndex]);
126 return data != nullptr;
127 }
128
129 /// Private Members
130 /// ===============
131 private:
132
133 unsigned m_dataIndex = 0u;
134 std::unique_ptr<ColumnAccessorDataArray> m_accessorData;
135 };
136 }
137
138
139
140 // the Array mode specialization for native types
141 template<ContainerIdConcept CI,typename CT,ColumnAccessMode CAM,ColumnarArrayModeConcept CM>
142 requires (CI::regularColumnAccessorArray && detail::ContainerFreeAccessor<CT,CAM,CM>::isDefined)
143 class AccessorTemplate<CI,CT,CAM,CM> final
144 {
145 /// Common Public Members
146 /// =====================
147 public:
148
149 static_assert (!std::is_const_v<CT>, "CT must not be const");
150
151 AccessorTemplate () noexcept = default;
152
153 AccessorTemplate (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnAccessorOptions&& options = {})
154 : m_accessor (columnarTool, std::move (options), detail::ColumnAccessorOptionsArray{.offsetName = CI::perEventRange ? CI::idName : eventContextCIName, .baseName = std::string (CI::idName) + "." + name})
155 {
156 static_assert (CI::perEventRange + CI::perEventId == 1, "ContainerId must be either per-event-range or per-event-id");
157 }
158
159 AccessorTemplate (AccessorTemplate&& that) = default;
160 AccessorTemplate& operator = (AccessorTemplate&& that) = default;
161 AccessorTemplate (const AccessorTemplate&) = delete;
162 AccessorTemplate& operator = (const AccessorTemplate&) = delete;
163
164 void reset (ColumnarTool<CM>& columnarTool, const std::string& name, ColumnAccessorOptions&& options = {})
165 {
166 *this = AccessorTemplate (columnarTool, name, std::move (options));
167 }
168
169 [[nodiscard]] decltype(auto) operator () (ObjectId<CI,CM> id) const
170 {
171 return m_accessor(id.getDataArea(), id.getIndex());
172 }
173
174 // Normally we should not pass ObjectRange by reference (it can
175 // break loop optimization), but since this function should be
176 // inlined and any member is only accessed once, it doesn't matter
177 // (either is sufficient) and this shuts up cppcheck.
178 [[nodiscard]] decltype(auto) operator () (const ObjectRange<CI,CM>& range) const
179 {
180 return m_accessor(range.getDataArea(), range.beginIndex(), range.endIndex());
181 }
182
183 template<typename T>
184 requires requires(T value) {std::declval<detail::ContainerFreeAccessor<CT,CAM,CM>>().set (nullptr,0,value);}
185 void set (ObjectId<CI,CM> id, T&& value) const
186 {
187 m_accessor.set (id.getDataArea(), id.getIndex(), std::forward<T>(value));
188 }
189
190 [[nodiscard]] bool isAvailable (ObjectId<CI,CM> id) const noexcept
191 {
192 return m_accessor.isAvailable (id.getDataArea());
193 }
194
195 // Normally we should not pass ObjectRange by reference (it can
196 // break loop optimization), but since this function should be
197 // inlined and any member is only accessed once, it doesn't matter
198 // (either is sufficient) and this shuts up cppcheck.
199 [[nodiscard]] bool isAvailable (const ObjectRange<CI,CM>& range) const noexcept
200 {
201 return m_accessor.isAvailable (range.getDataArea());
202 }
203
204 [[nodiscard]] auto getOptional (ObjectId<CI,CM> id) const
205 {
206 std::optional<std::decay_t<decltype(std::declval<detail::ContainerFreeAccessor<CT,CAM,CM>>()(nullptr,0))>> result;
207 if (m_accessor.isAvailable (id.getDataArea()))
208 result = m_accessor (id.getDataArea(), id.getIndex());
209 return result;
210 }
211
212 /// Private Members
213 /// ===============
214 private:
215
216 detail::ContainerFreeAccessor<CT,CAM,CM> m_accessor;
217 };
218}