forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortedList.cs
219 lines (184 loc) · 6.16 KB
/
SortedList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections.Generic;
using DataStructures.Common;
using DataStructures.Trees;
namespace DataStructures.SortedCollections
{
/// <summary>
/// Sorted List (RBTree-based).
/// </summary>
public class SortedList<T> : IEnumerable<T>, ICollection<T>, IList<T> where T : IComparable<T>
{
/// <summary>
/// The internal collection is a Red-Black Tree.
/// </summary>
private RedBlackTree<T> _collection { get; set; }
/// <summary>
/// Constructor.
/// </summary>
public SortedList()
{
this._collection = new RedBlackTree<T>();
}
/// <summary>
/// Returns true if list is empty; otherwise, false.
/// </summary>
public bool IsEmpty
{
get { return this.Count == 0; }
}
/// <summary>
/// Gets the count of items in list.
/// </summary>
public int Count
{
get { return this._collection.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
/// <summary>
/// Determines whether the current collection contains a specific value.
/// </summary>
public bool Contains(T item)
{
return _collection.Contains(item);
}
/// <summary>
/// Determines the index of a specific item in the current collection.
/// </summary>
public int IndexOf(T item)
{
// If the item doesn't exist in collection, return -1
if (!this.Contains(item))
return -1;
int index = 0;
var enumerator = this._collection.GetInOrderEnumerator();
while (enumerator.MoveNext())
{
// If the current item is found return index
if (enumerator.Current.IsEqualTo(item))
return index;
// Increment index
index++;
}
return -1;
}
/// <summary>
/// Gets or sets the item at the specified index.
/// </summary>
public T this[int index]
{
get
{
// In case list is empty
if (IsEmpty)
throw new Exception("List is empty.");
// Validate index range
if (index < 0 || index >= this.Count)
throw new IndexOutOfRangeException();
var enumerator = this._collection.GetInOrderEnumerator();
// Keep moving to the next item until index becomes 0
while (enumerator.MoveNext() && index > 0)
index--;
// Return the enumerator's Current value
return enumerator.Current;
}
set
{
try
{
this._collection.Remove(this[index]);
this.Add(value);
}
catch (IndexOutOfRangeException)
{
// Masks the get method (see above) exception with a new one.
throw new IndexOutOfRangeException();
}
}
}
/// <summary>
/// Adds the item to list.
/// </summary>
public void Add(T item)
{
this._collection.Insert(item);
}
/// <summary>
/// Removes the first occurrence of an item from list.
/// </summary>
public bool Remove(T item)
{
try
{
this._collection.Remove(item);
return true;
}
catch(Exception)
{
return false;
}
}
/// <summary>
/// Inserts the item at the specified index.
/// </summary>
public void Insert(int index, T item)
{
// It is meaningless to insert at a specific index since after every
// insert operation, the collection will be rebalanced and the insertion
// operation itself needs to ensure the sorting criteria, therefore the item
// item insert at index i might not be the same after the operation has completed.
throw new NotImplementedException();
}
/// <summary>
/// Removes an item at a specific index.
/// </summary>
public void RemoveAt(int index)
{
// Validate index range
if (index < 0 || index >= this.Count)
throw new IndexOutOfRangeException();
var enumerator = this._collection.GetInOrderEnumerator();
// Keep moving to the next item until index becomes 0
while (enumerator.MoveNext() && index > 0)
index--;
// Remove the enumerator's Current value from collection
this.Remove(enumerator.Current);
}
/// <summary>
/// Copies the items in list to an array starting from a given index.
/// </summary>
public void CopyTo(T[] array, int arrayIndex)
{
// Validate the array argument
if(array == null)
throw new ArgumentNullException("Array cannot be Null.");
var enumerator = this._collection.GetInOrderEnumerator();
// Copy the items from the inorder-walker of the tree to the passed array
while (enumerator.MoveNext() && arrayIndex < array.Length)
{
array[arrayIndex] = enumerator.Current;
arrayIndex++;
}
}
/// <summary>
/// Clears this instance.
/// </summary>
public void Clear()
{
this._collection = new RedBlackTree<T>();
}
#region IEnumerable implementation
public IEnumerator<T> GetEnumerator()
{
return this._collection.GetInOrderEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
}