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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
namespace Evictor
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
public class LinkedList<T> : ICollection<T>, ICollection, ICloneable
{
public LinkedList()
{
Clear();
}
public int Count
{
get
{
return _count;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public bool IsSynchronized
{
get
{
return false;
}
}
public object SyncRoot
{
get
{
return this;
}
}
public void CopyTo(T[] array, int index)
{
//
// Check preconditions.
//
if(array == null)
{
throw new ArgumentNullException("array", "array parameter must not be null");
}
if(index < 0)
{
throw new ArgumentOutOfRangeException("index", _count, "index must not be less than zero");
}
if(index >= array.Length)
{
throw new ArgumentException("index out of bounds for array", "index");
}
if(array.Length - index > _count)
{
throw new ArgumentException("insufficient room in array", "array");
}
if(array.Rank != 1)
{
throw new ArgumentException("array must be one-dimensional", "array");
}
//
// Copy the elements.
//
Node n = _head;
while(n != null)
{
array[index++] = n.val;
n = (Node)n.next;
}
}
void ICollection.CopyTo(Array array, int index)
{
T[] arr = array as T[];
if(arr == null)
{
throw new ArgumentException("array");
}
CopyTo(arr, index);
}
public IEnumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(this);
}
public object Clone()
{
LinkedList<T> l = new LinkedList<T>();
Node cursor = _head;
while(cursor != null)
{
l.Add(cursor.val);
cursor = cursor.next;
}
return l;
}
public void Add(T value)
{
Node n = new Node();
n.val = value;
if(_tail == null)
{
n.prev = null;
_head = n;
}
else
{
n.prev = _tail;
_tail.next = n;
}
n.next = null;
_tail = n;
_count++;
}
public void AddFirst(T value)
{
Node n = new Node();
n.val = value;
if(_head == null)
{
n.next = null;
_tail = n;
}
else
{
n.next = _head;
_head.prev = n;
}
n.prev = null;
_head = n;
_count++;
}
public void Clear()
{
_head = null;
_tail = null;
_count = 0;
}
public bool Contains(T value)
{
return Find(value) != null;
}
public bool Remove(T value)
{
Node n = Find(value);
if(n != null)
{
Remove(n);
return true;
}
return false;
}
private Node Find(T value)
{
Node n = _head;
while(n != null)
{
if(Object.Equals(value, n.val))
{
return n;
}
n = n.next;
}
return null;
}
private void Remove(Node n)
{
Debug.Assert(n != null);
Debug.Assert(_count != 0);
if(n.prev != null)
{
n.prev.next = n.next;
}
else
{
_head = n.next;
}
if(n.next != null)
{
n.next.prev = n.prev;
}
else
{
_tail = n.prev;
}
_count--;
}
internal class Node
{
internal Node next;
internal Node prev;
internal T val;
}
private Node _head;
private Node _tail;
private int _count;
public class Enumerator : IEnumerator<T>, IEnumerator, IDisposable
{
internal Enumerator(LinkedList<T> list)
{
_list = list;
_current = null;
_movePrev = null;
_moveNext = null;
_removed = false;
}
public void Reset()
{
_current = null;
_movePrev = null;
_moveNext = null;
_removed = false;
}
public T Current
{
get
{
if(_current == null)
{
throw new InvalidOperationException("iterator not positioned on an element");
}
return _current.val;
}
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public bool MoveNext()
{
if(_removed)
{
_current = _moveNext;
_moveNext = null;
_movePrev = null;
_removed = false;
}
else
{
if(_current == _list._tail) // Make sure the iterator "sticks" if on last element.
{
return false;
}
_current = _current == null ? _list._head : _current.next;
}
return _current != null;
}
public bool MovePrev()
{
if(_removed)
{
_current = _movePrev;
_movePrev = null;
_moveNext = null;
_removed = false;
}
else
{
if(_current == _list._head) // Make sure the iterator "sticks" if on first element.
{
return false;
}
_current = _current == null ? _list._tail : _current.prev;
}
return _current != null;
}
public void Remove()
{
if(_current == null)
{
throw new InvalidOperationException("iterator is not positioned on an element");
}
_removed = true;
_moveNext = _current.next; // Remember where to move next for call to MoveNext().
_movePrev = _current.prev; // Remember where to move next for call to MovePrev().
_list.Remove(_current);
_current = null;
}
public void Dispose()
{
if(_list == null)
{
throw new ObjectDisposedException(null);
}
Reset();
_list = null;
}
private LinkedList<T> _list; // The list we are iterating over.
private Node _current; // Current iterator position.
private Node _moveNext; // Remembers node that followed a removed element.
private Node _movePrev; // Remembers node that preceded a removed element.
private bool _removed; // True after a call to Remove(), false otherwise.
}
}
}
|