xtd 0.2.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 "string.hpp"
12#include <numeric>
13
15namespace xtd {
16 // Deduction guides for xtd::array
17 // {
18 template<class type_t, xtd::size length>
19 array(const type_t(&)[length]) -> array<type_t, 1>;
20
21 template<class type_t>
22 array(const type_t*, xtd::size) -> array<type_t, 1>;
23
24 template<class type_t>
25 array(const xtd::collections::generic::ienumerable<type_t>&) -> array<type_t, 1>;
26
27 template<class type_t>
28 array(const xtd::collections::generic::ilist<type_t>&) -> array<type_t, 1>;
29
30 template <class input_iterator_t>
31 array(input_iterator_t, input_iterator_t) -> array<std::iter_value_t<input_iterator_t>, 1>;
32
33 template<class type_t>
34 array(const std::vector<type_t>&) -> array<type_t, 1>;
35
36 template<class type_t>
37 array(std::vector<type_t>&&) -> array<type_t, 1>;
38
39 template<class type_t>
40 array(std::vector<std::vector<type_t>>) -> array<type_t, 2>;
41
42 template<class type_t>
43 array(std::vector<std::vector<std::vector<type_t>>>) -> array<type_t, 3>;
44
45 template<class type_t>
46 array(std::initializer_list<type_t>) -> array<type_t, 1>;
47
48 template<class type_t>
49 array(std::initializer_list<std::initializer_list<type_t>>) -> array<type_t, 2>;
50
51 template<class type_t>
52 array(std::initializer_list<std::initializer_list<std::initializer_list<type_t>>>) -> array<type_t, 3>;
53 // }
54}
55
56template<class type_t, class allocator_t>
57inline const type_t& xtd::basic_array<type_t, allocator_t>::get_value(const xtd::array<xtd::size>& indexes) const {
58 return operator()(indexes);
59}
60
61template<class type_t, class allocator_t>
63 auto result = xtd::array<xtd::size, 1>(rank());
64 for (auto r = xtd::size {}; r < rank(); ++r)
65 result[r] = get_length(r);
66 return result;
67}
68
69template<class type_t, class allocator_t>
71 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
72}
73
74template<class type_t, class allocator_t>
76 auto position = xtd::size {0};
77 for (auto index1 = xtd::size {0}; index1 < indexes.length(); ++index1) {
79 auto multiplicand = xtd::size {1};
80 for (auto index2 = index1 + 1; index2 < indexes.length(); ++index2)
81 multiplicand *= get_length(index2);
82 position += indexes[index1] * multiplicand;
83 }
85 return data_->items[position];
86}
87
88template<class type_t, class allocator_t>
89inline const type_t& xtd::basic_array<type_t, allocator_t>::operator()(const xtd::array<xtd::size>& indexes) const {
90 return data_->items[compute_index(self_, indexes)];
91}
92
93template<class type_t, class allocator_t>
94inline xtd::basic_array<type_t, allocator_t>::basic_array(const array<size_type, 1>& lengths) : basic_array(lengths, value_type {}) {
95}
96
97template<class type_t, class allocator_t>
98inline xtd::basic_array<type_t, allocator_t>::basic_array(const array<size_type, 1>& lengths, const value_type& value) {
99 data_->items = base_type(lengths.aggregate([&](const size_type & accumulator, const size_type & value) {return accumulator * value;}), value);
100 data_->lower_bound.clear();
101 data_->upper_bound.clear();
102 for (auto length : lengths) {
103 data_->lower_bound.push_back(0);
104 data_->upper_bound.push_back(length - 1);
105 }
106}
107
108template<class type_t, class allocator_t>
109template<class value_t>
110xtd::size xtd::basic_array<type_t, allocator_t>::compute_index(const xtd::basic_array<value_t>& items, const xtd::array < size_type >& indexes) {
111 auto position = xtd::size {0};
112 for (auto index1 = xtd::size {0}; index1 < indexes.length(); ++index1) {
114 auto multiplier = xtd::size {1};
115 for (auto index2 = index1 + 1; index2 < indexes.length(); ++index2)
116 multiplier *= items.get_length(index2);
117 position += indexes[index1] * multiplier;
118 }
120 return position;
121}
122
123template<class type_t, class allocator_t>
124template<class value_t>
125xtd::size xtd::basic_array<type_t, allocator_t>::compute_index(const xtd::basic_array<value_t>& items, xtd::size rank, xtd::size index) {
126 auto relative = index - items.get_lower_bound(rank);
127 auto multiplier = xtd::size {1};
128 for (auto r = rank + 1; r < items.rank(); ++r)
129 multiplier *= items.get_length(r);
131 return relative * multiplier;
132}
133
134template<class type_t, class allocator_t>
135template<class value_t>
137 auto result = xtd::string {"["};
138 for (auto index = items.get_lower_bound(rank); index <= items.get_upper_bound(rank); ++index) {
139 if (index != items.get_lower_bound(rank)) result += ", ";
140 auto offset = base_index + compute_index(items, rank, index);
141 if (rank + 1 < items.rank()) result += to_string(items, rank + 1, offset);
142 else result += xtd::string::format("{}", items[offset]);
143 }
144 result += "]";
145 return result;
146}
147
148template<class type_t, class allocator_t>
150 return xtd::collections::object_model::read_only_collection<type_t> {array};
151}
152
153template<class type_t, xtd::size rank_, class allocator_t>
156}
157
158template<class type_t, class allocator_t>
161}
162
163template<class type_t, class allocator_t>
166}
167
168template<class type_t, class allocator_t>
171}
173
175namespace xtd::collections::generic::extensions {
176 template <class enumerable_t, class source_t>
177 inline xtd::array<source_t> enumerable<enumerable_t, source_t>::to_array() const noexcept {
178 return xtd::linq::enumerable::to_array(self());
179 }
180}
181
182namespace xtd::linq {
183 template <class source_t>
184 inline auto enumerable::to_array(const xtd::collections::generic::ienumerable<source_t>& source) noexcept {
185 auto result = xtd::array<source_t> {};
186 result = xtd::array<source_t> {source};
187 return result;
188 }
189}
static xtd::collections::object_model::read_only_collection< type_t > as_read_only(const xtd::array< type_t, 1, allocator_t > &array)
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
virtual size_type rank() const noexcept
Gets the rank (number of dimensions) of the array.
Definition basic_array.hpp:139
const value_type & get_value(const xtd::array< size_type > &indexes) const
Gets the value at the specified position in the multidimensional array. The indexes are specified as ...
constexpr size_type get_length(size_type dimension) const
Gets the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:260
constexpr size_type get_lower_bound(size_type dimension) const
Gets the lower bound of the specified dimension in the array.
Definition basic_array.hpp:282
virtual size_type length() const noexcept
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:124
constexpr size_type get_upper_bound(size_type dimension) const
Gets the upper bound of the specified dimension in the array.
Definition basic_array.hpp:294
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
xtd::array< size_type, 1 > get_lengths() const
Gets an array of the number of elements of all the dimensions of the array.
type_t & operator()(const xtd::array< size_type > &indexes)
Gets the value at the specified position in the multidimensional array. The indexes are specified as ...
static basic_string join(const basic_string &separator, const collection_t &values) noexcept
Definition basic_string.hpp:1702
Provides the base class for a generic read-only collection.
Definition read_only_collection.hpp:38
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
static auto to_array(const ienumerable< source_t > &source) noexcept
Creates a xtd::array <type_t> from an xtd::collections::generic::ienumerable <type_t>.
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
@ 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
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
@ r
The R key.
Definition console_key.hpp:122
@ relative
The xtd::uri is a relative xtd::uri.
Definition uri_kind.hpp:25
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375
constexpr size_type length() const noexcept
Returns the length of the current read_only_span.
Definition read_only_span.hpp:229
Contains xtd::collections::object_model::read_only_collection class.
Contains xtd::string alias.