CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
base_set.h
1#pragma once
2
3#include "system/object.h"
4#include "system/array.h"
5#include <system/exceptions.h>
6#include "system/collections/iset.h"
7#include "system/collections/base_enumerator.h"
8#include "system/cycles_detection.h"
9#include "system/collections/iequality_comparer.h"
10#include "system/collections/iread_only_collection.h"
11
12namespace System {
13namespace Collections {
14namespace Generic {
15
16template<typename T, typename SET_T>
17class ASPOSECPP_SHARED_CLASS BaseSet
18 : virtual public System::Object
19 , public ICollection<T>
20 , public IReadOnlyCollection<T>
21{
22 ASPOSE_COLLECTION_POINTER_MODE_CONTROL(T)
23
24public:
30 using set_t = SET_T;
32 using iterator = typename set_t::iterator;
34 using const_iterator = typename set_t::const_iterator;
36 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<System::Object>);
37
38protected:
41
42public:
44 using ValueType = T;
51
56 class Enumerator : public SimpleEnumerator<set_t>
57 {
59
60 public:
63 Enumerator(const ThisPtr& set) : SimpleEnumerator<set_t>(set, set->m_data) { }
65 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<System::Object>);
66 };
67
68protected:
70 BaseSet() : ASPOSE_COLLECTION_INIT_ALLOCATOR() {}
71
73 BaseSet(int capacity) : ASPOSE_COLLECTION_INIT_ALLOCATOR()
74 {
75 if (capacity < 0)
76 {
77 throw ArgumentOutOfRangeException(u"capacity");
78 }
79 m_data.reserve(capacity);
80 }
81
86 BaseSet(const EqualityComparerHashAdapter<T>& hashAdapter, const EqualityComparerAdapter<T>& comparerAdapter)
87 : m_data(8, hashAdapter, comparerAdapter, ASPOSE_COLLECTION_ALLOCATOR)
88 {
89 }
90
94 BaseSet(const ComparerAdapter<T>& comparerAdapter)
95 : m_data(comparerAdapter, ASPOSE_COLLECTION_ALLOCATOR)
96 {
97 }
98
100 BaseSet(const SharedPtr<IEnumerable<T>>& items) : ASPOSE_COLLECTION_INIT_ALLOCATOR()
101 {
102 auto e = items->GetEnumerator();
103 while (e->MoveNext())
104 m_data.insert(e->get_Current());
105 }
106
107public:
110 const_iterator begin() const noexcept { return m_data.begin(); }
113 const_iterator end() const noexcept { return m_data.end(); }
114
117 const_iterator cbegin() const noexcept { return m_data.cbegin(); }
120 const_iterator cend() const noexcept { return m_data.cend(); }
121
125 {
126 return MakeObject<Enumerator>(MakeSharedPtr(this));
127 }
128
131 int get_Count() const override { return ASPOSECPP_CHECKED_CAST(int, m_data.size()); }
132
135 void Add(const T& item) override
136 {
137 TryAdd(item);
138 }
142 bool TryAdd(const T& item)
143 {
144 if (m_data.find(item) != m_data.end())
145 return false;
146
147 m_data.insert(item);
148 return true;
149 }
153 bool Remove(const T& item) override
154 {
155 auto it = m_data.find(item);
156 if (it == m_data.end())
157 return false;
158
159 m_data.erase(it);
160 return true;
161 }
163 void Clear() override
164 {
165 m_data.clear();
166 }
170 bool Contains(const T& item) const override
171 {
172 return m_data.find(item) != m_data.end();
173 }
174
176 {
177 if (other == nullptr)
178 {
179 throw ArgumentNullException(u"other");
180 }
181
182 auto enumerator = other->GetEnumerator();
183 while (enumerator->MoveNext())
184 {
185 if (!Contains(enumerator->get_Current()))
186 {
187 Add(enumerator->get_Current());
188 }
189 }
190 }
191
194 set_t& data() { return m_data; }
197 const set_t& data() const { return m_data; }
200 void _add_range(std::initializer_list<T> list)
201 {
202 std::for_each(list.begin(), list.end(), [this](const T &v) { m_data.insert(v); });
203 }
204
206 using BaseType::get_Count;
207
211 void CopyTo(ArrayPtr<T> arr, int index) override
212 {
213 if (!arr)
214 throw ArgumentNullException();
215
216 if (index < 0)
218
219 if (index + get_Count() > arr->get_Length())
220 throw ArgumentException();
221
222 SharedPtr<IEnumerator<T>> e = this->GetEnumerator();
223 while (e->MoveNext())
224 (*arr)[index++] = e->get_Current();
225 }
226
228 System::Details::VirtualizedIteratorBase<T>* virtualizeBeginIterator() override
229 {
230 return new System::Details::NativeIteratorWrapper<T, iterator>(m_data.begin(), m_data.end());
231 }
233 System::Details::VirtualizedIteratorBase<T>* virtualizeEndIterator() override
234 {
235 return new System::Details::NativeIteratorWrapper<T, iterator>(m_data.end(), m_data.end());
236 }
238 System::Details::VirtualizedIteratorBase<T>* virtualizeBeginConstIterator() const override
239 {
240 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(m_data.begin(), m_data.end());
241 }
243 System::Details::VirtualizedIteratorBase<T>* virtualizeEndConstIterator() const override
244 {
245 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(m_data.end(), m_data.end());
246 }
247
248#ifdef __DBG_FOR_EACH_MEMBER
249public:
252 void DBG_for_each_member(DBG::for_each_member_visitor &visitor) const override
253 {
254 visitor.add_self(this);
255 DBG::for_each_of_Object(this, m_data, visitor);
256 }
259 const char* DBG_class_name() const override { return "BaseSet<T>"; }
260#endif
261
262protected:
264 ~BaseSet() override {}
265#ifdef ASPOSE_GET_SHARED_MEMBERS
267 DEFINE_GET_SHARED_MEMBERS(m_data);
268#endif
269};
270
271} // namespace Generic
272} // namespace Collections
273} // namespace System
Enumerator class. Objects of this class should only be allocated using System::MakeObject() function....
Definition: base_set.h:57
RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo< System::Object >)
RTTI information.
Enumerator(const ThisPtr &set)
Creates enumerator iterating through set object.
Definition: base_set.h:63
Definition: base_set.h:21
System::Details::VirtualizedIteratorBase< T > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: base_set.h:233
const_iterator begin() const noexcept
Gets iterator to the first element of the const-qualified collection.
Definition: base_set.h:110
void Add(const T &item) override
Adds element into set.
Definition: base_set.h:135
void UnionWith(const SharedPtr< IEnumerable< T > > &other)
Definition: base_set.h:175
bool TryAdd(const T &item)
Adds element into set.
Definition: base_set.h:142
typename set_t::const_iterator const_iterator
Const iterator type.
Definition: base_set.h:34
BaseSet()
Creates empty set.
Definition: base_set.h:70
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginIterator() override
Gets the implementation of begin iterator for the current container.
Definition: base_set.h:228
set_t m_data
Internal data structure.
Definition: base_set.h:40
BaseSet(int capacity)
Creates empty set with specified capacity.
Definition: base_set.h:73
BaseSet(const SharedPtr< IEnumerable< T > > &items)
Creates set based on enumerable values.
Definition: base_set.h:100
const_iterator cbegin() const noexcept
Gets iterator to the first const-qualified element of collection.
Definition: base_set.h:117
BaseSet(const ComparerAdapter< T > &comparerAdapter)
Creates empty set that uses the specified comparer adapter Used for creation SortedSet.
Definition: base_set.h:94
bool Remove(const T &item) override
Removes element from set.
Definition: base_set.h:153
int get_Count() const override
Gets number of elements in set.
Definition: base_set.h:131
SET_T set_t
Underlying data type.
Definition: base_set.h:30
RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo< System::Object >)
RTTI information.
void _add_range(std::initializer_list< T > list)
C++ specific.
Definition: base_set.h:200
typename set_t::iterator iterator
Iterator type.
Definition: base_set.h:32
System::Details::VirtualizedIteratorBase< T > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: base_set.h:243
BaseSet(const EqualityComparerHashAdapter< T > &hashAdapter, const EqualityComparerAdapter< T > &comparerAdapter)
Creates empty set that uses the specified equality comparer adapters Used for creation HashSet.
Definition: base_set.h:86
set_t & data()
Underlying data structure accessor.
Definition: base_set.h:194
~BaseSet() override
Destructor.
Definition: base_set.h:264
bool Contains(const T &item) const override
Checks if element is present in set.
Definition: base_set.h:170
const_iterator cend() const noexcept
Gets iterator for a non-existent const-qualified element behind the end of the collection.
Definition: base_set.h:120
IEnumeratorPtr GetEnumerator() override
Creates enumerator.
Definition: base_set.h:124
T ValueType
Value type.
Definition: base_set.h:44
const_iterator end() const noexcept
Gets iterator for a non-existent element behind the end of the const-qualified collection.
Definition: base_set.h:113
void Clear() override
Deletes all elements in set.
Definition: base_set.h:163
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: base_set.h:238
void CopyTo(ArrayPtr< T > arr, int index) override
Copies hash contents into existing array elements.
Definition: base_set.h:211
const set_t & data() const
Underlying data structure accessor.
Definition: base_set.h:197
Interface of collection of elements. Objects of this class should only be allocated using System::Mak...
Definition: icollection.h:20
Interface of object providing enumerator on contained elements.
Definition: ienumerable.h:26
Interface of read-only collection of elements. Objects of this class should only be allocated using S...
Definition: iread_only_collection.h:19
Iterator class for simple containers holding elements directly using rbegin() and rend() functions....
Definition: base_enumerator.h:98
Template that represents wrapper of exceptions that are derived from Exception class.
Definition: exception.h:118
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:51
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
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1640
System::ExceptionWrapper< Details_ArgumentOutOfRangeException > ArgumentOutOfRangeException
Definition: throw_helper.h:24
Adapter to use IComparer within STL environment. Uses IComparer if set; otherwise,...
Definition: icomparer.h:45
Adapter making it possible using IEqualityComparer with STL-styled collections and algorithms....
Definition: iequality_comparer.h:39
Adapter to use IEqualityComparer for hashing. Uses comparator object, if set; otherwise,...
Definition: iequality_comparer.h:181