Skip to content

Commit f025cab

Browse files
committed
Implement IEnumerable for skip list.
1 parent 22fe5ce commit f025cab

File tree

4 files changed

+181
-109
lines changed

4 files changed

+181
-109
lines changed

Advanced.Algorithms.Tests/DataStructures/Lists/ArrayList_Tests.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ namespace Advanced.Algorithms.Tests.DataStructures
88
[TestClass]
99
public class ArrayList_Tests
1010
{
11-
/// <summary>
12-
/// A dynamic array test
13-
/// </summary>
1411
[TestMethod]
1512
public void ArrayList_Test()
1613
{
@@ -23,14 +20,12 @@ public void ArrayList_Test()
2320
Assert.AreEqual(true, arrayList.Contains(i));
2421
}
2522

26-
2723
for (int i = 0; i <= nodeCount; i++)
2824
{
29-
arrayList.RemoveItem(0);
25+
arrayList.RemoveAt(0);
3026
Assert.AreEqual(false, arrayList.Contains(i));
3127
}
3228

33-
3429
var rnd = new Random();
3530
var testSeries = Enumerable.Range(1, nodeCount).OrderBy(x => rnd.Next()).ToList();
3631

@@ -42,7 +37,7 @@ public void ArrayList_Test()
4237

4338
for (int i = 1; i <= nodeCount; i++)
4439
{
45-
arrayList.RemoveItem(0);
40+
arrayList.RemoveAt(0);
4641
}
4742

4843
}
@@ -62,7 +57,6 @@ public void ArrayList_InsertAt_Test()
6257
arrayList.InsertAt(5, 50000);
6358
Assert.AreEqual(true, arrayList.Contains(50000));
6459
Assert.AreEqual(nodeCount + 2, arrayList.Length);
65-
6660
}
6761
}
6862
}

Advanced.Algorithms.Tests/DataStructures/Lists/SkipList_Tests.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using Advanced.Algorithms.DataStructures;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Linq;
35

