CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
keyed_collection.h
1
2#pragma once
3#include <system/diagnostics/debug.h>
4
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>
15
16#include "system/object_ext.h"
17
18namespace System {
19namespace Collections {
20namespace ObjectModel {
21
28template<typename TKey, typename TItem>
29class ABSTRACT KeyedCollection : public Collection<TItem>
30{
36 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<BaseType>);
37
38public:
40 using Collection<TItem>::get_Count;
42 using Collection<TItem>::get_Items;
45 virtual void Add(const TItem& item) override
46 {
47 int index = get_Count();
48 InsertItem(index, item);
49 }
50
52 static const int defaultThreshold;
61
65 {
66 return comparer;
67 }
68
72 TItem idx_get(TKey key)
73 {
74 if (dict != nullptr)
75 {
76 return dict->idx_get(key);
77 }
78
79 auto enumerator_0 = (get_Items())->GetEnumerator();
80 while (enumerator_0->MoveNext())
81 {
82 auto&& item_0 = enumerator_0->get_Current();
83 if (comparer->Equals(GetKeyForItem(item_0), key))
84 {
85 return item_0;
86 }
87 }
88 throw Generic::KeyNotFoundException();
89 }
93 bool Contains(TKey key)
94 {
95 if (dict != nullptr)
96 {
97 return dict->ContainsKey(key);
98 }
99
100 auto enumerator_1 = (get_Items())->GetEnumerator();
101 while (enumerator_1->MoveNext())
102 {
103 auto&& item_1 = enumerator_1->get_Current();
104 if (comparer->Equals(GetKeyForItem(item_1), key))
105 {
106 return true;
107 }
108 }
109 return false;
110 }
114 bool Remove(TKey key)
115 {
116 if (dict != nullptr)
117 {
118 if (dict->ContainsKey(key))
119 {
120 return Collection<TItem>::Remove(dict->idx_get(key));
121 }
122
123 return false;
124 }
125
126 for (int i = 0; i < get_Items()->get_Count(); i++)
127 {
128 if (comparer->Equals(GetKeyForItem(get_Items()->idx_get(i)), key))
129 {
130 RemoveItem(i);
131 return true;
132 }
133 }
134 return false;
135 }
136
138 void SetTemplateWeakPtr(uint32_t argument) override
139 {
140 dict->SetTemplateWeakPtr(argument);
141 }
142
143protected:
145 ~KeyedCollection() override = default;
146
150 {
151 return dict;
152 }
154 KeyedCollection() : KeyedCollection(nullptr, defaultThreshold) { }
158 : KeyedCollection(comparer, defaultThreshold) { }
163 : /*Collection<TItem>(System::MakeObject<Generic::List<TItem>>()),*/ keyCount(0), threshold(0)
164 {
165 if (comparer == nullptr)
166 {
167 // comparer = System::Collections::Generic::EqualityComparer<TKey>::get_Default();
168 }
169 if (dictionaryCreationThreshold == -1)
170 {
171 dictionaryCreationThreshold = (std::numeric_limits<int>::max)();
172 }
173 if (dictionaryCreationThreshold < -1)
174 {
175 throw SystemException(u"ArgumentOutOfRange_InvalidThreshold");
176 //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.dictionaryCreationThreshold, ExceptionResource.ArgumentOutOfRange_InvalidThreshold);
177 }
178 this->comparer = comparer;
179 this->threshold = dictionaryCreationThreshold;
180 }
184 void ChangeItemKey(TItem item, TKey newKey)
185 {
186 // check if the item exists in the collection
187 if (!ContainsItem(item))
188 {
189 throw ArgumentException(u"Argument_ItemNotExist");
190 }
191
192 TKey oldKey = GetKeyForItem(item);
193 if (!comparer->Equals(oldKey, newKey))
194 {
195 //if (newKey != nullptr)
196 {
197 AddKey(newKey, item);
198 }
199
200 //if (oldKey != nullptr)
201 {
202 RemoveKey(oldKey);
203 }
204 }
205 }
207 virtual void ClearItems() override
208 {
210 if (dict != nullptr)
211 {
212 dict->Clear();
213 }
214
215 keyCount = 0;
216 }
217
218 /*
219 You can implement this method to return null for a collection that contains items without keys,
220 in which case the items can be accessed only by their index. This method is an O(1) operation.
221 */
225 virtual TKey GetKeyForItem(TItem item) = 0;
229 virtual void InsertItem(int index, const TItem& item) override
230 {
231 TKey key = GetKeyForItem(item);
232 //if (key != nullptr)
233 {
234 AddKey(key, item);
235 }
237 }
240 virtual void RemoveItem(int index) override
241 {
242 TKey key = GetKeyForItem(get_Items()->idx_get(index));
243 //if (key != nullptr)
244 {
245 RemoveKey(key);
246 }
248 }
252 virtual void SetItem(int index, TItem item)
253 {
254 TKey newKey = GetKeyForItem(item);
255 TKey oldKey = GetKeyForItem(get_Items()->idx_get(index));
256
257 if (comparer->Equals(oldKey, newKey))
258 {
259 //if (newKey != nullptr && dict != nullptr)
260 {
261 dict->idx_set(newKey, item);
262 }
263 }
264 else
265 {
266 //if (newKey != nullptr)
267 {
268 AddKey(newKey, item);
269 }
270
271 //if (oldKey != nullptr)
272 {
273 RemoveKey(oldKey);
274 }
275 }
276 Collection<TItem>::SetItem(index, item);
277 }
278
279private:
283 bool ContainsItem(TItem item)
284 {
285 TKey key;
286 if ((dict == nullptr) /*|| ((key = GetKeyForItem(item)) == nullptr)*/)
287 {
288 return get_Items()->Contains(item);
289 }
290
291 TItem itemInDict;
292 bool exist = dict->TryGetValue(key, itemInDict);
293 if (exist)
294 {
295 //return Generic::EqualityComparer<TItem>::get_Default()->Equals(itemInDict, item);
296 return itemInDict == item;
297 }
298 return false;
299 }
303 void AddKey(TKey key, TItem item)
304 {
305 if (dict != nullptr)
306 {
307 dict->Add(key, item);
308 }
309 else if (keyCount == threshold)
310 {
311 CreateDictionary();
312 dict->Add(key, item);
313 }
314 else
315 {
316 if (Contains(key))
317 {
318 throw SystemException(u"Argument_AddingDuplicate");
319 }
320
321 keyCount++;
322 }
323 }
325 void CreateDictionary()
326 {
327 dict = System::MakeObject<System::Collections::Generic::Dictionary<TKey, TItem>>(comparer);
328
329 for (int i = 0; i < get_Count(); i++)
330 {
331 TItem item = Collection<TItem>::idx_get(i);
332 TKey key = GetKeyForItem(item);
333 //if (key != nullptr)
334 {
335 dict->Add(key, item);
336 }
337
338 }
339 }
342 void RemoveKey(TKey key)
343 {
344 //CODEPORTING_DEBUG_ASSERT(key != nullptr);
345 if (dict != nullptr)
346 {
347 dict->Remove(key);
348 }
349 else
350 {
351 keyCount--;
352 }
353 }
354};
355
357template<typename TKey, typename TItem>
359
360} // namespace ObjectModel
361} // namespace Collections
362} // namespace System
363
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