3#include <system/diagnostics/debug.h>
5#include <system/shared_ptr.h>
6#include <system/primitive_types.h>
7#include <system/object.h>
8#include <system/exceptions.h>
9#include <system/collections/object_model/collection.h>
10#include <system/collections/list.h>
11#include <system/collections/ilist.h>
12#include <system/collections/iequality_comparer.h>
13#include <system/collections/ienumerator.h>
14#include <system/collections/dictionary.h>
16#include "system/object_ext.h"
19namespace Collections {
20namespace ObjectModel {
28template<
typename TKey,
typename TItem>
36 RTTI_INFO_TEMPLATE_CLASS(
ThisType, System::BaseTypesInfo<BaseType>);
45 virtual void Add(
const TItem& item)
override
47 int index = get_Count();
48 InsertItem(index, item);
76 return dict->idx_get(key);
79 auto enumerator_0 = (get_Items())->GetEnumerator();
80 while (enumerator_0->MoveNext())
82 auto&& item_0 = enumerator_0->get_Current();
83 if (comparer->Equals(GetKeyForItem(item_0), key))
88 throw Generic::KeyNotFoundException();
97 return dict->ContainsKey(key);
100 auto enumerator_1 = (get_Items())->GetEnumerator();
101 while (enumerator_1->MoveNext())
103 auto&& item_1 = enumerator_1->get_Current();
104 if (comparer->Equals(GetKeyForItem(item_1), key))
118 if (dict->ContainsKey(key))
126 for (
int i = 0; i < get_Items()->get_Count(); i++)
128 if (comparer->Equals(GetKeyForItem(get_Items()->idx_get(i)), key))
140 dict->SetTemplateWeakPtr(argument);
163 : keyCount(0), threshold(0)
165 if (comparer ==
nullptr)
169 if (dictionaryCreationThreshold == -1)
171 dictionaryCreationThreshold = (std::numeric_limits<int>::max)();
173 if (dictionaryCreationThreshold < -1)
175 throw SystemException(u
"ArgumentOutOfRange_InvalidThreshold");
178 this->comparer = comparer;
179 this->threshold = dictionaryCreationThreshold;
187 if (!ContainsItem(item))
189 throw ArgumentException(u
"Argument_ItemNotExist");
192 TKey oldKey = GetKeyForItem(item);
193 if (!comparer->Equals(oldKey, newKey))
197 AddKey(newKey, item);
229 virtual void InsertItem(
int index,
const TItem& item)
override
231 TKey key = GetKeyForItem(item);
242 TKey key = GetKeyForItem(get_Items()->idx_get(index));
254 TKey newKey = GetKeyForItem(item);
255 TKey oldKey = GetKeyForItem(get_Items()->idx_get(index));
257 if (comparer->Equals(oldKey, newKey))
261 dict->idx_set(newKey, item);
268 AddKey(newKey, item);
283 bool ContainsItem(TItem item)
286 if ((dict ==
nullptr) )
288 return get_Items()->Contains(item);
292 bool exist = dict->TryGetValue(key, itemInDict);
296 return itemInDict == item;
303 void AddKey(TKey key, TItem item)
307 dict->Add(key, item);
309 else if (keyCount == threshold)
312 dict->Add(key, item);
318 throw SystemException(u
"Argument_AddingDuplicate");
325 void CreateDictionary()
327 dict = System::MakeObject<System::Collections::Generic::Dictionary<TKey, TItem>>(comparer);
329 for (
int i = 0; i < get_Count(); i++)
332 TKey key = GetKeyForItem(item);
335 dict->Add(key, item);
342 void RemoveKey(TKey key)
357template<
typename TKey,
typename TItem>
Interface providing means to compare two objects for equality. Objects of this class should only be a...
Definition: iequality_comparer.h:17
Base type for generic collection. Objects of this class should only be allocated using System::MakeOb...
Definition: collection.h:19
bool Remove(const T &item) override
Removes specific item.
Definition: collection.h:147
virtual void RemoveItem(int index)
Removes item from specified position.
Definition: collection.h:297
virtual T idx_get(int index) const override
Gets value at specified index.
Definition: collection.h:56
virtual void InsertItem(int index, const T &item)
Inserts item into specified position.
Definition: collection.h:291
virtual void SetItem(int index, const T &item)
Sets item at specified position.
Definition: collection.h:304
virtual void ClearItems()
Deletes all items.
Definition: collection.h:284
Abstract collection of elements with embedded keys. Objects of this class should only be allocated us...
Definition: keyed_collection.h:30
KeyedCollection(const System::SharedPtr< System::Collections::Generic::IEqualityComparer< TKey > > &comparer, int dictionaryCreationThreshold)
Creates empty keyed collection.
Definition: keyed_collection.h:162
int keyCount
Number of keys inserted into collection.
Definition: keyed_collection.h:58
void SetTemplateWeakPtr(uint32_t argument) override
Makes specific template argument to be treated as weak pointer instead of shared pointer (if applicab...
Definition: keyed_collection.h:138
bool Contains(TKey key)
Checks if key is present in container.
Definition: keyed_collection.h:93
System::SharedPtr< System::Collections::Generic::IEqualityComparer< TKey > > comparer
Comparer to use.
Definition: keyed_collection.h:54
KeyedCollection(const System::SharedPtr< System::Collections::Generic::IEqualityComparer< TKey > > &comparer)
Creates empty keyed collection.
Definition: keyed_collection.h:157
virtual void ClearItems() override
Deletes all elements.
Definition: keyed_collection.h:207
virtual TKey GetKeyForItem(TItem item)=0
Defines a way to extract key from item.
bool Remove(TKey key)
Removes key from container.
Definition: keyed_collection.h:114
virtual void RemoveItem(int index) override
Removes item at specified index.
Definition: keyed_collection.h:240
virtual void InsertItem(int index, const TItem &item) override
Inserts item at specific index.
Definition: keyed_collection.h:229
virtual void Add(const TItem &item) override
Add item to container end.
Definition: keyed_collection.h:45
TItem idx_get(TKey key)
Gets item at specific index.
Definition: keyed_collection.h:72
int threshold
Lookup dictionary creation threshold, local.
Definition: keyed_collection.h:60
static const int defaultThreshold
Lookup dictionary creation threshold, default.
Definition: keyed_collection.h:52
virtual void SetItem(int index, TItem item)
Sets item at specified index.
Definition: keyed_collection.h:252
void ChangeItemKey(TItem item, TKey newKey)
Changes item's key.
Definition: keyed_collection.h:184
System::SharedPtr< System::Collections::Generic::IEqualityComparer< TKey > > get_Comparer()
Gets comparer.
Definition: keyed_collection.h:64
~KeyedCollection() override=default
Destructor.
System::SharedPtr< Generic::IDictionary< TKey, TItem > > get_Dictionary()
Gets dictionary.
Definition: keyed_collection.h:149
KeyedCollection()
Creates empty keyed collection.
Definition: keyed_collection.h:154
System::SharedPtr< System::Collections::Generic::Dictionary< TKey, TItem > > dict
Wrapped dictionary.
Definition: keyed_collection.h:56
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