xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
arranged_element_collection.h
Go to the documentation of this file.
1
4#pragma once
5#include <limits>
6#include <ostream>
7#include <vector>
9#include <xtd/event_args.h>
10#include <xtd/event_handler.h>
11#include <xtd/event.h>
12#include "sorter_none.h"
13
15namespace xtd {
17 namespace forms {
19 namespace layout {
27 template<typename type_t, typename sorter_t = sorter_none>
29 public:
31 class value_type : public type_t {
32 public:
34 value_type() {};
35 value_type(const value_type&) = default;
36 value_type(value_type&&) = default;
37 template <typename ...args_t>
38 value_type(args_t&& ...args) : type_t(args...) {}
39 value_type& operator=(const value_type& value) {
40 if (value.owner) owner = value.owner;
41 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->item_updated(pos, static_cast<type_t&>(const_cast<value_type&>(value)));
42 type_t::operator=(value);
43 return *this;
44 }
45 value_type& operator=(value_type&& value) {
46 if (value.owner) owner = value.owner;
47 if (owner != nullptr && !owner->inserting_ && !owner->erasing_) owner->item_updated(pos, static_cast<type_t&>(value));
48 type_t::operator=(value);
49 return *this;
50 }
51 operator type_t() {return *this;}
52 friend std::ostream& operator<<(std::ostream& os, const value_type& value) {return os << static_cast<const type_t&>(value);}
54
55 private:
56 friend class arranged_element_collection;
57 size_t pos = std::numeric_limits<size_t>::max();
58 arranged_element_collection* owner = nullptr;
59 };
60
62 using allocator_type = std::allocator<value_type>;
64 using size_type = std::size_t;
66 using difference_type = std::ptrdiff_t;
72 using pointer = typename std::allocator_traits<allocator_type>::pointer;
74 using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
76 using iterator = typename std::vector<value_type>::iterator;
78 using const_iterator = typename std::vector<value_type>::const_iterator;
80 using reverse_iterator = typename std::vector<value_type>::reverse_iterator;
82 using const_reverse_iterator = typename std::vector<value_type>::const_reverse_iterator;
83
87 explicit arranged_element_collection(const allocator_type& allocator = allocator_type()) : collection_(allocator) {}
90 arranged_element_collection(const std::initializer_list<type_t>& il) {
91 for (auto item : il)
92 push_back(item);
93 }
94
96 arranged_element_collection(const std::vector<type_t>& collection) {
97 for (auto item : collection)
98 push_back(item);
99 }
100 arranged_element_collection(const arranged_element_collection& collection) {push_back_range(collection);}
101 arranged_element_collection& operator=(const arranged_element_collection& collection) {
102 clear();
103 push_back_range(collection);
104 return *this;
105 }
106 arranged_element_collection(arranged_element_collection&&) = default;
107 bool operator==(const arranged_element_collection& value) const {return collection_ == value.collection_;}
108 bool operator!=(const arranged_element_collection& value) const {return !operator==(value);}
110
112 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_added;
113
115 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_updated;
116
118 event<arranged_element_collection, delegate<void(size_t, type_t& item)>> item_erased;
119
122 allocator_type get_allocator() const {return collection_.get_allocator();}
123
128 collection_[pos].pos = pos;
129 collection_[pos].owner = this;
130 return collection_.at(pos);
131 }
135 const_reference at(size_type pos) const {return collection_.at(pos);}
136
139 reference front() {return collection_.front();}
142 const_reference front() const {return collection_.front();}
143
146 reference back() {return collection_.back();}
149 const_reference back() const {return collection_.back();}
150
153 pointer data() {return collection_.data();}
156 const_pointer data() const {return collection_.data();}
157
161 collection_[pos].pos = pos;
162 collection_[pos].owner = this;
163 return collection_[pos];
164 }
167 const_reference operator[](size_type pos) const {return collection_[pos];}
168
171 iterator begin() {return collection_.begin();}
174 const_iterator begin() const {return collection_.begin();}
177 const_iterator cbegin() const {return collection_.cbegin();}
178
181 iterator end() {return collection_.end();}
184 const_iterator end() const {return collection_.end();}
187 const_iterator cend() const {return collection_.cend();}
188
191 reverse_iterator rbegin() {return collection_.rbegin();}
194 const_reverse_iterator rbegin() const {return collection_.rbegin();}
197 const_reverse_iterator crbegin() const {return collection_.crbegin();}
198
201 reverse_iterator rend() {return collection_.rend();}
204 const_reverse_iterator rend() const {return collection_.rend();}
207 const_reverse_iterator crend() const {return collection_.crend();}
208
211 bool empty() const {return collection_.empty();}
212
215 size_type size() const {return collection_.size();}
216
219 size_type max_size() const {return collection_.max_size();}
220
222 void reserve(size_type size) {collection_.reserve(size);}
223
226 size_type capacity() const {return collection_.capacity();}
227
229 void shrink_to_fit() {collection_.shrink_to_fit();}
230
233 bool sorted() const {return sorted_;}
236 void sorted(bool value) {
237 if (sorted_ != value) {
238 sorted_ = value;
239 if (sorted_) sort();
240 }
241 }
242
244 void clear() {
245 iterator it = begin();
246 while (it != end())
247 it = erase(it);
248 }
249
253 iterator insert(iterator pos, const value_type& value) {
254 size_t index = pos - begin();
255 inserting_ = true;
256 iterator result = collection_.insert(pos, value);
257 inserting_ = false;
258 (*this)[index].owner = this;
259 (*this)[index].pos = index;
260 item_added(index, collection_[index]);
261 if (sorted_) sort();
262 return result;
263 }
268 size_t index = pos - begin();
269 inserting_ = true;
270 iterator result = collection_.insert(pos, value);
271 inserting_ = false;
272 (*this)[index].owner = this;
273 (*this)[index].pos = index;
274 item_added(index, collection_[index]);
275 if (sorted_) sort();
276 return result;
277 }
282 size_t index = pos - begin();
283 inserting_ = true;
284 iterator result = collection_.insert(pos, value);
285 inserting_ = false;
286 (*this)[index].owner = this;
287 (*this)[index].pos = index;
288 item_added(index, collection_[index]);
289 if (sorted_) sort();
290 return result;
291 }
292
296 void insert_at(size_t index, const value_type& value) {
298 insert(begin() + index, value);
299 }
300
304 item_erased(pos - begin(), *pos);
305 erasing_ = true;
306 iterator result = collection_.erase(pos);
307 erasing_ = false;
308 return result;
309 }
313 item_erased(pos - begin(), *pos);
314 erasing_ = true;
315 iterator result = collection_.erase(pos);
316 erasing_ = false;
317 return result;
318 }
319
324 iterator result = end();
325 for (iterator it = first; it <= last; it++)
326 result = erase(it);
327 return result;
328 }
333 iterator result = this->bend();
334 for (const_iterator it = first; it <= last; it++)
335 result = erase(it);
336 return result;
337 }
338
341 void erase_at(size_t index) {
343 erase(begin() + index);
344 }
345
348 void push_back(const value_type& item) {
349 collection_.push_back(item);
350 size_t index = collection_.size() - 1;
351 (*this)[index].owner = this;
352 (*this)[index].pos = index;
353 item_added(index, collection_[index]);
354 if (sorted_) sort();
355 }
358 void push_back(value_type&& item) {
359 collection_.push_back(item);
360 size_t index = collection_.size() - 1;
361 (*this)[index].owner = this;
362 (*this)[index].pos = index;
363 item_added(index, collection_[index]);
364 if (sorted_) sort();
365 }
366
370 for(value_type item : collection)
371 push_back(item);
372 }
375 void push_back_range(const std::vector<value_type>& collection) {
376 for(value_type item : collection)
377 push_back(item);
378 }
381 void push_back_range(const std::initializer_list<value_type>& collection) {
382 for(value_type item : collection)
383 push_back(item);
384 }
387 template<typename collection_t>
388 void push_back_range(collection_t collection) {
389 for(const auto& item : collection)
390 push_back(value_type(item));
391 }
392
394 void sort() {
395 sorter_t sorter;
396 sorter(begin(), end());
397 }
398
401 std::vector<type_t> to_array() const {
402 std::vector<type_t> array;
403 for (auto item : collection_)
404 array.push_back(item);
405 return array;
406 }
407
410 std::vector<type_t> to_vector() const {return to_array();}
411
413 static const size_type npos = std::numeric_limits<size_type>::max();
414
415 private:
416 std::vector<value_type, allocator_type> collection_;
417 bool inserting_ = false;
418 bool erasing_ = false;
419 bool sorted_ = false;
420 };
421 }
422 }
423}
Contains xtd::argument_out_of_range_exception exception.
The exception that is thrown when one of the arguments provided to a method is out of range.
Definition: argument_out_of_range_exception.h:18
Represents the value type of the collection.
Definition: arranged_element_collection.h:31
Represents a collection of objects.
Definition: arranged_element_collection.h:28
std::vector< type_t > to_array() const
Gets an array with the elements of the container.
Definition: arranged_element_collection.h:401
arranged_element_collection(const std::initializer_list< type_t > &il)
Creates a new object xtd::diagnostics::trace_listener_collection with specified initializer list.
Definition: arranged_element_collection.h:90
void clear()
clears the contents.
Definition: arranged_element_collection.h:244
size_type max_size() const
Returns the maximum possible number of elements.
Definition: arranged_element_collection.h:219
allocator_type get_allocator() const
Returns the associated allocator.
Definition: arranged_element_collection.h:122
event< arranged_element_collection, delegate< void(size_t, type_t &item)> > item_erased
Occurs when an item is erased from the collection.
Definition: arranged_element_collection.h:118
const_iterator cbegin() const
Returns an iterator to the beginning.
Definition: arranged_element_collection.h:177
typename std::vector< value_type >::reverse_iterator reverse_iterator
Represents the reverse iterator type of the collection.
Definition: arranged_element_collection.h:80
const_reference front() const
Access the first element.
Definition: arranged_element_collection.h:142
void push_back_range(const std::vector< value_type > &collection)
Adds elements to the end.
Definition: arranged_element_collection.h:375
iterator erase(iterator pos)
Erases element at specified position.
Definition: arranged_element_collection.h:303
bool empty() const
Checks whether the container is empty.
Definition: arranged_element_collection.h:211
event< arranged_element_collection, delegate< void(size_t, type_t &item)> > item_updated
Occurs when an item is updated in the collection.
Definition: arranged_element_collection.h:115
iterator begin()
Returns an iterator to the beginning.
Definition: arranged_element_collection.h:171
iterator erase(const_iterator pos)
Erases element at specified position.
Definition: arranged_element_collection.h:312
std::size_t size_type
Represents the size type of the collection.
Definition: arranged_element_collection.h:64
typename std::vector< value_type >::iterator iterator
Represents the iterator type of the collection.
Definition: arranged_element_collection.h:76
std::allocator< value_type > allocator_type
Represents the allocator type of the collection.
Definition: arranged_element_collection.h:62
typename std::allocator_traits< allocator_type >::const_pointer const_pointer
Represents the value type const pointer of the collection.
Definition: arranged_element_collection.h:74
void insert_at(size_t index, const value_type &value)
Inserts specified element at specified index.
Definition: arranged_element_collection.h:296
iterator end()
Returns an iterator to the end.
Definition: arranged_element_collection.h:181
std::vector< type_t > to_vector() const
Gets an array with the elements of the container.
Definition: arranged_element_collection.h:410
void push_back_range(collection_t collection)
Adds elements to the end.
Definition: arranged_element_collection.h:388
reference operator[](size_type pos)
Access specified element.
Definition: arranged_element_collection.h:160
static const size_type npos
This is a special value equal to the maximum value representable by the type size_t.
Definition: arranged_element_collection.h:413
void push_back_range(const arranged_element_collection &collection)
Adds elements to the end.
Definition: arranged_element_collection.h:369
const_iterator cend() const
Returns an iterator to the end.
Definition: arranged_element_collection.h:187
reverse_iterator rend()
Returns a reverse iterator to the end.
Definition: arranged_element_collection.h:201
reference back()
Access the last element.
Definition: arranged_element_collection.h:146
iterator erase(iterator first, iterator last)
Erases elements at specified range.
Definition: arranged_element_collection.h:323
pointer data()
Direct access to the underlying array.
Definition: arranged_element_collection.h:153
void shrink_to_fit()
Reduces memory usage by freeing unused memory.
Definition: arranged_element_collection.h:229
iterator insert(const_iterator pos, const value_type &&value)
Inserts specified element at specified position.
Definition: arranged_element_collection.h:281
bool sorted() const
Checks whether the container is sorted.
Definition: arranged_element_collection.h:233
void push_back(const value_type &item)
Adds an element to the end.
Definition: arranged_element_collection.h:348
reverse_iterator rbegin()
Returns a reverse iterator to the end.
Definition: arranged_element_collection.h:191
reference front()
Access the first element.
Definition: arranged_element_collection.h:139
size_type size() const
Returns the number of elements.
Definition: arranged_element_collection.h:215
typename std::vector< value_type >::const_iterator const_iterator
Represents the const iterator type of the collection.
Definition: arranged_element_collection.h:78
void sort()
Sorts the content.
Definition: arranged_element_collection.h:394
const_reverse_iterator rbegin() const
Returns a reverse iterator to the end.
Definition: arranged_element_collection.h:194
void push_back_range(const std::initializer_list< value_type > &collection)
Adds elements to the end.
Definition: arranged_element_collection.h:381
const_reference back() const
Access the last element.
Definition: arranged_element_collection.h:149
const_reference at(size_type pos) const
Access specified element with bounds checking.
Definition: arranged_element_collection.h:135
const_iterator end() const
Returns an iterator to the end.
Definition: arranged_element_collection.h:184
const_iterator begin() const
Returns an iterator to the beginning.
Definition: arranged_element_collection.h:174
const_reference operator[](size_type pos) const
Access specified element.
Definition: arranged_element_collection.h:167
const_reverse_iterator rend() const
Returns a reverse iterator to the end.
Definition: arranged_element_collection.h:204
arranged_element_collection(const allocator_type &allocator=allocator_type())
Creates a new object xtd::forms::layout::arranged_element_collection with specified allocator (option...
Definition: arranged_element_collection.h:87
void erase_at(size_t index)
Erases element at specified index.
Definition: arranged_element_collection.h:341
typename std::allocator_traits< allocator_type >::pointer pointer
Represents the value type pointer of the collection.
Definition: arranged_element_collection.h:72
const_pointer data() const
Direct access to the underlying array.
Definition: arranged_element_collection.h:156
void push_back(value_type &&item)
Adds an element to the end.
Definition: arranged_element_collection.h:358
void reserve(size_type size)
Reserves storage.
Definition: arranged_element_collection.h:222
const_reverse_iterator crend() const
Returns a reverse iterator to the end.
Definition: arranged_element_collection.h:207
const_reverse_iterator crbegin() const
Returns a reverse iterator to the end.
Definition: arranged_element_collection.h:197
typename std::vector< value_type >::const_reverse_iterator const_reverse_iterator
Represents the const reverse iterator type of the collection.
Definition: arranged_element_collection.h:82
size_type capacity() const
Returns the number of elements that can be held in currently allocated storage.
Definition: arranged_element_collection.h:226
iterator insert(iterator pos, const value_type &value)
Inserts specified element at specified position.
Definition: arranged_element_collection.h:253
reference at(size_type pos)
Access specified element with bounds checking.
Definition: arranged_element_collection.h:127
iterator erase(const_iterator first, const_iterator last)
Erases elements at specified range.
Definition: arranged_element_collection.h:332
event< arranged_element_collection, delegate< void(size_t, type_t &item)> > item_added
Occurs when an item is added to the collection.
Definition: arranged_element_collection.h:112
std::ptrdiff_t difference_type
Represents the pointer difference type of the collection.
Definition: arranged_element_collection.h:66
void sorted(bool value)
Sets the container is sorted.
Definition: arranged_element_collection.h:236
iterator insert(const_iterator pos, const value_type &value)
Inserts specified element at specified position.
Definition: arranged_element_collection.h:267
Contains xtd::event event.
Contains xtd::event_args event args.
Contains xtd::event_handler event handler.
#define current_stack_frame_
Provides information about the current stack frame.
Definition: stack_frame.h:201
size_t size
Represents a size of any object in bytes.
Definition: types.h:171
@ insert
The INSERT key.
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::forms::layout::sorter_none class.