Skip to content

Commit 61825c8

Browse files
committed
SortedDictionary skeleton class.
1 parent 3914568 commit 61825c8

File tree

3 files changed

+156
-14
lines changed

3 files changed

+156
-14
lines changed

DataStructures/DataStructures.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@
9191
<Compile Include="Graphs\UnweightedEdge.cs" />
9292
<Compile Include="Graphs\IEdge.cs" />
9393
<Compile Include="SortedCollections\SortedList.cs" />
94+
<Compile Include="SortedCollections\SortedDictionary.cs" />
95+
<Compile Include="Trees\RedBlackTreeMap.cs" />
96+
<Compile Include="Trees\RedBlackTreeMapNode.cs" />
97+
<Compile Include="Trees\BinarySearchTreeMapNode.cs" />
98+
<Compile Include="Trees\BinarySearchTreeMap.cs" />
9499
</ItemGroup>
95100
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
96101
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
using DataStructures.Common;
5+
using DataStructures.Trees;
6+
7+
namespace DataStructures.SortedCollections
8+
{
9+
/// <summary>
10+
/// Sorted Dictionary collection (Red-Black Tree based).
11+
/// </summary>
12+
public class SortedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>
13+
where TKey : IComparable<TKey>
14+
{
15+
/// <summary>
16+
/// The internal collection is a Red-Black Tree Map.
17+
/// </summary>
18+
private RedBlackTreeMap<TKey, TValue> _collection { get; set; }
19+
20+
21+
/// <summary>
22+
/// Constructor.
23+
/// </summary>
24+
public SortedDictionary()
25+
{
26+
_collection = new RedBlackTreeMap<TKey, TValue>(allowDuplicates: false);
27+
}
28+
29+
30+
/// <summary>
31+
/// Gets the count of enteries in dictionary.
32+
/// </summary>
33+
public int Count
34+
{
35+
get { return _collection.Count; }
36+
}
37+
38+
/// <summary>
39+
/// Returns true if dictionary is empty; otherwise, false.
40+
/// </summary>
41+
public bool IsEmpty
42+
{
43+
get { return Count == 0; }
44+
}
45+
46+
public bool IsReadOnly
47+
{
48+
get { return false; }
49+
}
50+
51+
/// <summary>
52+
/// Determines whether the current dictionary contains an entry with the specified key.
53+
/// </summary>
54+
public bool ContainsKey(TKey key)
55+
{
56+
throw new NotImplementedException();
57+
}
58+
59+
public bool Contains(KeyValuePair<TKey, TValue> item)
60+
{
61+
throw new NotImplementedException();
62+
}
63+
64+
public void Add(TKey key, TValue value)
65+
{
66+
throw new NotImplementedException();
67+
}
68+
69+
public bool Remove(TKey key)
70+
{
71+
throw new NotImplementedException();
72+
}
73+
74+
public bool TryGetValue(TKey key, out TValue value)
75+
{
76+
throw new NotImplementedException();
77+
}
78+
79+
public TValue this[TKey index]
80+
{
81+
get
82+
{
83+
throw new NotImplementedException();
84+
}
85+
set
86+
{
87+
throw new NotImplementedException();
88+
}
89+
}
90+
91+
public ICollection<TKey> Keys
92+
{
93+
get
94+
{
95+
throw new NotImplementedException();
96+
}
97+
}
98+
99+
public ICollection<TValue> Values
100+
{
101+
get
102+
{
103+
throw new NotImplementedException();
104+
}
105+
}
106+
107+
public void Add(KeyValuePair<TKey, TValue> item)
108+
{
109+
throw new NotImplementedException();
110+
}
111+
112+
public void Clear()
113+
{
114+
_collection = new RedBlackTreeMap<TKey, TValue>(allowDuplicates: false);
115+
}
116+
117+
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
118+
{
119+
throw new NotImplementedException();
120+
}
121+
122+
public bool Remove(KeyValuePair<TKey, TValue> item)
123+
{
124+
throw new NotImplementedException();
125+
}
126+
127+
128+
#region IEnumerable implementation
129+
130+
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
131+
{
132+
throw new NotImplementedException();
133+
}
134+
135+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
136+
{
137+
throw new NotImplementedException();
138+
}
139+
140+
#endregion
141+
}
142+
}
143+

DataStructures/SortedCollections/SortedList.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Linq;
32
using System.Collections.Generic;
43

54
using DataStructures.Common;
@@ -29,31 +28,22 @@ public SortedList()
2928
/// <summary>
3029
/// Returns true if list is empty; otherwise, false.
3130
/// </summary>
32-
public bool Empty
31+
public bool IsEmpty
3332
{
34-
get
35-
{
36-
return this.Count == 0;
37-
}
33+
get { return this.Count == 0; }
3834
}
3935

4036
/// <summary>
4137
/// Gets the count of items in list.
4238
/// </summary>
4339
public int Count
4440
{
45-
get
46-
{
47-
return this._collection.Count;
48-
}
41+
get { return this._collection.Count; }
4942
}
5043

5144
public bool IsReadOnly
5245
{
53-
get
54-
{
55-
return false;
56-
}
46+
get { return false; }
5747
}
5848

5949
/// <summary>
@@ -96,6 +86,10 @@ public T this[int index]
9686
{
9787
get
9888
{
89+
// In case list is empty
90+
if (IsEmpty)
91+
throw new Exception("List is empty.");
92+
9993
// Validate index range
10094
if (index < 0 || index >= this.Count)
10195
throw new IndexOutOfRangeException();

0 commit comments

Comments
 (0)