xtd 1.0.0
Loading...
Searching...
No Matches
ordered_dictionary.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "dictionary.hpp"
6#include "list.hpp"
9
11namespace xtd {
13 namespace collections {
15 namespace generic {
31 template<typename key_t, typename value_t, typename allocator_t = xtd::collections::generic::helpers::allocator<std::pair<const key_t, value_t>>>
33 public:
35
56
58
75 ordered_dictionary() noexcept = default;
84 data_->items = dictionary;
85 data_->keys.capacity(dictionary.count());
86 for (const auto& item : dictionary)
87 data_->keys.add(item.first);
88 }
89
98 data_->items = base_type(dictionary, comparer);
99 data_->keys.capacity(dictionary.count());
100 for (const auto& item : dictionary)
101 data_->keys.add(item.first);
102 }
103
106 ordered_dictionary(const xtd::collections::generic::ienumerable < value_type >& collection) {
107 for (const auto& item : collection)
108 add(item);
109 }
110
112 template<xtd::iterable iterable_t>
113 ordered_dictionary(iterable_t&& items) {
114 for (const auto& [key, value] : items)
115 add({key, value});
116 }
117
121 ordered_dictionary(const xtd::collections::generic::ienumerable < value_type >& collection, const xtd::collections::generic::iequality_comparer<key_type>& comparer) {
122 data_->items = base_type(comparer);
123 for (const auto& item : collection)
124 add(item);
125 }
126
135 data_->keys.capacity(capacity);
136 data_->items.ensure_capacity(capacity);
137 }
138
148 data_->items = base_type(capacity, comparer);
149 data_->keys.capacity(capacity);
150 }
151
162
168 template < class input_iterator_t >
169 explicit ordered_dictionary(input_iterator_t first, input_iterator_t last) {
170 for (auto iterator = first; iterator != last; ++iterator)
171 add(*iterator);
172 }
173
176 ordered_dictionary(const ordered_dictionary & other) noexcept : data_(xtd::new_ptr<dictionary_data>(other.data_->items, other.data_->version)) {}
181 ordered_dictionary(std::initializer_list<base_value_type> init) {
182 for (const auto& [key, value] : init)
183 add(key, value);
184 }
192 template < class init_key_t, typename init_value_t >
193 explicit ordered_dictionary(std::initializer_list<key_value_pair<init_key_t, init_value_t>> init) {
194 for (const auto& [key, value] : init)
195 add(key, value);
196 }
197
198
200
207 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.count();}
208
212 [[nodiscard]] auto comparer() const noexcept -> const iequality_comparer < key_t >& {
213 return data_->items.comparer();
214 }
215
218 [[nodiscard]] virtual auto items() const noexcept -> const base_type& {return data_->items;}
221 [[nodiscard]] virtual auto items() noexcept -> base_type& {return data_->items;}
222
227 [[nodiscard]] auto keys() const noexcept -> key_collection override {
228 auto keys = key_collection {};
229 for (const auto& key : data_->keys)
230 keys.add(key);
231 return keys;
232 }
233
238 [[nodiscard]] auto values() const noexcept -> value_collection override {
239 auto values = value_collection {};
240 for (const auto& key : data_->keys)
241 values.add(data_->items[key]);
242 return values;
243 }
244
245
247
255 auto add(const key_t & key, const value_t& value) -> void override {
256 insert(count(), key, value);
257 }
258
262 auto add(const value_type & item) -> void override {
263 insert(count(), item.key(), item.value());
264 }
265
268 auto clear() noexcept -> void override {
269 lock_(data_->sync_op) {
270 data_->keys.clear();
271 data_->items.clear();
272 }
273 ++data_->version;
274 }
275
279 [[nodiscard]] auto contains(const value_type & item) const noexcept -> bool override {
280 return data_->items.contains(item);
281 }
282
287 [[nodiscard]] auto contains_key(const key_t & key) const noexcept -> bool override {
288 return data_->items.contains_key(key);
289 }
290
295 [[nodiscard]] auto contains_value(const value_t& value) const noexcept -> bool {
296 return data_->items.contains_value(value);
297 }
298
303 auto copy_to(xtd::array<value_type>& array, xtd::usize array_index) const -> void override {
305 for (const auto& item : self_)
306 array[array_index++] = item;
307 }
308
312 struct ordered_dictionary_enumerator : public ienumerator<value_type> {
313 explicit ordered_dictionary_enumerator(const ordered_dictionary & items, xtd::usize version) : items_(items), version_(version) {}
314
315 [[nodiscard]] const value_type& current() const override {
317 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
318 return (value_ = value_type {key_t {items_.data_->keys[index_]}, value_t {items_.data_->items[items_.data_->keys[index_]]}});
319 }
320
321 [[nodiscard]] bool move_next() override {
322 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
323 return ++index_ < items_.data_->keys.count();
324 }
325
326 void reset() override {
327 version_ = items_.data_->version;
328 index_ = xtd::npos;
329 }
330
331 private:
332 size_type index_ = xtd::npos;
333 const ordered_dictionary& items_;
334 mutable value_type value_;
335 size_type version_ = 0;
336 };
337
338 return {new_ptr<ordered_dictionary_enumerator>(self_, data_->version)};
339 }
340
346 auto insert(xtd::usize index, const key_t & key) -> void {insert(index, key, value_t {});}
347
354 auto insert(xtd::usize index, const key_t & key, const value_t& value) -> void {
356 lock_(data_->sync_op) {
357 data_->items.add(key, value);
358 data_->keys.insert(index, key);
359 }
360 ++data_->version;
361 }
362
367 auto remove(const key_t & key) noexcept -> bool override {
368 if (!contains_key(key)) return false;
369 lock_(data_->sync_op) {
370 remove_at(get_index(key));
371 }
372 ++data_->version;
373 return true;
374 }
375
379 auto remove(const value_type & item) noexcept -> bool override {
380 lock_(data_->sync_op) {
381 auto result = data_->items.remove(item.first);
382 if (!result) return false;
383 data_->keys.remove_at(get_index(item.first));
384 }
385 ++data_->version;
386 return true;
387 }
388
394 auto remove_at(xtd::usize index) -> void {
395 lock_(data_->sync_op) {
396 data_->items.remove(data_->keys[index]);
397 data_->keys.remove_at(index);
398 }
399 ++data_->version;
400 }
401
404 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("{{{}}}", xtd::string::join(", ", self_));}
405
410 [[nodiscard]] auto try_get_value(const key_t & key, value_t& value) const -> bool override {
411 return data_->items.try_get_value(key, value);
412 }
413
414
416
426 *data_ = *other.data_;
427 return self_;
428 }
429
432 auto operator =(std::initializer_list<base_value_type> ilist) -> ordered_dictionary& {
433 clear();
434 for (const auto& [key, value] : ilist)
435 add(key, value);
436 return self_;
437 }
438
441 template < class init_key_t, typename init_value_t >
443 clear();
444 for (const auto& [key, value] : ilist)
445 add(key, value);
446 return self_;
447 }
448
456 auto operator()(xtd::usize index) const -> const value_t& {return operator [](data_->keys[index]);}
464 auto operator()(xtd::usize index) -> value_t& {return operator [](data_->keys[index]);}
465
473 const value_t& operator [](const key_t & key) const override {return data_->items[key];}
480 value_t& operator [](const key_t & key) override {
481 auto iterator = data_->items.items().find(key);
482 if (iterator != data_->items.items().end())
483 return iterator->second;
484 data_->keys.add(key);
485 return data_->items[key];
486 }
487
490 operator const base_type& () const noexcept {return data_->items;}
493 operator base_type& () noexcept {return data_->items;}
495
496 private:
497 auto get_index(const key_t & key) const noexcept -> xtd::usize {
498 auto index = xtd::usize {0};
499 for (const auto& item_key : data_->keys) {
500 if (item_key == key) return index;
501 ++index;
502 }
503 return xtd::npos;
504 }
505
506 auto is_read_only() const noexcept -> bool override {return false;}
507 auto is_synchronized() const noexcept -> bool override {return false;}
508 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
509
510 struct dictionary_data {
511 dictionary_data() noexcept = default;
512 dictionary_data(const base_type & items, size_type version) noexcept : items {items}, version {version} {
513 for (const auto& item : this->items)
514 keys.add(item.first);
515 }
516 dictionary_data(base_type&& items, size_type version) noexcept : items {items}, version {version} {
517 for (const auto& item : this->items)
518 keys.add(item.first);
519 }
520
521 list_type keys;
522 base_type items;
523 size_type version = 0;
524 xtd::object sync_root;
525 xtd::object sync_op;
526 };
528 };
529
531 // Deduction guides for xtd::collections::specialized::ordered_dictionary
532 // {
533 template < class key_t, typename value_t >
534 ordered_dictionary(xtd::collections::generic::idictionary < key_t, value_t >) -> ordered_dictionary < key_t, value_t>;
535
536 template < class key_t, typename value_t >
537 ordered_dictionary(xtd::collections::generic::ienumerable < key_value_pair < key_t, value_t>>) -> ordered_dictionary<key_t, value_t>;
538
539 template<xtd::iterable iterable_t>
540 ordered_dictionary(iterable_t&&) -> ordered_dictionary<std::remove_const_t<std::tuple_element_t<0, xtd::iterable_value_type<iterable_t>>>, std::remove_const_t<std::tuple_element_t<1, xtd::iterable_value_type<iterable_t>>>>;
541
542 template < class key_t, typename value_t >
543 ordered_dictionary(std::initializer_list < key_value_pair < key_t, value_t>>) -> ordered_dictionary < key_t, value_t >;
544
545 template < class key_t, typename value_t >
546 ordered_dictionary(std::initializer_list < std::pair < key_t, value_t>>) -> ordered_dictionary < key_t, value_t >;
547
548 template < class input_iterator_t >
549 ordered_dictionary(input_iterator_t, input_iterator_t) -> ordered_dictionary < xtd::collections::generic::helpers::iterator_key_t < input_iterator_t>, xtd::collections::generic::helpers::iterator_mapped_t<input_iterator_t >>;
550 // }
552 }
553 }
554}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
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:1259
Represents a collection of keys and values.
Definition dictionary.hpp:63
auto keys() const noexcept -> key_collection override
Gets a collection containing the keys in the xtd::collections::generic::dictionary <key_t,...
Definition dictionary.hpp:305
auto count() const noexcept -> size_type override
Gets the number of key/value pairs contained in the xtd::collections::generic::dictionary <key_t,...
Definition dictionary.hpp:292
Represents a generic collection of key/value pairs.
Definition idictionary.hpp:47
xtd::collections::generic::list< mapped_type > value_collection
Definition idictionary.hpp:65
xtd::collections::generic::list< key_type > key_collection
Definition idictionary.hpp:63
virtual auto clear() -> void=0
Removes all items from the xtd::collections::generic::icollection <type_t>.
virtual auto sync_root() const noexcept -> const xtd::object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
virtual auto count() const noexcept -> xtd::usize=0
Gets the number of elements contained in the xtd::collections::generic::icollection <type_t>.
typename xtd::collections::generic::icollection< value_type >::iterator iterator
Definition idictionary.hpp:59
virtual auto is_read_only() const noexcept -> bool=0
Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
virtual auto is_synchronized() const noexcept -> bool=0
Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is sync...
typename xtd::collections::generic::icollection< xtd::collections::generic::key_value_pair< xtd::any_object, xtd::any_object > >::value_type value_type
Definition idictionary.hpp:57
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.hpp:34
Represents a collection of objects that can be individually accessed by index.
Definition ilist.hpp:44
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:82
Represents a collection of key/value pairs that are accessible by the key or index.
Definition ordered_dictionary.hpp:32
xtd::usize size_type
Represents the dictionary size type.
Definition ordered_dictionary.hpp:44
auto clear() noexcept -> void override
Removes all keys and values from the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:268
const value_t & operator[](const key_t &key) const override
Gets the element with the specified key.
Definition ordered_dictionary.hpp:473
auto insert(xtd::usize index, const key_t &key, const value_t &value) -> void
Inserts a new entry into the xtd::collections::generic::ordered_dictionary collection with the specif...
Definition ordered_dictionary.hpp:354
auto add(const key_t &key, const value_t &value) -> void override
Adds an element with the provided key and value to the xtd::collections::generic::ordered_dictionary ...
Definition ordered_dictionary.hpp:255
xtd::collections::generic::enumerator< value_type > get_enumerator() const noexcept override
Returns an enumerator that iterates through the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:311
auto to_string() const noexcept -> xtd::string override
Gets a string that represents the current object.
Definition ordered_dictionary.hpp:404
xtd::collections::generic::dictionary< key_type, mapped_type > base_type
Represents the dictionary base type.
Definition ordered_dictionary.hpp:48
typename xtd::collections::generic::idictionary< key_type, mapped_type >::value_collection value_collection
Represents the idictionary value collection type.
Definition ordered_dictionary.hpp:54
auto contains(const value_type &item) const noexcept -> bool override
Determines whether an element is in the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:279
auto count() const noexcept -> size_type override
Gets the number of key/value pairs contained in the xtd::collections::generic::ordered_dictionary <ke...
Definition ordered_dictionary.hpp:207
auto contains_value(const value_t &value) const noexcept -> bool
Determines whether the xtd::collections::generic::ordered_dictionary <key_t, value_t> contains the sp...
Definition ordered_dictionary.hpp:295
auto remove(const value_type &item) noexcept -> bool override
Removes the first occurrence of a specific object from the xtd::collections::generic::ordered_diction...
Definition ordered_dictionary.hpp:379
ordered_dictionary(const xtd::collections::generic::ienumerable< value_type > &collection)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:106
ordered_dictionary(const ordered_dictionary &other) noexcept
Initializes instance of the xtd::collections::generic::ordered_dictionary <key_t, value_t> class from...
Definition ordered_dictionary.hpp:176
xtd::collections::generic::list< key_type > list_type
Represents the dictionary base type.
Definition ordered_dictionary.hpp:50
auto remove_at(xtd::usize index) -> void
Removes the entry at the specified index from the OrderedDictionary collection.
Definition ordered_dictionary.hpp:394
ordered_dictionary(const xtd::collections::generic::idictionary< key_t, value_t > &dictionary, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:97
auto operator=(ordered_dictionary &&other) noexcept -> ordered_dictionary &=default
Move assignment operator. Replaces the contents with a copy of the contents of other.
typename xtd::collections::generic::idictionary< key_type, mapped_type >::value_type value_type
Represents the dictionary value type.
Definition ordered_dictionary.hpp:42
auto add(const value_type &item) -> void override
Adds an item to the xtd::collections::generic::icollection <type_t>.
Definition ordered_dictionary.hpp:262
virtual auto items() noexcept -> base_type &
Returns the underlying base type items.
Definition ordered_dictionary.hpp:221
auto values() const noexcept -> value_collection override
Gets a collection containing the values in the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:238
auto comparer() const noexcept -> const iequality_comparer< xtd::any_object > &
Definition ordered_dictionary.hpp:212
auto operator()(xtd::usize index) const -> const value_t &
Gets the value at the specified index.
Definition ordered_dictionary.hpp:456
ordered_dictionary(std::initializer_list< key_value_pair< init_key_t, init_value_t > > init)
Initializes instance of the xtd::collections::generic::ordered_dictionary <key_t, value_t> class from...
Definition ordered_dictionary.hpp:193
auto copy_to(xtd::array< value_type > &array, xtd::usize array_index) const -> void override
Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array,...
Definition ordered_dictionary.hpp:303
typename xtd::collections::generic::idictionary< key_t, value_t >::mapped_type mapped_type
Represents the dictionary mapped type.
Definition ordered_dictionary.hpp:40
typename xtd::collections::generic::idictionary< key_t, value_t >::key_type key_type
Represents the dictionary key type.
Definition ordered_dictionary.hpp:38
auto insert(xtd::usize index, const key_t &key) -> void
Inserts a new entry into the xtd::collections::generic::ordered_dictionary collection with the specif...
Definition ordered_dictionary.hpp:346
ordered_dictionary(input_iterator_t first, input_iterator_t last)
Initializes instance of the xtd::collections::generic::ordered_dictionary <key_t, value_t> class from...
Definition ordered_dictionary.hpp:169
ordered_dictionary(iterable_t &&items)
Initializes a new instance of the xtd::iterable that contains elements copied from the specified coll...
Definition ordered_dictionary.hpp:113
virtual auto items() const noexcept -> const base_type &
Definition ordered_dictionary.hpp:218
xtd::collections::generic::key_value_pair< key_type, mapped_type > base_value_type
Represents the dictionary base value type.
Definition ordered_dictionary.hpp:46
ordered_dictionary(xtd::usize capacity, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:147
auto operator()(xtd::usize index) -> value_t &
Sets the value at the specified index.
Definition ordered_dictionary.hpp:464
auto contains_key(const key_t &key) const noexcept -> bool override
Determines whether the xtd::collections::generic::ordered_dictionary <key_t, value_t> contains the sp...
Definition ordered_dictionary.hpp:287
auto remove(const key_t &key) noexcept -> bool override
Removes the value with the specified key from the xtd::collections::generic::ordered_dictionary <key_...
Definition ordered_dictionary.hpp:367
ordered_dictionary() noexcept=default
Initializes a new instance of the xtd::collections::generic::ordered_dictionary class.
ordered_dictionary(const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:159
ordered_dictionary(ordered_dictionary &&other) noexcept=default
Initializes instance of the xtd::collections::generic::ordered_dictionary <key_t, value_t> class from...
typename xtd::collections::generic::idictionary< key_type, mapped_type >::key_collection key_collection
Represents the idictionary key collection type.
Definition ordered_dictionary.hpp:52
auto keys() const noexcept -> key_collection override
Gets a collection containing the keys in the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:227
ordered_dictionary(xtd::usize capacity)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:134
ordered_dictionary(const xtd::collections::generic::ienumerable< value_type > &collection, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:121
auto try_get_value(const key_t &key, value_t &value) const -> bool override
Gets the value associated with the specified key.
Definition ordered_dictionary.hpp:410
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
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:40
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.hpp:115
Contains xtd::collections::generic::dictionary <key_t, value_t> class.
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
std::tuple_element_t< 1, iterator_value_t< input_iterator_t > > iterator_mapped_t
Represents the mapped iterator type.
Definition iterator.hpp:57
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ 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
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition lock.hpp:68
constexpr auto npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ other
The operating system is other.
Definition platform_id.hpp:60
Contains iteraors aliases.
Contains xtd::collections::generic::list <type_t> class.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
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
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:262
auto last() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:287
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:39
Defines a key/value pair that can be set or retrieved.
Definition key_value_pair.hpp:37
Contains xtd::threading::lock class.