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
114 ordered_dictionary(const xtd::collections::generic::ienumerable < value_type >& collection, const xtd::collections::generic::iequality_comparer<key_type>& comparer) {
115 data_->items = base_type(comparer);
116 for (const auto& item : collection)
117 add(item);
118 }
119
128 data_->keys.capacity(capacity);
129 data_->items.ensure_capacity(capacity);
130 }
131
141 data_->items = base_type(capacity, comparer);
142 data_->keys.capacity(capacity);
143 }
144
155
161 template < class input_iterator_t >
162 explicit ordered_dictionary(input_iterator_t first, input_iterator_t last) {
163 for (auto iterator = first; iterator != last; ++iterator)
164 add(*iterator);
165 }
166
169 ordered_dictionary(const ordered_dictionary & other) noexcept : data_(xtd::new_ptr<dictionary_data>(other.data_->items, other.data_->version)) {}
174 ordered_dictionary(std::initializer_list<base_value_type> init) {
175 for (const auto& [key, value] : init)
176 add(key, value);
177 }
185 template < class init_key_t, typename init_value_t >
186 explicit ordered_dictionary(std::initializer_list<key_value_pair<init_key_t, init_value_t>> init) {
187 for (const auto& [key, value] : init)
188 add(key, value);
189 }
190
191
193
200 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.count();}
201
205 [[nodiscard]] auto comparer() const noexcept -> const iequality_comparer < key_t >& {
206 return data_->items.comparer();
207 }
208
211 [[nodiscard]] virtual auto items() const noexcept -> const base_type& {return data_->items;}
214 [[nodiscard]] virtual auto items() noexcept -> base_type& {return data_->items;}
215
220 [[nodiscard]] auto keys() const noexcept -> key_collection override {
221 auto keys = key_collection {};
222 for (const auto& key : data_->keys)
223 keys.add(key);
224 return keys;
225 }
226
231 [[nodiscard]] auto values() const noexcept -> value_collection override {
232 auto values = value_collection {};
233 for (const auto& key : data_->keys)
234 values.add(data_->items[key]);
235 return values;
236 }
237
238
240
248 auto add(const key_t & key, const value_t& value) -> void override {
249 insert(count(), key, value);
250 }
251
255 auto add(const value_type & item) -> void override {
256 insert(count(), item.key(), item.value());
257 }
258
261 auto clear() noexcept -> void override {
262 lock_(data_->sync_op) {
263 data_->keys.clear();
264 data_->items.clear();
265 }
266 ++data_->version;
267 }
268
272 [[nodiscard]] auto contains(const value_type & item) const noexcept -> bool override {
273 return data_->items.contains(item);
274 }
275
280 [[nodiscard]] auto contains_key(const key_t & key) const noexcept -> bool override {
281 return data_->items.contains_key(key);
282 }
283
288 [[nodiscard]] auto contains_value(const value_t& value) const noexcept -> bool {
289 return data_->items.contains_value(value);
290 }
291
296 auto copy_to(xtd::array<value_type>& array, xtd::usize array_index) const -> void override {
298 for (const auto& item : self_)
299 array[array_index++] = item;
300 }
301
305 struct ordered_dictionary_enumerator : public ienumerator<value_type> {
306 explicit ordered_dictionary_enumerator(const ordered_dictionary & items, xtd::usize version) : items_(items), version_(version) {}
307
308 [[nodiscard]] const value_type& current() const override {
310 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
311 return (value_ = value_type {key_t {items_.data_->keys[index_]}, value_t {items_.data_->items[items_.data_->keys[index_]]}});
312 }
313
314 [[nodiscard]] bool move_next() override {
315 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
316 return ++index_ < items_.data_->keys.count();
317 }
318
319 void reset() override {
320 version_ = items_.data_->version;
321 index_ = xtd::npos;
322 }
323
324 private:
325 size_type index_ = xtd::npos;
326 const ordered_dictionary& items_;
327 mutable value_type value_;
328 size_type version_ = 0;
329 };
330
331 return {new_ptr<ordered_dictionary_enumerator>(self_, data_->version)};
332 }
333
339 auto insert(xtd::usize index, const key_t & key) -> void {insert(index, key, value_t {});}
340
347 auto insert(xtd::usize index, const key_t & key, const value_t& value) -> void {
349 lock_(data_->sync_op) {
350 data_->items.add(key, value);
351 data_->keys.insert(index, key);
352 }
353 ++data_->version;
354 }
355
360 auto remove(const key_t & key) noexcept -> bool override {
361 if (!contains_key(key)) return false;
362 lock_(data_->sync_op) {
363 remove_at(get_index(key));
364 }
365 ++data_->version;
366 return true;
367 }
368
372 auto remove(const value_type & item) noexcept -> bool override {
373 lock_(data_->sync_op) {
374 auto result = data_->items.remove(item.first);
375 if (!result) return false;
376 data_->keys.remove_at(get_index(item.first));
377 }
378 ++data_->version;
379 return true;
380 }
381
387 auto remove_at(xtd::usize index) -> void {
388 lock_(data_->sync_op) {
389 data_->items.remove(data_->keys[index]);
390 data_->keys.remove_at(index);
391 }
392 ++data_->version;
393 }
394
397 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("{{{}}}", xtd::string::join(", ", self_));}
398
403 [[nodiscard]] auto try_get_value(const key_t & key, value_t& value) const -> bool override {
404 return data_->items.try_get_value(key, value);
405 }
406
407
409
419 *data_ = *other.data_;
420 return self_;
421 }
422
425 auto operator =(std::initializer_list<base_value_type> ilist) -> ordered_dictionary& {
426 clear();
427 for (const auto& [key, value] : ilist)
428 add(key, value);
429 return self_;
430 }
431
434 template < class init_key_t, typename init_value_t >
436 clear();
437 for (const auto& [key, value] : ilist)
438 add(key, value);
439 return self_;
440 }
441
449 auto operator()(xtd::usize index) const -> const value_t& {return operator [](data_->keys[index]);}
457 auto operator()(xtd::usize index) -> value_t& {return operator [](data_->keys[index]);}
458
466 const value_t& operator [](const key_t & key) const override {return data_->items[key];}
473 value_t& operator [](const key_t & key) override {
474 auto iterator = data_->items.items().find(key);
475 if (iterator != data_->items.items().end())
476 return iterator->second;
477 data_->keys.add(key);
478 return data_->items[key];
479 }
480
483 operator const base_type& () const noexcept {return data_->items;}
486 operator base_type& () noexcept {return data_->items;}
488
489 private:
490 auto get_index(const key_t & key) const noexcept -> xtd::usize {
491 auto index = xtd::usize {0};
492 for (const auto& item_key : data_->keys) {
493 if (item_key == key) return index;
494 ++index;
495 }
496 return xtd::npos;
497 }
498
499 auto is_read_only() const noexcept -> bool override {return false;}
500 auto is_synchronized() const noexcept -> bool override {return false;}
501 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
502
503 struct dictionary_data {
504 dictionary_data() noexcept = default;
505 dictionary_data(const base_type & items, size_type version) noexcept : items {items}, version {version} {
506 for (const auto& item : this->items)
507 keys.add(item.first);
508 }
509 dictionary_data(base_type&& items, size_type version) noexcept : items {items}, version {version} {
510 for (const auto& item : this->items)
511 keys.add(item.first);
512 }
513
514 list_type keys;
515 base_type items;
516 size_type version = 0;
517 xtd::object sync_root;
518 xtd::object sync_op;
519 };
521 };
522
524 // Deduction guides for xtd::collections::specialized::ordered_dictionary
525 // {
526 template < class key_t, typename value_t >
527 ordered_dictionary(xtd::collections::generic::idictionary < key_t, value_t >) -> ordered_dictionary < key_t, value_t>;
528
529 template < class key_t, typename value_t >
530 ordered_dictionary(xtd::collections::generic::ienumerable < key_value_pair < key_t, value_t>>) -> ordered_dictionary<key_t, value_t>;
531
532 template < class key_t, typename value_t >
533 ordered_dictionary(std::initializer_list < key_value_pair < key_t, value_t>>) -> ordered_dictionary < key_t, value_t >;
534
535 template < class key_t, typename value_t >
536 ordered_dictionary(std::initializer_list < std::pair < key_t, value_t>>) -> ordered_dictionary < key_t, value_t >;
537
538 template < class input_iterator_t >
539 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 >>;
540 // }
542 }
543 }
544}
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:1258
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:298
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:285
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 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: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:261
const value_t & operator[](const key_t &key) const override
Gets the element with the specified key.
Definition ordered_dictionary.hpp:466
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:347
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:248
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:304
auto to_string() const noexcept -> xtd::string override
Gets a string that represents the current object.
Definition ordered_dictionary.hpp:397
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:272
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:200
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:288
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:372
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:169
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:387
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:255
virtual auto items() noexcept -> base_type &
Returns the underlying base type items.
Definition ordered_dictionary.hpp:214
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:231
auto comparer() const noexcept -> const iequality_comparer< xtd::any_object > &
Definition ordered_dictionary.hpp:205
auto operator()(xtd::usize index) const -> const value_t &
Gets the value at the specified index.
Definition ordered_dictionary.hpp:449
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:186
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:296
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:339
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:162
virtual auto items() const noexcept -> const base_type &
Returns the underlying base type items.
Definition ordered_dictionary.hpp:211
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:140
auto operator()(xtd::usize index) -> value_t &
Sets the value at the specified index.
Definition ordered_dictionary.hpp:457
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:280
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:360
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:152
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:220
ordered_dictionary(xtd::usize capacity)
Initializes a new instance of the xtd::collections::generic::ordered_dictionary <key_t,...
Definition ordered_dictionary.hpp:127
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:114
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:403
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:45
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:249
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:274
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.