DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
SortedDictionary.h
Go to the documentation of this file.
1#pragma once
2
3#include "Common.h"
4#include "System/Object.h"
5#include "System/Array.h"
9
10namespace DotNetDupe {
11 namespace System {
12 namespace Collections {
13 namespace Generic {
14
22 template <typename TKey, typename TValue>
23 class SortedDictionary : public Object {
24 private:
25 struct KeyValuePair {
26 TKey Key;
27 TValue Value;
28
29 bool operator==(const KeyValuePair& other) const {
30 return Key == other.Key;
31 }
32
33 bool operator<(const KeyValuePair& other) const {
34 return Key < other.Key;
35 }
36
37 bool operator>(const KeyValuePair& other) const {
38 return Key > other.Key;
39 }
40 };
41
42 List<KeyValuePair> m_lstItems;
43
44 public:
46 SortedDictionary() = default;
47
50 int GetCount() const { return m_lstItems.GetCount(); }
51
55 TValue& operator[](const TKey& key) {
56 int index = m_lstItems.BinarySearch(KeyValuePair{ key, TValue() });
57 if (index >= 0) {
58 return m_lstItems[index].Value;
59 }
60
61 m_lstItems.Insert(~index, KeyValuePair{ key, TValue() });
62 return m_lstItems[~index].Value;
63 }
64
65 const TValue& operator[](const TKey& key) const {
66 int index = m_lstItems.BinarySearch(KeyValuePair{ key, TValue() });
67 if (index >= 0) {
68 return m_lstItems[index].Value;
69 }
70 throw System::ArgumentException("Key not found.");
71 }
72
73 void Add(const TKey& key, const TValue& value) {
74 int index = m_lstItems.BinarySearch(KeyValuePair{ key, TValue() });
75 if (index >= 0) throw System::ArgumentException("An item with the same key has already been added.");
76
77 m_lstItems.Insert(~index, KeyValuePair{ key, value });
78 }
79
80 void Clear() {
81 m_lstItems.Clear();
82 }
83
84 bool ContainsKey(const TKey& key) const {
85 return m_lstItems.BinarySearch(KeyValuePair{ key, TValue() }) >= 0;
86 }
87
88 bool Remove(const TKey& key) {
89 int index = m_lstItems.BinarySearch(KeyValuePair{ key, TValue() });
90 if (index < 0) return false;
91
92 m_lstItems.RemoveAt(index);
93 return true;
94 }
95
96 bool TryGetValue(const TKey& key, TValue& value) const {
97 int index = m_lstItems.BinarySearch(KeyValuePair{ key, TValue() });
98 if (index >= 0) {
99 value = m_lstItems[index].Value;
100 return true;
101 }
102 return false;
103 }
104
106 int count = m_lstItems.GetCount();
107 Array<TKey> arrKeys(count);
108 for (int i = 0; i < count; ++i) {
109 arrKeys[i] = m_lstItems[i].Key;
110 }
111 return arrKeys;
112 }
113
115 int count = m_lstItems.GetCount();
116 Array<TValue> arrValues(count);
117 for (int i = 0; i < count; ++i) {
118 arrValues[i] = m_lstItems[i].Value;
119 }
120 return arrValues;
121 }
122 };
123
124 }
125 }
126 }
127}
Defines the exception thrown when an invalid argument is provided to a method.
Provides methods for creating, manipulating, searching, and sorting arrays.
Defines common cross-platform macros, export decorators, and fundamental types.
Defines the exception thrown when a method call is invalid for the object's current state.
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Base object class for DotNetDupe mirroring .NET System.Object.
The exception that is thrown when one of the arguments provided to a method is not valid.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
bool TryGetValue(const TKey &key, TValue &value) const
void Add(const TKey &key, const TValue &value)
SortedDictionary()=default
Initializes a new instance of the SortedDictionary class that is empty and is sorted by the key.
TValue & operator[](const TKey &key)
Gets or sets the value associated with the specified key.
int GetCount() const
Gets the number of key/value pairs contained in the SortedDictionary.
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
bool operator==(const Object &obj) const
Determines reference equality between two objects.
Definition Object.cpp:6