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
11namespace System {
12namespace Collections {
13namespace Generic {
14
15template<typename T, typename SET_T>
16class ASPOSECPP_SHARED_CLASS BaseSet
17 : virtual public System::Object
18 , public ICollection<T>
19{
20 ASPOSE_COLLECTION_POINTER_MODE_CONTROL(T)
21
22public:
28 using set_t = SET_T;
30 using iterator = typename set_t::iterator;
32 using const_iterator = typename set_t::const_iterator;
34 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<System::Object>);
35
36protected:
39
40public:
42 using ValueType = T;
49
54 class Enumerator : public SimpleEnumerator<set_t>
55 {
57
58 public:
61 Enumerator(const ThisPtr& set) : SimpleEnumerator<set_t>(set, set->m_data) { }
63 RTTI_INFO_TEMPLATE_CLASS(ThisType, System::BaseTypesInfo<System::Object>);
64 };
65
66protected:
68 BaseSet() : ASPOSE_COLLECTION_INIT_ALLOCATOR() {}
69
71 BaseSet(int capacity) : ASPOSE_COLLECTION_INIT_ALLOCATOR()
72 {
73 if (capacity < 0)
74 {
75 throw ArgumentOutOfRangeException(u"capacity");
76 }
77 m_data.reserve(capacity);
78 }
79
84 BaseSet(const EqualityComparerHashAdapter<T>& hashAdapter, const EqualityComparerAdapter<T>& comparerAdapter)
85 : m_data(8, hashAdapter, comparerAdapter, ASPOSE_COLLECTION_ALLOCATOR)
86 {
87 }
88
92 BaseSet(const ComparerAdapter<T>& comparerAdapter)
93 : m_data(comparerAdapter, ASPOSE_COLLECTION_ALLOCATOR)
94 {
95 }
96
98 BaseSet(const SharedPtr<IEnumerable<T>>& items) : ASPOSE_COLLECTION_INIT_ALLOCATOR()
99 {
100 auto e = items->GetEnumerator();
101 while (e->MoveNext())
102 m_data.insert(e->get_Current());
103 }
104
105public:
108 const_iterator begin() const noexcept { return m_data.begin(); }
111 const_iterator end() const noexcept { return m_data.end(); }
112
115 const_iterator cbegin() const noexcept { return m_data.cbegin(); }
118 const_iterator cend() const noexcept { return m_data.cend(); }
119
123 {
124 return MakeObject<Enumerator>(MakeSharedPtr(this));
125 }
126
129 int get_Count() const override { return ASPOSECPP_CHECKED_CAST(int, m_data.size()); }
130
133 void Add(const T& item) override
134 {
135 TryAdd(item);
136 }
140 bool TryAdd(const T& item)
141 {
142 if (m_data.find(item) != m_data.end())
143 return false;
144
145 m_data.insert(item);
146 return true;
147 }
151 bool Remove(const T& item) override
152 {
153 auto it = m_data.find(item);
154 if (it == m_data.end())
155 return false;
156
157 m_data.erase(it);
158 return true;
159 }
161 void Clear() override
162 {
163 m_data.clear();
164 }
168 bool Contains(const T& item) const override
169 {
170 return m_data.find(item) != m_data.end();
171 }
172
175 set_t& data() { return m_data; }
178 const set_t& data() const { return m_data; }
181 void _add_range(std::initializer_list<T> list)
182 {
183 std::for_each(list.begin(), list.end(), [this](const T &v) { m_data.insert(v); });
184 }
185
187 using BaseType::get_Count;
188
192 void CopyTo(ArrayPtr<T> arr, int index) override
193 {
194 if (!arr)
195 throw ArgumentNullException();
196
197 if (index < 0)
198 throw ArgumentOutOfRangeException();
199
200 if (index + get_Count() > arr->get_Length())
201 throw ArgumentException();
202
203 SharedPtr<IEnumerator<T>> e = this->GetEnumerator();
204 while (e->MoveNext())
205 (*arr)[index++] = e->get_Current();
206 }
207
209 System::Details::VirtualizedIteratorBase<T>* virtualizeBeginIterator() override
210 {
211 return new System::Details::NativeIteratorWrapper<T, iterator>(m_data.begin(), m_data.end());
212 }
214 System::Details::VirtualizedIteratorBase<T>* virtualizeEndIterator() override
215 {
216 return new System::Details::NativeIteratorWrapper<T, iterator>(m_data.end(), m_data.end());
217 }
219 System::Details::VirtualizedIteratorBase<T>* virtualizeBeginConstIterator() const override
220 {
221 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(m_data.begin(), m_data.end());
222 }
224 System::Details::VirtualizedIteratorBase<T>* virtualizeEndConstIterator() const override
225 {
226 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(m_data.end(), m_data.end());
227 }
228
229#ifdef __DBG_FOR_EACH_MEMBER
230public:
233 void DBG_for_each_member(DBG::for_each_member_visitor &visitor) const override
234 {
235 visitor.add_self(this);
236 DBG::for_each_of_Object(this, m_data, visitor);
237 }
240 const char* DBG_class_name() const override { return "BaseSet<T>"; }
241#endif
242
243protected:
245 ~BaseSet() override {}
246#ifdef ASPOSE_GET_SHARED_MEMBERS
248 DEFINE_GET_SHARED_MEMBERS(m_data);
249#endif
250};
251
252} // namespace Generic
253} // namespace Collections
254} // namespace System
Enumerator class. Objects of this class should only be allocated using System::MakeObject() function....
Definition: base_set.h:55
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:61
Definition: base_set.h:19
System::Details::VirtualizedIteratorBase< T > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: base_set.h:214
const_iterator begin() const noexcept
Gets iterator to the first element of the const-qualified collection.
Definition: base_set.h:108
void Add(const T &item) override
Adds element into set.
Definition: base_set.h:133
bool TryAdd(const T &item)
Adds element into set.
Definition: base_set.h:140
typename set_t::const_iterator const_iterator
Const iterator type.
Definition: base_set.h:32
BaseSet()
Creates empty set.
Definition: base_set.h:68
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginIterator() override
Gets the implementation of begin iterator for the current container.
Definition: base_set.h:209
set_t m_data
Internal data structure.
Definition: base_set.h:38
BaseSet(int capacity)
Creates empty set with specified capacity.
Definition: base_set.h:71
BaseSet(const SharedPtr< IEnumerable< T > > &items)
Creates set based on enumerable values.
Definition: base_set.h:98
const_iterator cbegin() const noexcept
Gets iterator to the first const-qualified element of collection.
Definition: base_set.h:115
BaseSet(const ComparerAdapter< T > &comparerAdapter)
Creates empty set that uses the specified comparer adapter Used for creation SortedSet.
Definition: base_set.h:92
bool Remove(const T &item) override
Removes element from set.
Definition: base_set.h:151
int get_Count() const override
Gets number of elements in set.
Definition: base_set.h:129
SET_T set_t
Underlying data type.
Definition: base_set.h:28
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:181
typename set_t::iterator iterator
Iterator type.
Definition: base_set.h:30
System::Details::VirtualizedIteratorBase< T > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: base_set.h:224
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:84
set_t & data()
Underlying data structure accessor.
Definition: base_set.h:175
~BaseSet() override
Destructor.
Definition: base_set.h:245
bool Contains(const T &item) const override
Checks if element is present in set.
Definition: base_set.h:168
const_iterator cend() const noexcept
Gets iterator for a non-existent const-qualified element behind the end of the collection.
Definition: base_set.h:118
IEnumeratorPtr GetEnumerator() override
Creates enumerator.
Definition: base_set.h:122
T ValueType
Value type.
Definition: base_set.h:42
const_iterator end() const noexcept
Gets iterator for a non-existent element behind the end of the const-qualified collection.
Definition: base_set.h:111
void Clear() override
Deletes all elements in set.
Definition: base_set.h:161
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: base_set.h:219
void CopyTo(ArrayPtr< T > arr, int index) override
Copies hash contents into existing array elements.
Definition: base_set.h:192
const set_t & data() const
Underlying data structure accessor.
Definition: base_set.h:178
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:25
Iterator class for simple containers holding elements directly using rbegin() and rend() functions....
Definition: base_enumerator.h:81
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
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:1650
Adapter to use IComparer within STL environment. Uses IComparer if set; otherwise,...
Definition: icomparer.h:44
Adapter making it possible using IEqualityComparer with STL-styled collections and algorithms....
Definition: iequality_comparer.h:38
Adapter to use IEqualityComparer for hashing. Uses comparator object, if set; otherwise,...
Definition: iequality_comparer.h:159