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
|
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: ArraySegment<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;
using System.Runtime.InteropServices;
using System.Diagnostics.Contracts;
namespace System
{
// Note: users should make sure they copy the fields out of an ArraySegment 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 ArraySegment from one call to another
// (ie, users could assign a new value to the old location).
[Serializable]
public struct ArraySegment<T> : IList<T>, IReadOnlyList<T>
{
private T[] _array;
private int _offset;
private int _count;
public ArraySegment(T[] array)
{
if (array == null)
throw new ArgumentNullException("array");
Contract.EndContractBlock();
_array = array;
_offset = 0;
_count = array.Length;
}
public ArraySegment(T[] array, int offset, int count)
{
if (array == null)
throw new ArgumentNullException("array");
if (offset < 0)
throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (count < 0)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (array.Length - offset < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
Contract.EndContractBlock();
_array = array;
_offset = offset;
_count = count;
}
public T[] Array
{
get
{
Contract.Assert( (null == _array && 0 == _offset && 0 == _count)
|| (null != _array && _offset >= 0 && _count >= 0 && _offset + _count <= _array.Length),
"ArraySegment is invalid");
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.
Contract.Ensures(Contract.Result<int>() >= 0);
Contract.Assert( (null == _array && 0 == _offset && 0 == _count)
|| (null != _array && _offset >= 0 && _count >= 0 && _offset + _count <= _array.Length),
"ArraySegment is invalid");
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 ArraySegment into their stack.
Contract.Ensures(Contract.Result<int>() >= 0);
Contract.Assert( (null == _array && 0 == _offset && 0 == _count)
|| (null != _array && _offset >= 0 && _count >= 0 && _offset + _count <= _array.Length),
"ArraySegment is invalid");
return _count;
}
}
public override int GetHashCode()
{
return null == _array
? 0
: _array.GetHashCode() ^ _offset ^ _count;
}
public override bool Equals(Object obj)
{
if (obj is ArraySegment<T>)
return Equals((ArraySegment<T>)obj);
else
return false;
}
public bool Equals(ArraySegment<T> obj)
{
return obj._array == _array && obj._offset == _offset && obj._count == _count;
}
public static bool operator ==(ArraySegment<T> a, ArraySegment<T> b)
{
return a.Equals(b);
}
public static bool operator !=(ArraySegment<T> a, ArraySegment<T> b)
{
return !(a == b);
}
#region IList<T>
T IList<T>.this[int index]
{
get
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException("index");
Contract.EndContractBlock();
return _array[_offset + index];
}
set
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException("index");
Contract.EndContractBlock();
_array[_offset + index] = value;
}
}
int IList<T>.IndexOf(T item)
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
Contract.EndContractBlock();
int index = System.Array.IndexOf<T>(_array, item, _offset, _count);
Contract.Assert(index == -1 ||
(index >= _offset && index < _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(Environment.GetResourceString("InvalidOperation_NullArray"));
if (index < 0 || index >= _count)
throw new ArgumentOutOfRangeException("index");
Contract.EndContractBlock();
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(Environment.GetResourceString("InvalidOperation_NullArray"));
Contract.EndContractBlock();
int index = System.Array.IndexOf<T>(_array, item, _offset, _count);
Contract.Assert(index == -1 ||
(index >= _offset && index < _offset + _count));
return index >= 0;
}
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
Contract.EndContractBlock();
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(Environment.GetResourceString("InvalidOperation_NullArray"));
Contract.EndContractBlock();
return new ArraySegmentEnumerator(this);
}
#endregion
#region IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
if (_array == null)
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NullArray"));
Contract.EndContractBlock();
return new ArraySegmentEnumerator(this);
}
#endregion
[Serializable]
private sealed class ArraySegmentEnumerator : IEnumerator<T>
{
private T[] _array;
private int _start;
private int _end;
private int _current;
internal ArraySegmentEnumerator(ArraySegment<T> arraySegment)
{
Contract.Requires(arraySegment.Array != null);
Contract.Requires(arraySegment.Offset >= 0);
Contract.Requires(arraySegment.Count >= 0);
Contract.Requires(arraySegment.Offset + arraySegment.Count <= arraySegment.Array.Length);
_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(Environment.GetResourceString(ResId.InvalidOperation_EnumNotStarted));
if (_current >= _end) throw new InvalidOperationException(Environment.GetResourceString(ResId.InvalidOperation_EnumEnded));
return _array[_current];
}
}
object IEnumerator.Current
{
get
{
return Current;
}
}
void IEnumerator.Reset()
{
_current = _start - 1;
}
public void Dispose()
{
}
}
}
}
|