Project
Loading...
Searching...
No Matches
OrderedSet.h
Go to the documentation of this file.
1// Copyright 2019-2023 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
15
16#ifndef RANS_INTERNAL_CONTAINER_ORDEREDSET_H_
17#define RANS_INTERNAL_CONTAINER_ORDEREDSET_H_
18
19#include <cstdint>
20#include <string>
21#include <vector>
22#include <algorithm>
23#include <iterator>
24#include <cassert>
25
26#include <fairlogger/Logger.h>
27
29
30namespace o2::rans::internal
31{
32
33template <typename P>
34class OrderedSetIterator;
35
37 unordered };
38
39template <class source_T, class value_T>
41{
42 public:
44 using value_type = value_T;
45 using tuple_type = std::pair<source_T, value_T>;
46 using container_type = std::vector<tuple_type>;
47 using size_type = size_t;
48 using difference_type = std::ptrdiff_t;
52 using const_pointer = const value_type*;
55
56 OrderedSet() = default;
57 OrderedSet(value_type nullElement) : mNullElement{std::move(nullElement)} {};
58 OrderedSet(container_type container, value_type nullElement, OrderedSetState state = OrderedSetState::unordered) : mContainer{std::move(container)}, mNullElement{std::move(nullElement)}
59 {
61 std::sort(mContainer.begin(), mContainer.end(), [](const tuple_type& a, const tuple_type& b) {
62 return a.first < b.first;
63 });
64 }
65
66#if !defined(NDEBUG)
67 if (!container.empty()) {
68 auto iter = mContainer.begin();
69 auto value = iter->first;
70 for (++iter; iter != mContainer.end(); ++iter) {
71 assert(value < iter->first);
72 value = iter->first;
73 }
74 }
75#endif
76 };
77
78 [[nodiscard]] inline const_reference getNullElement() const { return mNullElement; };
79
80 [[nodiscard]] inline const_reference operator[](source_type sourceSymbol) const
81 {
82 auto iter = findImpl(sourceSymbol);
83 if (iter != mContainer.end()) {
84 return iter->second;
85 } else {
86 return getNullElement();
87 }
88 };
89
90 [[nodiscard]] inline reference operator[](source_type sourceSymbol)
91 {
92 auto iter = findImpl(sourceSymbol);
93 if (iter != mContainer.end()) {
94 return iter->second;
95 } else {
96 throw Exception(fmt::format("sourceSymbol {} is not contained in data-structure", sourceSymbol));
97 }
98 };
99
100 [[nodiscard]] inline const_iterator find(source_type sourceSymbol) const { return {findImpl(sourceSymbol)}; };
101
102 [[nodiscard]] inline iterator find(source_type sourceSymbol) { return {findImpl(sourceSymbol)}; };
103
104 [[nodiscard]] inline size_type size() const noexcept { return mContainer.size(); };
105
106 [[nodiscard]] inline bool empty() const noexcept { return mContainer.empty(); };
107
108 [[nodiscard]] inline const_iterator cbegin() const noexcept { return {mContainer.cbegin().base()}; };
109
110 [[nodiscard]] inline const_iterator cend() const noexcept { return {mContainer.cend().base()}; };
111
112 [[nodiscard]] inline const_iterator begin() const noexcept { return cbegin(); };
113
114 [[nodiscard]] inline const_iterator end() const noexcept { return cend(); };
115
116 [[nodiscard]] inline iterator begin() noexcept { return {mContainer.begin().base()}; };
117
118 [[nodiscard]] inline iterator end() noexcept { return {mContainer.end().base()}; };
119
120 [[nodiscard]] inline container_type release() && noexcept { return std::move(this->mContainer); };
121
122 friend void swap(OrderedSet& a, OrderedSet& b) noexcept
123 {
124 using std::swap;
125 swap(a.mContainer, b.mContainer);
126 swap(a.mNullElement, b.mNullElement);
127 };
128
129 protected:
130 struct Comparator {
131 bool operator()(const tuple_type& a, const source_type& b) const
132 {
133 return a.first < b;
134 }
135
136 bool operator()(const source_type& a, const tuple_type& b) const
137 {
138 return a < b.first;
139 }
140 };
141
142 [[nodiscard]] inline decltype(auto) findImpl(source_type sourceSymbol)
143 {
144 auto iter = std::lower_bound(mContainer.begin(), mContainer.end(), sourceSymbol, Comparator());
145 if (iter != mContainer.end()) {
146 if (iter->first != sourceSymbol) {
147 return mContainer.end();
148 }
149 }
150 return iter;
151 };
152
153 [[nodiscard]] inline decltype(auto) findImpl(source_type sourceSymbol) const
154 {
155 auto iter = std::lower_bound(mContainer.begin(), mContainer.end(), sourceSymbol, Comparator());
156 if (iter != mContainer.end()) {
157 if (iter->first != sourceSymbol) {
158 return mContainer.end();
159 }
160 }
161 return iter;
162 };
163
166};
167
168template <typename P>
170{
171
172 using pair_type = std::remove_cv_t<std::remove_pointer_t<P>>;
173
174 public:
175 class PtrHelper;
176
177 using source_type = typename pair_type::first_type;
178 using difference_type = std::ptrdiff_t;
179 using value_type = std::pair<const source_type, std::add_lvalue_reference_t<std::conditional_t<std::is_const_v<std::remove_pointer_t<P>>,
180 std::add_const_t<typename pair_type::second_type>,
181 typename pair_type::second_type>>>;
184 using iterator_category = std::random_access_iterator_tag;
185
186 inline constexpr OrderedSetIterator() noexcept = default;
187
188 inline constexpr OrderedSetIterator(P ptr) noexcept : mPtr{ptr} {};
189 inline constexpr OrderedSetIterator(const OrderedSetIterator& iter) noexcept = default;
190 inline constexpr OrderedSetIterator(OrderedSetIterator&& iter) noexcept = default;
191 inline constexpr OrderedSetIterator& operator=(const OrderedSetIterator& other) noexcept = default;
192 inline constexpr OrderedSetIterator& operator=(OrderedSetIterator&& other) noexcept = default;
193 inline ~OrderedSetIterator() noexcept = default;
194
195 // pointer arithmetics
196 inline constexpr OrderedSetIterator& operator++() noexcept
197 {
198 ++mPtr;
199 return *this;
200 };
201
202 inline constexpr OrderedSetIterator operator++(int) noexcept
203 {
204 auto res = *this;
205 ++(*this);
206 return res;
207 };
208
209 inline constexpr OrderedSetIterator& operator--() noexcept
210 {
211 --mPtr;
212 return *this;
213 };
214
215 inline constexpr OrderedSetIterator operator--(int) noexcept
216 {
217 auto res = *this;
218 --(*this);
219 return res;
220 };
221
222 inline constexpr OrderedSetIterator& operator+=(difference_type i) noexcept
223 {
224 mPtr += i;
225 return *this;
226 };
227
228 inline constexpr OrderedSetIterator operator+(difference_type i) const noexcept
229 {
230 auto tmp = *const_cast<OrderedSetIterator*>(this);
231 return tmp += i;
232 }
233
234 inline constexpr OrderedSetIterator& operator-=(difference_type i) noexcept
235 {
236 mPtr -= i;
237 return *this;
238 };
239
240 inline constexpr OrderedSetIterator operator-(difference_type i) const noexcept
241 {
242 auto tmp = *const_cast<OrderedSetIterator*>(this);
243 return tmp -= i;
244 };
245
246 inline constexpr difference_type operator-(const OrderedSetIterator& other) const noexcept
247 {
248 return this->mPtr - other.mPtr;
249 };
250
251 // comparison
252 inline constexpr bool operator==(const OrderedSetIterator& other) const noexcept { return this->mPtr == other.mPtr; };
253 inline constexpr bool operator!=(const OrderedSetIterator& other) const noexcept { return this->mPtr != other.mPtr; };
254 inline constexpr bool operator<(const OrderedSetIterator& other) const noexcept { return this->mPtr < other->mPtr; };
255 inline constexpr bool operator>(const OrderedSetIterator& other) const noexcept { return this->mPtr > other->mPtr; };
256 inline constexpr bool operator>=(const OrderedSetIterator& other) const noexcept { return this->mPtr >= other->mPtr; };
257 inline constexpr bool operator<=(const OrderedSetIterator& other) const noexcept { return this->mPtr <= other->mPtr; };
258
259 // dereference
260 inline constexpr value_type operator*() const { return {mPtr->first, mPtr->second}; };
261
262 inline constexpr pointer operator->() const { return {operator*()}; };
263
264 inline constexpr value_type operator[](difference_type i) const
265 {
266 auto& val = mPtr[i];
267 return {val.first, val.second};
268 };
269
271 {
272 public:
273 PtrHelper() = default;
274 PtrHelper(value_type value) : mValue{std::move(value)} {};
275
276 value_type* operator->() const { return &mValue; }
277 value_type* operator->() { return &mValue; }
278
279 private:
280 value_type mValue{};
281 };
282
283 private:
284 value_type get() { return {mPtr->first, mPtr->second}; };
285 P mPtr;
286};
287
288} // namespace o2::rans::internal
289
290#endif /* RANS_INTERNAL_CONTAINER_ORDEREDSET_H_ */
benchmark::State & state
int32_t i
uint32_t res
Definition RawData.h:0
TBranch * ptr
common helper classes and functions
constexpr bool operator==(const OrderedSetIterator &other) const noexcept
Definition OrderedSet.h:252
std::random_access_iterator_tag iterator_category
Definition OrderedSet.h:184
constexpr bool operator>=(const OrderedSetIterator &other) const noexcept
Definition OrderedSet.h:256
constexpr OrderedSetIterator operator+(difference_type i) const noexcept
Definition OrderedSet.h:228
constexpr OrderedSetIterator & operator-=(difference_type i) noexcept
Definition OrderedSet.h:234
constexpr value_type operator*() const
Definition OrderedSet.h:260
typename pair_type::first_type source_type
Definition OrderedSet.h:177
constexpr bool operator<=(const OrderedSetIterator &other) const noexcept
Definition OrderedSet.h:257
constexpr value_type operator[](difference_type i) const
Definition OrderedSet.h:264
constexpr OrderedSetIterator operator-(difference_type i) const noexcept
Definition OrderedSet.h:240
constexpr bool operator!=(const OrderedSetIterator &other) const noexcept
Definition OrderedSet.h:253
constexpr OrderedSetIterator & operator+=(difference_type i) noexcept
Definition OrderedSet.h:222
constexpr OrderedSetIterator(OrderedSetIterator &&iter) noexcept=default
constexpr OrderedSetIterator operator--(int) noexcept
Definition OrderedSet.h:215
constexpr OrderedSetIterator operator++(int) noexcept
Definition OrderedSet.h:202
std::pair< const source_type, std::add_lvalue_reference_t< std::conditional_t< std::is_const_v< std::remove_pointer_t< P > >, std::add_const_t< typename pair_type::second_type >, typename pair_type::second_type > > > value_type
Definition OrderedSet.h:181
constexpr OrderedSetIterator(const OrderedSetIterator &iter) noexcept=default
constexpr bool operator>(const OrderedSetIterator &other) const noexcept
Definition OrderedSet.h:255
constexpr OrderedSetIterator & operator=(OrderedSetIterator &&other) noexcept=default
constexpr bool operator<(const OrderedSetIterator &other) const noexcept
Definition OrderedSet.h:254
constexpr OrderedSetIterator() noexcept=default
constexpr difference_type operator-(const OrderedSetIterator &other) const noexcept
Definition OrderedSet.h:246
constexpr pointer operator->() const
Definition OrderedSet.h:262
constexpr OrderedSetIterator & operator=(const OrderedSetIterator &other) noexcept=default
constexpr OrderedSetIterator & operator--() noexcept
Definition OrderedSet.h:209
OrderedSet(container_type container, value_type nullElement, OrderedSetState state=OrderedSetState::unordered)
Definition OrderedSet.h:58
bool empty() const noexcept
Definition OrderedSet.h:106
const_iterator cbegin() const noexcept
Definition OrderedSet.h:108
container_type release() &&noexcept
Definition OrderedSet.h:120
const value_type * const_pointer
Definition OrderedSet.h:52
std::vector< tuple_type > container_type
Definition OrderedSet.h:46
const_iterator end() const noexcept
Definition OrderedSet.h:114
const_iterator begin() const noexcept
Definition OrderedSet.h:112
decltype(auto) findImpl(source_type sourceSymbol)
Definition OrderedSet.h:142
const value_type & const_reference
Definition OrderedSet.h:50
reference operator[](source_type sourceSymbol)
Definition OrderedSet.h:90
size_type size() const noexcept
Definition OrderedSet.h:104
decltype(auto) findImpl(source_type sourceSymbol) const
Definition OrderedSet.h:153
iterator end() noexcept
Definition OrderedSet.h:118
const_reference getNullElement() const
Definition OrderedSet.h:78
iterator begin() noexcept
Definition OrderedSet.h:116
const_reference operator[](source_type sourceSymbol) const
Definition OrderedSet.h:80
const_iterator find(source_type sourceSymbol) const
Definition OrderedSet.h:100
std::pair< source_T, value_T > tuple_type
Definition OrderedSet.h:45
const_iterator cend() const noexcept
Definition OrderedSet.h:110
iterator find(source_type sourceSymbol)
Definition OrderedSet.h:102
friend void swap(OrderedSet &a, OrderedSet &b) noexcept
Definition OrderedSet.h:122
OrderedSet(value_type nullElement)
Definition OrderedSet.h:57
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLuint GLfloat * val
Definition glcorearb.h:1582
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
if constexpr(std::is_unsigned_v< source_T >)
Defining DataPointCompositeObject explicitly as copiable.
bool operator()(const source_type &a, const tuple_type &b) const
Definition OrderedSet.h:136
bool operator()(const tuple_type &a, const source_type &b) const
Definition OrderedSet.h:131
VectorOfTObjectPtrs other