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.h> instead."
8#endif
9
10#include "string.hpp"
12#include <numeric>
13
15namespace xtd {
16 // C++17 deduction
17 // {
18 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
19 array(type_t, type_t, allocator_t = allocator_t()) -> array<type_t, rank, allocator_t>;
20
21 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
22 array(std::initializer_list<type_t>) -> array<type_t, rank, allocator_t>;
23
24 template<typename type_t, xtd::size length, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
25 array(const type_t(&a)[length]) -> array<type_t, rank, allocator_t>;
26
27 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
28 array(const xtd::collections::generic::ienumerable<type_t>&) -> array<type_t, rank, allocator_t>;
29
30 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
31 array(const xtd::collections::generic::ilist<type_t>&) -> array<type_t, rank, allocator_t>;
32
33 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
34 array(const std::vector<type_t>&) -> array<type_t, rank, allocator_t>;
35
36 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
37 array(const array<type_t, rank, allocator_t>&) -> array<type_t, rank, allocator_t>;
38
39 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
40 array(std::vector<type_t>&&) -> array<type_t, rank, allocator_t>;
41
42 template<typename type_t, xtd::size rank = 1, typename allocator_t = xtd::collections::generic::helpers::allocator<typename std::conditional<std::is_same<bool, type_t>::value, char, type_t>::type>>
43 array(array<type_t, rank, allocator_t>&&) -> array<type_t, rank, allocator_t>;
44 // }
45}
46
47template<typename type_t, typename allocator_t>
48inline const type_t& xtd::basic_array<type_t, allocator_t>::get_value(const xtd::array<xtd::size>& indexes) const {
49 return operator()(indexes);
50}
51
52template<typename type_t, typename allocator_t>
54 return xtd::string::format("[{}]", xtd::string::join(", ", *this));
55}
56
57template<typename type_t, typename allocator_t>
59 auto position = xtd::size {0};
60 for (auto index1 = xtd::size {0}; index1 < indexes.size(); ++index1) {
61 if (indexes[index1] >= get_length(index1)) __throw_index_out_of_range_exception(__FILE__, __LINE__, __func__);
62 auto multiplicand = xtd::size {1};
63 for (auto index2 = index1 + 1; index2 < indexes.size(); ++index2)
64 multiplicand *= get_length(index2);
65 position += indexes[index1] * multiplicand;
66 }
67 if (position >= size()) __throw_index_out_of_range_exception(__FILE__, __LINE__, __func__);
68 return data_->items[position];
69}
70
71template<typename type_t, typename allocator_t>
72inline const type_t& xtd::basic_array<type_t, allocator_t>::operator()(const xtd::array<xtd::size>& indexes) const {
73 auto position = xtd::size {0};
74 for (auto index1 = xtd::size {0}; index1 < indexes.size(); ++index1) {
75 if (indexes[index1] >= get_length(index1)) __throw_index_out_of_range_exception(__FILE__, __LINE__, __func__);
76 auto multiplicand = xtd::size {1};
77 for (auto index2 = index1 + 1; index2 < indexes.size(); ++index2)
78 multiplicand *= get_length(index2);
79 position += indexes[index1] * multiplicand;
80 }
81 if (position >= size()) __throw_index_out_of_range_exception(__FILE__, __LINE__, __func__);
82 return data_->items[position];
83}
84
85template<typename type_t, typename allocator_t>
86inline xtd::basic_array<type_t, allocator_t>::basic_array(const array<size_type, 1>& lengths) {
87 data_->items = base_type(std::accumulate(lengths.begin(), lengths.end(), size_type {0}));
88 data_->lower_bound.clear();
89 data_->upper_bound.clear();
90 for (auto length : lengths) {
91 data_->lower_bound.push_back(0);
92 data_->upper_bound.push_back(length - 1);
93 }
94}
95
96//template<>
97template<typename type_t, typename allocator_t>
99 return xtd::collections::object_model::read_only_collection<type_t> {new_ptr<class xtd::array<type_t>>(array)};
100}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:58
Base object that represent array.
Definition basic_array.hpp:27
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 ...
virtual const base_type & items() const noexcept
Returns the underlying base type items.
Definition basic_array.hpp:163
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
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 ...
virtual size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(xtd::array::begin(),...
Definition basic_array.hpp:211
Represents text as a sequence of character units.
Definition basic_string.hpp:79
static basic_string join(const basic_string separator, const collection_t &values) noexcept
Concatenates a specified separator basic_string between each element of a specified object array,...
Definition basic_string.hpp:2296
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:35
Represents a collection of objects that can be individually accessed by index.
Definition ilist.hpp:41
Provides the base class for a generic read-only collection.
Definition read_only_collection.hpp:40
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::type_info type
Stores information about a type.
Definition type.hpp:23
@ a
The A key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::collections::object_model::read_only_collection class.
Contains xtd::string alias.