HerdSoftware  0.1.1
ArrayForwarder.h
Go to the documentation of this file.
1 /*
2  * ArrayForwarder.h
3  *
4  * Created on: Feb 8, 2019
5  * Author: Nicola Mori
6  */
7 
8 #ifndef HERD_ARRAYFORWARDER_H_
9 #define HERD_ARRAYFORWARDER_H_
10 
11 // C/C++ headers
12 #include <array>
13 #include <type_traits>
14 
15 namespace Herd {
16 
17 // Define constexpr macro for C++17 for non-gnu implementations of the standard library
18 #ifndef _GLIBCXX17_CONSTEXPR
19 #if __cplusplus >= 201703L
20 #define _GLIBCXX17_CONSTEXPR constexpr
21 #else
22 #define _GLIBCXX17_CONSTEXPR
23 #endif
24 #endif
25 
48 template <typename _Tp, std::size_t _Nm> struct ArrayForwarder;
49 
50 // Array comparisons.
51 template <typename _Tp, std::size_t _Nm>
52 inline bool operator==(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two) {
53  return std::equal(__one._content.begin(), __one._content.end(), __two._content.begin());
54 }
55 
56 template <typename _Tp, std::size_t _Nm>
57 inline bool operator!=(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two) {
58  return !(__one == __two);
59 }
60 
61 template <typename _Tp, std::size_t _Nm>
62 inline bool operator<(const ArrayForwarder<_Tp, _Nm> &__a, const ArrayForwarder<_Tp, _Nm> &__b) {
63  return std::lexicographical_compare(__a._content.begin(), __a._content.end(), __b._content.begin(),
64  __b._content.end());
65 }
66 
67 template <typename _Tp, std::size_t _Nm>
68 inline bool operator>(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two) {
69  return __two < __one;
70 }
71 
72 template <typename _Tp, std::size_t _Nm>
73 inline bool operator<=(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two) {
74  return !(__one > __two);
75 }
76 
77 template <typename _Tp, std::size_t _Nm>
78 inline bool operator>=(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two) {
79  return !(__one < __two);
80 }
81 
82 template <typename _Tp, std::size_t _Nm> struct ArrayForwarder {
83 
84  typedef _Tp value_type;
85  typedef value_type *pointer;
86  typedef const value_type *const_pointer;
87  typedef value_type &reference;
88  typedef const value_type &const_reference;
89  typedef value_type *iterator;
90  typedef const value_type *const_iterator;
91  typedef std::size_t size_type;
92  typedef std::ptrdiff_t difference_type;
93  typedef std::reverse_iterator<iterator> reverse_iterator;
94  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
95 
97 
98  ArrayForwarder(std::array<_Tp, _Nm> arr) { _content = std::move(arr); }
99 
100  // No explicit construct/copy/destroy for aggregate type.
101 
102  // DR 776.
103  void fill(const value_type &__u) { _content.fill(__u); };
104 
105 #if __cplusplus >= 201703L
106  void swap(ArrayForwarder &__other) noexcept(std::__is_nothrow_swappable<_Tp>::value) {
107  _content.swap(__other._content);
108  }
109 #else
110  void swap(ArrayForwarder &__other) noexcept(noexcept(std::swap(std::declval<_Tp &>(), std::declval<_Tp &>()))) {
111  std::swap_ranges(_content.begin(), _content.end(), __other._content.begin());
112  }
113 #endif
114 
115  // Iterators.
116  _GLIBCXX17_CONSTEXPR iterator begin() noexcept { return _content.begin(); }
117 
118  _GLIBCXX17_CONSTEXPR const_iterator begin() const noexcept { return _content.begin(); }
119 
120  _GLIBCXX17_CONSTEXPR iterator end() noexcept { return _content.end(); }
121 
122  _GLIBCXX17_CONSTEXPR const_iterator end() const noexcept { return _content.end(); }
123 
124  _GLIBCXX17_CONSTEXPR reverse_iterator rbegin() noexcept { return _content.rbegin(); }
125 
126  _GLIBCXX17_CONSTEXPR const_reverse_iterator rbegin() const noexcept { return _content.rbegin(); }
127 
128  _GLIBCXX17_CONSTEXPR reverse_iterator rend() noexcept { return _content.rend(); }
129 
130  _GLIBCXX17_CONSTEXPR const_reverse_iterator rend() const noexcept { return _content.rend(); }
131 
132  _GLIBCXX17_CONSTEXPR const_iterator cbegin() const noexcept { return _content.cbegin(); }
133 
134  _GLIBCXX17_CONSTEXPR const_iterator cend() const noexcept { return _content.cend(); }
135 
136  _GLIBCXX17_CONSTEXPR const_reverse_iterator crbegin() const noexcept { return _content.crbegin(); }
137 
138  _GLIBCXX17_CONSTEXPR const_reverse_iterator crend() const noexcept { return _content.crend(); }
139 
140  // Capacity.
141  constexpr size_type size() const noexcept { return _content.size(); }
142 
143  constexpr size_type max_size() const noexcept { return _content.size(); }
144 
145  constexpr bool empty() const noexcept { return _content.empty(); }
146 
147  // Element access.
148  _GLIBCXX17_CONSTEXPR reference operator[](size_type __n) noexcept { return _content.operator[](__n); }
149 
150  constexpr const_reference operator[](size_type __n) const noexcept { return _content.operator[](__n); }
151 
152  _GLIBCXX17_CONSTEXPR reference at(size_type __n) { return _content.at(__n); }
153 
154  constexpr const_reference at(size_type __n) const { return _content.at(__n); }
155 
156  _GLIBCXX17_CONSTEXPR reference front() noexcept { return _content.front(); }
157 
158  constexpr const_reference front() const noexcept { return _content.front(); }
159 
160  _GLIBCXX17_CONSTEXPR reference back() noexcept { return _content.back(); }
161 
162  constexpr const_reference back() const noexcept { return _content.back(); }
163 
164  _GLIBCXX17_CONSTEXPR pointer data() noexcept { return _content.data(); }
165 
166  _GLIBCXX17_CONSTEXPR const_pointer data() const noexcept { return _content.data(); }
167 
168 private:
169  std::array<_Tp, _Nm> _content;
170  friend bool operator==<>(const ArrayForwarder<_Tp, _Nm> &, const ArrayForwarder<_Tp, _Nm> &);
171  friend bool operator!=<>(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two);
172  // clang-format off
173  friend bool operator< <>(const ArrayForwarder<_Tp, _Nm> &__a, const ArrayForwarder<_Tp, _Nm> &__b);
174  // clang-format on
175  friend bool operator><>(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two);
176  friend bool operator<=<>(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two);
177  friend bool operator>=<>(const ArrayForwarder<_Tp, _Nm> &__one, const ArrayForwarder<_Tp, _Nm> &__two);
178 };
179 
180 // Specialized algorithms.
181 template <typename _Tp, std::size_t _Nm>
182 inline
183 #if __cplusplus > 201402L // c++1z
184  // Constrained free swap overload, see p0185r1
185  typename std::enable_if<std::__is_swappable<_Tp>::value>::type
186 #else
187  void
188 #endif
189  swap(ArrayForwarder<_Tp, _Nm> &__one, ArrayForwarder<_Tp, _Nm> &__two) noexcept(noexcept(__one.swap(__two))) {
190  __one.swap(__two);
191 }
192 
193 #if __cplusplus > 201402L // c++1z
194 template <typename _Tp, std::size_t _Nm>
195 typename std::enable_if<!std::__is_swappable<_Tp>::value>::type swap(ArrayForwarder<_Tp, _Nm> &,
196  ArrayForwarder<_Tp, _Nm> &) = delete;
197 #endif
198 
199 template <std::size_t _Int, typename _Tp, std::size_t _Nm>
200 constexpr _Tp &get(ArrayForwarder<_Tp, _Nm> &__arr) noexcept {
201  return std::get<_Int, _Tp, _Nm>(__arr._content);
202 }
203 
204 template <std::size_t _Int, typename _Tp, std::size_t _Nm>
205 constexpr _Tp &&get(ArrayForwarder<_Tp, _Nm> &&__arr) noexcept {
206  return std::get<_Int, _Tp, _Nm>(std::move(__arr._content));
207 }
208 
209 template <std::size_t _Int, typename _Tp, std::size_t _Nm>
210 constexpr const _Tp &get(const ArrayForwarder<_Tp, _Nm> &__arr) noexcept {
211  return std::get<_Int, _Tp, _Nm>(__arr._content);
212 }
213 
214 template <std::size_t _Int, typename _Tp, std::size_t _Nm>
215 constexpr const _Tp &&get(const ArrayForwarder<_Tp, _Nm> &&__arr) noexcept {
216  return std::get<_Int, _Tp, _Nm>(std::move(__arr._content));
217 }
218 
219 } // namespace Herd
220 #endif /* HERD_ARRAYFORWARDER_H_ */
bool operator>(const ArrayForwarder< _Tp, _Nm > &__one, const ArrayForwarder< _Tp, _Nm > &__two)
Definition: ArrayForwarder.h:68
bool operator==(const HoughTrackStub &lhs, const HoughTrackStub &rhs)
Definition: HoughTrackStub.h:41
ArrayForwarder(std::array< _Tp, _Nm > arr)
Definition: ArrayForwarder.h:98
CssGeoParams.h CssGeoParams class declaration.
Definition: CaloPDCalibrationAlgo.h:24
_GLIBCXX17_CONSTEXPR reference back() noexcept
Definition: ArrayForwarder.h:160
_GLIBCXX17_CONSTEXPR const_iterator cbegin() const noexcept
Definition: ArrayForwarder.h:132
bool operator!=(const ArrayForwarder< _Tp, _Nm > &__one, const ArrayForwarder< _Tp, _Nm > &__two)
Definition: ArrayForwarder.h:57
_GLIBCXX17_CONSTEXPR const_reverse_iterator rend() const noexcept
Definition: ArrayForwarder.h:130
_GLIBCXX17_CONSTEXPR const_reverse_iterator crbegin() const noexcept
Definition: ArrayForwarder.h:136
#define _GLIBCXX17_CONSTEXPR
Definition: ArrayForwarder.h:22
std::array< _Tp, _Nm > _content
Definition: ArrayForwarder.h:169
_GLIBCXX17_CONSTEXPR const_reverse_iterator rbegin() const noexcept
Definition: ArrayForwarder.h:126
A forwarder class for std::array.
Definition: ArrayForwarder.h:48
_Tp value_type
Definition: ArrayForwarder.h:84
void swap(ArrayForwarder &__other) noexcept(noexcept(std::swap(std::declval< _Tp &>(), std::declval< _Tp &>())))
Definition: ArrayForwarder.h:110
void swap(ArrayForwarder< _Tp, _Nm > &__one, ArrayForwarder< _Tp, _Nm > &__two) noexcept(noexcept(__one.swap(__two)))
Definition: ArrayForwarder.h:189
constexpr const_reference back() const noexcept
Definition: ArrayForwarder.h:162
_GLIBCXX17_CONSTEXPR const_iterator end() const noexcept
Definition: ArrayForwarder.h:122
value_type * iterator
Definition: ArrayForwarder.h:89
std::reverse_iterator< iterator > reverse_iterator
Definition: ArrayForwarder.h:93
constexpr size_type max_size() const noexcept
Definition: ArrayForwarder.h:143
_GLIBCXX17_CONSTEXPR reference operator[](size_type __n) noexcept
Definition: ArrayForwarder.h:148
constexpr const_reference at(size_type __n) const
Definition: ArrayForwarder.h:154
_GLIBCXX17_CONSTEXPR iterator begin() noexcept
Definition: ArrayForwarder.h:116
std::ptrdiff_t difference_type
Definition: ArrayForwarder.h:92
ArrayForwarder()
Definition: ArrayForwarder.h:96
const value_type * const_pointer
Definition: ArrayForwarder.h:86
std::size_t size_type
Definition: ArrayForwarder.h:91
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: ArrayForwarder.h:94
const value_type & const_reference
Definition: ArrayForwarder.h:88
bool operator>=(const ArrayForwarder< _Tp, _Nm > &__one, const ArrayForwarder< _Tp, _Nm > &__two)
Definition: ArrayForwarder.h:78
_GLIBCXX17_CONSTEXPR const_iterator cend() const noexcept
Definition: ArrayForwarder.h:134
constexpr size_type size() const noexcept
Definition: ArrayForwarder.h:141
constexpr bool empty() const noexcept
Definition: ArrayForwarder.h:145
_GLIBCXX17_CONSTEXPR reverse_iterator rbegin() noexcept
Definition: ArrayForwarder.h:124
const value_type * const_iterator
Definition: ArrayForwarder.h:90
_GLIBCXX17_CONSTEXPR const_pointer data() const noexcept
Definition: ArrayForwarder.h:166
value_type * pointer
Definition: ArrayForwarder.h:85
value_type & reference
Definition: ArrayForwarder.h:87
_GLIBCXX17_CONSTEXPR reference at(size_type __n)
Definition: ArrayForwarder.h:152
_GLIBCXX17_CONSTEXPR const_reverse_iterator crend() const noexcept
Definition: ArrayForwarder.h:138
_GLIBCXX17_CONSTEXPR reverse_iterator rend() noexcept
Definition: ArrayForwarder.h:128
_GLIBCXX17_CONSTEXPR const_iterator begin() const noexcept
Definition: ArrayForwarder.h:118
void fill(const value_type &__u)
Definition: ArrayForwarder.h:103
constexpr const_reference operator[](size_type __n) const noexcept
Definition: ArrayForwarder.h:150
_GLIBCXX17_CONSTEXPR iterator end() noexcept
Definition: ArrayForwarder.h:120
constexpr const_reference front() const noexcept
Definition: ArrayForwarder.h:158
_GLIBCXX17_CONSTEXPR pointer data() noexcept
Definition: ArrayForwarder.h:164
_GLIBCXX17_CONSTEXPR reference front() noexcept
Definition: ArrayForwarder.h:156