DNA Calib 1.1
Project brief
ArrayView.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5// *INDENT-OFF*
6#ifndef TRUST_ARRAYVIEW_H
7#define TRUST_ARRAYVIEW_H
8
9#ifdef _MSC_VER
10 #pragma warning(push)
11 #pragma warning(disable : 4365 4987)
12#endif
13#include <algorithm>
14#include <cassert>
15#include <cstddef>
16#ifdef _MSC_VER
17 #pragma warning(pop)
18#endif
19
20namespace trust {
21
22template<typename T>
24 using value_type = T;
25 using reference = T&;
26 using const_reference = const T&;
27 using pointer = T*;
28 using const_pointer = const T*;
29 using size_type = std::size_t;
30 using difference_type = std::ptrdiff_t;
31};
32
33template<typename T>
34struct ArrayViewTraits<const T> {
35 using value_type = const T;
36 using reference = const T&;
37 using const_reference = const T&;
38 using pointer = const T*;
39 using const_pointer = const T*;
40 using size_type = std::size_t;
41 using difference_type = std::ptrdiff_t;
42};
43
54template<typename T>
55class ArrayView {
56 public:
64
65 ArrayView() = default;
66 ~ArrayView() noexcept = default;
67
68 ArrayView(const ArrayView&) = default;
69 ArrayView& operator=(const ArrayView&) = default;
70
71 ArrayView(ArrayView&&) = default;
72 ArrayView& operator=(ArrayView&&) = default;
73
75 ptr{src},
76 sz{size} {
77 }
78
79 ArrayView(std::nullptr_t /*unused*/, size_type /*unused*/) : ArrayView{nullptr, {}} {
80 }
81
82 template<typename U>
83 ArrayView(ArrayView<U>& src) : ArrayView{src.data(), src.size()} {
84 }
85
86 template<typename U>
87 ArrayView(const ArrayView<U>& src) : ArrayView{src.data(), src.size()} {
88 }
89
90 template<typename U>
91 ArrayView(ArrayView<U>&& src) : ArrayView{src.data(), src.size()} {
92 }
93
94 template<typename U, typename std::enable_if<!std::is_rvalue_reference<U &&>::value, int>::type = 0>
95 explicit ArrayView(U&& src) : ArrayView{src.data(), src.size()} {
96 }
97
98 size_type size() const {
99 return sz;
100 }
101
103 return ptr;
104 }
105
107 return ptr;
108 }
109
111 return ptr;
112 }
113
115 return ptr + sz;
116 }
117
119 return ptr;
120 }
121
123 return ptr + sz;
124 }
125
127 return cbegin();
128 }
129
131 return cend();
132 }
133
134 reference operator[](std::size_t index) {
135 assert(index < sz);
136 return ptr[index];
137 }
138
139 const_reference operator[](std::size_t index) const {
140 assert(index < sz);
141 return ptr[index];
142 }
143
144 reference at(std::size_t index) {
145 return this->operator[](index);
146 }
147
148 const_reference at(std::size_t index) const {
149 return this->operator[](index);
150 }
151
152 ArrayView subview(std::size_t offset, std::size_t count) const {
153 assert(offset <= sz);
154 assert((offset + count) <= sz);
155 return {ptr + offset, count};
156 }
157
158 ArrayView first(std::size_t count) const {
159 assert(count <= sz);
160 return {ptr, count};
161 }
162
163 ArrayView last(std::size_t count) const {
164 assert(count <= sz);
165 return {ptr + (sz - count), count};
166 }
167
168 private:
169 pointer ptr{nullptr};
171};
172
173template<typename T, typename U>
174bool operator==(const ArrayView<T>& lhs, const ArrayView<U>& rhs) {
175 if (lhs.size() != rhs.size()) {
176 return false;
177 }
178 if (lhs.data() == rhs.data()) {
179 return true;
180 }
181 #if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900)
182 // Under Visual Studio 2015, the overload of std::equal accepting 4 parameters must be used,
183 // because the 3-parameter version causes insuppressible warnings
184 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
185 #else
186 return std::equal(lhs.begin(), lhs.end(), rhs.begin());
187 #endif
188}
189
190template<typename T, typename U>
191bool operator!=(const ArrayView<T>& lhs, const ArrayView<U>& rhs) {
192 return !(lhs == rhs);
193}
194
195template<typename T, typename TContainer>
196bool operator==(const ArrayView<T>& lhs, const TContainer& rhs) {
197 if (lhs.size() != rhs.size()) {
198 return false;
199 }
200 #if __cplusplus >= 201402L || (defined(_MSC_VER) && _MSC_VER >= 1900)
201 // Under Visual Studio 2015, the overload of std::equal accepting 4 parameters must be used,
202 // because the 3-parameter version causes insuppressible warnings
203 return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
204 #else
205 return std::equal(lhs.begin(), lhs.end(), rhs.begin());
206 #endif
207}
208
209template<typename T, typename TContainer>
210bool operator!=(const ArrayView<T>& lhs, const TContainer& rhs) {
211 return !(lhs == rhs);
212}
213
214template<typename T, typename TContainer>
215bool operator==(const TContainer& lhs, const ArrayView<T>& rhs) {
216 return (rhs == lhs);
217}
218
219template<typename T, typename TContainer>
220bool operator!=(const TContainer& lhs, const ArrayView<T>& rhs) {
221 return !(lhs == rhs);
222}
223
224template<typename T>
226
227} // namespace trust
228
229#endif // TRUST_ARRAYVIEW_H
230// *INDENT-ON*
A view over a continuous sequence of objects.
Definition: ArrayView.h:55
typename ArrayViewTraits< T >::pointer pointer
Definition: ArrayView.h:61
~ArrayView() noexcept=default
const_pointer end() const
Definition: ArrayView.h:130
const_pointer data() const
Definition: ArrayView.h:106
typename ArrayViewTraits< T >::difference_type difference_type
Definition: ArrayView.h:63
ArrayView(ArrayView< U > &&src)
Definition: ArrayView.h:91
ArrayView subview(std::size_t offset, std::size_t count) const
Definition: ArrayView.h:152
pointer ptr
Definition: ArrayView.h:169
ArrayView(ArrayView< U > &src)
Definition: ArrayView.h:83
const_pointer begin() const
Definition: ArrayView.h:126
const_pointer cend() const
Definition: ArrayView.h:122
typename ArrayViewTraits< T >::const_reference const_reference
Definition: ArrayView.h:59
typename ArrayViewTraits< T >::value_type value_type
Definition: ArrayView.h:57
reference at(std::size_t index)
Definition: ArrayView.h:144
pointer data()
Definition: ArrayView.h:102
pointer end()
Definition: ArrayView.h:114
ArrayView(const ArrayView< U > &src)
Definition: ArrayView.h:87
pointer begin()
Definition: ArrayView.h:110
size_type size() const
Definition: ArrayView.h:98
typename ArrayViewTraits< T >::reference reference
Definition: ArrayView.h:58
ArrayView first(std::size_t count) const
Definition: ArrayView.h:158
ArrayView()=default
typename ArrayViewTraits< T >::size_type size_type
Definition: ArrayView.h:62
const_pointer cbegin() const
Definition: ArrayView.h:118
typename ArrayViewTraits< T >::const_pointer const_pointer
Definition: ArrayView.h:60
ArrayView last(std::size_t count) const
Definition: ArrayView.h:163
ArrayView(U &&src)
Definition: ArrayView.h:95
reference operator[](std::size_t index)
Definition: ArrayView.h:134
const_reference operator[](std::size_t index) const
Definition: ArrayView.h:139
const_reference at(std::size_t index) const
Definition: ArrayView.h:148
ArrayView(std::nullptr_t, size_type)
Definition: ArrayView.h:79
size_type sz
Definition: ArrayView.h:170
Definition: ArrayView.h:20
bool operator==(const TContainer &lhs, const ArrayView< T > &rhs)
Definition: ArrayView.h:215
bool operator!=(const TContainer &lhs, const ArrayView< T > &rhs)
Definition: ArrayView.h:220
const T & const_reference
Definition: ArrayView.h:37
std::size_t size_type
Definition: ArrayView.h:40
std::ptrdiff_t difference_type
Definition: ArrayView.h:41
const T value_type
Definition: ArrayView.h:35
const T * pointer
Definition: ArrayView.h:38
const T & reference
Definition: ArrayView.h:36
const T * const_pointer
Definition: ArrayView.h:39
Definition: ArrayView.h:23
T value_type
Definition: ArrayView.h:24
T & reference
Definition: ArrayView.h:25
const T & const_reference
Definition: ArrayView.h:26
const T * const_pointer
Definition: ArrayView.h:28
std::size_t size_type
Definition: ArrayView.h:29
T * pointer
Definition: ArrayView.h:27
std::ptrdiff_t difference_type
Definition: ArrayView.h:30