xtd 0.2.0
Loading...
Searching...
No Matches
fixed_array.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "array.hpp"
6#include "iequatable.hpp"
7#include "optional.hpp"
8#include "npos.hpp"
9#include "string.hpp"
12
14namespace xtd {
25 template<typename type_t, xtd::usize len>
26 class fixed_array : public xtd::object, public xtd::collections::generic::ienumerable<type_t>, public xtd::iequatable<fixed_array<type_t, len>> {
27 public:
29
32 using value_type = type_t;
44 using const_pointer = const value_type*;
50
52
56 fixed_array() = default;
60 fixed_array(std::initializer_list<type_t> items) {
61 auto index = xtd::usize {0};
62 for (const auto& item : items)
63 items_[index++] = item;
64 }
65
66
68 fixed_array(fixed_array&&) = default;
69 fixed_array& operator =(fixed_array&&) = default;
70 fixed_array(const fixed_array&) = default;
71 fixed_array& operator =(const fixed_array&) = default;
73
75
79 [[nodiscard]] auto count() const noexcept -> size_type {return size();}
80
84 [[nodiscard]] auto data() const noexcept -> const_pointer {return items_;}
88 [[nodiscard]] auto data() noexcept -> pointer {return items_;}
89
92 [[nodiscard]] virtual auto empty() const noexcept -> bool {return len == xtd::usize {0};}
93
96 [[nodiscard]] virtual auto items() const noexcept -> const_base_type {return items_;}
99 [[nodiscard]] virtual auto items() noexcept -> base_type {return items_;}
100
104 [[nodiscard]] virtual auto length() const noexcept -> size_type {return len;}
105
108 [[nodiscard]] auto size() const noexcept -> size_type {return length();}
110
112
116 auto clear() noexcept -> void {fill(value_type {});}
117
120 [[nodiscard]] auto contains(const value_type& value) const noexcept -> bool {
121 for (const auto& item : items_)
122 if (xtd::collections::generic::helpers::equator<type_t> {}(reinterpret_cast<const value_type&>(item), value)) return true;
123 return false;
124 }
125
129 auto copy_to(xtd::array<value_type>& array) const -> void {
130 copy_to(0, array, 0);
131 }
132
137 auto copy_to(xtd::array<value_type>& array, size_type array_index) const -> void {
138 return copy_to(0, array, array_index);
139 }
140
145 auto copy_to(const size_type index, xtd::array<value_type>& array, size_type array_index) const -> void {
146 return copy_to(index, array, array_index, length() - index);
147 }
148
154 auto copy_to(const size_type index, xtd::array<value_type>& array, size_type array_index, size_type count) const -> void {
157 for (auto i = index; i < (index + count); ++i)
158 array[array_index++] = self_[i];
159 }
160
164 [[nodiscard]] auto equals(const object & obj) const noexcept -> bool override {return dynamic_cast<const fixed_array<value_type, len>*>(&obj) && equals(static_cast<const fixed_array<value_type, len>&>(obj));}
169 [[nodiscard]] auto equals(const fixed_array & rhs) const noexcept -> bool override {
170 if (count() != rhs.count()) return false;
171 for (size_type i = 0; i < count(); i++)
172 if (!xtd::collections::generic::helpers::equator<type_t> {}(items_[i], rhs.items_[i])) return false;
173 return true;
174 }
175
178 auto fill(const value_type& value) -> void {
179 for (auto& item : *this)
180 item = value;
181 }
182
185 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
186 struct fixed_array_enumerator : public xtd::collections::generic::ienumerator < value_type > {
187 explicit fixed_array_enumerator(const fixed_array & items) : items_(items) {}
188
189 [[nodiscard]] const value_type& current() const override {
191 return items_[index_];
192 }
193 bool move_next() override {return ++index_ < items_.count();}
194 void reset() override {index_ = xtd::npos;}
195
196 private:
197 size_type index_ = xtd::npos;
198 const fixed_array& items_;
199 };
200 return {new_ptr<fixed_array_enumerator>(*this)};
201 }
202
206 [[nodiscard]] auto index_of(const value_type& value) const noexcept -> size_type {return index_of(value, 0, length());}
212 [[nodiscard]] auto index_of(const value_type& value, size_type index) const -> size_type {return index_of(value, index, length() - index);}
219 [[nodiscard]] auto index_of(const value_type& value, size_type index, size_type count) const -> size_type {
221 for (auto increment = index; increment < (index + count); ++increment)
222 if (xtd::collections::generic::helpers::equator<type_t> {}(items_[increment], value)) return increment;
223 return xtd::npos;
224 }
225
236
242 template<typename comparison_t>
243 auto sort(comparison_t&& comparison) -> fixed_array& {
244 std::sort(items_, items_ + length(), [&](const type_t& x, const type_t& y) {return comparison(x, y) < 0;});
245 return self_;
246 }
247
255 return sort(0, count(), comparer);
256 }
257
271
274 [[nodiscard]] virtual auto to_array() const noexcept -> xtd::array<value_type> {return size() ? xtd::array<value_type>(items_, items_ + len) : xtd::array<value_type> {};}
275
278 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", *this));}
280
282
284
289 auto operator [](size_type index) const -> const_reference {
291 return items_[index == epos ? size() - 1 : index];
292 }
293
298 return items_[index == epos ? size() - 1 : index];
299 }
300
301
302 private:
303 type_t items_[len];
304 };
305
307 // Deduction guides for xtd::fixed_array
308 // {
309 template<typename type_t, typename... args_t>
310 fixed_array(type_t, args_t...) -> fixed_array <type_t, 1 + sizeof...(args_t)>;
311 // }
313}
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
xtd::usize rank() const noexcept override
Gets the rank (number of dimensions) of the array.
Definition array.hpp:158
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
static auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1255
static const comparer< xtd::any_object > default_comparer
Definition comparer.hpp:50
Exposes a method that compares two objects.
Definition icomparer.hpp:30
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Represents a fixed array class.
Definition fixed_array.hpp:26
virtual auto items() noexcept -> base_type
Returns the underlying base type items.
Definition fixed_array.hpp:99
auto sort(xtd::usize index, xtd::usize count, const xtd::collections::generic::icomparer< type_t > &comparer) -> fixed_array &
Sorts the elements in a range of elements in xtd::fixed_array <type_t> using the specified comparer.
Definition fixed_array.hpp:266
auto data() const noexcept -> const_pointer
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:84
xtd::usize size_type
Represents the array size type (usually xtd::usize).
Definition fixed_array.hpp:46
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition fixed_array.hpp:185
auto data() noexcept -> pointer
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:88
auto fill(const value_type &value) -> void
Fills the elements of this span with a specified value.
Definition fixed_array.hpp:178
auto equals(const fixed_array &rhs) const noexcept -> bool override
Determines whether this instance and another specified xtd::fixed_array object have the same value.
Definition fixed_array.hpp:169
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition fixed_array.hpp:278
auto index_of(const value_type &value, size_type index) const -> size_type
Determines the index of a specific item in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:212
auto clear() noexcept -> void
Clears the contents of this xtd::span <type> object.
Definition fixed_array.hpp:116
const value_type * const_pointer
Represents the const pointer of array value type.
Definition fixed_array.hpp:44
auto count() const noexcept -> size_type
Gets the number of elements contained in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:79
auto equals(const object &obj) const noexcept -> bool override
Determines whether this instance and a specified object, which must also be a xtd::fixed_array object...
Definition fixed_array.hpp:164
value_type * pointer
Represents the pointer of array value type.
Definition fixed_array.hpp:42
auto copy_to(const size_type index, xtd::array< value_type > &array, size_type array_index, size_type count) const -> void
Copies the elements of the xtd::array <type_t> from a specified index to an xtd::array,...
Definition fixed_array.hpp:154
auto operator[](size_type index) const -> const_reference
Returns a reference to the element at specified location index.
Definition fixed_array.hpp:289
virtual auto to_array() const noexcept -> xtd::array< value_type >
Copies the elements of the xtd::fixed_array <type_t> to a new array.
Definition fixed_array.hpp:274
auto copy_to(xtd::array< value_type > &array, size_type array_index) const -> void
Copies the elements of the xtd::array <type_t> to an xtd::array, starting at a particular xtd::array ...
Definition fixed_array.hpp:137
virtual auto items() const noexcept -> const_base_type
Returns the underlying base type items.
Definition fixed_array.hpp:96
auto size() const noexcept -> size_type
Returns the number of elements in the container, i.e. std::distance(xtd::array::begin(),...
Definition fixed_array.hpp:108
type_t value_type
Represents the array value type.
Definition fixed_array.hpp:32
fixed_array(std::initializer_list< type_t > items)
Initializes a new instance of the fixed_array class with specified initializer list.
Definition fixed_array.hpp:60
virtual auto empty() const noexcept -> bool
Checks if the container has no elements, i.e. whether xtd::array::begin() == xtd::array::end().
Definition fixed_array.hpp:92
auto sort() -> fixed_array &
Determines the index of a specific item in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:235
auto copy_to(const size_type index, xtd::array< value_type > &array, size_type array_index) const -> void
Copies the elements of the xtd::array <type_t> from a specified index to an xtd::array,...
Definition fixed_array.hpp:145
const value_type * const_base_type
Represents the const array base type.
Definition fixed_array.hpp:36
xtd::ptrdiff difference_type
Represents the array difference type (usually xtd::ptrdiff).
Definition fixed_array.hpp:48
const value_type & const_reference
Represents the const reference of array value type.
Definition fixed_array.hpp:40
auto sort(comparison_t &&comparison) -> fixed_array &
Sorts the elements in the entire xtd::fixed_array <type_t> using the specified xtd::comparison <type_...
Definition fixed_array.hpp:243
auto index_of(const value_type &value) const noexcept -> size_type
Determines the index of a specific item in the xtd::array <type_t>.
Definition fixed_array.hpp:206
value_type & reference
Represents the reference of array value type.
Definition fixed_array.hpp:38
fixed_array()=default
Initializes a new instance of the fixed_array class that is empty.
value_type * base_type
Represents the array base type.
Definition fixed_array.hpp:34
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 fixed_array.hpp:104
auto copy_to(xtd::array< value_type > &array) const -> void
Copies the entire xtd::array <type_t> to a compatible one-dimensional array.
Definition fixed_array.hpp:129
auto sort(const xtd::collections::generic::icomparer< type_t > &comparer) -> fixed_array &
Sorts the elements in the entire xtd::fixed_array <type_t> using the specified comparer.
Definition fixed_array.hpp:254
auto contains(const value_type &value) const noexcept -> bool
Determines whether an element is in the array.
Definition fixed_array.hpp:120
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Contains xtd::collections::generic::ienumerable <type_t> interface.
xtd::delegate< int32(type_t x, type_t y)> comparison
Represents the method that compares two objects of the same type.
Definition comparison.hpp:33
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ index_out_of_range
The index is out of range.
Definition exception_case.hpp:61
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
constexpr xtd::usize epos
Represents the index of the last valid element in a collection.
Definition epos.hpp:33
constexpr xtd::usize npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ y
The Y key.
Definition console_key.hpp:136
@ i
The I key.
Definition console_key.hpp:104
@ x
The X key.
Definition console_key.hpp:134
Contains xtd::iequatable interface.
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
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:62
constexpr size_type size() const noexcept
Returns the number of elements.
Definition read_only_span.hpp:241
Contains xtd::npos constant.
Contains xtd::optional type.
Contains xtd::string alias.
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
Implements a function object for compare data.
Definition lesser.hpp:39
Represents a value_type struct.
Definition value_type.hpp:34
Contains xtd::helpers::throw_helper class.