xtd 0.2.0
Loading...
Searching...
No Matches
__xtd_raw_array_data.hpp
Go to the documentation of this file.
1
4#pragma once
6#if !defined(__XTD_CORE_INTERNAL__)
7#error "Do not include this file: Internal use only"
8#endif
10
11#include <cstdint>
12#include <vector>
13
30template<class type_t, class allocator_t = std::allocator<type_t>>
31class __xtd_raw_array_data__ {
32public:
33 using value_type = type_t;
34 using storage_value_type = std::conditional_t<std::is_same_v<value_type, bool>, std::uint8_t, value_type >;
35 using storage_allocator_type = typename std::allocator_traits<allocator_t>::template rebind_alloc<storage_value_type>;
36 using base_type = std::vector<storage_value_type, storage_allocator_type>;
37 using const_base_type = const base_type;
38 using allocator_type = typename base_type::allocator_type;
39 using size_type = size_t;
40 using difference_type = std::ptrdiff_t;
41 using reference = value_type&;
42 using const_reference = const value_type&;
43 using pointer = value_type*;
44 using const_pointer = const value_type*;
45 //using iterator = typename base_type::iterator;
46 class iterator {
47 public:
48 using iterator_base_type = typename base_type::iterator;
49 using iterator_category = std::random_access_iterator_tag;
50 using value_type = type_t;
51 using difference_type = std::ptrdiff_t;
52 using pointer = type_t*;
53 using reference = type_t&;
54
55 iterator() = default;
56 explicit iterator(iterator_base_type it) : it_(it) {}
57
58 reference operator*() const { return reinterpret_cast<reference>(*it_); }
59 pointer operator->() const { return reinterpret_cast<pointer>(std::addressof(*it_)); }
60
61 iterator & operator++() { ++it_; return *this; }
62 iterator operator++(int) { iterator tmp(*this); ++(*this); return tmp; }
63 iterator & operator--() { --it_; return *this; }
64 iterator operator--(int) { iterator tmp(*this); --(*this); return tmp; }
65
66 iterator & operator+=(difference_type n) { it_ += n; return *this; }
67 iterator operator+(difference_type n) const { return iterator(it_ + n); }
68 iterator & operator-=(difference_type n) { it_ -= n; return *this; }
69 iterator operator-(difference_type n) const { return iterator(it_ - n); }
70 difference_type operator-(const iterator & other) const { return it_ - other.it_; }
71
72 reference operator[](difference_type n) const { return reinterpret_cast<reference>(it_[n]); }
73
74 bool operator==(const iterator & other) const { return it_ == other.it_; }
75 bool operator!=(const iterator & other) const { return it_ != other.it_; }
76 bool operator< (const iterator & other) const { return it_ < other.it_; }
77 bool operator<=(const iterator & other) const { return it_ <= other.it_; }
78 bool operator> (const iterator & other) const { return it_ > other.it_; }
79 bool operator>=(const iterator & other) const { return it_ >= other.it_; }
80
81 iterator_base_type to_base_type() const {return it_;}
82
83 private:
84 friend class __xtd_raw_array_data__;
85 iterator_base_type it_;
86 };
87 //using const_iterator = typename base_type::const_iterator;
88 class const_iterator {
89 public:
90 using iterator_base_type = typename base_type::const_iterator;
91 using iterator_category = std::random_access_iterator_tag;
92 using value_type = const type_t;
93 using difference_type = std::ptrdiff_t;
94 using pointer = const type_t*;
95 using reference = const type_t&;
96
97 const_iterator() = default;
98 explicit const_iterator(iterator_base_type it) : it_(it) {}
99 // conversion depuis iterator
100 const_iterator(const iterator & it) : it_(it.it_) {}
101
102 reference operator*() const { return reinterpret_cast<reference>(*it_); }
103 pointer operator->() const { return reinterpret_cast<pointer>(std::addressof(*it_)); }
104
105 const_iterator & operator++() { ++it_; return *this; }
106 const_iterator operator++(int) { const_iterator tmp(*this); ++(*this); return tmp; }
107 const_iterator & operator--() { --it_; return *this; }
108 const_iterator operator--(int) { const_iterator tmp(*this); --(*this); return tmp; }
109
110 const_iterator & operator+=(difference_type n) { it_ += n; return *this; }
111 const_iterator operator+(difference_type n) const { return const_iterator(it_ + n); }
112 const_iterator & operator-=(difference_type n) { it_ -= n; return *this; }
113 const_iterator operator-(difference_type n) const { return const_iterator(it_ - n); }
114 difference_type operator-(const const_iterator & other) const { return it_ - other.it_; }
115
116 reference operator[](difference_type n) const { return reinterpret_cast<reference>(it_[n]); }
117
118 bool operator==(const const_iterator & other) const { return it_ == other.it_; }
119 bool operator!=(const const_iterator & other) const { return it_ != other.it_; }
120 bool operator< (const const_iterator & other) const { return it_ < other.it_; }
121 bool operator<=(const const_iterator & other) const { return it_ <= other.it_; }
122 bool operator> (const const_iterator & other) const { return it_ > other.it_; }
123 bool operator>=(const const_iterator & other) const { return it_ >= other.it_; }
124
125 iterator_base_type to_base_type() const {return it_;}
126
127 private:
128 friend class __xtd_raw_array_data__;
129 iterator_base_type it_;
130 };
131 using reverse_iterator = std::reverse_iterator<iterator>;
132 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
133
134 __xtd_raw_array_data__() noexcept = default;
135 explicit __xtd_raw_array_data__(const allocator_type & alloc) noexcept : items_(alloc) {}
136 __xtd_raw_array_data__(size_type count, const type_t& value, const allocator_type& alloc = allocator_type()) : items_(count, value, alloc) {}
137 explicit __xtd_raw_array_data__(size_type count, const allocator_type& alloc = allocator_type()) : items_(count, alloc) {}
138 template<class input_iterator_t>
139 __xtd_raw_array_data__(input_iterator_t first, input_iterator_t last, const allocator_type& alloc = allocator_type()) : items_(first, last, alloc) {}
140 __xtd_raw_array_data__(const __xtd_raw_array_data__ & vector) : items_(vector.items_) {}
141 __xtd_raw_array_data__(const base_type & vector) : items_(vector) {}
142 __xtd_raw_array_data__(const __xtd_raw_array_data__ & vector, const allocator_type & alloc) : items_(vector.items_, alloc) {}
143 __xtd_raw_array_data__(const base_type & vector, const allocator_type & alloc) : items_(vector, alloc) {}
144 __xtd_raw_array_data__(std::initializer_list<type_t> items, const allocator_type& alloc = allocator_type()) requires(!std::is_same_v<type_t, bool>) : items_(items, alloc) {}
145 __xtd_raw_array_data__(std::initializer_list<bool> items, const allocator_type& alloc = allocator_type()) requires(std::is_same_v<type_t, bool>) : items_(alloc) {
146 items_.reserve(items.size());
147 for (auto b : items)
148 items_.push_back(b ? 1 : 0);
149 }
150 __xtd_raw_array_data__(__xtd_raw_array_data__&& other) : items_(std::move(other.items_)) {}
151 __xtd_raw_array_data__(base_type&& other) : items_(std::move(other)) {}
152 __xtd_raw_array_data__(__xtd_raw_array_data__&& other, const allocator_type & alloc) : items_(std::move(other.items_), alloc) {}
153 __xtd_raw_array_data__(base_type&& other, const allocator_type & alloc) : items_(std::move(other), alloc) {}
154
155 reference back() {return at(size() - 1);}
156 const_reference back() const {return at(size() - 1);}
157
158 iterator begin() noexcept {return to_type_iterator(items_.begin());}
159 const_iterator begin() const noexcept {return to_type_iterator(items_.cbegin());}
160
161 size_type capacity() const noexcept {return items_.capacity();}
162
163 const_iterator cbegin() const noexcept {return to_type_iterator(items_.cbegin());}
164 const_iterator cend() const noexcept {return to_type_iterator(items_.cend()); }
165
166 const_reverse_iterator crbegin() const noexcept {return const_reverse_iterator(end());}
167 const_reverse_iterator crend() const noexcept {return const_reverse_iterator(begin());}
168
169 pointer data() noexcept {return reinterpret_cast<pointer>(items_.data());}
170 const_pointer data() const noexcept {return reinterpret_cast<const_pointer>(items_.data());}
171
172 bool empty() const noexcept {return items_.empty();}
173
174 iterator end() noexcept {return to_type_iterator(items_.end());}
175 const_iterator end() const noexcept {return to_type_iterator(items_.cend());}
176
177 reference front() {return at(0);}
178 const_reference front() const {return at(0);}
179
180 const_base_type & items() const noexcept {return items_;}
181 base_type & items() noexcept {return items_;}
182
183 size_type max_size() const noexcept {return items_.max_size();}
184
185 reverse_iterator rbegin() noexcept {return reverse_iterator(end());}
186 const_reverse_iterator rbegin() const noexcept {return const_reverse_iterator(end());}
187
188 reverse_iterator rend() noexcept {return reverse_iterator(begin());}
189 const_reverse_iterator rend() const noexcept {return const_reverse_iterator(begin());}
190
191 size_type size() const noexcept {return items_.size();}
192
193 void assign(size_type count, const type_t& value) {items_.assign(count, value);}
194 template<class input_iterator_t>
195 void assign(input_iterator_t first, input_iterator_t last) {items_.assign(first, last);}
196 void assign(std::initializer_list<type_t> items) {items_.assign(items.begin(), items.end());}
197
198 reference at(size_type index) {return reinterpret_cast<reference>(items_.at(index));}
199 const_reference at(size_type index) const {return reinterpret_cast<const_reference>(items_.at(index));}
200
201 void clear() {items_.clear();}
202
203 template<class ...args_t>
204 iterator emplace(const_iterator pos, args_t&&... args) {return to_type_iterator(items_.emplace(pos.to_base_type(), std::forward<args_t>(args)...));}
205 template<class ...args_t>
206 reference emplace_back(args_t&&... args) {return reinterpret_cast<reference>(items_.emplace_back(std::forward<args_t>(args)...));}
207
208 iterator erase(const_iterator pos) {return to_type_iterator(items_.erase(pos.to_base_type()));}
209 iterator erase(const_iterator first, const_iterator last) {return to_type_iterator(items_.erase(first.to_base_type(), last.to_base_type()));}
210
211 allocator_type get_allocator() const {return items_.get_allocator();}
212
213 base_type & get_base_type() noexcept {return items_;}
214 const base_type & get_base_type() const noexcept {return items_;}
215
216 iterator insert(const_iterator pos, const type_t& value) {return to_type_iterator(items_.insert(pos.to_base_type(), value));}
217 iterator insert(const_iterator pos, type_t&& value) {return to_type_iterator(items_.insert(pos.to_base_type(), std::move(value)));}
218 iterator insert(const_iterator pos, size_type count, const type_t& value) {return to_type_iterator(items_.insert(pos.to_base_type(), count, value));}
219 iterator insert(const_iterator pos, size_type count, type_t&& value) {return to_type_iterator(items_.insert(pos.to_base_type(), count, std::move(value)));}
220 template<class input_iterator_t>
221 iterator insert(const_iterator pos, input_iterator_t first, input_iterator_t last) {return to_type_iterator(items_.insert(pos.to_base_type(), first, last));}
222 iterator insert(const_iterator pos, const std::initializer_list<type_t>& items) {return to_type_iterator(items_.insert(pos.to_base_type(), items.begin(), items.end()));}
223
224 void pop_back() {items_.pop_back();}
225 void push_back(const type_t& value) {items_.push_back(value);}
226 void push_back(type_t&& value) {items_.push_back(std::move(value));}
227
228 void reserve(size_type new_cap) {items_.reserve(new_cap);}
229
230 void resize(size_type count) {resize(count, value_type {});}
231 void resize(size_type count, const value_type & value) {items_.resize(count, value);}
232
233 void shrink_to_fit() {items_.shrink_to_fit();}
234
235 void swap(__xtd_raw_array_data__ & other) noexcept {items_.swap(other.items_);}
236
237 __xtd_raw_array_data__& operator =(const __xtd_raw_array_data__ & other) = default;
238 __xtd_raw_array_data__& operator =(__xtd_raw_array_data__&& other) noexcept = default;
239 __xtd_raw_array_data__& operator =(std::initializer_list<type_t>& items) requires(!std::is_same_v<type_t, bool>) {
240 items_ = items;
241 return *this;
242 }
243 __xtd_raw_array_data__& operator =(std::initializer_list<bool>& items) requires(std::is_same_v<type_t, bool>) {
244 items_.clear();
245 items_.reserve(items.size());
246 for (auto b : items)
247 items_.push_back(b ? 1 : 0);
248 return *this;
249 }
250
251 const_reference operator [](size_type index) const {return at(index);}
252 reference operator [](size_type index) {return at(index);}
253
254 operator const base_type & () const noexcept {return items_;}
255 operator base_type & () noexcept {return items_;}
256
257private:
258 iterator to_type_iterator(typename base_type::iterator it) { return iterator(it); }
259 const_iterator to_type_iterator(typename base_type::const_iterator it) const { return const_iterator(it); }
260
261 base_type items_;
262};
263
264template<class type_t>
265__xtd_raw_array_data__(std::initializer_list<type_t>) -> __xtd_raw_array_data__<type_t, std::allocator<type_t >>;
266
267template<class type_t>
268__xtd_raw_array_data__(const std::vector<type_t>&) -> __xtd_raw_array_data__<type_t, std::allocator<type_t >>;
269
270template<class type_t, class allocator_t = std::allocator<type_t>>
271__xtd_raw_array_data__(const __xtd_raw_array_data__<type_t, allocator_t>&) -> __xtd_raw_array_data__<type_t, allocator_t>;
272
273template<class type_t>
274__xtd_raw_array_data__(std::vector<type_t>&&) -> __xtd_raw_array_data__<type_t, std::allocator<type_t >>;
275
276template<class type_t, class allocator_t = std::allocator<type_t>>
277__xtd_raw_array_data__(__xtd_raw_array_data__<type_t, allocator_t>&&) -> __xtd_raw_array_data__<type_t, allocator_t>;
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ other
The operating system is other.
Definition platform_id.hpp:58
@ clear
The CLEAR key.
Definition console_key.hpp:26
@ n
The N key.
Definition console_key.hpp:114
@ insert
The INS (INSERT) key.
Definition console_key.hpp:62
size_type
Specifies how rows or columns of user interface (UI) elements should be sized relative to their conta...
Definition size_type.hpp:22
const_reference front() const
Gets the first element.
Definition read_only_span.hpp:218
const_reverse_iterator rend() const
Returns a reverse iterator to the end.
Definition read_only_span.hpp:237
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183
const_iterator cbegin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:187
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213
read_only_span< type_t, count > last() const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:307
const_iterator cend() const
Returns an iterator to the end.
Definition read_only_span.hpp:190
const_reverse_iterator rbegin() const
Returns a reverse iterator to the beginning.
Definition read_only_span.hpp:233
const_reverse_iterator crbegin() const
Returns a reverse iterator to the beginning.
Definition read_only_span.hpp:194
const_reference at(size_type pos) const
Gets the specified element with bounds checking.
Definition read_only_span.hpp:255
const_reference back() const
Gets the last element.
Definition read_only_span.hpp:176
constexpr const_pointer data() const noexcept
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:201
constexpr bool empty() const noexcept
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:205
const_reverse_iterator crend() const
Returns a reverse iterator to the end.
Definition read_only_span.hpp:197