Project
Loading...
Searching...
No Matches
ShiftableVector.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_SHIFTABLEVECTOR_H_
17#define RANS_INTERNAL_CONTAINER_SHIFTABLEVECTOR_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 <class source_T, class value_T>
35{
36 public:
38 using value_type = value_T;
39 using container_type = std::vector<value_type>;
40 using size_type = size_t;
41 using difference_type = std::ptrdiff_t;
45 using const_pointer = const value_type*;
48 using reverse_iterator = std::reverse_iterator<iterator>;
49 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
50
51 ShiftableVector() = default;
52
54
56 {
57 this->mContainer = vec.mContainer;
58 this->setOffset(vec.getOffset());
59 }
60
62
64 {
65 ShiftableVector tmp = vec;
66 swap(tmp, *this);
67 return *this;
68 }
69
71
73
74 ~ShiftableVector() = default;
75
76 [[nodiscard]] inline const_reference operator[](source_type sourceSymbol) const
77 {
78 assert(static_cast<size_type>(sourceSymbol) - this->getOffset() < this->size());
79 return *this->getAddressAt(sourceSymbol);
80 };
81
82 [[nodiscard]] inline reference operator[](source_type sourceSymbol) { return const_cast<reference>(static_cast<const ShiftableVector&>(*this)[sourceSymbol]); };
83
84 [[nodiscard]] inline const_reference operator()(size_type index) const
85 {
86 assert(index < this->size());
87 return mContainer[index];
88 };
89
90 [[nodiscard]] inline reference operator()(size_type index) { return const_cast<reference>(static_cast<const ShiftableVector&>(*this)(index)); };
91
92 [[nodiscard]] inline const_pointer data() const noexcept { return mContainer.data(); };
93
94 [[nodiscard]] inline pointer data() noexcept { return mContainer.data(); };
95
96 [[nodiscard]] inline size_type size() const noexcept { return mContainer.size(); };
97
98 [[nodiscard]] inline bool empty() const noexcept { return mContainer.empty(); };
99
100 [[nodiscard]] inline source_type getOffset() const noexcept { return mOffset; };
101
102 inline void setOffset(source_type offset) noexcept
103 {
104 mOffset = offset;
105 mShiftedBegin = reinterpret_cast<intptr_t>(mContainer.data()) - mOffset * sizeof(value_type); // circumvent undefined behavior
106 assert(static_cast<difference_type>(this->size() + this->getOffset()) <= static_cast<difference_type>(std::numeric_limits<source_type>::max()) + 1);
107 assert(this->getAddressAt(offset) == mContainer.data());
108 };
109
110 inline void reserve(size_type newSize)
111 {
112 mContainer.reserve(newSize);
113 this->setOffset(this->getOffset());
114 };
115
116 inline void resize(size_type newSize, source_type offset, const value_type& value)
117 {
118 assert(newSize <= utils::pow2(utils::toBits<source_type>()));
119 this->mContainer.resize(newSize, value);
120 this->setOffset(offset);
121 };
122 inline void resize(size_type newSize, source_type offset)
123 {
124 this->mContainer.resize(newSize, offset, {});
125 };
126
127 inline void resize(size_type newSize, const value_type& value)
128 {
129 this->resize(newSize, this->getOffset(), value);
130 };
131
132 inline void resize(size_type newSize)
133 {
134 this->resize(newSize, this->getOffset());
135 };
136
138 {
139 mContainer.push_back(std::move(value));
140 this->setOffset(this->getOffset()); // update in case of reallocation
141 };
142
143 template <class... Args>
144 inline void emplace_back(Args&&... args)
145 {
146 mContainer.emplace_back(std::forward<Args>(args)...);
147 this->setOffset(this->getOffset()); // update in case of reallocation
148 };
149
150 [[nodiscard]] inline const_iterator cbegin() const noexcept { return this->data(); };
151
152 [[nodiscard]] inline const_iterator cend() const noexcept { return this->data() + this->size(); };
153
154 [[nodiscard]] inline const_iterator begin() const noexcept { return cbegin(); };
155
156 [[nodiscard]] inline const_iterator end() const noexcept { return cend(); };
157
158 [[nodiscard]] inline iterator begin() noexcept { return const_cast<iterator>(static_cast<const ShiftableVector&>(*this).begin()); };
159
160 [[nodiscard]] inline iterator end() noexcept { return const_cast<iterator>(static_cast<const ShiftableVector&>(*this).end()); };
161
162 [[nodiscard]] inline const_reverse_iterator crbegin() const noexcept { return std::reverse_iterator{this->cend()}; };
163
164 [[nodiscard]] inline const_reverse_iterator crend() const noexcept { return std::reverse_iterator{this->cbegin()}; };
165
166 [[nodiscard]] inline const_reverse_iterator rbegin() const noexcept { return crbegin(); };
167
168 [[nodiscard]] inline const_reverse_iterator rend() const noexcept { return crend(); };
169
170 [[nodiscard]] inline reverse_iterator rbegin() noexcept { return std::reverse_iterator{this->end()}; };
171
172 [[nodiscard]] inline reverse_iterator rend() noexcept { return std::reverse_iterator{this->begin()}; };
173
174 [[nodiscard]] inline container_type release() && noexcept { return std::move(this->mContainer); };
175
176 friend void swap(ShiftableVector& a, ShiftableVector& b) noexcept
177 {
178 using std::swap;
179 swap(a.mContainer, b.mContainer);
180 swap(a.mOffset, b.mOffset);
181 swap(a.mShiftedBegin, b.mShiftedBegin);
182 };
183
184 protected:
185 [[nodiscard]] inline const_pointer getAddressAt(source_type sourceSymbol) const
186 {
187 return reinterpret_cast<const value_type*>(mShiftedBegin + sourceSymbol * sizeof(value_type)); // circumvent undefined behavior
188 }
189
190 [[nodiscard]] inline pointer getAddressAt(source_type sourceSymbol)
191 {
192 return const_cast<pointer>(static_cast<const ShiftableVector&>(*this).getAddressAt(sourceSymbol));
193 }
194
198};
199
200} // namespace o2::rans::internal
201
202#endif /* RANS_INTERNAL_CONTAINER_SHIFTABLEVECTOR_H_ */
common helper classes and functions
const_iterator end() const noexcept
const_pointer data() const noexcept
reference operator[](source_type sourceSymbol)
friend void swap(ShiftableVector &a, ShiftableVector &b) noexcept
const_pointer getAddressAt(source_type sourceSymbol) const
const_reverse_iterator crbegin() const noexcept
ShiftableVector(size_type size, source_type offset=0)
reverse_iterator rbegin() noexcept
const_reference operator[](source_type sourceSymbol) const
const_reverse_iterator rend() const noexcept
reference operator()(size_type index)
void resize(size_type newSize, const value_type &value)
void resize(size_type newSize, source_type offset)
const_reverse_iterator crend() const noexcept
void resize(size_type newSize, source_type offset, const value_type &value)
pointer getAddressAt(source_type sourceSymbol)
reverse_iterator rend() noexcept
std::reverse_iterator< iterator > reverse_iterator
const_iterator cend() const noexcept
ShiftableVector & operator=(ShiftableVector &&)=default
const_iterator begin() const noexcept
ShiftableVector & operator=(const ShiftableVector &vec)
void setOffset(source_type offset) noexcept
container_type release() &&noexcept
ShiftableVector(ShiftableVector &&)=default
std::vector< value_type > container_type
ShiftableVector(container_type container, source_type offset=0)
const_reverse_iterator rbegin() const noexcept
std::reverse_iterator< const_iterator > const_reverse_iterator
const_iterator cbegin() const noexcept
ShiftableVector(const ShiftableVector &vec)
source_type getOffset() const noexcept
size_type size() const noexcept
const_reference operator()(size_type index) const
GLsizeiptr size
Definition glcorearb.h:659
GLuint index
Definition glcorearb.h:781
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLintptr offset
Definition glcorearb.h:660
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
constexpr size_t pow2(size_t n) noexcept
Definition utils.h:165
std::vector< o2::ctf::BufferType > vec