-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyList.cs
200 lines (175 loc) · 4.97 KB
/
MyList.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
namespace MyListTemplate
{
using System;
using System.Collections;
using System.Collections.Generic;
public class MyList<T> : IList<T>
{
private T[] _items;
private int _count;
static readonly T[] _emptyArray = new T[0];
public MyList()
{
_items = _emptyArray;
_count = 0;
}
public T this[int index]
{
get
{
if ((uint)index >= (uint)_count)
throw new Exception();
return _items[index];
}
set
{
if ((uint)index >= (uint)_count)
throw new Exception();
_items[index] = value;
}
}
public int Capacity
{
get => _items.Length;
set
{
if (value < _items.Length)
// TODO: realise exception
throw new System.Exception();
ExpandList(value);
}
}
public int Count => _count;
public bool IsReadOnly => false;
public void Add(T item)
{
if (_count == _items.Length)
ExpandList(_count + 1);
_items[_count++] = item;
}
public void Clear()
{
if (_count > 0)
{
Array.Clear(_items, 0, _count);
_count = 0;
}
}
public bool Contains(T item)
{
// TODO: check null and change Equal() to EqualityComparer
for (int i = 0; i < _count; i++)
{
if (_items[i].Equals(item))
return true;
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
if (array == null && array.Rank == 1)
// TODO: Exception
throw new Exception();
try
{
Array.Copy(_items, arrayIndex, array, 0, _count);
}
catch (ArrayTypeMismatchException)
{
// TODO: Exception
throw new Exception();
}
}
public IEnumerator<T> GetEnumerator()
{
return new MyEnumerator(this);
}
public int IndexOf(T item)
{
return Array.IndexOf(_items, item, 0, _count);
}
public void Insert(int index, T item)
{
if ((uint)index >= _count)
// TODO: Exception
throw new Exception();
if (_count == _items.Length)
ExpandList(_count + 1);
Array.Copy(_items, index, _items, index + 1, _count - index);
_items[index] = item;
_count++;
}
public bool Remove(T item)
{
int index = IndexOf(item);
if (index >= 0)
{
RemoveAt(index);
return true;
}
return false;
}
public void RemoveAt(int index)
{
if ((uint)index >= (uint)_count)
// TODO: Exception
throw new Exception();
_count--;
if (index < _count)
Array.Copy(_items, index + 1, _items, index, _count - index);
_items[_count] = default(T);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new MyEnumerator(this);
}
private void ExpandList(int value)
{
T[] newItemsList = new T[value];
Array.Copy(_items, newItemsList, _items.Length);
_items = newItemsList;
}
public struct MyEnumerator : IEnumerator<T>, System.Collections.IEnumerator
{
private int _index;
private MyList<T> _list;
private T _current;
internal MyEnumerator(MyList<T> list)
{
_list = list;
_index = 0;
_current = default(T);
}
public T Current => _current;
object IEnumerator.Current
{
get
{
if (_index == 0 || _index == _list._count + 1)
// TODO: Exception
throw new Exception();
return Current;
}
}
public void Dispose()
{
}
public bool MoveNext()
{
MyList<T> localList = _list;
if ((uint)_index < (uint)localList._count)
{
_current = localList._items[_index];
_index++;
return true;
}
return false;
}
public void Reset()
{
_index = 0;
_current = default(T);
}
}
}
}