Skip to content

Commit 84e5afb

Browse files
committed
comments
1 parent 5ea702c commit 84e5afb

File tree

9 files changed

+145
-130
lines changed

9 files changed

+145
-130
lines changed

Advanced.Algorithms/DataStructures/Dictionary/Dictionary.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ namespace Advanced.Algorithms.DataStructures
88
/// A dictionary implementation.
99
/// </summary>
1010
/// <typeparam name="K">The key datatype.</typeparam>
11-
/// <typeparam name="TV">The value datatype.</typeparam>
12-
public class Dictionary<TK, TV> : IEnumerable<KeyValuePair<TK, TV>>
11+
/// <typeparam name="V">The value datatype.</typeparam>
12+
public class Dictionary<K, V> : IEnumerable<KeyValuePair<K, V>>
1313
{
14-
private readonly IDictionary<TK, TV> dictionary;
14+
private readonly IDictionary<K, V> dictionary;
1515

1616
/// <summary>
1717
/// Constructor.
@@ -27,11 +27,11 @@ public Dictionary(DictionaryType type = DictionaryType.SeparateChaining, int ini
2727

2828
if (type == DictionaryType.SeparateChaining)
2929
{
30-
dictionary = new SeparateChainingDictionary<TK, TV>(initialBucketSize);
30+
dictionary = new SeparateChainingDictionary<K, V>(initialBucketSize);
3131
}
3232
else
3333
{
34-
dictionary = new OpenAddressDictionary<TK, TV>(initialBucketSize);
34+
dictionary = new OpenAddressDictionary<K, V>(initialBucketSize);
3535
}
3636
}
3737

@@ -44,7 +44,7 @@ public Dictionary(DictionaryType type = DictionaryType.SeparateChaining, int ini
4444
/// Get/set value for given key.
4545
/// Time complexity: O(1) amortized.
4646
/// </summary>
47-
public TV this[TK key]
47+
public V this[K key]
4848
{
4949
get => dictionary[key];
5050
set => dictionary[key] = value;
@@ -56,7 +56,7 @@ public TV this[TK key]
5656
/// </summary>
5757
/// <param name="value">The key to check.</param>
5858
/// <returns>True if this dictionary contains the given key.</returns>
59-
public bool ContainsKey(TK key)
59+
public bool ContainsKey(K key)
6060
{
6161
return dictionary.ContainsKey(key);
6262
}
@@ -67,7 +67,7 @@ public bool ContainsKey(TK key)
6767
/// </summary>
6868
/// <param name="key">The key to add.</param>
6969
/// <param name="value">The value for the given key.</param>
70-
public void Add(TK key, TV value)
70+
public void Add(K key, V value)
7171
{
7272
dictionary.Add(key, value);
7373
}
@@ -77,7 +77,7 @@ public void Add(TK key, TV value)
7777
/// Time complexity: O(1) amortized.
7878
/// </summary>
7979
/// <param name="key">The key to remove.</param>
80-
public void Remove(TK key)
80+
public void Remove(K key)
8181
{
8282
dictionary.Remove(key);
8383
}
@@ -96,36 +96,39 @@ IEnumerator IEnumerable.GetEnumerator()
9696
return GetEnumerator();
9797
}
9898

99-
public IEnumerator<KeyValuePair<TK, TV>> GetEnumerator()
99+
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
100100
{
101101
return dictionary.GetEnumerator();
102102
}
103103
}
104104

105-
internal interface IDictionary<TK, TV> : IEnumerable<KeyValuePair<TK, TV>>
105+
internal interface IDictionary<K, V> : IEnumerable<KeyValuePair<K, V>>
106106
{
107-
TV this[TK key] { get; set; }
107+
V this[K key] { get; set; }
108108

109-
bool ContainsKey(TK key);
110-
void Add(TK key, TV value);
111-
void Remove(TK key);
109+
bool ContainsKey(K key);
110+
void Add(K key, V value);
111+
void Remove(K key);
112112
void Clear();
113113

114114
int Count { get; }
115115
}
116116

117-
internal class DictionaryNode<TK, TV>
117+
internal class DictionaryNode<K, V>
118118
{
119-
internal TK Key;
120-
internal TV Value;
119+
internal K Key;
120+
internal V Value;
121121

122-
internal DictionaryNode(TK key, TV value)
122+
internal DictionaryNode(K key, V value)
123123
{
124124
this.Key = key;
125125
this.Value = value;
126126
}
127127
}
128128

129+
/// <summary>
130+
/// The dictionary implementation type.
131+
/// </summary>
129132
public enum DictionaryType
130133
{
131134
SeparateChaining,

Advanced.Algorithms/DataStructures/Dictionary/SortedDictionary.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ namespace Advanced.Algorithms.DataStructures
88
/// A sorted Dictionary implementation using balanced binary search tree. IEnumerable will enumerate in sorted order.
99
/// This may be better than regular Dictionary implementation which can give o(K) in worst case (but O(1) amortized when collisions K is avoided).
1010
/// </summary>
11-
/// <typeparam name="TK">The key datatype.</typeparam>
12-
/// <typeparam name="TV">The value datatype.</typeparam>
13-
public class SortedDictionary<TK, TV> : IEnumerable<KeyValuePair<TK, TV>> where TK : IComparable
11+
/// <typeparam name="K">The key datatype.</typeparam>
12+
/// <typeparam name="V">The value datatype.</typeparam>
13+
public class SortedDictionary<K, V> : IEnumerable<KeyValuePair<K, V>> where K : IComparable
1414
{
1515
//use red-black tree as our balanced BST since it gives good performance for both deletion/insertion
16-
private readonly RedBlackTree<SortedDictionaryNode<TK, TV>> binarySearchTree;
16+
private readonly RedBlackTree<SortedDictionaryNode<K, V>> binarySearchTree;
1717

1818
public int Count => binarySearchTree.Count;
1919

2020
public SortedDictionary()
2121
{
22-
binarySearchTree = new RedBlackTree<SortedDictionaryNode<TK, TV>>();
22+
binarySearchTree = new RedBlackTree<SortedDictionaryNode<K, V>>();
2323
}
2424

2525
/// <summary>
@@ -28,29 +28,29 @@ public SortedDictionary()
2828
/// </summary>
2929
/// <param name="key">The key to check.</param>
3030
/// <returns>True if this dictionary contains the given key.</returns>
31-
public bool ContainsKey(TK key)
31+
public bool ContainsKey(K key)
3232
{
33-
return binarySearchTree.HasItem(new SortedDictionaryNode<TK, TV>(key, default(TV)));
33+
return binarySearchTree.HasItem(new SortedDictionaryNode<K, V>(key, default(V)));
3434
}
3535

3636
/// <summary>
3737
/// Add a new value for given key.
3838
/// Time complexity: O(log(n)).
3939
/// </summary>
40-
public void Add(TK key, TV value)
40+
public void Add(K key, V value)
4141
{
42-
binarySearchTree.Insert(new SortedDictionaryNode<TK, TV>(key, value));
42+
binarySearchTree.Insert(new SortedDictionaryNode<K, V>(key, value));
4343
}
4444

4545
/// <summary>
4646
/// Get/set value for given key.
4747
/// Time complexity: O(log(n)).
4848
/// </summary>
49-
public TV this[TK key]
49+
public V this[K key]
5050
{
5151
get
5252
{
53-
var node = binarySearchTree.FindNode(new SortedDictionaryNode<TK, TV>(key, default(TV)));
53+
var node = binarySearchTree.FindNode(new SortedDictionaryNode<K, V>(key, default(V)));
5454
if (node == null)
5555
{
5656
throw new Exception("Key not found.");
@@ -73,43 +73,43 @@ public TV this[TK key]
7373
/// Remove the given key.
7474
/// Time complexity: O(log(n)).
7575
/// </summary>
76-
public void Remove(TK key)
76+
public void Remove(K key)
7777
{
78-
binarySearchTree.Delete(new SortedDictionaryNode<TK, TV>(key, default(TV)));
78+
binarySearchTree.Delete(new SortedDictionaryNode<K, V>(key, default(V)));
7979
}
8080

8181
/// <summary>
8282
/// Return the next higher key-value pair after given key in this dictionary.
8383
/// Time complexity: O(log(n)).
8484
/// </summary>
8585
/// <returns>Null if the given key does'nt exist or next key does'nt exist.</returns>
86-
public KeyValuePair<TK, TV> NextHigher(TK key)
86+
public KeyValuePair<K, V> NextHigher(K key)
8787
{
88-
var next = binarySearchTree.NextHigher(new SortedDictionaryNode<TK, TV>(key, default(TV)));
88+
var next = binarySearchTree.NextHigher(new SortedDictionaryNode<K, V>(key, default(V)));
8989

9090
if(next == null)
9191
{
92-
return default(KeyValuePair<TK, TV>);
92+
return default(KeyValuePair<K, V>);
9393
}
9494

95-
return new KeyValuePair<TK, TV>(next.Key, next.Value);
95+
return new KeyValuePair<K, V>(next.Key, next.Value);
9696
}
9797

9898
/// <summary>
9999
/// Return the next lower key-value pair before given key in this dictionary.
100100
/// Time complexity: O(log(n)).
101101
/// </summary>
102102
/// <returns>Null if the given key does'nt exist or previous key does'nt exist.</returns>
103-
public KeyValuePair<TK, TV> NextLower(TK key)
103+
public KeyValuePair<K, V> NextLower(K key)
104104
{
105-
var prev = binarySearchTree.NextLower(new SortedDictionaryNode<TK, TV>(key, default(TV)));
105+
var prev = binarySearchTree.NextLower(new SortedDictionaryNode<K, V>(key, default(V)));
106106

107107
if (prev == null)
108108
{
109-
return default(KeyValuePair<TK, TV>);
109+
return default(KeyValuePair<K, V>);
110110
}
111111

112-
return new KeyValuePair<TK, TV>(prev.Key, prev.Value);
112+
return new KeyValuePair<K, V>(prev.Key, prev.Value);
113113
}
114114

115115
/// <summary>
@@ -126,27 +126,27 @@ IEnumerator IEnumerable.GetEnumerator()
126126
return GetEnumerator();
127127
}
128128

129-
public IEnumerator<KeyValuePair<TK, TV>> GetEnumerator()
129+
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
130130
{
131-
return new SortedDictionaryEnumerator<TK, TV>(binarySearchTree);
131+
return new SortedDictionaryEnumerator<K, V>(binarySearchTree);
132132
}
133133
}
134134

135-
internal class SortedDictionaryNode<TK, TV> : IComparable
136-
where TK : IComparable
135+
internal class SortedDictionaryNode<K, V> : IComparable
136+
where K : IComparable
137137
{
138-
internal TK Key { get; }
139-
internal TV Value { get; set; }
138+
internal K Key { get; }
139+
internal V Value { get; set; }
140140

141-
internal SortedDictionaryNode(TK key, TV value)
141+
internal SortedDictionaryNode(K key, V value)
142142
{
143143
this.Key = key;
144144
this.Value = value;
145145
}
146146

147147
public int CompareTo(object obj)
148148
{
149-
if (obj is SortedDictionaryNode<TK, TV> itemToComare)
149+
if (obj is SortedDictionaryNode<K, V> itemToComare)
150150
{
151151
return Key.CompareTo(itemToComare.Key);
152152
}
@@ -155,12 +155,12 @@ public int CompareTo(object obj)
155155
}
156156
}
157157

158-
internal class SortedDictionaryEnumerator<TK, TV> : IEnumerator<KeyValuePair<TK, TV>> where TK : IComparable
158+
internal class SortedDictionaryEnumerator<K, V> : IEnumerator<KeyValuePair<K, V>> where K : IComparable
159159
{
160-
private RedBlackTree<SortedDictionaryNode<TK, TV>> bst;
161-
private IEnumerator<SortedDictionaryNode<TK, TV>> enumerator;
160+
private RedBlackTree<SortedDictionaryNode<K, V>> bst;
161+
private IEnumerator<SortedDictionaryNode<K, V>> enumerator;
162162

163-
internal SortedDictionaryEnumerator(RedBlackTree<SortedDictionaryNode<TK, TV>> bst)
163+
internal SortedDictionaryEnumerator(RedBlackTree<SortedDictionaryNode<K, V>> bst)
164164
{
165165
this.bst = bst;
166166
this.enumerator = bst.GetEnumerator();
@@ -178,11 +178,11 @@ public void Reset()
178178

179179
object IEnumerator.Current => Current;
180180

181-
public KeyValuePair<TK, TV> Current
181+
public KeyValuePair<K, V> Current
182182
{
183183
get
184184
{
185-
return new KeyValuePair<TK, TV>(enumerator.Current.Key, enumerator.Current.Value);
185+
return new KeyValuePair<K, V>(enumerator.Current.Key, enumerator.Current.Value);
186186
}
187187
}
188188

Advanced.Algorithms/DataStructures/HashSet/HashSet.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ namespace Advanced.Algorithms.DataStructures
77
/// <summary>
88
/// A hash table implementation.
99
/// </summary>
10-
/// <typeparam name="TV">The value datatype.</typeparam>
11-
public class HashSet<TV> : IEnumerable<HashSetNode<TV>>
10+
/// <typeparam name="T">The value datatype.</typeparam>
11+
public class HashSet<T> : IEnumerable<HashSetNode<T>>
1212
{
13-
private readonly IHashSet<TV> hashSet;
13+
private readonly IHashSet<T> hashSet;
1414

1515
/// <summary>
1616
/// Constructor.
@@ -26,11 +26,11 @@ public HashSet(HashSetType type = HashSetType.SeparateChaining, int initialBucke
2626
}
2727
if (type == HashSetType.SeparateChaining)
2828
{
29-
hashSet = new SeparateChainingHashSet<TV>(initialBucketSize);
29+
hashSet = new SeparateChainingHashSet<T>(initialBucketSize);
3030
}
3131
else
3232
{
33-
hashSet = new OpenAddressHashSet<TV>(initialBucketSize);
33+
hashSet = new OpenAddressHashSet<T>(initialBucketSize);
3434
}
3535
}
3636

@@ -46,7 +46,7 @@ public HashSet(HashSetType type = HashSetType.SeparateChaining, int initialBucke
4646
/// </summary>
4747
/// <param name="value">The value to check.</param>
4848
/// <returns>True if this hashset contains the given value.</returns>
49-
public bool Contains(TV value)
49+
public bool Contains(T value)
5050
{
5151
return hashSet.Contains(value);
5252
}
@@ -56,7 +56,7 @@ public bool Contains(TV value)
5656
/// Time complexity: O(1) amortized.
5757
/// </summary>
5858
/// <param name="value">The value to add.</param>
59-
public void Add(TV value)
59+
public void Add(T value)
6060
{
6161
hashSet.Add(value);
6262
}
@@ -66,7 +66,7 @@ public void Add(TV value)
6666
/// Time complexity: O(1) amortized.
6767
/// </summary>
6868
/// <param name="value">The value to remove.</param>
69-
public void Remove(TV value)
69+
public void Remove(T value)
7070
{
7171
hashSet.Remove(value);
7272
}
@@ -85,32 +85,35 @@ IEnumerator IEnumerable.GetEnumerator()
8585
return hashSet.GetEnumerator();
8686
}
8787

88-
public IEnumerator<HashSetNode<TV>> GetEnumerator()
88+
public IEnumerator<HashSetNode<T>> GetEnumerator()
8989
{
9090
return hashSet.GetEnumerator();
9191
}
9292
}
9393

94-
internal interface IHashSet<TV> : IEnumerable<HashSetNode<TV>>
94+
internal interface IHashSet<T> : IEnumerable<HashSetNode<T>>
9595
{
96-
bool Contains(TV value);
97-
void Add(TV value);
98-
void Remove(TV key);
96+
bool Contains(T value);
97+
void Add(T value);
98+
void Remove(T key);
9999
void Clear();
100100

101101
int Count { get; }
102102
}
103103

104-
public class HashSetNode<TV>
104+
public class HashSetNode<T>
105105
{
106-
public TV Value;
106+
public T Value;
107107

108-
public HashSetNode(TV value)
108+
public HashSetNode(T value)
109109
{
110110
this.Value = value;
111111
}
112112
}
113113

114+
/// <summary>
115+
/// The hash set implementation type.
116+
/// </summary>
114117
public enum HashSetType
115118
{
116119
SeparateChaining,

0 commit comments

Comments
 (0)