Skip to content

Commit 936fb30

Browse files
committed
Correct formatting and modify methods to be testable
1 parent 0408a25 commit 936fb30

File tree

1 file changed

+40
-55
lines changed

1 file changed

+40
-55
lines changed

Algorithms/Search/BinarySearcher.cs

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@ namespace Algorithms.Search
77
{
88
public class BinarySearcher<T> : IEnumerator<T>
99
{
10-
1110
private readonly IList<T> _collection;
1211
private readonly Comparer<T> _comparer;
13-
12+
private T _item;
1413
private int _currentItemIndex;
1514
private int _leftIndex;
1615
private int _rightIndex;
1716

1817
/// <summary>
1918
/// The value of the current item
2019
/// </summary>
21-
public T Current {
22-
20+
public T Current
21+
{
2322
get
2423
{
2524
return _collection[_currentItemIndex];
2625
}
27-
2826
}
2927

30-
3128
object IEnumerator.Current => Current;
3229

3330
/// <summary>
@@ -41,59 +38,39 @@ public BinarySearcher(IList<T> collection, Comparer<T> comparer)
4138
{
4239
throw new NullReferenceException("List is null");
4340
}
41+
_collection = collection;
42+
_comparer = comparer;
43+
HeapSorter.HeapSort(_collection);
44+
}
4445

45-
this._collection = collection;
46-
this._comparer = comparer;
47-
46+
public void SearchFor(T item)
47+
{
48+
if (item == null)
49+
{
50+
throw new NullReferenceException("Item to search for is not set");
51+
}
52+
Reset();
53+
_item = item;
4854
}
4955

50-
5156
/// <summary>
5257
/// Apply Binary Search in a list.
5358
/// </summary>
54-
/// <param name="item">The item we search for</param>
5559
/// <returns>If item found, its' index, -1 otherwise</returns>
56-
public int BinarySearch(T item)
60+
public int BinarySearch()
5761
{
58-
_currentItemIndex = -1;
59-
_leftIndex = 0;
60-
_rightIndex = _collection.Count - 1;
61-
HeapSorter.HeapSort(_collection);
62-
InternalBinarySearch(item);
63-
return _currentItemIndex;
64-
}
62+
bool notFound = true;
6563

66-
67-
/// <summary>
68-
/// An implementation of binary search algorithm.
69-
/// </summary>
70-
/// <param name="item">The item we search for</param>
71-
/// <returns></returns>
72-
private void InternalBinarySearch(T item)
73-
{
74-
bool found = false;
75-
76-
while (MoveNext() && !found)
64+
while ((_leftIndex <= _rightIndex) && notFound)
7765
{
78-
if (_comparer.Compare(item, Current) < 0)
79-
{
80-
_rightIndex = _currentItemIndex - 1;
81-
}
82-
else if (_comparer.Compare(item, Current) > 0)
83-
{
84-
_leftIndex = _currentItemIndex + 1;
85-
}
86-
else
87-
{
88-
found = true;
89-
}
66+
notFound = MoveNext();
9067
}
91-
92-
if (!found)
68+
69+
if (notFound)
9370
{
94-
this.Reset();
71+
Reset();
9572
}
96-
73+
return _currentItemIndex;
9774
}
9875

9976
/// <summary>
@@ -104,23 +81,31 @@ public bool MoveNext()
10481
{
10582
_currentItemIndex = this._leftIndex + (this._rightIndex - this._leftIndex) / 2;
10683

107-
if (_leftIndex <= _rightIndex)
84+
if (_comparer.Compare(_item, Current) < 0)
85+
{
86+
_rightIndex = _currentItemIndex - 1;
87+
}
88+
else if (_comparer.Compare(_item, Current) > 0)
10889
{
109-
return true;
90+
_leftIndex = _currentItemIndex + 1;
11091
}
11192
else
11293
{
11394
return false;
11495
}
115-
96+
return true;
11697
}
11798

118-
119-
public void Reset(){ this._currentItemIndex = -1; }
120-
121-
122-
public void Dispose(){}
99+
public void Reset()
100+
{
101+
this._currentItemIndex = -1;
102+
_leftIndex = 0;
103+
_rightIndex = _collection.Count - 1;
104+
}
123105

106+
public void Dispose()
107+
{
108+
//not implementing this, since there are no managed resources to release
109+
}
124110
}
125-
126111
}

0 commit comments

Comments
 (0)