4#include <system/collections/idictionary.h>
5#include <system/collections/iread_only_dictionary.h>
6#include <system/cycles_detection.h>
7#include <system/collections/base_enumerator.h>
8#include <system/details/hash_quality_checker.h>
9#include <system/collections/virtualized_iterator.h>
14namespace System {
namespace Collections {
namespace Generic {
18template <
typename Dict>
20 :
public System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>
21 ,
private System::Details::IteratorPointerUpdater<typename Dict::KeyValuePairType, false>
23 using System::Details::NativeIteratorWrapperBase<
typename Dict::KeyValuePairType,
typename Dict::map_t::const_iterator>::m_iterator;
24 using System::Details::NativeIteratorWrapperBase<
typename Dict::KeyValuePairType,
typename Dict::map_t::const_iterator>::m_end;
25 using System::Details::VirtualizedIteratorBase<
typename Dict::KeyValuePairType>::m_is_end;
26 using System::Details::VirtualizedIteratorBase<
typename Dict::KeyValuePairType>::m_pointer;
27 using System::Details::IteratorPointerUpdater<
typename Dict::KeyValuePairType,
false>::InitializeIteratorPointer;
28 using System::Details::IteratorPointerUpdater<
typename Dict::KeyValuePairType,
false>::UpdateIteratorPointer;
34 DictionaryIterator(
typename Dict::map_t::const_iterator &&iterator,
typename Dict::map_t::const_iterator &&end) noexcept
35 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(
36 std::forward<typename Dict::map_t::const_iterator>(iterator),
37 std::forward<typename Dict::map_t::const_iterator>(end)
40 InitializeIteratorPointer(m_pointer);
42 UpdateIteratorPointer(m_pointer,
typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
47 DictionaryIterator(
const typename Dict::map_t::const_iterator &iterator,
const typename Dict::map_t::const_iterator &end)
48 :
System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(iterator, end)
50 InitializeIteratorPointer(m_pointer);
52 UpdateIteratorPointer(m_pointer,
typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
57 : System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>(std::forward(other.m_iterator))
60 UpdateIteratorPointer(m_pointer,
typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
68 if (System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::IteratorIncrementImplementation())
69 UpdateIteratorPointer(m_pointer,
typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
74 System::Details::IteratorDecrementer<typename Dict::map_t::const_iterator>::decrement(m_iterator);
75 UpdateIteratorPointer(m_pointer,
typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
81 if (System::Details::NativeIteratorWrapperBase<typename Dict::KeyValuePairType, typename Dict::map_t::const_iterator>::IteratorShiftImplementation(offset))
82 UpdateIteratorPointer(m_pointer,
typename Dict::KeyValuePairType(m_iterator->first, m_iterator->second));
86 System::Details::VirtualizedIteratorBase<typename Dict::KeyValuePairType>*
CloneIterator()
const override
101 :
public IDictionary<typename Map::key_type, typename Map::mapped_type>,
104 ASPOSE_MAP_POINTER_MODE_CONTROL(Map)
106 typedef System::BaseTypesInfo<IDictionary<typename Map::key_type, typename Map::mapped_type>> BaseTypesInfo;
114 static_assert(std::is_same<
decltype(m_map.get_allocator()),
typename pointer_mode_t::allocator_type>::value,
"Container's allocator is not set to typename pointer_mode_t::allocator_type");
115 m_pointer_mode.SetWeak(argument, m_map);
126 typedef typename Map::key_type key_t;
128 typedef typename Map::mapped_type mapped_t;
130 typedef typename Map::iterator mapIt_t;
132 typedef typename Map::const_iterator stl_const_iterator;
135 static_assert(std::is_same<allocator_t, typename Map::allocator_type>::value,
"Using wrong map type with BaseDictionary");
162 template<
class... Args>
164 : m_map(args..., ASPOSE_COLLECTION_ALLOCATOR)
171 template<
class... Args>
173 : m_map(args..., ASPOSE_COLLECTION_ALLOCATOR)
195 return static_cast<int32_t
>(m_map.size());
216 mapped_t
idx_get(
const key_t& key)
const override
219 auto it = m_map.find(key);
220 if (it == m_map.end())
222 throw KeyNotFoundException();
230 void idx_set(
const key_t& key, mapped_t value)
override
234#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
235 System::Details::CheckHashFunctionQuality(m_map);
242 void Add(
const key_t& key,
const mapped_t& value)
override
246 auto op_result = m_map.insert(std::make_pair(key,
value));
247 if (! op_result.second)
251#if defined(ASPOSE_DETECT_BAD_QUALITY_HASH)
252 System::Details::CheckHashFunctionQuality(m_map);
261 return m_map.find(key) != m_map.end();
269 auto it = m_map.find(key);
271 if (it != m_map.end())
283 bool TryGetValue(
const key_t& key, mapped_t& value)
const override
285 auto it = m_map.find(key);
287 if (it != m_map.end())
305 for (
const auto& itr : m_map)
307 if (itr.second ==
value)
318 const Map&
data()
const {
return m_map; }
321 void _add_range(std::initializer_list<typename Map::value_type> list)
323 std::for_each(list.begin(), list.end(), [
this](
const typename Map::value_type &v) { m_map.insert(v); });
329 auto it = m_map.find(key);
331 if (it != m_map.end())
342 auto it = m_map.find(key);
344 if (it != m_map.end())
355 auto it = m_map.find(key);
357 if (it != m_map.end())
371 return m_map.begin();
386 stl_const_iterator
cbegin() const noexcept
388 return m_map.cbegin();
394 stl_const_iterator
cend() const noexcept
430 auto it = m_map.find(item.
m_pair.first);
432 return it != m_map.end() && it->second == item.
m_pair.second;
439 auto it = m_map.find(item.
m_pair.first);
441 if (it != m_map.end() && it->second == item.
m_pair.second)
450#ifdef ASPOSE_GET_SHARED_MEMBERS
453 virtual void GetSharedMembers(System::Object::shared_members_type& result)
const override
455 Object::GetSharedMembers(result);
456 result.PopulateSharedMembers(
"System::Collections::Generic::BaseDictionary<Map>::m_map[]", m_map);
463 void CopyFrom(BaseType* src)
466 throw ArgumentNullException(u
"Argument cannot be nullptr");
469 while (enumerator->MoveNext())
474 void Add(
const KeyValuePair<key_t, mapped_t>& item)
override
476 m_map.insert(item.m_pair);
479 static NotSupportedException ReadOnlyException()
481 return NotSupportedException(u
"The collection is readonly");
488#ifdef __DBG_FOR_EACH_MEMBER
492 void DBG_for_each_member(DBG::for_each_member_visitor &visitor)
const override
494 visitor.add_self(
this);
495 DBG::for_each_of_Object(
this, m_map, visitor);
500 const char* DBG_class_name()
const override {
return "BaseDictionary<T>"; }
507 template <
typename T,
class =
void>
508 struct has_equality_operator : std::false_type {};
512 template <
typename T>
513 struct has_equality_operator<T, typename std::enable_if<!std::is_enum<T>::value, decltype((void)(std::declval<T>() == nullptr))>::type> : std::true_type
519 template <
typename T>
520 typename std::enable_if<has_equality_operator<T>::value,
void>::type CheckIfNull(
const T& instance)
const
522 if (instance ==
nullptr)
524 throw System::ArgumentNullException(u
"key");
530 template <
typename T>
531 typename std::enable_if<!has_equality_operator<T>::value,
void>::type CheckIfNull(
const T& instance)
const
Implements common code for various dictionary-alike data structures (e. g. Dictionary,...
Definition: base_dictionary.h:103
BaseDictionary(BaseType *src, const Args &... args)
Copying constructor.
Definition: base_dictionary.h:172
ICollection< mapped_t > ValueCollection
Collection of values.
Definition: base_dictionary.h:141
KVPair ValueType
Value type name.
Definition: base_dictionary.h:149
const_iterator begin() const noexcept
Returns an iterator to the KVPair-wrapper for key-value-element of the container. Implemented in C# s...
Definition: base_dictionary.h:369
void SetTemplateWeakPtr(unsigned int argument) override
Definition: base_dictionary.h:112
const_iterator end() const noexcept
Returns an iterator to the KVPair-wrapper for key-value-element following the last element of the con...
Definition: base_dictionary.h:378
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: base_dictionary.h:410
pointer_mode_t m_pointer_mode
Definition: base_dictionary.h:122
mapped_t GetValueOrDefault(const key_t &key, const mapped_t &defaultValue) const override
Returns value if found; or defaultValue otherwise.
Definition: base_dictionary.h:340
KeyValuePair< key_t, mapped_t > KVPair
Key-value pair type.
Definition: base_dictionary.h:143
void _add_range(std::initializer_list< typename Map::value_type > list)
C++ specific.
Definition: base_dictionary.h:321
virtual mapped_t & operator[](const key_t &key)
Accessor function.
Definition: base_dictionary.h:207
SharedPtr< IEnumerator< KeyValuePair< key_t, mapped_t > > > GetEnumerator() override=0
Creates enumerator instance, should be implemented by subclass.
Map m_map
Underlying C++ data structure.
Definition: base_dictionary.h:422
bool Contains(const KeyValuePair< key_t, mapped_t > &item) const override
Checks if key-value pair is present in dictionary, comparing both keys and values.
Definition: base_dictionary.h:428
stl_const_iterator cend() const noexcept
Returns an iterator to the element following the last element of the container. Implemented in STL-st...
Definition: base_dictionary.h:394
void Add(const key_t &key, const mapped_t &value) override
Adds key-value pair into dictionary.
Definition: base_dictionary.h:242
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: base_dictionary.h:405
bool ContainsKey(const key_t &key) const override
Checks if key is present in dictionary.
Definition: base_dictionary.h:258
ICollection< key_t > KeyCollection
Make sure we use correct allocator with underlying storage type.
Definition: base_dictionary.h:139
~BaseDictionary() override
Destructor.
Definition: base_dictionary.h:486
const Map & data() const
Underlying data storage accessor.
Definition: base_dictionary.h:318
Map map_t
Internal map type.
Definition: base_dictionary.h:119
stl_const_iterator cbegin() const noexcept
Returns an iterator to the first element of the container. Implemented in STL-style....
Definition: base_dictionary.h:386
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: base_dictionary.h:415
mapped_t GetValueOrNull(const key_t &key) const override
Returns value if found; or null otherwise. Make sense only for reference types.
Definition: base_dictionary.h:353
void idx_set(const key_t &key, mapped_t value) override
Keyed setter function. Alters or creates element.
Definition: base_dictionary.h:230
Map & data()
Underlying data storage accessor.
Definition: base_dictionary.h:315
KVPairIterator< KVPair, Map > iterator
Iterator type.
Definition: base_dictionary.h:152
bool Remove(const key_t &key) override
Removes specific key from dictionary.
Definition: base_dictionary.h:266
BaseDictionary()
Creates empty data structure.
Definition: base_dictionary.h:157
void Clear() override
Deletes all elements.
Definition: base_dictionary.h:198
mapped_t GetValueOrDefault(const key_t &key) const override
Returns value if found; or Value() otherwise.
Definition: base_dictionary.h:327
KVPairIterator< KVPair, Map > const_iterator
Const iterator type.
Definition: base_dictionary.h:154
BaseDictionary(BaseType *src)
Copying constructor.
Definition: base_dictionary.h:180
mapped_t idx_get(const key_t &key) const override
Keyed getter function.
Definition: base_dictionary.h:216
BaseDictionary(int, const Args &... args)
Forwarding constructor to push arguments into underlying map constructor.
Definition: base_dictionary.h:163
bool Remove(const KeyValuePair< key_t, mapped_t > &item) override
Removes specified key-value pair from dictionary, compares both keys and values.
Definition: base_dictionary.h:437
bool ContainsValue(const mapped_t &value)
Checks if value is present in dictionary. Uses operator == to compare values.
Definition: base_dictionary.h:303
bool TryGetValue(const key_t &key, mapped_t &value) const override
Looks for keyed value and retreives it if found.
Definition: base_dictionary.h:283
int32_t get_Count() const override
Gets elements count.
Definition: base_dictionary.h:193
IDictionary< typename Map::key_type, typename Map::mapped_type > BaseType
Implemented interface.
Definition: base_dictionary.h:146
System::Details::VirtualizedIteratorBase< KVPair > * virtualizeBeginIterator() override
Gets the implementation of begin iterator for the current container.
Definition: base_dictionary.h:400
Dictionary iterator that provides KeyValuePair notation.
Definition: base_dictionary.h:22
virtual ~DictionaryIterator()=default
Destructor.
DictionaryIterator(DictionaryIterator &&other) noexcept
Move constructor.
Definition: base_dictionary.h:56
DictionaryIterator(typename Dict::map_t::const_iterator &&iterator, typename Dict::map_t::const_iterator &&end) noexcept
Constructor.
Definition: base_dictionary.h:34
void DecrementIterator() override
Moves the iterator step back.
Definition: base_dictionary.h:72
void IncrementIterator() override
Moves the iterator step forward.
Definition: base_dictionary.h:66
void ShiftIteratorBy(std::ptrdiff_t offset) override
Moves the iterator by the specified number of steps.
Definition: base_dictionary.h:79
DictionaryIterator(const typename Dict::map_t::const_iterator &iterator, const typename Dict::map_t::const_iterator &end)
Constructor.
Definition: base_dictionary.h:47
System::Details::VirtualizedIteratorBase< typename Dict::KeyValuePairType > * CloneIterator() const override
Clones current iterator.
Definition: base_dictionary.h:86
Interface of collection of elements. Objects of this class should only be allocated using System::Mak...
Definition: icollection.h:20
Interface for dictionary-alike containers. Objects of this class should only be allocated using Syste...
Definition: idictionary.h:21
Interface for read-only dictionary-alike containers. Objects of this class should only be allocated u...
Definition: iread_only_dictionary.h:19
Adapting iterator, wraps std::pair into KVPair expected from Dictionary.
Definition: base_enumerator.h:126
Pair of key and value. This type should be allocated on stack and passed to functions by value or by ...
Definition: keyvalue_pair.h:20
std::pair< TKey, TValue > m_pair
Internal data structure.
Definition: keyvalue_pair.h:23
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Definition: db_command.h:9
System::ExceptionWrapper< Details_ArgumentException > ArgumentException
Definition: throw_helper.h:23