/
FixedArray.cs
80 lines (71 loc) · 2.31 KB
/
FixedArray.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
#region Header
// FixedArray.cs
// PTIRelianceLib
// Cory Todd
// 19-06-2018
// 7:50 AM
#endregion
namespace PTIRelianceLib
{
using System.Collections.Generic;
using System.Collections.Immutable;
/// <summary>
/// A fixed length array wrapper
/// </summary>
/// <typeparam name="T">Data type to hold</typeparam>
public struct FixedArray<T>
{
private readonly IList<T> _mData;
/// <summary>
/// Create a new fixed length array of this type
/// </summary>
/// <param name="size"></param>
public FixedArray(int size)
{
Size = size;
_mData = new List<T>(size);
}
/// <summary>
/// Returns the fixed length of this array. This is the max size.
/// </summary>
public int Size { get; }
/// <summary>
/// Returns the count of elements in the fixed array.
/// </summary>
public int Count => _mData.Count;
/// <summary>
/// Raw raw contents of this array as a comma-separate string using
/// ToString on the objects in this array.
/// </summary>
public string Content => string.Join(",", _mData);
/// <summary>
/// Puts data into this fixed length array starting from the current tail
/// of the fixed array. If the fixed array size limit has been reached,
/// no more data will be added and this method will return.
/// </summary>
/// <param name="data"></param>
public void SetData(params T[] data)
{
foreach(var d in data)
{
if (_mData.Count >= Size)
{
break;
}
_mData.Add(d);
}
}
/// <summary>
/// Returns a readonly array containg all data
/// </summary>
/// <returns></returns>
public ImmutableArray<T> GetData() => _mData.ToImmutableArray();
/// <summary>
/// Returns the element at specified index
/// </summary>
/// <param name="index">Index of element to retrieve</param>
/// <remarks>Returns a reference to object so take care with mutations</remarks>
/// <returns>Object at index</returns>
internal T this[int index] => _mData[index];
}
}