Skip to content

Commit d497d4b

Browse files
committed
SortedDictionary implementation and tests.
1 parent 43d0acb commit d497d4b

File tree

6 files changed

+346
-100
lines changed

6 files changed

+346
-100
lines changed

C-Sharp-Algorithms.sln

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2012
4-
VisualStudioVersion = 12.0.31101.0
5-
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MainProgram", "MainProgram\MainProgram.csproj", "{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}"
7-
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStructures", "DataStructures\DataStructures.csproj", "{464251A0-3667-42BA-A3D5-0581D65C442B}"
9-
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms", "Algorithms\Algorithms.csproj", "{8F59D573-A1B8-438F-A67A-A7F11FACF201}"
11-
EndProject
12-
Global
13-
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14-
Debug|Any CPU = Debug|Any CPU
15-
Release|Any CPU = Release|Any CPU
16-
EndGlobalSection
17-
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18-
{464251A0-3667-42BA-A3D5-0581D65C442B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19-
{464251A0-3667-42BA-A3D5-0581D65C442B}.Debug|Any CPU.Build.0 = Debug|Any CPU
20-
{464251A0-3667-42BA-A3D5-0581D65C442B}.Release|Any CPU.ActiveCfg = Release|Any CPU
21-
{464251A0-3667-42BA-A3D5-0581D65C442B}.Release|Any CPU.Build.0 = Release|Any CPU
22-
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23-
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
24-
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
25-
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Release|Any CPU.Build.0 = Release|Any CPU
26-
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27-
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Debug|Any CPU.Build.0 = Debug|Any CPU
28-
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Release|Any CPU.ActiveCfg = Release|Any CPU
29-
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Release|Any CPU.Build.0 = Release|Any CPU
30-
EndGlobalSection
31-
GlobalSection(MonoDevelopProperties) = preSolution
32-
Policies = $0
33-
$0.TextStylePolicy = $1
34-
$1.inheritsSet = VisualStudio
35-
$1.inheritsScope = text/plain
36-
$1.scope = text/x-csharp
37-
$0.CSharpFormattingPolicy = $2
38-
$2.IndentSwitchBody = True
39-
$2.IndentBlocksInsideExpressions = True
40-
$2.AnonymousMethodBraceStyle = NextLine
41-
$2.PropertyBraceStyle = NextLine
42-
$2.PropertyGetBraceStyle = NextLine
43-
$2.PropertySetBraceStyle = NextLine
44-
$2.EventBraceStyle = NextLine
45-
$2.EventAddBraceStyle = NextLine
46-
$2.EventRemoveBraceStyle = NextLine
47-
$2.StatementBraceStyle = NextLine
48-
$2.ElseNewLinePlacement = NewLine
49-
$2.CatchNewLinePlacement = NewLine
50-
$2.FinallyNewLinePlacement = NewLine
51-
$2.WhileNewLinePlacement = DoNotCare
52-
$2.ArrayInitializerWrapping = DoNotChange
53-
$2.ArrayInitializerBraceStyle = NextLine
54-
$2.BeforeMethodDeclarationParentheses = False
55-
$2.BeforeMethodCallParentheses = False
56-
$2.BeforeConstructorDeclarationParentheses = False
57-
$2.NewLineBeforeConstructorInitializerColon = NewLine
58-
$2.NewLineAfterConstructorInitializerColon = SameLine
59-
$2.BeforeDelegateDeclarationParentheses = False
60-
$2.NewParentheses = False
61-
$2.SpacesBeforeBrackets = False
62-
$2.inheritsSet = Mono
63-
$2.inheritsScope = text/x-csharp
64-
$2.scope = text/x-csharp
65-
EndGlobalSection
66-
GlobalSection(CodealikeProperties) = postSolution
67-
SolutionGuid = f7178bdb-e6ee-479b-8876-5fd96edf50a4
68-
EndGlobalSection
69-
GlobalSection(SolutionProperties) = preSolution
70-
HideSolutionNode = FALSE
71-
EndGlobalSection
72-
EndGlobal
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2012
4+
VisualStudioVersion = 12.0.31101.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MainProgram", "MainProgram\MainProgram.csproj", "{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStructures", "DataStructures\DataStructures.csproj", "{464251A0-3667-42BA-A3D5-0581D65C442B}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms", "Algorithms\Algorithms.csproj", "{8F59D573-A1B8-438F-A67A-A7F11FACF201}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{464251A0-3667-42BA-A3D5-0581D65C442B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{464251A0-3667-42BA-A3D5-0581D65C442B}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{464251A0-3667-42BA-A3D5-0581D65C442B}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{464251A0-3667-42BA-A3D5-0581D65C442B}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{7DD9570B-B0B6-414C-A5E5-ACB622A294B6}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{8F59D573-A1B8-438F-A67A-A7F11FACF201}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(MonoDevelopProperties) = preSolution
32+
Policies = $0
33+
$0.TextStylePolicy = $1
34+
$1.inheritsSet = VisualStudio
35+
$1.inheritsScope = text/plain
36+
$1.scope = text/x-csharp
37+
$0.CSharpFormattingPolicy = $2
38+
$2.IndentSwitchBody = True
39+
$2.IndentBlocksInsideExpressions = True
40+
$2.AnonymousMethodBraceStyle = NextLine
41+
$2.PropertyBraceStyle = NextLine
42+
$2.PropertyGetBraceStyle = NextLine
43+
$2.PropertySetBraceStyle = NextLine
44+
$2.EventBraceStyle = NextLine
45+
$2.EventAddBraceStyle = NextLine
46+
$2.EventRemoveBraceStyle = NextLine
47+
$2.StatementBraceStyle = NextLine
48+
$2.ElseNewLinePlacement = NewLine
49+
$2.CatchNewLinePlacement = NewLine
50+
$2.FinallyNewLinePlacement = NewLine
51+
$2.WhileNewLinePlacement = DoNotCare
52+
$2.ArrayInitializerWrapping = DoNotChange
53+
$2.ArrayInitializerBraceStyle = NextLine
54+
$2.BeforeMethodDeclarationParentheses = False
55+
$2.BeforeMethodCallParentheses = False
56+
$2.BeforeConstructorDeclarationParentheses = False
57+
$2.NewLineBeforeConstructorInitializerColon = NewLine
58+
$2.NewLineAfterConstructorInitializerColon = SameLine
59+
$2.BeforeDelegateDeclarationParentheses = False
60+
$2.NewParentheses = False
61+
$2.SpacesBeforeBrackets = False
62+
$2.inheritsSet = Mono
63+
$2.inheritsScope = text/x-csharp
64+
$2.scope = text/x-csharp
65+
EndGlobalSection
66+
GlobalSection(CodealikeProperties) = postSolution
67+
SolutionGuid = f7178bdb-e6ee-479b-8876-5fd96edf50a4
68+
EndGlobalSection
69+
GlobalSection(SolutionProperties) = preSolution
70+
HideSolutionNode = FALSE
71+
EndGlobalSection
72+
EndGlobal

DataStructures/SortedCollections/SortedDictionary.cs

Lines changed: 151 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33

4-
using DataStructures.Common;
54
using DataStructures.Trees;
65

76
namespace DataStructures.SortedCollections
@@ -53,91 +52,218 @@ public bool IsReadOnly
5352
/// </summary>
5453
public bool ContainsKey(TKey key)
5554
{
56-
throw new NotImplementedException();
55+
return _collection.Contains(key);
5756
}
5857

58+
/// <summary>
59+
/// Determines whether the current collection contains a specific key-value pair.
60+
/// </summary>
5961
public bool Contains(KeyValuePair<TKey, TValue> item)
6062
{
61-
throw new NotImplementedException();
63+
try
64+
{
65+
var entry = _collection.Find(item.Key);
66+
return entry.Value.Equals(item.Value);
67+
}
68+
catch(Exception)
69+
{
70+
return false;
71+
}
6272
}
6373

64-
public void Add(TKey key, TValue value)
74+
/// <summary>
75+
/// Try to get the value of a key or default(TValue). Returns true if key exists; otherwise, false.
76+
/// </summary>
77+
public bool TryGetValue(TKey key, out TValue value)
6578
{
66-
throw new NotImplementedException();
67-
}
79+
// Set value to the default value of type TValue
80+
value = default(TValue);
6881

69-
public bool Remove(TKey key)
70-
{
71-
throw new NotImplementedException();
72-
}
82+
try
83+
{
84+
// Assign the returned object to value
85+
value = _collection.Find(key).Value;
7386

74-
public bool TryGetValue(TKey key, out TValue value)
75-
{
76-
throw new NotImplementedException();
87+
// return Success.
88+
return true;
89+
}
90+
catch(KeyNotFoundException)
91+
{
92+
// No entry was found with the specified key.
93+
// return Failure.
94+
return false;
95+
}
7796
}
7897

98+
/// <summary>
99+
/// Gets or sets the value at the specified key.
100+
/// </summary>
79101
public TValue this[TKey index]
80102
{
81103
get
82104
{
83-
throw new NotImplementedException();
105+
// In case dictionary is empty
106+
if (IsEmpty)
107+
throw new Exception("Dictionary is empty.");
108+
109+
try
110+
{
111+
return _collection.Find(index).Value;
112+
}
113+
catch(KeyNotFoundException)
114+
{
115+
// Mask the tree's exception with a new one.
116+
throw new KeyNotFoundException("Key doesn't exist in dictionary.");
117+
}
84118
}
85119
set
86120
{
87-
throw new NotImplementedException();
121+
if (ContainsKey(index))
122+
_collection.Update(index, value);
123+
else
124+
Add(index, value);
88125
}
89126
}
90127

128+
/// <summary>
129+
/// Gets the collection of keys in the dictionary.
130+
/// </summary>
91131
public ICollection<TKey> Keys
92132
{
93133
get
94134
{
95-
throw new NotImplementedException();
135+
var keys = new System.Collections.Generic.List<TKey>(Count);
136+
var enumerator = _collection.GetInOrderEnumerator();
137+
138+
while (enumerator.MoveNext())
139+
keys.Add(enumerator.Current.Key);
140+
141+
return keys;
96142
}
97143
}
98144

145+
/// <summary>
146+
/// Gets the collection of values in the dictionary.
147+
/// </summary>
99148
public ICollection<TValue> Values
100149
{
101150
get
102151
{
103-
throw new NotImplementedException();
152+
var values = new System.Collections.Generic.List<TValue>(Count);
153+
var enumerator = _collection.GetInOrderEnumerator();
154+
155+
while (enumerator.MoveNext())
156+
values.Add(enumerator.Current.Value);
157+
158+
return values;
104159
}
105160
}
106161

162+
/// <summary>
163+
/// Add the specified key and value to the dictionary.
164+
/// </summary>
165+
public void Add(TKey key, TValue value)
166+
{
167+
// Throw an duplicate key exception if an entry with the same key exists
168+
try
169+
{
170+
_collection.Insert(key, value);
171+
}
172+
catch(InvalidOperationException)
173+
{
174+
throw new InvalidOperationException("An entry with the same key already exists in dictionary.");
175+
}
176+
}
177+
178+
/// <summary>
179+
/// Removes the item with specific Key from the dictionary.
180+
/// </summary>
181+
public bool Remove(TKey key)
182+
{
183+
try
184+
{
185+
// Try removing it and return Success
186+
_collection.Remove(key);
187+
return true;
188+
}
189+
catch(Exception)
190+
{
191+
// Item was not found. Return Failure.
192+
return false;
193+
}
194+
}
195+
196+
/// <summary>
197+
/// Add the key-value pair to the dictionary.
198+
/// </summary>
107199
public void Add(KeyValuePair<TKey, TValue> item)
108200
{
109-
throw new NotImplementedException();
201+
Add(item.Key, item.Value);
110202
}
111203

112-
public void Clear()
204+
/// <summary>
205+
/// Removes the first occurrence of an item from the current collection Key and Value will be matched.
206+
/// </summary>
207+
public bool Remove(KeyValuePair<TKey, TValue> item)
113208
{
114-
_collection = new RedBlackTreeMap<TKey, TValue>(allowDuplicates: false);
209+
if (IsEmpty)
210+
return false;
211+
212+
// Get the entry from collection
213+
var entry = _collection.Find(item.Key);
214+
215+
// If the entry's value match the value of the specified item, remove it
216+
if (entry.Value.Equals(item.Value))
217+
{
218+
_collection.Remove(item.Key);
219+
return true;
220+
}
221+
else
222+
{
223+
return false;
224+
}
115225
}
116226

227+
/// <summary>
228+
/// Copies the key-value pairs to a given array starting from specified index.
229+
/// </summary>
117230
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
118231
{
119-
throw new NotImplementedException();
232+
if (array == null)
233+
throw new ArgumentNullException();
234+
235+
var enumerator = _collection.GetInOrderEnumerator();
236+
237+
while (enumerator.MoveNext() && arrayIndex < array.Length)
238+
{
239+
array[arrayIndex] = enumerator.Current;
240+
arrayIndex++;
241+
}
120242
}
121243

122-
public bool Remove(KeyValuePair<TKey, TValue> item)
244+
/// <summary>
245+
/// Clears this instance.
246+
/// </summary>
247+
public void Clear()
123248
{
124-
throw new NotImplementedException();
249+
_collection = new RedBlackTreeMap<TKey, TValue>(allowDuplicates: false);
125250
}
126251

127252

128253
#region IEnumerable implementation
129254

130255
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
131256
{
132-
throw new NotImplementedException();
257+
return _collection.GetInOrderEnumerator();
133258
}
134259

135260
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
136261
{
137-
throw new NotImplementedException();
262+
return GetEnumerator();
138263
}
139264

140265
#endregion
141266
}
267+
142268
}
143269

0 commit comments

Comments
 (0)