forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortedDictionary.cs
267 lines (228 loc) · 7.45 KB
/
SortedDictionary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System;
using System.Collections.Generic;
using DataStructures.Trees;
namespace DataStructures.SortedCollections
{
/// <summary>
/// Sorted Dictionary collection (Red-Black Tree based).
/// </summary>
public class SortedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>
where TKey : IComparable<TKey>
{
/// <summary>
/// The internal collection is a Red-Black Tree Map.
/// </summary>
private RedBlackTreeMap<TKey, TValue> _collection { get; set; }
/// <summary>
/// Constructor.
/// </summary>
public SortedDictionary()
{
_collection = new RedBlackTreeMap<TKey, TValue>(allowDuplicates: false);
}
/// <summary>
/// Gets the count of enteries in dictionary.
/// </summary>
public int Count
{
get { return _collection.Count; }
}
/// <summary>
/// Returns true if dictionary is empty; otherwise, false.
/// </summary>
public bool IsEmpty
{
get { return Count == 0; }
}
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Determines whether the current dictionary contains an entry with the specified key.
/// </summary>
public bool ContainsKey(TKey key)
{
return _collection.Contains(key);
}
/// <summary>
/// Determines whether the current collection contains a specific key-value pair.
/// </summary>
public bool Contains(KeyValuePair<TKey, TValue> item)
{
try
{
var entry = _collection.Find(item.Key);
return entry.Value.Equals(item.Value);
}
catch(Exception)
{
return false;
}
}
/// <summary>
/// Try to get the value of a key or default(TValue). Returns true if key exists; otherwise, false.
/// </summary>
public bool TryGetValue(TKey key, out TValue value)
{
// Set value to the default value of type TValue
value = default(TValue);
try
{
// Assign the returned object to value
value = _collection.Find(key).Value;
// return Success.
return true;
}
catch(KeyNotFoundException)
{
// No entry was found with the specified key.
// return Failure.
return false;
}
}
/// <summary>
/// Gets or sets the value at the specified key.
/// </summary>
public TValue this[TKey index]
{
get
{
// In case dictionary is empty
if (IsEmpty)
throw new Exception("Dictionary is empty.");
try
{
return _collection.Find(index).Value;
}
catch(KeyNotFoundException)
{
// Mask the tree's exception with a new one.
throw new KeyNotFoundException("Key doesn't exist in dictionary.");
}
}
set
{
if (ContainsKey(index))
_collection.Update(index, value);
else
Add(index, value);
}
}
/// <summary>
/// Gets the collection of keys in the dictionary.
/// </summary>
public ICollection<TKey> Keys
{
get
{
var keys = new System.Collections.Generic.List<TKey>(Count);
var enumerator = _collection.GetInOrderEnumerator();
while (enumerator.MoveNext())
keys.Add(enumerator.Current.Key);
return keys;
}
}
/// <summary>
/// Gets the collection of values in the dictionary.
/// </summary>
public ICollection<TValue> Values
{
get
{
var values = new System.Collections.Generic.List<TValue>(Count);
var enumerator = _collection.GetInOrderEnumerator();
while (enumerator.MoveNext())
values.Add(enumerator.Current.Value);
return values;
}
}
/// <summary>
/// Add the specified key and value to the dictionary.
/// </summary>
public void Add(TKey key, TValue value)
{
// Throw an duplicate key exception if an entry with the same key exists
try
{
_collection.Insert(key, value);
}
catch(InvalidOperationException)
{
throw new InvalidOperationException("An entry with the same key already exists in dictionary.");
}
}
/// <summary>
/// Removes the item with specific Key from the dictionary.
/// </summary>
public bool Remove(TKey key)
{
try
{
// Try removing it and return Success
_collection.Remove(key);
return true;
}
catch(Exception)
{
// Item was not found. Return Failure.
return false;
}
}
/// <summary>
/// Add the key-value pair to the dictionary.
/// </summary>
public void Add(KeyValuePair<TKey, TValue> item)
{
Add(item.Key, item.Value);
}
/// <summary>
/// Removes the first occurrence of an item from the current collection Key and Value will be matched.
/// </summary>
public bool Remove(KeyValuePair<TKey, TValue> item)
{
if (IsEmpty)
return false;
// Get the entry from collection
var entry = _collection.Find(item.Key);
// If the entry's value match the value of the specified item, remove it
if (entry.Value.Equals(item.Value))
{
_collection.Remove(item.Key);
return true;
}
return false;
}
/// <summary>
/// Copies the key-value pairs to a given array starting from specified index.
/// </summary>
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (array == null)
throw new ArgumentNullException();
var enumerator = _collection.GetInOrderEnumerator();
while (enumerator.MoveNext() && arrayIndex < array.Length)
{
array[arrayIndex] = enumerator.Current;
arrayIndex++;
}
}
/// <summary>
/// Clears this instance.
/// </summary>
public void Clear()
{
_collection = new RedBlackTreeMap<TKey, TValue>(allowDuplicates: false);
}
#region IEnumerable implementation
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _collection.GetInOrderEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}