xtd 0.2.0
fixed_array.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "array.hpp"
6#include "optional.hpp"
7#include "string.hpp"
9
11namespace xtd {
22 template<class type_t, xtd::size len>
23 class fixed_array : public xtd::object {
24 public:
26
29 using value_type = type_t;
41 using const_iterator = const value_type*;
45 using const_pointer = const value_type*;
51 using reverse_iterator = std::reverse_iterator<iterator>;
53 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
55
57
59 fixed_array() = default;
60 fixed_array(fixed_array&&) = default;
61 fixed_array& operator =(fixed_array&&) = default;
62 fixed_array(const fixed_array&) = default;
63 fixed_array& operator =(const fixed_array&) = default;
64 fixed_array(std::initializer_list<type_t> il) {
65 auto index = xtd::size {0};
66 for (const auto& item : il)
67 items_[index++] = item;
68 }
70
72
77 virtual reference back() {return at(count() - 1);}
81 virtual const_reference back() const {return at(count() - 1);}
82
85 const_iterator begin() const noexcept {return cbegin();}
88 iterator begin() noexcept {return iterator {data()};}
89
92 const_iterator cbegin() const noexcept {return const_iterator {data()};}
93
96 const_iterator cend() const noexcept {return const_iterator {data() + len};}
97
100 size_type count() const noexcept {return size();}
101
105 const_pointer data() const noexcept {return items_;}
109 pointer data() noexcept {return items_;}
110
113 virtual bool empty() const noexcept {return len == xtd::size {0};}
114
117 const_iterator end() const noexcept {return cend();}
120 iterator end() noexcept {return iterator {data() + len};}
124 virtual reference front() {return at(0);}
128 virtual const_reference front() const {return at(0);}
129
132 virtual const_base_type items() const noexcept {return items_;}
135 virtual base_type items() noexcept {return items_;}
136
140 virtual size_type length() const noexcept {return size();}
141
144 xtd::size size() const noexcept {return len;}
146
148
154 virtual const_reference at(size_type index) const {
156 return items_[index];
157 }
162 virtual reference at(size_type index) {
164 return items_[index];
165 }
166
169 void clear() noexcept {
170 for (auto& item : *this)
171 item = value_type {};
172 }
173
176 void fill(const type_t& value) {
177 for (auto& item : *this)
178 item = value;
179 }
180
184
187 xtd::string to_string() const noexcept override {return xtd::string::format("[{}]", xtd::string::join(", ", *this));}
189
191
193
198 const_reference operator [](xtd::size index) const {return at(index);}
202 reference operator [](xtd::size index) {return at(index);}
204
205 private:
206 type_t items_[len];
207 };
208
209 template <class type_t, class... args_t>
210 fixed_array(type_t, args_t...) -> fixed_array<type_t, 1 + sizeof...(args_t)>;
211}
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
Represents text as a sequence of character units.
Definition basic_string.hpp:71
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:2288
Represents a fixed array class.
Definition fixed_array.hpp:23
size_type count() const noexcept
Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
Definition fixed_array.hpp:100
const_iterator end() const noexcept
Returns an iterator to the element following the last element of the enumarable.
Definition fixed_array.hpp:117
const_iterator cbegin() const noexcept
Returns an iterator to the first element of the enumarable.
Definition fixed_array.hpp:92
virtual base_type items() noexcept
Returns the underlying base type items.
Definition fixed_array.hpp:135
iterator end() noexcept
Returns an iterator to the element following the last element of the enumarable.
Definition fixed_array.hpp:120
virtual const_reference back() const
Returns a reference to the last element in the container.
Definition fixed_array.hpp:81
virtual const_base_type items() const noexcept
Returns the underlying base type items.
Definition fixed_array.hpp:132
virtual reference back()
Returns a reference to the last element in the container.
Definition fixed_array.hpp:77
void fill(const type_t &value)
Fills the elements of this span with a specified value.
Definition fixed_array.hpp:176
virtual reference front()
Returns a reference to the first element in the container.
Definition fixed_array.hpp:124
virtual reference at(size_type index)
Returns a reference to the element at specified location pos, with bounds checking.
Definition fixed_array.hpp:162
const_iterator cend() const noexcept
Returns an iterator to the element following the last element of the enumarable.
Definition fixed_array.hpp:96
pointer data() noexcept
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:109
const_pointer data() const noexcept
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:105
iterator begin() noexcept
Returns an iterator to the first element of the enumarable.
Definition fixed_array.hpp:88
const value_type * const_pointer
Represents the const pointer of array value type.
Definition fixed_array.hpp:45
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
Definition fixed_array.hpp:187
value_type * pointer
Represents the pointer of array value type.
Definition fixed_array.hpp:43
virtual const_reference at(size_type index) const
Returns a reference to the element at specified location pos, with bounds checking.
Definition fixed_array.hpp:154
virtual xtd::array< value_type > to_array() const noexcept
Copies the elements of the xtd::collections::generic::list <type_t> to a new array.
Definition fixed_array.hpp:183
virtual bool empty() const noexcept
Checks if the container has no elements, i.e. whether xtd::array::begin() == xtd::array::end().
Definition fixed_array.hpp:113
type_t value_type
Represents the array value type.
Definition fixed_array.hpp:29
const value_type * const_iterator
Represents the const iterator of array value type.
Definition fixed_array.hpp:41
virtual const_reference front() const
Returns a reference to the first element in the container.
Definition fixed_array.hpp:128
std::reverse_iterator< iterator > reverse_iterator
Represents the reverse iterator of array value type.
Definition fixed_array.hpp:51
const value_type * const_base_type
Represents the const array base type.
Definition fixed_array.hpp:33
xtd::ptrdiff difference_type
Represents the array difference type (usually xtd::ptrdiff).
Definition fixed_array.hpp:49
const value_type & const_reference
Represents the const reference of array value type.
Definition fixed_array.hpp:37
std::reverse_iterator< const_iterator > const_reverse_iterator
Represents the const reverse iterator of array value type.
Definition fixed_array.hpp:53
virtual size_type length() const noexcept
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition fixed_array.hpp:140
const_iterator begin() const noexcept
Returns an iterator to the first element of the enumarable.
Definition fixed_array.hpp:85
xtd::size size() const noexcept
Returns the number of elements in the container, i.e. std::distance(xtd::array::begin(),...
Definition fixed_array.hpp:144
value_type * iterator
Represents the iterator of array value type.
Definition fixed_array.hpp:39
value_type & reference
Represents the reference of array value type.
Definition fixed_array.hpp:35
value_type * base_type
Represents the array base type.
Definition fixed_array.hpp:31
const_reference operator[](xtd::size index) const
Returns a reference to the element at specified location index.
Definition fixed_array.hpp:198
xtd::size size_type
Represents the array size type (usually xtd::size).
Definition fixed_array.hpp:47
void clear() noexcept
Clears the contents of this xtd::span <type> object.
Definition fixed_array.hpp:169
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
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 ...
@ index_out_of_range
The index is out of range.
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::optional type.
Contains xtd::string alias.
Contains xtd::helpers::exception_case enum class.