-
Notifications
You must be signed in to change notification settings - Fork 5
/
arraysegment.cs
317 lines (260 loc) · 7.6 KB
/
arraysegment.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
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
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: ArraySegmentEx<T>
**
**
** Purpose: Convenient wrapper for an array, an offset, and
** a count. Ideally used in streams & collections.
** Net Classes will consume an array of these.
**
**
===========================================================*/
using System.Collections;
using System.Collections.Generic;
namespace System
{
// Note: users should make sure they copy the fields out of an ArraySegmentEx onto their stack
// then validate that the fields describe valid bounds within the array. This must be done
// because assignments to value types are not atomic, and also because one thread reading
// three fields from an ArraySegment may not see the same ArraySegmentEx from one call to another
// (ie, users could assign a new value to the old location).
[Serializable]
public struct ArraySegmentEx<T> : IList<T>, IReadOnlyList<T>
{
private const string InvalidOperation_NullArray = "The underlying array is null.";
private const string InvalidOperation_EnumNotStarted = "Enumeration has not started. Call MoveNext.";
private const string InvalidOperation_EnumEnded = "Enumeration already finished.";
private const string ArgumentOutOfRange_NeedNonNegNum = "Non-negative number required.";
private const string Argument_InvalidOffLen = "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.";
private readonly T[] _array;
private readonly int _offset;
private readonly int _count;
public ArraySegmentEx (T[] array)
{
if (array == null)
throw new ArgumentNullException ("array");
_array = array;
_offset = 0;
_count = array.Length;
}
public ArraySegmentEx (T[] array, int offset, int count)
{
if (array == null)
throw new ArgumentNullException ("array");
if (offset < 0)
throw new ArgumentOutOfRangeException ("offset", ArgumentOutOfRange_NeedNonNegNum);
if (count < 0)
throw new ArgumentOutOfRangeException ("count", ArgumentOutOfRange_NeedNonNegNum);
if (array.Length - offset < count)
throw new ArgumentException (Argument_InvalidOffLen);
_array = array;
_offset = offset;
_count = count;
}
public T[] Array
{
get
{
return _array;
}
}
public int Offset
{
get
{
// Since copying value types is not atomic & callers cannot atomically
// read all three fields, we cannot guarantee that Offset is within
// the bounds of Array. That is our intent, but let's not specify
// it as a postcondition - force callers to re-verify this themselves
// after reading each field out of an ArraySegment into their stack.
return _offset;
}
}
public int Count
{
get
{
// Since copying value types is not atomic & callers cannot atomically
// read all three fields, we cannot guarantee that Count is within
// the bounds of Array. That's our intent, but let's not specify
// it as a postcondition - force callers to re-verify this themselves
// after reading each field out of an ArraySegmentEx into their stack.
return _count;
}
}
public override int GetHashCode ()
{
return null == _array
? 0
: _array.GetHashCode () ^ _offset ^ _count;
}
public override bool Equals (Object obj)
{
if (obj is ArraySegmentEx<T>)
return Equals ((ArraySegmentEx<T>)obj);
return false;
}
public bool Equals (ArraySegmentEx<T> obj)
{
return obj._array == _array && obj._offset == _offset && obj._count == _count;
}
public static bool operator == (ArraySegmentEx<T> a, ArraySegmentEx<T> b)
{
return a.Equals (b);
}
public static bool operator != (ArraySegmentEx<T> a, ArraySegmentEx<T> b)
{
return !(a == b);
}
#region IList<T>
T IList<T>.this [int index]
{
get
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException ("index");
return _array[_offset + index];
}
set
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException ("index");
_array[_offset + index] = value;
}
}
int IList<T>.IndexOf (T item)
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
int index = System.Array.IndexOf (_array, item, _offset, _count);
return index >= 0 ? index - _offset : -1;
}
void IList<T>.Insert (int index, T item)
{
throw new NotSupportedException ();
}
void IList<T>.RemoveAt (int index)
{
throw new NotSupportedException ();
}
#endregion
#region IReadOnlyList<T>
T IReadOnlyList<T>.this [int index]
{
get
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException ("index");
return _array[_offset + index];
}
}
#endregion IReadOnlyList<T>
#region ICollection<T>
bool ICollection<T>.IsReadOnly
{
get
{
// the indexer setter does not throw an exception although IsReadOnly is true.
// This is to match the behavior of arrays.
return true;
}
}
void ICollection<T>.Add (T item)
{
throw new NotSupportedException ();
}
void ICollection<T>.Clear ()
{
throw new NotSupportedException ();
}
bool ICollection<T>.Contains (T item)
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
int index = System.Array.IndexOf (_array, item, _offset, _count);
return index >= 0;
}
void ICollection<T>.CopyTo (T[] array, int arrayIndex)
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
System.Array.Copy (_array, _offset, array, arrayIndex, _count);
}
bool ICollection<T>.Remove (T item)
{
throw new NotSupportedException ();
}
#endregion
#region IEnumerable<T>
IEnumerator<T> IEnumerable<T>.GetEnumerator ()
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
return new ArraySegmentEnumerator (this);
}
#endregion
#region IEnumerable
IEnumerator IEnumerable.GetEnumerator ()
{
if (_array == null)
throw new InvalidOperationException (InvalidOperation_NullArray);
return new ArraySegmentEnumerator (this);
}
#endregion
[Serializable]
private sealed class ArraySegmentEnumerator : IEnumerator<T>
{
private readonly T[] _array;
private readonly int _start;
private readonly int _end;
private int _current;
internal ArraySegmentEnumerator (ArraySegmentEx<T> arraySegment)
{
_array = arraySegment._array;
_start = arraySegment._offset;
_end = _start + arraySegment._count;
_current = _start - 1;
}
public bool MoveNext ()
{
if (_current < _end)
{
_current++;
return (_current < _end);
}
return false;
}
public T Current
{
get
{
if (_current < _start) throw new InvalidOperationException (InvalidOperation_EnumNotStarted);
if (_current >= _end) throw new InvalidOperationException (InvalidOperation_EnumEnded);
return _array[_current];
}
}
object IEnumerator.Current
{
get { return Current; }
}
void IEnumerator.Reset ()
{
_current = _start - 1;
}
public void Dispose ()
{
}
}
}
}