CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
sorted_list.h
1
2#pragma once
3
4#include "system/object.h"
5#include "system/collections/ilist.h"
6#include "system/collections/icomparer.h"
7#include "system/collections/base_dictionary.h"
8#include "system/collections/keyvalue_list.h"
9#include "system/exceptions.h"
10#include "system/cycles_detection.h"
11
12#include <vector>
13#include <utility>
14#include <algorithm>
15
16namespace System { namespace Collections { namespace Generic {
17
18namespace Detail {
23 template <typename Key, typename Mapped, typename Comparer>
24 class ASPOSECPP_SHARED_CLASS FlatMap
25 {
26 public:
28 typedef Key key_type;
30 typedef Mapped mapped_type;
32 typedef std::pair<key_type, mapped_type> value_type;
34 typedef Comparer comparer_type;
36 typedef ASPOSE_VECTOR_ALLOCATOR_TYPE(key_type, mapped_type) allocator_type;
38 typedef std::vector<value_type, allocator_type> vector_type;
40 typedef typename vector_type::iterator iterator;
42 typedef typename vector_type::const_iterator const_iterator;
44 typedef typename vector_type::reverse_iterator reverse_iterator;
46 typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
48 typedef typename vector_type::size_type size_type;
49
52 FlatMap(const allocator_type &allocator = allocator_type()) : m_data(allocator) {}
56 FlatMap(const comparer_type &cmp, const allocator_type &allocator = allocator_type()) : m_data(allocator), m_comparer(cmp) {}
60 FlatMap(const FlatMap &map, const allocator_type &allocator) : m_data(map.m_data, allocator) {}
61
64 iterator begin() noexcept { return m_data.begin(); }
67 iterator end() noexcept { return m_data.end(); }
68
71 const_iterator begin() const noexcept { return m_data.begin(); }
74 const_iterator end() const noexcept { return m_data.end(); }
75
78 const_iterator cbegin() const noexcept { return m_data.cbegin(); }
81 const_iterator cend() const noexcept { return m_data.cend(); }
82
85 reverse_iterator rbegin() noexcept { return m_data.rbegin(); }
88 reverse_iterator rend() noexcept { return m_data.rend(); }
89
92 const_reverse_iterator rbegin() const noexcept { return m_data.rbegin(); }
95 const_reverse_iterator rend() const noexcept { return m_data.rend(); }
96
97
100 const_reverse_iterator crbegin() const noexcept { return m_data.crbegin(); }
103 const_reverse_iterator crend() const noexcept { return m_data.crend(); }
104
108 mapped_type& operator [] (const key_type &key)
109 {
110 const iterator position = std::lower_bound(begin(), end(), key, m_comparer);
111 if (position != end() && !m_comparer(key, position->first))
112 return position->second;
113
114 return m_data.insert(position, value_type(key, mapped_type()))->second;
115 }
116
120 iterator erase(const_iterator position)
121 {
122 return m_data.erase(position);
123 }
128 std::pair<iterator, bool> insert(const value_type &pair)
129 {
130 const iterator position = std::lower_bound(begin(), end(), pair, m_comparer);
131 if (position != end() && !m_comparer(pair.first, position->first))
132 return std::make_pair(position, false);
133
134 return std::make_pair(m_data.insert(position, pair), true);
135 }
139 iterator find(const key_type &key)
140 {
141 const iterator position = std::lower_bound(begin(), end(), key, m_comparer);
142 if (position == end() || m_comparer(key, position->first))
143 return end();
144 else
145 return position;
146 }
150 const_iterator find(const key_type &key) const
151 {
152 const const_iterator position = std::lower_bound(begin(), end(), key, m_comparer);
153 if (position == end() || m_comparer(key, position->first))
154 return end();
155 else
156 return position;
157 }
161 size_type count(const key_type &key) const
162 {
163 const const_iterator position = std::lower_bound(begin(), end(), key, m_comparer);
164 if (position == end() || m_comparer(key, position->first))
165 return 0;
166 else
167 return 1;
168 }
169
171 void clear()
172 {
173 m_data.clear();
174 }
177 size_type size() const
178 {
179 return m_data.size();
180 }
181
184 void reserve(size_type cnt)
185 {
186 m_data.reserve(cnt);
187 }
191 bool operator == (const FlatMap &other) const
192 {
193 return size() == other.size() && std::equal(begin(), end(), other.begin());
194 }
195
198 vector_type& data() noexcept
199 {
200 return m_data;
201 }
202
205 vector_type const& data() const noexcept
206 {
207 return m_data;
208 }
209
212 allocator_type get_allocator() const
213 {
214 return m_data.allocator_type();
215 }
216
220 size_type capacity() const
221 {
222 return m_data.capacity();
223 }
224 private:
226 class value_type_comparer {
227 public:
229 inline value_type_comparer() {}
232 inline value_type_comparer(const comparer_type &cmp) : m_comparer(cmp) {}
239 template <class P, class Q>
240 inline bool operator () (const P &a, const Q &b) const
241 {
242 return m_comparer(getKey(a), getKey(b));
243 }
244 private:
248 static inline const key_type& getKey(const key_type &key) { return key; }
252 static inline const key_type& getKey(const value_type &pair) { return pair.first; }
254 comparer_type m_comparer;
255 };
256
258 vector_type m_data;
260 value_type_comparer m_comparer;
261 };
262
263} // namespace Detail
264
268template<typename TKey, typename TValue>
269class ASPOSECPP_SHARED_CLASS SortedListHelper
270{
271public:
273 {
274 return get_KeysImpl();
275 }
277 {
278 return get_ValuesImpl();
279 }
280protected:
283};
284
285
331template<typename TKey, typename TValue>
332class ASPOSECPP_SHARED_CLASS SortedList
333 : public SortedListHelper<TKey, TValue>, public BaseDictionary<Detail::FlatMap<TKey, TValue, ComparerAdapter<TKey> > >
334{
342 RTTI_INFO_TEMPLATE_CLASS(_ThisType, System::BaseTypesInfo<_BaseType>);
343
344protected:
346 using _BaseType::m_map;
347
348public:
354 typedef Detail::FlatMap<TKey, TValue, ComparerAdapter<TKey> > map_t;
365
371 typedef typename map_t::reverse_iterator reverse_iterator;
373 typedef typename map_t::const_reverse_iterator const_reverse_iterator;
374
377 : _BaseType(0, ComparerAdapter<TKey>(GetDefaultComparer<TKey>()))
378 {}
382 : _BaseType(0, ComparerAdapter<TKey>(comparer)) // use forwarding ctor
383 {}
387 : _BaseType(src.get())
388 {}
391 SortedList(const map_t& map)
392 : _BaseType(0, map) // 0 - use forwarding ctor
393 {}
396 SortedList(int capacity)
397 {
398 this->m_map.reserve(static_cast<typename map_t::size_type>(capacity));
399 }
402 int get_Capacity() const
403 {
404 return static_cast<int>(this->m_map.capacity());
405 }
408 void set_Capacity(int capacity)
409 {
410 this->m_map.reserve(capacity);
411 }
412
415 void RemoveAt(int index)
416 {
417 this->m_map.erase(this->m_map.begin() + index);
418 }
422 int IndexOfKey(TKey key) const
423 {
424 auto iter = this->m_map.find(key);
425 if (iter == this->m_map.end())
426 return -1;
427 return ASPOSECPP_CHECKED_CAST(int, std::distance(this->m_map.begin(), iter));
428 }
432 int IndexOfValue(TValue value) const
433 {
434 for (auto iter = this->m_map.begin(); iter != this->m_map.end(); ++iter)
435 {
436 if (iter->second == value)
437 {
438 return ASPOSECPP_CHECKED_CAST(int, std::distance(this->m_map.begin(), iter));
439 }
440 }
441 return -1;
442 }
443
448 class Enumerator : public SimpleEnumerator<map_t, KVPair>
449 {
450 public:
452 Enumerator(const Ptr& dict) : SimpleEnumerator<map_t, KVPair>(dict, dict->m_map) { }
456 RTTI_INFO_TEMPLATE_CLASS(EnumeratorType, System::BaseTypesInfo<System::Object>);
457 };
461 {
462 return MakeObject<Enumerator>(MakeSharedPtr(this));
463 }
464
468 {
469 return m_map.rbegin();
470 }
475 {
476 return m_map.rend();
477 }
478
482 {
483 return m_map.rbegin();
484 }
489 {
490 return m_map.rend();
491 }
492
496 {
497 return rbegin();
498 }
503 {
504 return rend();
505 }
506
507protected:
509 ~SortedList() override {}
510
514 {
515 return get_Keys();
516 }
520 {
521 return get_Values();
522 }
523#ifdef ASPOSE_GET_SHARED_MEMBERS
525 DEFINE_GET_SHARED_MEMBERS(_BaseType::m_map)
526#endif
527public:
528 using _HelperType::get_Values;
529 using _HelperType::get_Keys;
530protected:
532 {
533 return MakeObject<_KeyList<SortedList<TKey, TValue>>>(MakeSharedPtr(this));
534 }
536 {
537 return MakeObject<_ValueList<SortedList<TKey, TValue>>>(MakeSharedPtr(this));
538 }
539
540private:
543 template<typename T>
544 class DefaultComparer : public IComparer<T>
545 {
546 public:
547#ifdef ASPOSE_COMPARE_BY_REF
549 typedef const T& args_type;
550#else
552 typedef T args_type;
553#endif
554 };
555
556 template<typename T>
557 class DefaultClassComparer : public DefaultComparer<T>
558 {
559 using args_type = typename DefaultComparer<T>::args_type;
560 public:
565 int32_t Compare(args_type c1, args_type c2) const override
566 {
567 return c1->CompareTo(c2);
568 }
569 };
570
571 template<typename T>
572 class DefaultStructComparer : public DefaultComparer<T>
573 {
574 using args_type = typename DefaultComparer<T>::args_type;
575 public:
580 int32_t Compare(args_type c1, args_type c2) const override
581 {
582 return c1.CompareTo(c2);
583 }
584 };
585
589 template<typename T>
590 typename std::enable_if<
591 detail::has_method_compareto_via_arrow<T>::value,
592 SharedPtr<IComparer<T>>
593 >::type GetDefaultComparer()
594 {
595 return MakeObject<DefaultClassComparer<T>>();
596 }
600 template<typename T>
601 typename std::enable_if<
602 detail::has_method_compareto_via_dot<T>::value,
603 SharedPtr<IComparer<T>>
604 >::type GetDefaultComparer()
605 {
606 return MakeObject<DefaultStructComparer<T>>();
607 }
611 template<typename T>
612 typename std::enable_if<
613 !detail::has_method_compareto_via_arrow<T>::value && !detail::has_method_compareto_via_dot<T>::value,
614 SharedPtr<IComparer<T>>
615 >::type GetDefaultComparer()
616 {
617 return SharedPtr<IComparer<T> >();
618 }
619};
620
621}}} // namespace System::Collections::Generic
Implements common code for various dictionary-alike data structures (e. g. Dictionary,...
Definition: base_dictionary.h:100
Default comparator class. Uses operator < and operator == to compare values. Objects of this class sh...
Definition: icomparer.h:97
Interface of collection of elements. Objects of this class should only be allocated using System::Mak...
Definition: icollection.h:20
Interface that compares two objects in greater-equal-less sense. Objects of this class should only be...
Definition: icomparer.h:20
const T & args_type
Comparison argument type.
Definition: icomparer.h:27
Interface for dictionary-alike containers. Objects of this class should only be allocated using Syste...
Definition: idictionary.h:21
Adapting iterator, wraps std::pair into KVPair expected from Dictionary.
Definition: base_enumerator.h:109
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
Iterator class for simple containers holding elements directly using rbegin() and rend() functions....
Definition: base_enumerator.h:81
Enumerator class to iterate through list. Objects of this class should only be allocated using System...
Definition: sorted_list.h:449
Enumerator(const Ptr &dict)
Creates enumerator iterating through specific dictionary.
Definition: sorted_list.h:452
SortedList< TKey, TValue >::Enumerator EnumeratorType
Enumerator type alias.
Definition: sorted_list.h:454
RTTI_INFO_TEMPLATE_CLASS(EnumeratorType, System::BaseTypesInfo< System::Object >)
RTTI information.
This helper class is used to mask virtual functions get_Keys get_Values that come from IDictionary in...
Definition: sorted_list.h:270
SharedPtr< IList< TValue > > get_Values() const
Definition: sorted_list.h:276
virtual SharedPtr< IList< TValue > > get_ValuesImpl() const =0
virtual SharedPtr< IList< TKey > > get_KeysImpl() const =0
SharedPtr< IList< TKey > > get_Keys() const
Definition: sorted_list.h:272
Sorted list wrapping FlatMap structure. Objects of this class should only be allocated using System::...
Definition: sorted_list.h:334
ICollection< TKey > KeyCollection
Key collection type.
Definition: sorted_list.h:350
~SortedList() override
Destructor.
Definition: sorted_list.h:509
ICollection< TValue > ValueCollection
Value collection type.
Definition: sorted_list.h:352
SharedPtr< IList< TValue > > get_ValuesImpl() const override
Definition: sorted_list.h:535
int get_Capacity() const
Gets current list capacity.
Definition: sorted_list.h:402
map_t::reverse_iterator reverse_iterator
Reverse iterator type.
Definition: sorted_list.h:371
SortedList< TKey, TValue > this_t
This type.
Definition: sorted_list.h:356
SortedList()
Constructs empty list.
Definition: sorted_list.h:376
SharedPtr< IEnumerable< KVPair > > IEnumerablePtr
Collection of same pairs type.
Definition: sorted_list.h:362
IEnumeratorPtr GetEnumerator() override
Gets enumerator iterating through current list.
Definition: sorted_list.h:460
reverse_iterator rend() noexcept
Gets a reverse iterator for a non-existent element before the start of the collection.
Definition: sorted_list.h:474
SortedList(int capacity)
Constructs empty list.
Definition: sorted_list.h:396
const_reverse_iterator crend() const noexcept
Gets a reverse iterator for a non-existent const-qualified element before the start of the collection...
Definition: sorted_list.h:502
SharedPtr< IList< TKey > > get_KeysImpl() const override
Definition: sorted_list.h:531
void set_Capacity(int capacity)
Sets current list capacity.
Definition: sorted_list.h:408
Detail::FlatMap< TKey, TValue, ComparerAdapter< TKey > > map_t
Underlying data type.
Definition: sorted_list.h:354
_BaseType::const_iterator const_iterator
Const iterator type.
Definition: sorted_list.h:369
SortedList(const SharedPtr< IDictionary< TKey, TValue > > &src)
Copy constructor.
Definition: sorted_list.h:386
int IndexOfValue(TValue value) const
Looks for specific value.
Definition: sorted_list.h:432
const_reverse_iterator rend() const noexcept
Gets a reverse iterator for a non-existent element before the start of the const-qualified collection...
Definition: sorted_list.h:488
const_reverse_iterator crbegin() const noexcept
Gets a reverse iterator to the last const-qualified element of collection (first in reverse).
Definition: sorted_list.h:495
SortedList(const map_t &map)
Copy constructor.
Definition: sorted_list.h:391
SharedPtr< IEnumerator< KVPair > > IEnumeratorPtr
Enumerator type.
Definition: sorted_list.h:364
int IndexOfKey(TKey key) const
Looks for specific key.
Definition: sorted_list.h:422
_BaseType::iterator iterator
Iterator type.
Definition: sorted_list.h:367
virtual SharedPtr< ValueCollection > get_ValuesInternal() const override
Implementation of get_Values() method.
Definition: sorted_list.h:519
SortedList(const SharedPtr< IComparer< TKey > > &comparer)
Constructs empty list.
Definition: sorted_list.h:381
const_reverse_iterator rbegin() const noexcept
Gets a reverse iterator to the last element of the const-qualified collection (first in reverse).
Definition: sorted_list.h:481
KeyValuePair< TKey, TValue > KVPair
Key value pair type.
Definition: sorted_list.h:360
SharedPtr< this_t > Ptr
Pointer type.
Definition: sorted_list.h:358
reverse_iterator rbegin() noexcept
Gets a reverse iterator to the last element of collection (first in reverse).
Definition: sorted_list.h:467
void RemoveAt(int index)
Removes item at specified position.
Definition: sorted_list.h:415
virtual SharedPtr< KeyCollection > get_KeysInternal() const override
Implementation of get_Keys() method.
Definition: sorted_list.h:513
map_t::const_reverse_iterator const_reverse_iterator
Const reverse iterator type.
Definition: sorted_list.h:373
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
bool operator==(const KeyValuePair< TKey, TValue > &left, const KeyValuePair< TKey, TValue > &right)
Compares two key-value pairs using 'equals' semantics. Uses operator == or EqualsTo method for both k...
Definition: keyvalue_pair.h:126
Definition: db_command.h:9
std::enable_if_t<!std::is_floating_point< TA >::value &&!std::is_floating_point< TB >::value, int > Compare(const TA &a, const TB &b)
Compares two values.
Definition: primitive_types.h:113
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