xtd 1.0.0
Loading...
Searching...
No Matches
concurrent_dictionary.hpp
Go to the documentation of this file.
1
4#pragma once
9#include "../../object.hpp"
10
12namespace xtd {
14 namespace collections {
17 namespace concurrent {
36 template<typename key_t, typename value_t, typename hasher_t = xtd::collections::generic::helpers::hasher<key_t>, typename equator_t = xtd::collections::generic::helpers::equator<key_t>, typename allocator_t = xtd::collections::generic::helpers::allocator<std::pair<const key_t, value_t>>>
38 public:
40
59
61
69 lock_guard_(items_)
70 for (const auto& item : collection)
71 items_.add(item);
72 }
73
76 lock_guard_(items_)
77 for (const auto& item : collection)
78 items_add(item);
79 }
80
87 lock_guard_(items_)
88 for (const auto& item : collection)
89 items_.add(item);
90 }
91
95 lock_guard_(items_)
96 for (const auto& item : collection)
97 items_add(item);
98 }
99
102 concurrent_dictionary(xtd::usize concurrent_level, xtd::usize capacity) : items_ {capacity}, concurrent_level_ {concurrent_level} {}
108 lock_guard_(items_)
109 for (const auto& item : collection)
110 items_.add(item);
111 }
112
117 lock_guard_(items_)
118 for (const auto& item : collection)
119 items_add(item);
120 }
121
125 concurrent_dictionary(xtd::usize concurrent_level, xtd::usize capacity, const xtd::collections::generic::iequality_comparer<key_type>& comparer) : items_ {capacity, comparer}, concurrent_level_ {concurrent_level} {}
133 concurrent_dictionary(std::initializer_list<std::pair<key_t, value_t>> items) {
134 lock_guard_(items_) {
135 items_.ensure_capacity(items.size());
136 for (const auto& [key, value] : items)
137 items_.add(key, value);
138 }
139 }
140
142 concurrent_dictionary(std::initializer_list<value_type> items) {
143 lock_guard_(items_) {
144 items_.ensure_capacity(items.size());
145 for (const auto& item : items)
146 items_.add(item);
147 }
148 }
149
150
152
157 [[nodiscard]] auto comparer() const noexcept -> const xtd::collections::generic::iequality_comparer<key_t>& {
158 return items_.comparer();
159 }
160
163 [[nodiscard]] auto count() const noexcept -> xtd::usize override {
164 lock_guard_(items_)
165 return items_.count();
167 }
168
172 [[nodiscard]] auto is_empty() const noexcept -> bool {
173 lock_guard_(items_)
174 return items_.is_empty();
176 }
177
181 [[nodiscard]] auto is_read_only() const noexcept -> bool override {return false;}
182
197 [[nodiscard]] auto is_synchronized() const noexcept -> bool override {return true;}
198
202 auto keys() const noexcept -> key_collection override {
203 lock_guard_(items_)
204 return items_.keys();
206 }
207
211 auto values() const noexcept -> value_collection override {
212 lock_guard_(items_)
213 return items_.values();
215 }
216
217
219
227 auto add_or_update(const key_t& key, xtd::callable<value_t, key_t> auto&& add_value_factory, xtd::callable<value_t, key_t, value_t> auto&& update_value_factory) -> const value_t& {
228 lock_guard_(items_) {
229 if (!items_.contains_key(key)) items_.add(key, add_value_factory(key));
230 else items_[key] = update_value_factory(key, items_[key]);
231 return items_[key];
232 }
234 }
235
240 auto add_or_update(const key_t& key, const value_t& add_value, xtd::callable<value_t, key_t, value_t> auto&& update_value_factory) -> const value_t& {
241 lock_guard_(items_) {
242 if (!items_.contains_key(key)) items_.add(key, add_value);
243 else items_[key] = update_value_factory(key, items_[key]);
244 return items_[key];
245 }
247 }
248
255 template<typename arg_t>
256 auto add_or_update(const key_t& key, xtd::callable<value_t, key_t, arg_t> auto&& add_value_factory, xtd::callable<value_t, key_t, value_t, arg_t> auto&& update_value_factory, arg_t&& factory_argument) -> const value_t& {
257 lock_guard_(items_) {
258 if (!items_.contains_key(key)) items_.add(key, add_value_factory(key, factory_argument));
259 else items_[key] = update_value_factory(key, items_[key], factory_argument);
260 return items_[key];
261 }
263 }
264
266 auto clear() -> void override {
267 lock_guard_(items_)
268 items_.clear();
269 }
270
274 auto contains_key(const key_t& key) const noexcept -> bool override {
275 lock_guard_(items_)
276 return items_.contains_key(key);
278 }
279
284 auto copy_to(xtd::array<value_type>& array, xtd::usize array_index) const -> void override {
285 lock_guard_(items_)
286 items_.copy_to(array, array_index);
287 }
288
292 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
293 static thread_local auto items = xtd::array<value_type> {};
294 items = to_array();
295 return items.get_enumerator();
296 }
297
302 [[nodiscard]] auto get_or_add(const key_t& key, xtd::callable<value_t, key_t> auto&& add_value_factory) -> const value_t& {
303 lock_guard_(items_) {
304 if (!items_.contains_key(key)) items_.add(key, add_value_factory(key));
305 return items_[key];
306 }
308 }
309
313 [[nodiscard]] auto get_or_add(const key_t& key, const value_t& add_value) -> const value_t& {
314 lock_guard_(items_) {
315 if (!items_.contains_key(key)) items_.add(key, add_value);
316 return items_[key];
317 }
319 }
320
325 template<typename arg_t>
326 [[nodiscard]] auto get_or_add(const key_t& key, xtd::callable<value_t, key_t, arg_t> auto&& add_value_factory, arg_t&& factory_argument) -> const value_t& {
327 lock_guard_(items_) {
328 if (!items_.contains_key(key)) items_.add(key, add_value_factory(key, factory_argument));
329 return items_[key];
330 }
332 }
333
337 [[nodiscard]] auto to_array() const -> xtd::array<value_type> {
338 lock_guard_(items_)
339 return items_.to_array();
341 }
342
345 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {
346 return to_array().to_string();
347 }
348
353 auto try_add(const key_t& key, const value_t& value) -> bool {
354 lock_guard_(items_) {
355 if (items_.contains_key(key)) return false;
356 items_.add(key, value);
357 return true;
358 }
360 }
361
365 auto try_get_value(const key_t& key, value_t& value) const -> bool override {
366 lock_guard_(items_)
367 return items_.try_get_value(key, value);
369 }
370
374 auto try_remove(const key_t& key) -> bool {
375 auto value = value_t {};
376 return try_remove(key, value);
377 }
378
382 auto try_remove(const key_t& key, value_t& value) -> bool {
383 lock_guard_(items_) {
384 if (!items_.try_get_value(key, value)) return false;
385 return items_.remove(key);
386 }
388 }
389
395 lock_guard_(items_) {
396 if (!items_.contains(item)) return false;
397 return items_.remove(item);
398 }
400 }
401
407 auto try_update(const key_t& key, const value_t& new_value, const value_t& comparison_value) -> bool {
408 lock_guard_(items_) {
409 if (!items_.contains({key, comparison_value})) return false;
410 items_[key] = new_value;
411 return true;
412 }
414 }
415
416
418
427 auto operator [](const key_t& key) const -> const value_t& override {
428 lock_guard_(items_)
429 return items_.operator [](key);
431 }
432
438 auto operator [](const key_t& key) -> value_t& override {
439 lock_guard_(items_)
440 return items_.operator [](key);
442 }
443
444
445 private:
446 auto sync_root() const noexcept -> const xtd::object& override {return xtd::as<xtd::collections::generic::idictionary<key_t, value_t>>(items_).sync_root();}
447
448 auto add(const xtd::collections::generic::key_value_pair<key_t, value_t>& item) -> void override {
449 lock_guard_(items_)
450 items_.add(item);
451 }
452 auto add(const key_t& key, const value_t& value) -> void override {
453 lock_guard_(items_)
454 items_.add(key, value);
455 }
456
457 auto contains(const xtd::collections::generic::key_value_pair<key_t, value_t>& item) const noexcept -> bool override {
458 lock_guard_(items_)
459 return items_.contains(item);
461 }
462
463 auto remove(const xtd::collections::generic::key_value_pair<key_t, value_t>& item) -> bool override {
464 lock_guard_(items_)
465 return items_.remove(item);
467 }
468
469 auto remove(const key_t& key) noexcept -> bool override {
470 lock_guard_(items_)
471 return items_.remove(key);
473 }
474
475 collection_type items_;
476 xtd::usize concurrent_level_ = xtd::environment::processor_count();
477 };
478
480 // Deduction guides for xtd::collections::concurrent::concurrent_dictionary
481 // {
482 template<typename key_t, typename value_t >
483 concurrent_dictionary(xtd::collections::generic::idictionary<key_t, value_t>) -> concurrent_dictionary<key_t, value_t>;
484
485 template<typename key_t, typename value_t >
486 concurrent_dictionary(xtd::collections::generic::ienumerable<xtd::collections::generic::key_value_pair<key_t, value_t>>) -> concurrent_dictionary<key_t, value_t>;
487
488 template<xtd::iterable iterable_t>
489 concurrent_dictionary(iterable_t&&) -> concurrent_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>>>>;
490
491 template<typename key_t, typename value_t >
492 concurrent_dictionary(std::initializer_list<xtd::collections::generic::key_value_pair<key_t, value_t>>) -> concurrent_dictionary<key_t, value_t>;
493
494 template<typename key_t, typename value_t >
495 concurrent_dictionary(std::initializer_list<std::pair<key_t, value_t>>) -> concurrent_dictionary<key_t, value_t>;
496
497 template<typename input_iterator_t >
499 // }
501 }
502 }
503}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concu...
Definition concurrent_dictionary.hpp:37
auto clear() -> void override
Removes all keys and values from the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:266
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition concurrent_dictionary.hpp:292
concurrent_dictionary(xtd::iterable auto &&collection, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:94
auto try_update(const key_t &key, const value_t &new_value, const value_t &comparison_value) -> bool
Updates the value associated with key to new_value if the existing value with key is equal to compari...
Definition concurrent_dictionary.hpp:407
auto is_empty() const noexcept -> bool
Gets a value that indicates whether the xtd::collections::concurrent::concurrent_dictionary <key_t,...
Definition concurrent_dictionary.hpp:172
concurrent_dictionary(const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:82
auto is_read_only() const noexcept -> bool override
Gets a value indicating whether the xtd::collections::concurrent::concurrent_dictionary <key_t,...
Definition concurrent_dictionary.hpp:181
auto get_or_add(const key_t &key, const value_t &add_value) -> const value_t &
Adds a key/value pair to the xtd::collections::concurrent::concurrent_dictionary <key_t,...
Definition concurrent_dictionary.hpp:313
typename xtd::collections::generic::idictionary< key_t, value_t >::key_type key_type
Represents the dictionary key type.
Definition concurrent_dictionary.hpp:43
concurrent_dictionary(xtd::usize concurrent_level, xtd::usize capacity, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:125
concurrent_dictionary(xtd::usize concurrent_level, xtd::collections::generic::ienumerable< value_type > &&collection, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:107
auto try_remove(const key_t &key, value_t &value) -> bool
Attempts to remove and return the value that has the specified key from the xtd::collections::concurr...
Definition concurrent_dictionary.hpp:382
auto keys() const noexcept -> key_collection override
Gets a collection containing the keys in the xtd::collections::generic::ictionary<key_t,...
Definition concurrent_dictionary.hpp:202
typename xtd::collections::generic::idictionary< key_type, mapped_type >::value_type value_type
Represents the dictionary value type.
Definition concurrent_dictionary.hpp:47
auto try_remove(const xtd::collections::generic::key_value_pair< key_t, value_t > &item) -> bool
Removes a key and value from the dictionary. @pram item The xtd::collections::generic::key_value_pair...
Definition concurrent_dictionary.hpp:394
auto get_or_add(const key_t &key, xtd::callable< value_t, key_t > auto &&add_value_factory) -> const value_t &
Adds a key/value pair to the xtd::collections::concurrent::concurrent_dictionary <key_t,...
Definition concurrent_dictionary.hpp:302
auto count() const noexcept -> xtd::usize override
Gets the number of elements contained in the xtd::collections::generic::icollection.
Definition concurrent_dictionary.hpp:163
const value_type & const_reference
Represents the const reference of list value type.
Definition concurrent_dictionary.hpp:51
concurrent_dictionary(xtd::usize concurrent_level, xtd::iterable auto &&collection, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:116
auto try_remove(const key_t &key) -> bool
Attempts to remove and return the value that has the specified key from the xtd::collections::concurr...
Definition concurrent_dictionary.hpp:374
concurrent_dictionary(xtd::iterable auto &&collection)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:75
auto to_array() const -> xtd::array< value_type >
Copies the elements contained in the xtd::collections::concurrent::concurrent_dictionary <key_t,...
Definition concurrent_dictionary.hpp:337
auto add_or_update(const key_t &key, xtd::callable< value_t, key_t, arg_t > auto &&add_value_factory, xtd::callable< value_t, key_t, value_t, arg_t > auto &&update_value_factory, arg_t &&factory_argument) -> const value_t &
Uses the specified functions and argument to add a key/value pair to the xtd::collections::concurrent...
Definition concurrent_dictionary.hpp:256
auto values() const noexcept -> value_collection override
Gets a collection that contains the values in the xtd::collections::generic::ictionary<key_t,...
Definition concurrent_dictionary.hpp:211
auto is_synchronized() const noexcept -> bool override
Gets a value indicating whether access to the xtd::collections::concurrent::concurrent_dictionary <ke...
Definition concurrent_dictionary.hpp:197
auto add_or_update(const key_t &key, xtd::callable< value_t, key_t > auto &&add_value_factory, xtd::callable< value_t, key_t, value_t > auto &&update_value_factory) -> const value_t &
Uses the specified functions to add a key/value pair to the xtd::collections::concurrent::concurrent_...
Definition concurrent_dictionary.hpp:227
auto try_add(const key_t &key, const value_t &value) -> bool
Attempts to add the specified key and value to the xtd::collections::concurrent::concurrent_dictionar...
Definition concurrent_dictionary.hpp:353
auto try_get_value(const key_t &key, value_t &value) const -> bool override
Attempts to get the value associated with the specified key from the xtd::collections::concurrent::co...
Definition concurrent_dictionary.hpp:365
auto add_or_update(const key_t &key, const value_t &add_value, xtd::callable< value_t, key_t, value_t > auto &&update_value_factory) -> const value_t &
Adds a key/value pair to the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:240
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition concurrent_dictionary.hpp:345
concurrent_dictionary()=default
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
auto copy_to(xtd::array< value_type > &array, xtd::usize array_index) const -> void override
Copies the elements of the xtd::collections::concurrent::concurrent_dictionary <key_t,...
Definition concurrent_dictionary.hpp:284
concurrent_dictionary(xtd::usize concurrent_level, xtd::usize capacity)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:102
concurrent_dictionary(xtd::collections::generic::ienumerable< value_type > &&collection, const xtd::collections::generic::iequality_comparer< key_type > &comparer)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:86
typename xtd::collections::generic::idictionary< key_t, value_t >::mapped_type mapped_type
Represents the dictionary mapped type.
Definition concurrent_dictionary.hpp:45
auto comparer() const noexcept -> const xtd::collections::generic::iequality_comparer< key_t > &
Gets the xtd::collections::generic::iequality_comparer <type_t> that is used to determine equality of...
Definition concurrent_dictionary.hpp:157
concurrent_dictionary(std::initializer_list< value_type > items)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition concurrent_dictionary.hpp:142
typename xtd::collections::generic::idictionary< key_type, mapped_type >::value_collection value_collection
Represents the idictionary value collection type.
Definition concurrent_dictionary.hpp:57
auto operator[](const key_t &key) const -> const value_t &override
Gets the element with the specified key.
Definition concurrent_dictionary.hpp:427
auto get_or_add(const key_t &key, xtd::callable< value_t, key_t, arg_t > auto &&add_value_factory, arg_t &&factory_argument) -> const value_t &
Adds a key/value pair to the xtd::collections::concurrent::concurrent_dictionary <key_t,...
Definition concurrent_dictionary.hpp:326
value_type & reference
Represents the reference of list value type.
Definition concurrent_dictionary.hpp:49
xtd::collections::generic::dictionary< key_t, value_t, hasher_t, equator_t, allocator_t > collection_type
Represents the concurrent bag collection type.
Definition concurrent_dictionary.hpp:53
typename xtd::collections::generic::idictionary< key_type, mapped_type >::key_collection key_collection
Represents the idictionary key collection type.
Definition concurrent_dictionary.hpp:55
concurrent_dictionary(std::initializer_list< std::pair< key_t, value_t > > items)
Initializes instance of the xtd::collections::generic::dictionary <key_t, value_t> class from a varie...
Definition concurrent_dictionary.hpp:133
auto contains_key(const key_t &key) const noexcept -> bool override
Determines whether the xtd::collections::concurrent::concurrent_dictionary<key_t,value_t> contains th...
Definition concurrent_dictionary.hpp:274
concurrent_dictionary(xtd::collections::generic::ienumerable< value_type > &&collection)
Initializes a new instance of the xtd::collections::concurrent::concurrent_dictionary<key_t,...
Definition concurrent_dictionary.hpp:68
Represents a collection of keys and values.
Definition dictionary.hpp:63
auto contains(const value_type &item) const noexcept -> bool override
Determines whether an element is in the xtd::collections::generic::dictionary <key_t,...
Definition dictionary.hpp:353
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::dictionary <key_t,...
Definition dictionary.hpp:332
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 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...
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
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:46
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.hpp:34
static auto processor_count() -> xtd::usize
Gets the number of processors on the current machine.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:40
Definition callable.hpp:11
Definition iterable.hpp:15
Contains xtd::collections::generic::dictionary <key_t, value_t> class.
Contains xtd::environment class.
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
generic::iequality_comparer< xtd::any_object > iequality_comparer
Defines methods to support the comparison of objects for equality.
Definition iequality_comparer.hpp:29
static auto unreachable() -> void
The function unreachable expands to a void expression. Executing unreachable() results in undefined b...
Definition environment.hpp:742
std::allocator< type_t > allocator
Represent an allocator alias.
Definition allocator.hpp:38
std::tuple_element_t< 1, iterator_value_t< input_iterator_t > > iterator_mapped_t
Represents the mapped iterator type.
Definition iterator.hpp:57
#define lock_guard_(object)
The lock_guard_ keyword marks a statement block_guard as a critical section by obtaining the mutual-e...
Definition lock_guard.hpp:65
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
Contains xtd::collections::concurrent::iproducer_consumer_collection <type_t> interface.
Provides several thread-safe collection classes that should be used in place of the corresponding typ...
Definition concurrent_bag.hpp:20
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
Contains xtd::object class.
Defines a key/value pair that can be set or retrieved.
Definition key_value_pair.hpp:37
Contains xtd::threading::lock_guard class.