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"
16namespace System {
namespace Collections {
namespace Generic {
23 template <
typename Key,
typename Mapped,
typename Comparer>
24 class ASPOSECPP_SHARED_CLASS FlatMap
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;
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) {}
64 iterator begin() noexcept {
return m_data.begin(); }
67 iterator end() noexcept {
return m_data.end(); }
71 const_iterator begin() const noexcept {
return m_data.begin(); }
74 const_iterator end() const noexcept {
return m_data.end(); }
78 const_iterator cbegin() const noexcept {
return m_data.cbegin(); }
81 const_iterator cend() const noexcept {
return m_data.cend(); }
85 reverse_iterator rbegin() noexcept {
return m_data.rbegin(); }
88 reverse_iterator rend() noexcept {
return m_data.rend(); }
92 const_reverse_iterator rbegin() const noexcept {
return m_data.rbegin(); }
95 const_reverse_iterator rend() const noexcept {
return m_data.rend(); }
100 const_reverse_iterator crbegin() const noexcept {
return m_data.crbegin(); }
103 const_reverse_iterator crend() const noexcept {
return m_data.crend(); }
108 mapped_type& operator [] (
const key_type &key)
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;
114 return m_data.insert(position, value_type(key, mapped_type()))->second;
120 iterator erase(const_iterator position)
122 return m_data.erase(position);
128 std::pair<iterator, bool> insert(
const value_type &pair)
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);
134 return std::make_pair(m_data.insert(position, pair),
true);
139 iterator find(
const key_type &key)
141 const iterator position = std::lower_bound(begin(), end(), key, m_comparer);
142 if (position == end() || m_comparer(key, position->first))
150 const_iterator find(
const key_type &key)
const
152 const const_iterator position = std::lower_bound(begin(), end(), key, m_comparer);
153 if (position == end() || m_comparer(key, position->first))
161 size_type count(
const key_type &key)
const
163 const const_iterator position = std::lower_bound(begin(), end(), key, m_comparer);
164 if (position == end() || m_comparer(key, position->first))
177 size_type size()
const
179 return m_data.size();
184 void reserve(size_type cnt)
193 return size() == other.size() && std::equal(begin(), end(), other.begin());
198 vector_type& data() noexcept
205 vector_type
const& data() const noexcept
212 allocator_type get_allocator()
const
214 return m_data.allocator_type();
220 size_type capacity()
const
222 return m_data.capacity();
226 class value_type_comparer {
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
242 return m_comparer(getKey(a), getKey(b));
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;
260 value_type_comparer m_comparer;
268template<
typename TKey,
typename TValue>
274 return get_KeysImpl();
278 return get_ValuesImpl();
331template<
typename TKey,
typename TValue>
342 RTTI_INFO_TEMPLATE_CLASS(
_ThisType, System::BaseTypesInfo<_BaseType>);
346 using _BaseType::m_map;
354 typedef Detail::FlatMap<TKey, TValue, ComparerAdapter<TKey> >
map_t;
398 this->m_map.reserve(
static_cast<typename map_t::size_type
>(capacity));
404 return static_cast<int>(this->m_map.capacity());
410 this->m_map.reserve(capacity);
417 this->m_map.erase(this->m_map.begin() + index);
424 auto iter = this->m_map.find(key);
425 if (iter == this->m_map.end())
427 return ASPOSECPP_CHECKED_CAST(
int, std::distance(this->m_map.begin(), iter));
434 for (
auto iter = this->m_map.begin(); iter != this->m_map.end(); ++iter)
436 if (iter->second == value)
438 return ASPOSECPP_CHECKED_CAST(
int, std::distance(this->m_map.begin(), iter));
469 return m_map.rbegin();
483 return m_map.rbegin();
523#ifdef ASPOSE_GET_SHARED_MEMBERS
525 DEFINE_GET_SHARED_MEMBERS(_BaseType::m_map)
528 using _HelperType::get_Values;
529 using _HelperType::get_Keys;
533 return MakeObject<_KeyList<SortedList<TKey, TValue>>>(
MakeSharedPtr(
this));
537 return MakeObject<_ValueList<SortedList<TKey, TValue>>>(
MakeSharedPtr(
this));
547#ifdef ASPOSE_COMPARE_BY_REF
549 typedef const T& args_type;
557 class DefaultClassComparer :
public DefaultComparer<T>
565 int32_t
Compare(args_type c1, args_type c2)
const override
567 return c1->CompareTo(c2);
572 class DefaultStructComparer :
public DefaultComparer<T>
580 int32_t
Compare(args_type c1, args_type c2)
const override
582 return c1.CompareTo(c2);
590 typename std::enable_if<
591 detail::has_method_compareto_via_arrow<T>::value,
592 SharedPtr<IComparer<T>>
593 >::type GetDefaultComparer()
595 return MakeObject<DefaultClassComparer<T>>();
601 typename std::enable_if<
602 detail::has_method_compareto_via_dot<T>::value,
603 SharedPtr<IComparer<T>>
604 >::type GetDefaultComparer()
606 return MakeObject<DefaultStructComparer<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()
617 return SharedPtr<IComparer<T> >();
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