forked from AlexCheero/CodexECS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleVector.cs
72 lines (63 loc) · 1.86 KB
/
SimpleVector.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
using System;
using System.Runtime.CompilerServices;
namespace ECS
{
//TODO: rename to simple list to match c# style
class SimpleVector<T>
{
public T[] _elements;
public int _end = 0;
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _end;
}
public int Reserved
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _elements.Length;
}
public ref T this[int i]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref _elements[i];
}
public SimpleVector(int reserved = 0)
{
_elements = new T[reserved];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Copy(in SimpleVector<T> other)
{
_end = other._end;
if (_elements.Length < _end)
Array.Resize(ref _elements, other._elements.Length);
Array.Copy(other._elements, _elements, _end);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Remove(int idx)
{
_end--;
if (idx < _end)
_elements[idx] = _elements[_end];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
_end = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(T element)
{
if (_end >= _elements.Length)
{
var newLength = _elements.Length > 0 ? _elements.Length * 2 : 2;
while (_end >= newLength)
newLength *= 2;
Array.Resize(ref _elements, newLength);
}
_elements[_end] = element;
_end++;
}
}
}