-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexedCollection.Buffer.cs
138 lines (117 loc) · 3.67 KB
/
IndexedCollection.Buffer.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace IndexedCollection
{
public partial class IndexedCollection<TItem>
{
public class Buffer<T> : IEnumerable<T>
{
public int Count;
public T[] Array;
public Buffer()
{
Array = new T[4];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
Count = 0;
Resize();
}
public void Refill(Buffer<T> buffer)
{
Count = buffer.Count;
Resize();
System.Array.Copy(buffer.Array, 0, Array, 0, Count);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T item)
{
Count++;
Resize();
Array[Count - 1] = item;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetAtAndResize(int index, T value)
{
if(Count <= index)
{
Count = index + 1;
Resize();
}
Array[index] = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool RemoveAndMixOrder(int index)
{
if(index >= 0 && index < Count)
{
Count--;
Array[index] = Array[Count];
Array[Count] = default;
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Resize()
{
if(Array.Length < Count)
{
var newArray = new T[Count * 2];
System.Array.Copy(Array, 0, newArray, 0, Array.Length);
Array = newArray;
}
for(int i = Count; i < Array.Length; i++)
{
Array[i] = default;
}
}
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public struct Enumerator : IEnumerator<T>
{
private Buffer<T> _buffer;
private int _index;
private T _current;
internal Enumerator(Buffer<T> buffer)
{
_buffer = buffer;
_index = 0;
_current = default(T);
}
public void Dispose()
{
_index = -1;
_current = default(T);
}
public bool MoveNext()
{
if(_index >= _buffer.Count) return false;
_current = _buffer.Array[_index];
++_index;
return true;
}
public void Reset()
{
_index = 0;
_current = default(T);
}
public T Current => _current;
object IEnumerator.Current => _current;
}
}
}
}