forked from sarbian/ModuleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayEnumerator.cs
More file actions
59 lines (48 loc) · 1.91 KB
/
Copy pathArrayEnumerator.cs
File metadata and controls
59 lines (48 loc) · 1.91 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
namespace ModuleManager.Collections
{
public struct ArrayEnumerator<T> : IEnumerator<T>
{
private readonly T[] array;
private readonly int startIndex;
private readonly int length;
private int index;
public ArrayEnumerator(params T[] array) : this(array, 0) { }
public ArrayEnumerator(T[] array, int startIndex) : this(array, startIndex, (array?.Length ?? -1) - startIndex) { }
public ArrayEnumerator(T[] array, int startIndex, int length)
{
this.array = array ?? throw new ArgumentNullException(nameof(array));
if (startIndex < 0)
throw new ArgumentException($"must be non-negative (got {startIndex})", nameof(startIndex));
if (startIndex > array.Length)
throw new ArgumentException(
$"must be less than or equal to array length (array length {array.Length}, startIndex {startIndex})",
nameof(startIndex)
);
if (length < 0)
throw new ArgumentException($"must be non-negative (got {length})", nameof(length));
if (startIndex + length > array.Length)
throw new ArgumentException(
$"must fit within the string (array length {array.Length}, startIndex {startIndex}, length {length})",
nameof(length)
);
this.startIndex = startIndex;
this.length = length;
index = startIndex - 1;
}
public T Current => array[index];
object IEnumerator.Current => Current;
public void Dispose() { }
public bool MoveNext()
{
index++;
return index < startIndex + length;
}
public void Reset()
{
index = startIndex - 1;
}
}
}