Skip to content

Commit 5ea702c

Browse files
committed
IEnumerable implementations
1 parent d075c7e commit 5ea702c

30 files changed

+576
-342
lines changed

Advanced.Algorithms.Tests/DataStructures/LinkedList/CircularLinkedList_Tests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Advanced.Algorithms.DataStructures;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Linq;
34

45
namespace Advanced.Algorithms.Tests.DataStructures
56
{
@@ -20,7 +21,6 @@ public void CircularLinkedList_Test()
2021
list.Insert("c");
2122

2223
Assert.AreEqual(list.Count(), 4);
23-
Assert.AreEqual(list.GetAllNodes().Count, 4);
2424

2525
list.Delete("a");
2626
Assert.AreEqual(list.Count(), 3);
@@ -46,7 +46,6 @@ public void CircularLinkedList_Test()
4646
list.Insert("c");
4747

4848
Assert.AreEqual(list.Count(), 4);
49-
Assert.AreEqual(list.GetAllNodes().Count, 4);
5049

5150
list.Delete("a");
5251
Assert.AreEqual(list.Count(), 3);

Advanced.Algorithms.Tests/DataStructures/LinkedList/DoublyLinkedList_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Advanced.Algorithms.DataStructures;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Linq;
34

45
namespace Advanced.Algorithms.Tests.DataStructures
56
{

Advanced.Algorithms.Tests/DataStructures/LinkedList/SinglyLinkedList_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Advanced.Algorithms.DataStructures;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Linq;
34

45
namespace Advanced.Algorithms.Tests.DataStructures
56
{

Advanced.Algorithms/DataStructures/Dictionary/OpenAddressDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private void setValue(TK key, TV value)
213213

214214
if (hashArray[index] == null)
215215
{
216-
throw new Exception("Item not found");
216+
Add(key, value);
217217
}
218218
else
219219
{

Advanced.Algorithms/DataStructures/Dictionary/SeparateChainingDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void setValue(K key, V value)
149149

150150
if (hashArray[index] == null)
151151
{
152-
throw new Exception("Item not found");
152+
Add(key, value);
153153
}
154154
else
155155
{

Advanced.Algorithms/DataStructures/Dictionary/SortedDictionary.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ public TV this[TK key]
6060
}
6161
set
6262
{
63-
var node = binarySearchTree.FindNode(new SortedDictionaryNode<TK, TV>(key, default(TV)));
64-
if (node == null)
63+
if(ContainsKey(key))
6564
{
66-
throw new Exception("Key not found.");
65+
Remove(key);
6766
}
68-
69-
node.Value.Value = value;
67+
68+
Add(key, value);
7069
}
7170
}
7271

@@ -84,9 +83,9 @@ public void Remove(TK key)
8483
/// Time complexity: O(log(n)).
8584
/// </summary>
8685
/// <returns>Null if the given key does'nt exist or next key does'nt exist.</returns>
87-
public KeyValuePair<TK, TV> Next(TK key)
86+
public KeyValuePair<TK, TV> NextHigher(TK key)
8887
{
89-
var next = binarySearchTree.Next(new SortedDictionaryNode<TK, TV>(key, default(TV)));
88+
var next = binarySearchTree.NextHigher(new SortedDictionaryNode<TK, TV>(key, default(TV)));
9089

9190
if(next == null)
9291
{
@@ -101,9 +100,9 @@ public KeyValuePair<TK, TV> Next(TK key)
101100
/// Time complexity: O(log(n)).
102101
/// </summary>
103102
/// <returns>Null if the given key does'nt exist or previous key does'nt exist.</returns>
104-
public KeyValuePair<TK, TV> Previous(TK key)
103+
public KeyValuePair<TK, TV> NextLower(TK key)
105104
{
106-
var prev = binarySearchTree.Previous(new SortedDictionaryNode<TK, TV>(key, default(TV)));
105+
var prev = binarySearchTree.NextLower(new SortedDictionaryNode<TK, TV>(key, default(TV)));
107106

108107
if (prev == null)
109108
{

Advanced.Algorithms/DataStructures/HashSet/OpenAddressHashSet.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public bool Contains(TV value)
6262

6363
public void Add(TV value)
6464
{
65-
6665
grow();
6766

6867
var hashCode = getHash(value);

Advanced.Algorithms/DataStructures/HashSet/SortedHashSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void Remove(V value)
5959
/// <returns>Null if the given value does'nt exist or next value does'nt exist.</returns>
6060
public V Next(V value)
6161
{
62-
return binarySearchTree.Next(value);
62+
return binarySearchTree.NextHigher(value);
6363
}
6464

6565
/// <summary>
@@ -69,7 +69,7 @@ public V Next(V value)
6969
/// <returns>Null if the given value does'nt exist or previous value does'nt exist.</returns>
7070
public V Previous(V value)
7171
{
72-
return binarySearchTree.Previous(value);
72+
return binarySearchTree.NextLower(value);
7373
}
7474

7575
/// <summary>

Advanced.Algorithms/DataStructures/Heap/Max/BMaxHeap.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45

56
namespace Advanced.Algorithms.DataStructures
67
{
7-
public class BMaxHeap<T> where T : IComparable
8+
public class BMaxHeap<T> : IEnumerable<T> where T : IComparable
89
{
910
private T[] heapArray;
1011

@@ -245,5 +246,15 @@ private void doubleArray()
245246

246247
heapArray = biggerArray;
247248
}
249+
250+
public IEnumerator<T> GetEnumerator()
251+
{
252+
return GetEnumerator();
253+
}
254+
255+
IEnumerator IEnumerable.GetEnumerator()
256+
{
257+
return new ArrayListEnumerator<T>(heapArray, Count);
258+
}
248259
}
249260
}

Advanced.Algorithms/DataStructures/Heap/Min/BMinHeap.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45

56
namespace Advanced.Algorithms.DataStructures
67
{
7-
public class BMinHeap<T> where T : IComparable
8+
public class BMinHeap<T> : IEnumerable<T> where T : IComparable
89
{
910
private T[] heapArray;
1011
private readonly IComparer<T> comparer;
@@ -306,5 +307,15 @@ private void doubleArray()
306307

307308
heapArray = biggerArray;
308309
}
310+
311+
public IEnumerator<T> GetEnumerator()
312+
{
313+
return GetEnumerator();
314+
}
315+
316+
IEnumerator IEnumerable.GetEnumerator()
317+
{
318+
return new ArrayListEnumerator<T>(heapArray, Count);
319+
}
309320
}
310321
}

0 commit comments

Comments
 (0)