DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
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
"
6
#include "
System/ArgumentException.h
"
7
#include "
System/InvalidOperationException.h
"
8
#include "
System/Collections/Generic/List.h
"
9
10
namespace
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
105
Array<TKey>
GetKeys
()
const
{
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
114
Array<TValue>
GetValues
()
const
{
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
}
ArgumentException.h
Defines the exception thrown when an invalid argument is provided to a method.
Array.h
Provides methods for creating, manipulating, searching, and sorting arrays.
Common.h
Defines common cross-platform macros, export decorators, and fundamental types.
InvalidOperationException.h
Defines the exception thrown when a method call is invalid for the object's current state.
List.h
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Object.h
Base object class for DotNetDupe mirroring .NET System.Object.
DotNetDupe::System::ArgumentException
The exception that is thrown when one of the arguments provided to a method is not valid.
Definition
ArgumentException.h:16
DotNetDupe::System::Array
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition
Array.h:29
DotNetDupe::System::Collections::Generic::List
Represents a strongly typed list of objects accessible by index.
Definition
List.h:29
DotNetDupe::System::Collections::Generic::SortedDictionary::TryGetValue
bool TryGetValue(const TKey &key, TValue &value) const
Definition
SortedDictionary.h:96
DotNetDupe::System::Collections::Generic::SortedDictionary::GetKeys
Array< TKey > GetKeys() const
Definition
SortedDictionary.h:105
DotNetDupe::System::Collections::Generic::SortedDictionary::ContainsKey
bool ContainsKey(const TKey &key) const
Definition
SortedDictionary.h:84
DotNetDupe::System::Collections::Generic::SortedDictionary::Clear
void Clear()
Definition
SortedDictionary.h:80
DotNetDupe::System::Collections::Generic::SortedDictionary::Add
void Add(const TKey &key, const TValue &value)
Definition
SortedDictionary.h:73
DotNetDupe::System::Collections::Generic::SortedDictionary::SortedDictionary
SortedDictionary()=default
Initializes a new instance of the SortedDictionary class that is empty and is sorted by the key.
DotNetDupe::System::Collections::Generic::SortedDictionary::operator[]
TValue & operator[](const TKey &key)
Gets or sets the value associated with the specified key.
Definition
SortedDictionary.h:55
DotNetDupe::System::Collections::Generic::SortedDictionary::GetCount
int GetCount() const
Gets the number of key/value pairs contained in the SortedDictionary.
Definition
SortedDictionary.h:50
DotNetDupe::System::Collections::Generic::SortedDictionary::operator[]
const TValue & operator[](const TKey &key) const
Definition
SortedDictionary.h:65
DotNetDupe::System::Collections::Generic::SortedDictionary::Remove
bool Remove(const TKey &key)
Definition
SortedDictionary.h:88
DotNetDupe::System::Collections::Generic::SortedDictionary::GetValues
Array< TValue > GetValues() const
Definition
SortedDictionary.h:114
DotNetDupe::System::Object
Supports all classes in the DotNetDupe class hierarchy.
Definition
Object.h:18
DotNetDupe::System::Object::operator==
bool operator==(const Object &obj) const
Determines reference equality between two objects.
Definition
Object.cpp:6
DotNetDupe::System::Collections::Generic
Definition
Dictionary.h:15
DotNetDupe::System::Collections
Definition
BlockingCollection.h:15
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
Include
System
Collections
Generic
SortedDictionary.h
Generated by
1.18.0