xtd 1.0.0
Loading...
Searching...
No Matches
array_.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#if !defined(__XTD_ARRAY_INTERNAL__)
7#error "Do not include this file: Internal use only. Include <xtd/array> or <xtd/array.hpp> instead."
8#endif
9
10#include "index.hpp"
11#include "string.hpp"
13#include <numeric>
14
16namespace xtd {
17 // Deduction guides for xtd::array
18 // {
19 template<typename type_t, xtd::usize length>
20 array(const type_t(&)[length]) -> array<type_t, 1>;
21
22 template<typename type_t>
23 array(const type_t*, xtd::usize) -> array<type_t, 1>;
24
25 template<typename type_t>
26 array(const xtd::collections::generic::ienumerable<type_t>&) -> array<type_t, 1>;
27
28 template<typename type_t>
29 array(const xtd::collections::generic::ilist<type_t>&) -> array<type_t, 1>;
30
31 template<typename input_iterator_t>
32 array(input_iterator_t, input_iterator_t) -> array<std::iter_value_t<input_iterator_t>, 1>;
33
34 template<typename type_t>
35 array(const std::vector<type_t>&) -> array<type_t, 1>;
36
37 template<typename type_t>
38 array(std::vector<type_t>&&) -> array<type_t, 1>;
39
40 template<typename type_t>
41 array(std::vector<std::vector<type_t>>) -> array<type_t, 2>;
42
43 template<typename type_t>
44 array(std::vector<std::vector<std::vector<type_t>>>) -> array<type_t, 3>;
45
46 template<typename type_t>
47 array(std::initializer_list<type_t>) -> array<type_t, 1>;
48
49 template<typename type_t>
50 array(std::initializer_list<std::initializer_list<type_t>>) -> array<type_t, 2>;
51
52 template<typename type_t>
53 array(std::initializer_list<std::initializer_list<std::initializer_list<type_t>>>) -> array<type_t, 3>;
54 // }
55}
56
57template<typename type_t, typename allocator_t>
58inline const type_t& xtd::basic_array<type_t, allocator_t>::get_value(const xtd::array<xtd::usize>& indexes) const {
59 return operator()(indexes);
60}
61
62template<typename type_t, typename allocator_t>
64 auto result = xtd::array<xtd::usize, 1>(rank());
65 for (auto r = xtd::usize {}; r < rank(); ++r)
66 result[r] = get_length(r);
67 return result;
68}
69
70template<typename type_t, typename allocator_t>
72 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
73}
74
75template<typename type_t, typename allocator_t>
76template<xtd::integer index_t>
78 return data_->items[compute_index(self_, indexes)];
79}
80
81template<typename type_t, typename allocator_t>
82template<xtd::integer index_t>
83inline const type_t& xtd::basic_array<type_t, allocator_t>::operator [](const xtd::array<index_t>& indexes) const {
84 return data_->items[compute_index(self_, indexes)];
85}
86
87template<typename type_t, typename allocator_t>
88template<xtd::integer index_t>
89inline type_t& xtd::basic_array<type_t, allocator_t>::operator [](const std::initializer_list<index_t>& indexes) {
90 return data_->items[compute_index(self_, xtd::array<index_t> {indexes})];
91}
92
93template<typename type_t, typename allocator_t>
94template<xtd::integer index_t>
95inline const type_t& xtd::basic_array<type_t, allocator_t>::operator [](const std::initializer_list<index_t>& indexes) const {
96 return data_->items[compute_index(self_, xtd::array<index_t> {indexes})];
97}
98
99template<typename type_t, typename allocator_t>
100template<xtd::integer index_t>
102 return operator [](indexes);
103}
104
105template<typename type_t, typename allocator_t>
106template<xtd::integer index_t>
107inline const type_t& xtd::basic_array<type_t, allocator_t>::operator()(const xtd::array<index_t>& indexes) const {
108 return operator [](indexes);
109}
110
111template<typename type_t, typename allocator_t>
112template<xtd::integer index_t>
113inline type_t& xtd::basic_array<type_t, allocator_t>::operator ()(const std::initializer_list<index_t>& indexes) {
114 return data_->items[compute_index(self_, xtd::array<index_t> {indexes})];
115}
116
117template<typename type_t, typename allocator_t>
118template<xtd::integer index_t>
119inline const type_t& xtd::basic_array<type_t, allocator_t>::operator ()(const std::initializer_list<index_t>& indexes) const {
120 return data_->items[compute_index(self_, xtd::array<index_t> {indexes})];
121}
122
123template<typename type_t, typename allocator_t>
124inline xtd::basic_array<type_t, allocator_t>::basic_array(const array<size_type, 1>& lengths) {
125 data_->items = base_type(lengths.aggregate([&](const size_type & accumulator, const size_type & value) {return accumulator * value;}));
126 data_->lower_bound.clear();
127 data_->upper_bound.clear();
128 for (auto length : lengths) {
129 data_->lower_bound.push_back(0);
130 data_->upper_bound.push_back(length - 1);
131 }
132}
133
134template<typename type_t, typename allocator_t>
135inline xtd::basic_array<type_t, allocator_t>::basic_array(const array<size_type, 1>& lengths, const value_type& value) {
136 data_->items = base_type(lengths.aggregate([&](const size_type & accumulator, const size_type & value) {return accumulator * value;}), value);
137 data_->lower_bound.clear();
138 data_->upper_bound.clear();
139 for (auto length : lengths) {
140 data_->lower_bound.push_back(0);
141 data_->upper_bound.push_back(length - 1);
142 }
143}
144
145template<typename type_t, typename allocator_t>
146template<typename value_t, xtd::integer index_t>
147xtd::usize xtd::basic_array<type_t, allocator_t>::compute_index(const xtd::basic_array<value_t>& items, const xtd::array <index_t>& indexes) {
148 auto position = xtd::usize {0};
149 for (auto index1 = xtd::usize {0}; index1 < indexes.length(); ++index1) {
151 auto multiplier = xtd::usize {1};
152 for (auto index2 = index1 + 1; index2 < indexes.length(); ++index2)
153 multiplier *= items.get_length(index2);
154 position += static_cast<size_type>(indexes[index1]) * multiplier;
155 }
157 return position;
158}
159
160template<typename type_t, typename allocator_t>
161template<typename value_t, xtd::integer index_t>
162xtd::usize xtd::basic_array<type_t, allocator_t>::compute_index(const xtd::basic_array<value_t>& items, index_t rank, index_t index) {
163 auto relative = index - items.get_lower_bound(rank);
164 auto multiplier = xtd::usize {1};
165 for (auto r = rank + 1; r < items.rank(); ++r)
166 multiplier *= items.get_length(r);
168 return relative * multiplier;
169}
170
171template<typename type_t, typename allocator_t>
172template<typename value_t>
174 if (!items.size()) return "[]";
175 auto result = xtd::string {"["};
176 for (auto index = items.get_lower_bound(rank); index <= items.get_upper_bound(rank); ++index) {
177 if (index != items.get_lower_bound(rank)) result += ", ";
178 auto offset = base_index + compute_index(items, rank, index);
179 if (rank + 1 < items.rank()) result += to_string(items, rank + 1, offset);
180 else result += xtd::string::format("{}", items[offset]);
181 }
182 result += "]";
183 return result;
184}
185
186template<typename type_t, typename allocator_t>
188 return xtd::collections::object_model::read_only_collection<type_t> {array};
189}
190
191template<typename type_t, xtd::usize rank_, typename allocator_t>
194}
195
196template<typename type_t, typename allocator_t>
199}
200
201template<typename type_t, typename allocator_t>
204}
205
206template<typename type_t, typename allocator_t>
209}
210
211template<typename type_t, typename allocator_t>
212inline type_t& xtd::basic_array<type_t, allocator_t>::operator [](const xtd::array<xtd::index>& indexes) {
213 return data_->items[compute_index(self_, indexes)];
214}
215
216template<typename type_t, typename allocator_t>
217inline const type_t& xtd::basic_array<type_t, allocator_t>::operator [](const xtd::array<xtd::index>& indexes) const {
218 return data_->items[compute_index(self_, indexes)];
219}
220
221template<typename type_t, typename allocator_t>
222inline type_t& xtd::basic_array<type_t, allocator_t>::operator [](const std::initializer_list<xtd::index>& indexes) {
223 return data_->items[compute_index(self_, xtd::array<xtd::index> {indexes})];
224}
225
226template<typename type_t, typename allocator_t>
227inline const type_t& xtd::basic_array<type_t, allocator_t>::operator [](const std::initializer_list<xtd::index>& indexes) const {
228 return data_->items[compute_index(self_, xtd::array<xtd::index> {indexes})];
229}
230
231template<typename type_t, typename allocator_t>
232inline type_t& xtd::basic_array<type_t, allocator_t>::operator ()(const xtd::array<xtd::index>& indexes) {
233 return data_->items[compute_index(self_, indexes)];
234}
235
236template<typename type_t, typename allocator_t>
237inline const type_t& xtd::basic_array<type_t, allocator_t>::operator ()(const xtd::array<xtd::index>& indexes) const {
238 return data_->items[compute_index(self_, indexes)];
239}
240
241template<typename type_t, typename allocator_t>
242inline type_t& xtd::basic_array<type_t, allocator_t>::operator ()(const std::initializer_list<xtd::index>& indexes) {
243 return data_->items[compute_index(self_, xtd::array<xtd::index> {indexes})];
244}
245
246template<typename type_t, typename allocator_t>
247inline const type_t& xtd::basic_array<type_t, allocator_t>::operator ()(const std::initializer_list<xtd::index>& indexes) const {
248 return data_->items[compute_index(self_, xtd::array<xtd::index> {indexes})];
249}
250
251template<typename type_t, typename allocator_t>
252template<typename value_t>
253xtd::usize xtd::basic_array<type_t, allocator_t>::compute_index(const xtd::basic_array<value_t>& items, const xtd::array <xtd::index>& indexes) {
254 auto size_type_indexes = xtd::array <size_type>(indexes.length());
255 for (auto index = size_type {0}; index < indexes.length(); ++index)
256 size_type_indexes[index] = indexes[index].get_offset(items.get_length(index));
257 return compute_index(items, size_type_indexes);
258}
259
260#if __cpp_multidimensional_subscript
261template<typename type_t, typename allocator_t>
262auto xtd::array<type_t, 2, allocator_t>::operator[](const xtd::index& index1, const xtd::index& index2) -> value_type& {
264}
265
266template<typename type_t, typename allocator_t>
267auto xtd::array<type_t, 2, allocator_t>::operator[](const xtd::index& index1, const xtd::index& index2) const -> const value_type& {
269}
270
271template<typename type_t, typename allocator_t>
272auto xtd::array<type_t, 3, allocator_t>::operator[](const xtd::index& index1, const xtd::index& index2, const xtd::index& index3) -> value_type& {
273 return xtd::basic_array<type_t, allocator_t>::operator[]({index1, index2, index3});
274}
275
276template<typename type_t, typename allocator_t>
277auto xtd::array<type_t, 3, allocator_t>::operator[](const xtd::index& index1, const xtd::index& index2, const xtd::index& index3) const -> const value_type& {
278 return xtd::basic_array<type_t, allocator_t>::operator[]({index1, index2, index3});
279}
280#endif
281
282template<typename type_t, typename allocator_t>
283auto xtd::array<type_t, 2, allocator_t>::operator()(const xtd::index& index1, const xtd::index& index2) -> value_type& {
285}
286
287template<typename type_t, typename allocator_t>
288auto xtd::array<type_t, 2, allocator_t>::operator()(const xtd::index& index1, const xtd::index& index2) const -> const value_type& {
290}
291
292template<typename type_t, typename allocator_t>
293auto xtd::array<type_t, 3, allocator_t>::operator()(const xtd::index& index1, const xtd::index& index2, const xtd::index& index3) -> value_type& {
294 return xtd::basic_array<type_t, allocator_t>::operator[]({index1, index2, index3});
295}
296
297template<typename type_t, typename allocator_t>
298auto xtd::array<type_t, 3, allocator_t>::operator()(const xtd::index& index1, const xtd::index& index2, const xtd::index& index3) const -> const value_type& {
299 return xtd::basic_array<type_t, allocator_t>::operator[]({index1, index2, index3});
300}
static auto as_read_only(const xtd::array< type_t, 1, allocator_t > &array) -> xtd::collections::object_model::read_only_collection< type_t >
Returns a read-only wrapper for the specified array.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
Base object that represent array.
Definition basic_array.hpp:27
auto get_value(const xtd::array< size_type > &indexes) const -> const value_type &
Gets the value at the specified position in the multidimensional array. The indexes are specified as ...
virtual auto rank() const noexcept -> size_type
Gets the rank (number of dimensions) of the array.
Definition basic_array.hpp:137
auto get_lengths() const -> xtd::array< size_type, 1 >
Gets an array of the number of elements of all the dimensions of the array.
constexpr auto get_length(size_type dimension) const -> size_type
Gets the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:286
auto operator()(const xtd::array< index_t > &indexes) -> type_t &
Gets the value at the specified position in the multidimensional array. The indexes are specified as ...
virtual auto length() const noexcept -> size_type
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:122
auto operator[](size_type index) const -> const_reference override
Returns a reference to the element at specified location index.
Definition basic_array.hpp:516
constexpr auto get_lower_bound(size_type dimension) const -> size_type
Gets the lower bound of the specified dimension in the array.
Definition basic_array.hpp:308
constexpr auto get_upper_bound(size_type dimension) const -> size_type
Gets the upper bound of the specified dimension in the array.
Definition basic_array.hpp:320
static auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1240
virtual auto size() const noexcept -> xtd::usize
Gets the number of elements contained in the xtd::collections::generic::icollection <type_t>.
Definition collection_common.hpp:43
virtual auto operator[](xtd::usize index) const -> const type_t &=0
Gets the element at the specified index.
Provides the base class for a generic read-only collection.
Definition read_only_collection.hpp:39
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
virtual auto to_string() const -> xtd::string
Returns a xtd::string that represents the current object.
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ index_out_of_range
The index is out of range.
Definition exception_case.hpp:61
@ rank
The rank is not valid.
Definition exception_case.hpp:93
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
@ r
The R key.
Definition console_key.hpp:122
@ relative
The xtd::uri is a relative xtd::uri.
Definition uri_kind.hpp:25
size_type
Specifies how rows or columns of user interface (UI) elements should be sized relative to their conta...
Definition size_type.hpp:22
Contains xtd::index struct.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
auto to_string() const noexcept -> xtd::string override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:354
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:65
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:209
auto operator()(size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition read_only_span.hpp:401
Contains xtd::collections::object_model::read_only_collection class.
Contains xtd::string alias.
Represents a type that can be used to index a collection either from the beginning or the end.
Definition index.hpp:38
Represents a value_type struct.
Definition value_type.hpp:34