-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReadArrayList{T}.cs
More file actions
201 lines (156 loc) · 5.5 KB
/
Copy pathReadArrayList{T}.cs
File metadata and controls
201 lines (156 loc) · 5.5 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
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
// Based on
// https://github.com/sebas77/Svelto.Common/blob/master/DataStructures/Arrays/FasterReadOnlyList.cs
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace System.Collections.ArrayBased
{
public readonly struct ReadArrayList<T> : IEnumerable<T>
{
public ref readonly T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (this.buffer == null || (uint)index >= this.Count)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return ref this.buffer[(uint)index];
}
}
public ref readonly T this[uint index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (this.buffer == null || index >= this.Count)
throw ThrowHelper.GetArgumentOutOfRange_IndexException();
return ref this.buffer[index];
}
}
public readonly IEqualityComparerIn<T> Comparer;
public readonly uint Count;
public readonly uint Capacity;
private readonly ArrayList<T> source;
private readonly T[] buffer;
public ReadArrayList(ArrayList<T> source)
{
this.source = source ?? ArrayList<T>.Empty;
this.Comparer = this.source.Comparer;
this.buffer = this.source.UnsafeBuffer;
this.Count = this.source.Count;
this.Capacity = this.source.Capacity;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ArrayList<T> GetSource()
=> this.source ?? ArrayList<T>.Empty;
public bool Contains(T item)
{
if (this.buffer == null)
return false;
for (var index = 0u; index < this.Count; index++)
if (this.Comparer.Equals(this.buffer[index], item))
return true;
return false;
}
public bool Contains(in T item)
{
if (this.buffer == null)
return false;
for (var index = 0u; index < this.Count; index++)
if (this.Comparer.Equals(in this.buffer[index], in item))
return true;
return false;
}
public uint? IndexOf(T item)
{
if (this.buffer == null)
return null;
for (var index = 0u; index < this.Count; index++)
if (this.Comparer.Equals(this.buffer[index], item))
return index;
return null;
}
public uint? IndexOf(in T item)
{
if (this.buffer == null)
return null;
for (var index = 0u; index < this.Count; index++)
if (this.Comparer.Equals(in this.buffer[index], in item))
return index;
return null;
}
public T[] ToArray()
{
if (this.buffer == null)
return Array.Empty<T>();
var destinationArray = new T[this.Count];
Array.Copy(this.buffer, 0, destinationArray, 0, this.Count);
return destinationArray;
}
public ref readonly T Peek()
{
if (this.buffer == null || this.Count == 0)
throw ThrowHelper.GetArgumentOutOfRange_CountException();
return ref this.buffer[this.Count - 1];
}
public void CopyTo(T[] array, int arrayIndex)
{
if (this.buffer == null)
return;
Array.Copy(this.buffer, 0, array, arrayIndex, this.Count);
}
public void CopyTo(T[] array, uint arrayIndex)
{
if (this.buffer == null)
return;
Array.Copy(this.buffer, 0, array, arrayIndex, this.Count);
}
public Enumerator GetEnumerator()
=> new Enumerator(this.buffer, this.Count);
IEnumerator<T> IEnumerable<T>.GetEnumerator()
=> GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
public static ReadArrayList<T> Empty { get; } = new ReadArrayList<T>(ArrayList<T>.Empty);
public static implicit operator ReadArrayList<T>(ArrayList<T> list)
=> new ReadArrayList<T>(list);
public struct Enumerator : IEnumerator<T>
{
private readonly T[] buffer;
private readonly uint count;
private uint index;
public T Current => this.buffer[this.index - 1];
object IEnumerator.Current => this.Current;
public Enumerator(T[] buffer, uint count)
{
if (buffer == null)
{
this.buffer = ArrayList<T>.Empty.UnsafeBuffer;
this.count = 0;
this.index = 0;
}
else
{
this.buffer = buffer;
this.count = count;
this.index = 0;
}
}
public bool MoveNext()
{
if (this.index < this.count)
{
this.index += 1;
return true;
}
return false;
}
public void Reset()
{
this.index = 0;
}
public void Dispose()
{
}
}
}
}