xtd 0.2.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"
10
12namespace xtd {
14 namespace collections {
16 namespace generic {
32 template<class key_t, class value_t, class allocator_t = xtd::collections::generic::helpers::allocator<std::pair<const key_t, value_t>>>
34 public:
36
57
59
76 ordered_dictionary() noexcept = default;
85 data_->items = dictionary;
86 data_->keys.capacity(dictionary.count());
87 for (const auto& item : dictionary)
88 data_->keys.add(item.first);
89 }
90
99 data_->items = base_type(dictionary, comparer);
100 data_->keys.capacity(dictionary.count());
101 for (const auto& item : dictionary)
102 data_->keys.add(item.first);
103 }
104
107 ordered_dictionary(const xtd::collections::generic::ienumerable < value_type >& collection) {
108 for (const auto& item : collection)
109 add(item);
110 }
111
115 ordered_dictionary(const xtd::collections::generic::ienumerable < value_type >& collection, const xtd::collections::generic::iequality_comparer<key_type>& comparer) {
116 data_->items = base_type(comparer);
117 for (const auto& item : collection)
118 add(item);
119 }
120
128 ordered_dictionary(size_t capacity) {
129 data_->keys.capacity(capacity);
130 data_->items.ensure_capacity(capacity);
131 }
132
142 data_->items = base_type(capacity, comparer);
143 data_->keys.capacity(capacity);
144 }
145
156
162 template < class input_iterator_t >
163 explicit ordered_dictionary(input_iterator_t first, input_iterator_t last) {
164 for (auto iterator = first; iterator != last; ++iterator)
165 add(*iterator);
166 }
167
170 ordered_dictionary(const ordered_dictionary & other) noexcept : data_(xtd::new_ptr<dictionary_data>(other.data_->items, other.data_->version)) {}
175 ordered_dictionary(std::initializer_list<base_value_type> init) {
176 for (const auto& [key, value] : init)
177 add(key, value);
178 }
186 template < class init_key_t, class init_value_t >
187 explicit ordered_dictionary(std::initializer_list<key_value_pair<init_key_t, init_value_t>> init) {
188 for (const auto& [key, value] : init)
189 add(key, value);
190 }
191
192
194
201 auto count() const noexcept -> size_type override {return data_->items.count();}
202
206 auto comparer() const noexcept -> const iequality_comparer < key_t >& {
207 return data_->items.comparer();
208 }
209
212 virtual auto items() const noexcept -> const base_type& {return data_->items;}
215 virtual auto items() noexcept -> base_type& {return data_->items;}
216
221 auto keys() const noexcept -> key_collection override {
222 auto keys = key_collection {};
223 for (const auto& key : data_->keys)
224 keys.add(key);
225 return keys;
226 }
227
232 auto values() const noexcept -> value_collection override {
233 auto values = value_collection {};
234 for (const auto& key : data_->keys)
235 values.add(data_->items[key]);
236 return values;
237 }
238
239
241
249 auto add(const key_t & key, const value_t& value) -> void override {
250 insert(count(), key, value);
251 }
252
256 auto add(const value_type & item) -> void override {
257 insert(count(), item.key(), item.value());
258 }
259
262 auto clear() noexcept -> void override {
263 lock_(data_->sync_op) {
264 data_->keys.clear();
265 data_->items.clear();
266 }
267 ++data_->version;
268 }
269
273 auto contains(const value_type & item) const noexcept -> bool override {
274 return data_->items.contains(item);
275 }
276
281 auto contains_key(const key_t & key) const noexcept -> bool override {
282 return data_->items.contains_key(key);
283 }
284
289 auto contains_value(const value_t& value) const noexcept -> bool {
290 return data_->items.contains_value(value);
291 }
292
297 auto copy_to(xtd::array<value_type>& array, xtd::size array_index) const -> void override {
299 for (const auto& item : self_)
300 array[array_index++] = item;
301 }
302
306 struct ordered_dictionary_enumerator : public ienumerator<value_type> {
307 explicit ordered_dictionary_enumerator(const ordered_dictionary & items, xtd::size version) : items_(items), version_(version) {}
308
309 const value_type& current() const override {
311 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
312 return (value_ = value_type {key_t {items_.data_->keys[index_]}, value_t {items_.data_->items[items_.data_->keys[index_]]}});
313 }
314
315 bool move_next() override {
316 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
317 return ++index_ < items_.data_->keys.count();
318 }
319
320 void reset() override {
321 version_ = items_.data_->version;
322 index_ = xtd::npos;
323 }
324
325 private:
326 size_type index_ = xtd::npos;
327 const ordered_dictionary& items_;
328 mutable value_type value_;
329 size_type version_ = 0;
330 };
331
332 return {new_ptr<ordered_dictionary_enumerator>(self_, data_->version)};
333 }
334
340 auto insert(xtd::size index, const key_t & key) -> void {insert(index, key, value_t {});}
341
348 auto insert(xtd::size index, const key_t & key, const value_t& value) -> void {
350 lock_(data_->sync_op) {
351 data_->items.add(key, value);
352 data_->keys.insert(index, key);
353 }
354 ++data_->version;
355 }
356
361 auto remove(const key_t & key) noexcept -> bool override {
362 if (!contains_key(key)) return false;
363 lock_(data_->sync_op) {
364 remove_at(get_index(key));
365 }
366 ++data_->version;
367 return true;
368 }
369
373 auto remove(const value_type & item) noexcept -> bool override {
374 lock_(data_->sync_op) {
375 auto result = data_->items.remove(item.first);
376 if (!result) return false;
377 data_->keys.remove_at(get_index(item.first));
378 }
379 ++data_->version;
380 return true;
381 }
382
388 auto remove_at(xtd::size index) -> void {
389 lock_(data_->sync_op) {
390 data_->items.remove(data_->keys[index]);
391 data_->keys.remove_at(index);
392 }
393 ++data_->version;
394 }
395
398 auto to_string() const noexcept -> xtd::string override {return xtd::string::format("{{{}}}", xtd::string::join(", ", self_));}
399
404 auto try_get_value(const key_t & key, value_t& value) const -> bool override {
405 return data_->items.try_get_value(key, value);
406 }
407
408
410
420 *data_ = *other.data_;
421 return self_;
422 }
423
426 auto operator =(std::initializer_list<base_value_type> ilist) -> ordered_dictionary& {
427 clear();
428 for (const auto& [key, value] : ilist)
429 add(key, value);
430 return self_;
431 }
432
435 template < class init_key_t, class init_value_t >
437 clear();
438 for (const auto& [key, value] : ilist)
439 add(key, value);
440 return self_;
441 }
442
450 auto operator()(xtd::size index) const -> const value_t& {return operator [](data_->keys[index]);}
458 auto operator()(xtd::size index) -> value_t& {return operator [](data_->keys[index]);}
459
467 const value_t& operator [](const key_t & key) const override {return data_->items[key];}
474 value_t& operator [](const key_t & key) override {
475 auto iterator = data_->items.items().find(key);
476 if (iterator != data_->items.items().end())
477 return iterator->second;
478 data_->keys.add(key);
479 return data_->items[key];
480 }
481
484 operator const base_type& () const noexcept {return data_->items;}
487 operator base_type& () noexcept {return data_->items;}
489
490 private:
491 auto get_index(const key_t & key) const noexcept -> xtd::size {
492 auto index = xtd::size {0};
493 for (const auto& item_key : data_->keys) {
494 if (item_key == key) return index;
495 ++index;
496 }
497 return xtd::npos;
498 }
499
500 auto is_read_only() const noexcept -> bool override {return false;}
501 auto is_synchronized() const noexcept -> bool override {return false;}
502 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
503
504 struct dictionary_data {
505 dictionary_data() noexcept = default;
506 dictionary_data(const base_type & items, size_type version) noexcept : items {items}, version {version} {
507 for (const auto& item : this->items)
508 keys.add(item.first);
509 }
510 dictionary_data(base_type&& items, size_type version) noexcept : items {items}, version {version} {
511 for (const auto& item : this->items)
512 keys.add(item.first);
513 }
514
515 list_type keys;
516 base_type items;
517 size_type version = 0;
518 xtd::object sync_root;
519 xtd::object sync_op;
520 };
522 };
523
525 // Deduction guides for xtd::collections::specialized::ordered_dictionary
526 // {
527 template < class key_t, class value_t >
528 ordered_dictionary(xtd::collections::generic::idictionary < key_t, value_t >) -> ordered_dictionary < key_t, value_t>;
529
530 template < class key_t, class value_t >
531 ordered_dictionary(xtd::collections::generic::ienumerable < key_value_pair < key_t, value_t>>) -> ordered_dictionary<key_t, value_t>;
532
533 template < class key_t, class value_t >
534 ordered_dictionary(std::initializer_list < key_value_pair < key_t, value_t>>) -> ordered_dictionary < key_t, value_t >;
535
536 template < class key_t, class value_t >
537 ordered_dictionary(std::initializer_list < std::pair < key_t, value_t>>) -> ordered_dictionary < key_t, value_t >;
538
539 template < class input_iterator_t >
540 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 >>;
541 // }
543 }
544 }
545}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents a collection of keys and values.
Definition dictionary.hpp:67
auto keys() const noexcept -> key_collection override
Gets a collection containing the keys in the xtd::collections::generic::dictionary <key_t,...
Definition dictionary.hpp:302
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:289
Represents a generic collection of key/value pairs.
Definition idictionary.hpp:44
xtd::collections::generic::list< mapped_type > value_collection
Definition idictionary.hpp:62
xtd::collections::generic::list< key_type > key_collection
Definition idictionary.hpp:60
typename xtd::collections::generic::icollection< value_type >::iterator iterator
Definition idictionary.hpp:56
typename xtd::collections::generic::icollection< xtd::collections::generic::key_value_pair< xtd::any_object, xtd::any_object > >::value_type value_type
Definition idictionary.hpp:54
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 strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
Represents a collection of key/value pairs that are accessible by the key or index.
Definition ordered_dictionary.hpp:33
auto clear() noexcept -> void override
Removes all keys and values from the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:262
const value_t & operator[](const key_t &key) const override
Gets the element with the specified key.
Definition ordered_dictionary.hpp:467
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:249
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:305
auto to_string() const noexcept -> xtd::string override
Gets a string that represents the current object.
Definition ordered_dictionary.hpp:398
auto insert(xtd::size 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:340
auto operator()(xtd::size index) -> value_t &
Sets the value at the specified index.
Definition ordered_dictionary.hpp:458
xtd::size size_type
Represents the dictionary size type.
Definition ordered_dictionary.hpp:45
xtd::collections::generic::dictionary< key_type, mapped_type > base_type
Represents the dictionary base type.
Definition ordered_dictionary.hpp:49
typename xtd::collections::generic::idictionary< key_type, mapped_type >::value_collection value_collection
Represents the idictionary value collection type.
Definition ordered_dictionary.hpp:55
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:273
auto remove_at(xtd::size index) -> void
Removes the entry at the specified index from the OrderedDictionary collection.
Definition ordered_dictionary.hpp:388
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:201
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:289
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:373
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:107
auto operator()(xtd::size index) const -> const value_t &
Gets the value at the specified index.
Definition ordered_dictionary.hpp:450
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:170
xtd::collections::generic::list< key_type > list_type
Represents the dictionary base type.
Definition ordered_dictionary.hpp:51
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:98
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:43
auto add(const value_type &item) -> void override
Adds an item to the xtd::collections::generic::icollection <type_t>.
Definition ordered_dictionary.hpp:256
virtual auto items() noexcept -> base_type &
Returns the underlying base type items.
Definition ordered_dictionary.hpp:215
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:232
ordered_dictionary(size_t 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:141
auto comparer() const noexcept -> const iequality_comparer< xtd::any_object > &
Definition ordered_dictionary.hpp:206
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:187
typename xtd::collections::generic::idictionary< key_t, value_t >::mapped_type mapped_type
Represents the dictionary mapped type.
Definition ordered_dictionary.hpp:41
typename xtd::collections::generic::idictionary< key_t, value_t >::key_type key_type
Represents the dictionary key type.
Definition ordered_dictionary.hpp:39
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:163
virtual auto items() const noexcept -> const base_type &
Returns the underlying base type items.
Definition ordered_dictionary.hpp:212
xtd::collections::generic::key_value_pair< key_type, mapped_type > base_value_type
Represents the dictionary base value type.
Definition ordered_dictionary.hpp:47
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:281
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:361
auto insert(xtd::size 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:348
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:153
ordered_dictionary(ordered_dictionary &&other) noexcept=default
Initializes instance of the xtd::collections::generic::ordered_dictionary <key_t, value_t> class from...
ordered_dictionary(size_t capacity)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:128
auto copy_to(xtd::array< value_type > &array, xtd::size array_index) const -> void override
Copies the elements of the xtd::collections::generic::icollection <type_t> to an xtd::array,...
Definition ordered_dictionary.hpp:297
typename xtd::collections::generic::idictionary< key_type, mapped_type >::key_collection key_collection
Represents the idictionary key collection type.
Definition ordered_dictionary.hpp:53
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:221
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:115
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:404
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
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.
Contains xtd::collections::generic::key_not_found_exception exception.
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:67
constexpr xtd::size npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
ptr< type_t > new_ptr(args_t &&... args)
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
@ add
The Add key.
Definition console_key.hpp:170
@ insert
The INS (INSERT) key.
Definition console_key.hpp:62
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
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
read_only_span< type_t, count > last() const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:307
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38
Defines a key/value pair that can be set or retrieved.
Definition key_value_pair.hpp:37
Contains xtd::threading::lock class.