-
-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathEnumerableExtensions.cs
More file actions
55 lines (42 loc) · 1.34 KB
/
EnumerableExtensions.cs
File metadata and controls
55 lines (42 loc) · 1.34 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Cpp2IL.Core.Extensions;
internal static class EnumerableExtensions
{
public static IEnumerable<T> MaybeAppend<T>(this IEnumerable<T> enumerable, T? item) where T : struct
{
if (item is not null)
{
return enumerable.Append(item.Value);
}
return enumerable;
}
public static MemoryEnumerable<T> AsEnumerable<T>(this ReadOnlyMemory<T> memory) => new(memory);
public static MemoryEnumerator<T> GetEnumerator<T>(this ReadOnlyMemory<T> memory) => new(memory);
public class MemoryEnumerable<T>(ReadOnlyMemory<T> memory) : IEnumerable<T>
{
public IEnumerator<T> GetEnumerator() => new MemoryEnumerator<T>(memory);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class MemoryEnumerator<T>(ReadOnlyMemory<T> memory) : IEnumerator<T>
{
private int _index = -1;
public bool MoveNext()
{
_index++;
return _index < memory.Length;
}
public void Reset()
{
_index = -1;
}
public T Current => memory.Span[_index];
object? IEnumerator.Current => Current;
public void Dispose()
{
// Nothing to dispose
}
}
}