forked from laicasaane/unity-supplements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadTable.cs
More file actions
78 lines (57 loc) · 2.19 KB
/
ReadTable.cs
File metadata and controls
78 lines (57 loc) · 2.19 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Table
{
public readonly struct ReadTable<T> : IReadTable<T> where T : IEntry
{
public int Count
=> GetSource().Count;
public IEnumerable<T> Entries
=> GetSource().Entries;
private readonly Table<T> source;
private readonly bool hasSource;
public ReadTable(Table<T> table)
{
this.source = table ?? _empty;
this.hasSource = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Table<T> GetSource()
=> this.hasSource ? this.source : _empty;
public bool ContainsId(int id)
=> GetSource().ContainsId(id);
public T GetEntry(int id = 0)
=> GetSource().GetEntry(id);
public bool TryGetEntry(int id, out T entry)
=> GetSource().TryGetEntry(id, out entry);
public Enumerator GetEnumerator()
=> new Enumerator(this.hasSource ? this : _empty);
IEnumerator<ReadEntry<T>> IEnumerable<ReadEntry<T>>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
public static implicit operator ReadTable<T>(Table<T> table)
=> new ReadTable<T>(table);
private static Table<T> _empty { get; } = new Table<T>(0);
public static ReadTable<T> Empty { get; } = new ReadTable<T>(_empty);
public struct Enumerator : IEnumerator<ReadEntry<T>>
{
private readonly IEnumerator<ReadEntry<T>> source;
internal Enumerator(in ReadTable<T> table)
{
this.source = table.GetSource().GetEnumerator();
}
public bool MoveNext()
=> this.source.MoveNext();
public ReadEntry<T> Current
=> this.source.Current;
object IEnumerator.Current
=> this.source.Current;
void IEnumerator.Reset()
=> this.source.Reset();
public void Dispose()
=> this.source.Dispose();
}
}
}