xtd 1.0.0
Loading...
Searching...
No Matches
raw_array.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <algorithm>
6#include <cstdint>
7#include <limits>
8#include <vector>
10
12namespace xtd {
14 namespace collections {
16 namespace generic {
18 namespace helpers {
29 template<typename type_t, typename allocator_t = std::allocator<type_t>>
30 class raw_array {
31 using internal_storage_value_type = std::conditional_t<std::is_same_v<type_t, bool>, std::uint8_t, type_t>;
32 using internal_storage_allocator_type = typename std::allocator_traits<allocator_t>::template rebind_alloc<internal_storage_value_type>;
33 using internal_base_type = std::vector<internal_storage_value_type, internal_storage_allocator_type>;
34
35 class internal_iterator {
36 public:
37 using iterator_base_type = typename internal_base_type::iterator;
38 using iterator_category = std::random_access_iterator_tag;
39 using value_type = type_t;
40 using difference_type = std::ptrdiff_t;
41 using pointer = type_t*;
42 using reference = type_t&;
43
44 internal_iterator() = default;
45 explicit internal_iterator(iterator_base_type it) : it_(it) {}
46
47 reference operator *() const {return reinterpret_cast<reference>(*it_);}
48 pointer operator ->() const {return reinterpret_cast<pointer>(std::addressof(*it_));}
49
50 internal_iterator& operator ++() {++it_; return *this;}
51 internal_iterator operator ++(int) {internal_iterator tmp(*this); ++(*this); return tmp;}
52 internal_iterator& operator --() {--it_; return *this;}
53 internal_iterator operator --(int) {internal_iterator tmp(*this); --(*this); return tmp;}
54
55 internal_iterator& operator +=(difference_type n) {it_ += n; return *this;}
56 internal_iterator operator +(difference_type n) const {return internal_iterator(it_ + n);}
57 internal_iterator& operator -=(difference_type n) {it_ -= n; return *this;}
58 internal_iterator operator -(difference_type n) const {return internal_iterator(it_ - n);}
59 difference_type operator -(const internal_iterator & other) const {return it_ - other.it_;}
60
61 reference operator [](difference_type n) const {return reinterpret_cast<reference>(it_[n]);}
62
63 bool operator ==(const internal_iterator & other) const {return it_ == other.it_;}
64 bool operator !=(const internal_iterator & other) const {return it_ != other.it_;}
65 bool operator <(const internal_iterator & other) const {return it_ < other.it_;}
66 bool operator <=(const internal_iterator & other) const {return it_ <= other.it_;}
67 bool operator >(const internal_iterator & other) const {return it_ > other.it_;}
68 bool operator >=(const internal_iterator & other) const {return it_ >= other.it_;}
69
70 iterator_base_type to_base_type() const {return it_;}
71
72 private:
73 friend class raw_array;
74 iterator_base_type it_;
75 };
76
77 class internal_const_iterator {
78 public:
79 using iterator_base_type = typename internal_base_type::const_iterator;
80 using iterator_category = std::random_access_iterator_tag;
81 using value_type = const type_t;
82 using difference_type = std::ptrdiff_t;
83 using pointer = const type_t*;
84 using reference = const type_t&;
85
86 internal_const_iterator() = default;
87 explicit internal_const_iterator(iterator_base_type it) : it_(it) {}
88 internal_const_iterator(const internal_iterator & it) : it_(it.it_) {}
89
90 reference operator*() const {return reinterpret_cast<reference>(*it_);}
91 pointer operator->() const {return reinterpret_cast<pointer>(std::addressof(*it_));}
92
93 internal_const_iterator& operator ++() {++it_; return *this;}
94 internal_const_iterator operator ++(int) {internal_const_iterator tmp(*this); ++(*this); return tmp;}
95 internal_const_iterator& operator --() {--it_; return *this;}
96 internal_const_iterator operator --(int) {internal_const_iterator tmp(*this); --(*this); return tmp;}
97
98 internal_const_iterator& operator +=(difference_type n) {it_ += n; return *this;}
99 internal_const_iterator operator +(difference_type n) const {return internal_const_iterator(it_ + n);}
100 internal_const_iterator& operator -=(difference_type n) {it_ -= n; return *this;}
101 internal_const_iterator operator -(difference_type n) const {return internal_const_iterator(it_ - n);}
102 difference_type operator -(const internal_const_iterator & other) const {return it_ - other.it_;}
103
104 reference operator [](difference_type n) const { return reinterpret_cast<reference>(it_[n]);}
105
106 bool operator ==(const internal_const_iterator & other) const {return it_ == other.it_;}
107 bool operator !=(const internal_const_iterator & other) const {return it_ != other.it_;}
108 bool operator <(const internal_const_iterator & other) const {return it_ < other.it_;}
109 bool operator <=(const internal_const_iterator & other) const {return it_ <= other.it_;}
110 bool operator >(const internal_const_iterator & other) const {return it_ > other.it_;}
111 bool operator >=(const internal_const_iterator & other) const {return it_ >= other.it_;}
112
113 iterator_base_type to_base_type() const {return it_;}
114
115 private:
116 friend class raw_array;
117 iterator_base_type it_;
118 };
119 public:
121
123 using value_type = type_t;
124 using base_type = internal_base_type;
126 using allocator_type = typename base_type::allocator_type;
127 using size_type = std::size_t;
128 using difference_type = std::ptrdiff_t;
132 using const_pointer = const value_type*;
133 using iterator = internal_iterator;
134 using const_iterator = internal_const_iterator;
135 using reverse_iterator = std::reverse_iterator<iterator>;
136 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
138
140
142 inline static constexpr size_type npos = std::numeric_limits<size_type>::max();
143 static inline constexpr size_type bpos = 0;
144 static inline constexpr size_type epos = npos - 1;
146
148
151 raw_array() noexcept = default;
154 explicit raw_array(const allocator_type & alloc) noexcept : items_(alloc) {}
159 raw_array(size_type count, const type_t& value, const allocator_type& alloc = allocator_type()) : items_(count, value, alloc) {}
163 explicit raw_array(size_type count, const allocator_type& alloc = allocator_type()) : items_(count, alloc) {}
168 template<typename input_iterator_t>
169 raw_array(input_iterator_t first, input_iterator_t last, const allocator_type& alloc = allocator_type()) : items_(first, last, alloc) {}
170 raw_array(const raw_array & vector) : items_(vector.items_) {}
171 raw_array(const base_type & vector) : items_(vector) {}
172 raw_array(const raw_array & vector, const allocator_type & alloc) : items_(vector.items_, alloc) {}
173 raw_array(const base_type & vector, const allocator_type & alloc) : items_(vector, alloc) {}
174 raw_array(std::initializer_list<type_t> items, const allocator_type& alloc = allocator_type()) requires(!std::is_same_v<type_t, bool>) : items_(items, alloc) {}
175 raw_array(std::initializer_list<bool> items, const allocator_type& alloc = allocator_type()) requires(std::is_same_v<type_t, bool>) : items_(alloc) {
176 items_.reserve(items.size());
177 for (const auto& b : items)
178 items_.push_back(b ? 1 : 0);
179 }
180 raw_array(raw_array&& other) : items_(std::move(other.items_)) {}
181 raw_array(base_type&& other) : items_(std::move(other)) {}
182 template<xtd::forward_iterable source_t>
183 raw_array(source_t&& other) : items_(std::move(other)) {}
184 raw_array(raw_array&& other, const allocator_type & alloc) : items_(std::move(other.items_), alloc) {}
185 raw_array(base_type&& other, const allocator_type & alloc) : items_(std::move(other), alloc) {}
187
189
191 [[nodiscard]] auto back() -> reference {return at(size() - 1);}
192 [[nodiscard]] auto back() const -> const_reference {return at(size() - 1);}
193
194 [[nodiscard]] auto begin() noexcept -> iterator {return to_type_iterator(items_.begin());}
195 [[nodiscard]] auto begin() const noexcept -> const_iterator {return to_type_iterator(items_.cbegin());}
196
197 [[nodiscard]] auto capacity() const noexcept -> size_type {return items_.capacity();}
198
199 [[nodiscard]] auto cbegin() const noexcept -> const_iterator {return to_type_iterator(items_.cbegin());}
200 [[nodiscard]] auto cend() const noexcept -> const_iterator {return to_type_iterator(items_.cend()); }
201
202 [[nodiscard]] auto crbegin() const noexcept -> const_reverse_iterator {return const_reverse_iterator(end());}
203 [[nodiscard]] auto crend() const noexcept -> const_reverse_iterator {return const_reverse_iterator(begin());}
204
205 [[nodiscard]] auto data() noexcept -> pointer {return reinterpret_cast<pointer>(items_.data());}
206 [[nodiscard]] auto data() const noexcept -> const_pointer {return reinterpret_cast<const_pointer>(items_.data());}
207
208 [[nodiscard]] auto empty() const noexcept -> bool {return items_.empty();}
209
210 [[nodiscard]] auto end() noexcept -> iterator {return to_type_iterator(items_.end());}
211 [[nodiscard]] auto end() const noexcept -> const_iterator {return to_type_iterator(items_.cend());}
212
213 [[nodiscard]] auto front() -> reference {return at(0);}
214 [[nodiscard]] auto front() const -> const_reference {return at(0);}
215
216 [[nodiscard]] auto items() const noexcept -> const_base_type& {return items_;}
217 [[nodiscard]] auto items() noexcept -> base_type& {return items_;}
218
219 [[nodiscard]] auto max_size() const noexcept -> size_type {return std::min(items_.max_size(), npos / 2);}
220
221 [[nodiscard]] auto rbegin() noexcept -> reverse_iterator {return reverse_iterator(end());}
222 [[nodiscard]] auto rbegin() const noexcept -> const_reverse_iterator {return const_reverse_iterator(end());}
223
224 [[nodiscard]] auto rend() noexcept -> reverse_iterator {return reverse_iterator(begin());}
225 [[nodiscard]] auto rend() const noexcept -> const_reverse_iterator {return const_reverse_iterator(begin());}
226
227 [[nodiscard]] auto size() const noexcept -> size_type {return items_.size();}
228
229 [[nodiscard]] auto version() const noexcept -> size_type {return version_;}
231
233
235 auto assign(size_type count, const type_t& value) -> void {++version_; items_.assign(count, value);}
236 template<typename input_iterator_t>
237 auto assign(input_iterator_t first, input_iterator_t last) -> void {++version_; items_.assign(first, last);}
238 auto assign(std::initializer_list<type_t> items) -> void {++version_; items_.assign(items.begin(), items.end());}
239
240 auto at(size_type index) -> reference {return reinterpret_cast<reference>(items_.at(index > npos / 2 ? size() - (npos - index) : index));}
241 auto at(size_type index) const -> const_reference {return reinterpret_cast<const_reference>(items_.at(index > npos / 2 ? size() - (npos - index) : index));}
242
243 auto clear() -> void {++version_; items_.clear();}
244
245 template<typename ...args_t>
246 auto emplace(const_iterator pos, args_t&&... args) -> iterator {++version_; return to_type_iterator(items_.emplace(pos.to_base_type(), std::forward<args_t>(args)...));}
247 template<typename ...args_t>
248 auto emplace_back(args_t&&... args) -> reference {++version_; return reinterpret_cast<reference>(items_.emplace_back(std::forward<args_t>(args)...));}
249
250 auto erase(const_iterator pos) -> iterator {++version_; return to_type_iterator(items_.erase(pos.to_base_type()));}
251 auto erase(const_iterator first, const_iterator last) -> iterator {++version_; return to_type_iterator(items_.erase(first.to_base_type(), last.to_base_type()));}
252
253 auto get_allocator() const -> allocator_type {return items_.get_allocator();}
254
255 auto increment_version() noexcept -> size_type {return ++version_;}
256
257 auto insert(const_iterator pos, const type_t& value) -> iterator {++version_; return to_type_iterator(items_.insert(pos.to_base_type(), value));}
258 auto insert(const_iterator pos, type_t&& value) -> iterator {++version_; return to_type_iterator(items_.insert(pos.to_base_type(), std::move(value)));}
259 auto insert(const_iterator pos, size_type count, const type_t& value) -> iterator {++version_; return to_type_iterator(items_.insert(pos.to_base_type(), count, value));}
260 auto insert(const_iterator pos, size_type count, type_t&& value) -> iterator {++version_; return to_type_iterator(items_.insert(pos.to_base_type(), count, std::move(value)));}
261 template<typename input_iterator_t>
262 auto insert(const_iterator pos, input_iterator_t first, input_iterator_t last) -> iterator {++version_; return to_type_iterator(items_.insert(pos.to_base_type(), first, last));}
263 auto insert(const_iterator pos, const std::initializer_list<type_t>& items) -> iterator {++version_; return to_type_iterator(items_.insert(pos.to_base_type(), items.begin(), items.end()));}
264
265 auto pop_back() -> void {++version_; items_.pop_back();}
266 auto push_back(const type_t& value) -> void {++version_; items_.push_back(value);}
267 auto push_back(type_t&& value) -> void {++version_; items_.push_back(std::move(value));}
268
269 auto reserve(size_type new_cap) -> void {++version_; items_.reserve(new_cap);}
270
271 auto resize(size_type count) -> void {++version_; resize(count, value_type {});}
272 auto resize(size_type count, const value_type & value) -> void {++version_; items_.resize(count, value);}
273
274 auto shrink_to_fit() -> void {++version_; items_.shrink_to_fit();}
275
276 auto swap(raw_array & other) noexcept -> void {++version_; items_.swap(other.items_);}
278
281 auto operator =(const raw_array & other) -> raw_array& = default;
282 auto operator =(raw_array&& other) noexcept -> raw_array& = default;
283 auto operator =(std::initializer_list<type_t>& items) -> raw_array& requires(!std::is_same_v<type_t, bool>) {
284 ++version_;
285 items_ = items;
286 return *this;
287 }
288 auto operator =(std::initializer_list<bool>& items) -> raw_array& requires(std::is_same_v<type_t, bool>) {
289 ++version_;
290 items_.clear();
291 items_.reserve(items.size());
292 for (const auto& b : items)
293 items_.push_back(b ? 1 : 0);
294 return *this;
295 }
296
297 friend auto operator ==(const raw_array<type_t>& a, const raw_array<type_t>& b) -> bool {
298 return a.items() == b.items();
299 }
300 friend auto operator ==(const raw_array<type_t>& a, const std::vector<type_t>& b) -> bool {
301 return a.items() == b;
302 }
303 friend auto operator ==(const std::vector<type_t>& a, const raw_array<type_t>& b) -> bool {
304 return a == b.items();
305 }
306
307 friend auto operator !=(const raw_array<type_t>& a, const raw_array<type_t>& b) -> bool {
308 return a.items() != b.items();
309 }
310 friend auto operator !=(const raw_array<type_t>& a, const std::vector<type_t>& b) -> bool {
311 return a.items() != b;
312 }
313 friend auto operator !=(const std::vector<type_t>& a, const raw_array<type_t>& b) -> bool {
314 return a != b.items();
315 }
316
317 auto operator [](size_type index) const -> const_reference {return at(index);}
318 auto operator [](size_type index) -> reference {return at(index);}
319
320 operator const base_type& () const noexcept {return items_;}
321 operator base_type& () noexcept {return items_;}
323
324 private:
325 auto to_type_iterator(typename base_type::iterator it) -> iterator { return iterator(it); }
326 auto to_type_iterator(typename base_type::const_iterator it) const -> const_iterator { return const_iterator(it); }
327
328 base_type items_;
329 size_type version_ = 0;
330 };
331
333 // Deduction guides for xtd::collections::generic::helpers::raw_array
334 // {
335 template<typename type_t>
336 raw_array(std::initializer_list<type_t>) -> raw_array<type_t, std::allocator<type_t >>;
337
338 template<typename type_t>
339 raw_array(const std::vector<type_t>&) -> raw_array<type_t, std::allocator<type_t >>;
340
341 template<typename type_t, typename allocator_t = std::allocator<type_t>>
343
344 template<typename type_t>
345 raw_array(std::vector<type_t>&&) -> raw_array<type_t, std::allocator<type_t >>;
346
347 template<typename type_t, typename allocator_t = std::allocator<type_t>>
349 // }
351 }
352 }
353 }
354}
Internal vector-like container used as a storage backend for xtd collections.
Definition raw_array.hpp:30
const value_type & const_reference
Const reference to element.
Definition raw_array.hpp:130
raw_array(size_type count, const allocator_type &alloc=allocator_type())
Create a new raw_array instance with specified count, and allocator.
Definition raw_array.hpp:163
internal_const_iterator const_iterator
Const forward iterator for raw_array.
Definition raw_array.hpp:134
std::ptrdiff_t difference_type
Type used for differences between iterators.
Definition raw_array.hpp:128
static constexpr size_type npos
Represents an invalid index.
Definition raw_array.hpp:142
raw_array(size_type count, const type_t &value, const allocator_type &alloc=allocator_type())
Create a new raw_array instance with specified count, value, and allocator.
Definition raw_array.hpp:159
internal_base_type base_type
Underlying vector type.
Definition raw_array.hpp:124
internal_iterator iterator
Forward iterator for raw_array.
Definition raw_array.hpp:133
value_type * pointer
Pointer to element.
Definition raw_array.hpp:131
const base_type const_base_type
Const version of base_type.
Definition raw_array.hpp:125
raw_array(input_iterator_t first, input_iterator_t last, const allocator_type &alloc=allocator_type())
Create a new raw_array instance with specified first iterator, last iterator, and allocator.
Definition raw_array.hpp:169
std::size_t size_type
Type used for sizes.
Definition raw_array.hpp:127
static constexpr size_type bpos
Beginning position.
Definition raw_array.hpp:143
static constexpr size_type epos
End position.
Definition raw_array.hpp:144
value_type & reference
Reference to element.
Definition raw_array.hpp:129
std::reverse_iterator< const_iterator > const_reverse_iterator
Const reverse iterator.
Definition raw_array.hpp:136
typename base_type::allocator_type allocator_type
Allocator type.
Definition raw_array.hpp:126
type_t value_type
Type of the elements.
Definition raw_array.hpp:123
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator.
Definition raw_array.hpp:135
raw_array() noexcept=default
Create a new raw_array instance.
const value_type * const_pointer
Const pointer to element.
Definition raw_array.hpp:132
Contains xtd::forward_iterable concept.
@ other
The operating system is other.
Definition platform_id.hpp:60
@ a
The A key.
Definition console_key.hpp:88
@ n
The N key.
Definition console_key.hpp:114
@ b
The B key.
Definition console_key.hpp:90
The xtd::collections::generic::helpers namespace contains helpers for generic collections,...
Definition allocator.hpp:14
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:248
constexpr auto empty() const noexcept -> bool
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:200
constexpr auto data() const noexcept -> const_pointer
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:196
auto last() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:273
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition read_only_span.hpp:216