46
namespace Advanced.Algorithms.Tests.DataStructures
57
{
68
[TestClass]
79
public class SkipList_Tests
810
{
9-
/// <summary>
10-
/// A skip list test
11-
/// </summary>
1211
[TestMethod]
1312
public void SkipList_Test()
1413
{
@@ -26,7 +25,6 @@ public void SkipList_Test()
2625

2726
Assert.AreEqual(0, skipList.Find(101));
2827

29-
3028
for (int i = 1; i < 100; i++)
3129
{
3230
skipList.Delete(i);
@@ -38,6 +36,23 @@ public void SkipList_Test()
3836
skipList.Insert(i);
3937
}
4038

39+
try
40+
{
41+
skipList.Insert(25);
42+
Assert.Fail("Duplicate insertion allowed.");
43+
}
44+
catch (Exception) { }
45+
46+
try
47+
{
48+
skipList.Delete(52);
49+
Assert.Fail("Deletion of item not in skip list did'nt throw exception.");
50+
}
51+
catch (Exception) { }
52+
53+
//IEnumerable test using linq
54+
Assert.AreEqual(skipList.Count, skipList.Count());
55+
4156
for (int i = 1; i < 50; i++)
4257
{
4358
Assert.AreEqual(i, skipList.Find(i));

Advanced.Algorithms/DataStructures/List/ArrayList.cs

Lines changed: 42 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55
namespace Advanced.Algorithms.DataStructures
66
{
77
/// <summary>
8-
/// A self expanding array (dynamic array) aka array vector
8+
/// A self expanding array implementation.
99
/// </summary>
10-
/// <typeparam name="T"></typeparam>
10+
/// <typeparam name="T">The datatype of this ArrayList.</typeparam>
1111
public class ArrayList<T> : IEnumerable<T>
1212
{
1313
private readonly int initialArraySize;
1414
private int arraySize;
15-
1615
private T[] array;
1716

18-
private int currentEndPosition;
19-
public int Length => currentEndPosition;
17+
public int Length { get; private set; }
2018

21-
//constructor init
19+
/// <summary>
20+
/// Constructor.
21+
/// TimeComplexity: O(1) if initial is empty otherwise O(n).
22+
/// </summary>
23+
/// <param name="initalArraySize">The initial array size.</param>
24+
/// <param name="initial">Initial values if any.</param>
2225
public ArrayList(int initalArraySize = 2, IEnumerable<T> initial = null)
2326
{
2427
if (initalArraySize < 2)
@@ -42,24 +45,25 @@ public ArrayList(int initalArraySize = 2, IEnumerable<T> initial = null)
4245
}
4346

4447
/// <summary>
45-
/// Overloaded constructor
48+
/// Constructor.
49+
/// TimeComplexity: O(1) if initial is empty otherwise O(n).
4650
/// </summary>
47-
/// <param name="initial"></param>
51+
/// <param name="initial">Initial values if any.</param>
4852
public ArrayList(IEnumerable<T> initial)
4953
: this (2, initial){ }
5054

5155
/// <summary>
52-
/// Expose indexer
56+
/// Indexed access to array.
57+
/// Time Complexity: O(1).
5358
/// </summary>
54-
/// <param name="index"></param>
59+
/// <param name="index">The index to write or read.</param>
5560
/// <returns></returns>
5661
public T this[int index]
5762
{
5863
get => itemAt(index);
5964
set => setItem(index, value);
6065
}
6166

62-
//O(1)
6367
private T itemAt(int i)
6468
{
6569
if (i >= Length)
@@ -68,51 +72,51 @@ private T itemAt(int i)
6872
return array[i];
6973
}
7074

71-
//O(1) amortized
75+
/// <summary>
76+
/// Add a new item to this array list.
77+
/// Time complexity: O(1) amortized.
78+
/// </summary>
79+
/// <param name="item"></param>
7280
public void Add(T item)
7381
{
7482
grow();
7583

76-
array[currentEndPosition] = item;
77-
currentEndPosition++;
84+
array[Length] = item;
85+
Length++;
7886
}
7987

8088
/// <summary>
8189
/// Insert element at specified index
90+
/// Time complexity: O(1) amortized.
8291
/// </summary>
83-
/// <param name="index"></param>
84-
/// <param name="item"></param>
92+
/// <param name="index">The index to insert at.<param>
93+
/// <param name="item">The item to insert.</param>
8594
public void InsertAt(int index, T item)
8695
{
8796
grow();
8897

8998
shift(index);
9099

91100
array[index] = item;
92-
currentEndPosition++;
101+
Length++;
93102
}
94103

95104
/// <summary>
96-
/// shift the position of elements right by one starting at this index
97-
/// create a blank field at index
105+
/// Shift the position of elements right by one starting at this index.
106+
/// Creates a blank field at index.
98107
/// </summary>
99-
/// <param name="index"></param>
100108
private void shift(int index)
101109
{
102110
Array.Copy(array, index, array, index + 1, Length - index);
103111
}
104112

105-
/// <summary>
106-
/// empty the list
107-
/// </summary>
108113
internal void Clear()
109114
{
110115
arraySize = initialArraySize;
111116
array = new T[arraySize];
112-
currentEndPosition = 0;
117+
Length = 0;
113118
}
114119

115-
//O(1)
116120
private void setItem(int i, T item)
117121
{
118122
if (i >= Length)
@@ -121,8 +125,12 @@ private void setItem(int i, T item)
121125
array[i] = item;
122126
}
123127

124-
//O(n)
125-
public void RemoveItem(int i)
128+
/// <summary>
129+
/// Remove the item at given index.
130+
/// Time complexity: O(1) amortized.
131+
/// </summary>
132+
/// <param name="i">The index to remove at.</param>
133+
public void RemoveAt(int i)
126134
{
127135
if (i >= Length)
128136
throw new System.Exception("Index exeeds array size");
@@ -133,18 +141,14 @@ public void RemoveItem(int i)
133141
array[j] = array[j + 1];
134142
}
135143

136-
currentEndPosition--;
144+
Length--;
137145

138146
shrink();
139147
}
140148

141-
142-
/// <summary>
143-
/// Grow array if needed
144-
/// </summary>
145149
private void grow()
146150
{
147-
if (currentEndPosition != arraySize)
151+
if (Length != arraySize)
148152
{
149153
return;
150154
}
@@ -153,16 +157,13 @@ private void grow()
153157
arraySize *= 2;
154158

155159
var biggerArray = new T[arraySize];
156-
Array.Copy(array, 0, biggerArray, 0, currentEndPosition);
160+
Array.Copy(array, 0, biggerArray, 0, Length);
157161
array = biggerArray;
158162
}
159163

160-
/// <summary>
161-
/// Shrink array if needed
162-
/// </summary>
163164
private void shrink()
164165
{
165-
if (currentEndPosition != arraySize / 2 || arraySize == initialArraySize)
166+
if (Length != arraySize / 2 || arraySize == initialArraySize)
166167
{
167168
return;
168169
}
@@ -171,42 +172,10 @@ private void shrink()
171172
arraySize /= 2;
172173

173174
var smallerArray = new T[arraySize];
174-
Array.Copy(array, 0, smallerArray, 0, currentEndPosition);
175+
Array.Copy(array, 0, smallerArray, 0, Length);
175176
array = smallerArray;
176177
}
177178

178-
179-
/// <summary>
180-
/// Returns as an array
181-
/// </summary>
182-
/// <returns></returns>
183-
public T[] ToArray()
184-
{
185-
var result = new T[Length];
186-
187-
var i = 0;
188-
foreach(var item in this)
189-
{
190-
result[i] = item;
191-
i++;
192-
}
193-
194-
return result;
195-
}
196-
197-
/// <summary>
198-
/// Add's the given array to the end
199-
/// </summary>
200-
/// <param name="array"></param>
201-
public void AddRange(T[] array)
202-
{
203-
foreach(var item in array)
204-
{
205-
Add(item);
206-
}
207-
}
208-
209-
//Implementation for the GetEnumerator method.
210179
IEnumerator IEnumerable.GetEnumerator()
211180
{
212181
return GetEnumerator();
@@ -218,8 +187,7 @@ public IEnumerator<T> GetEnumerator()
218187
}
219188
}
220189

221-
// implement IEnumerator.
222-
public class ArrayListEnumerator<T> : IEnumerator<T>
190+
internal class ArrayListEnumerator<T> : IEnumerator<T>
223191
{
224192
private T[] array;
225193

@@ -228,7 +196,7 @@ public class ArrayListEnumerator<T> : IEnumerator<T>
228196
private int position = -1;
229197
private int length;
230198

231-
public ArrayListEnumerator(T[] list, int length)
199+
internal ArrayListEnumerator(T[] list, int length)
232200
{
233201
this.length = length;
234202
array = list;
@@ -265,8 +233,6 @@ public T Current
265233
public void Dispose()
266234
{
267235
array = null;
268-
length = 0;
269-
position = -1;
270236
}
271237
}
272238

0 commit comments

Comments
 (0)