source: Common/EnumIndexedArray.cs

Last change on this file was 15, checked in by chronos, 6 months ago
  • Modified: Updated files.
File size: 1.1 KB
Line 
1using System;
2using System.Collections;
3using System.Linq;
4
5namespace Common
6{
7 public class EnumIndexedArray<T, TIndex> : IEnumerable where TIndex : Enum // requires C# 7.3 or later
8 {
9 private readonly T[] array;
10 private readonly int lower;
11
12 public EnumIndexedArray()
13 {
14 lower = Convert.ToInt32(Enum.GetValues(typeof(TIndex)).Cast<TIndex>().Min());
15 int upper = Convert.ToInt32(Enum.GetValues(typeof(TIndex)).Cast<TIndex>().Max());
16 array = new T[1 + upper - lower];
17 }
18
19 public T this[TIndex key]
20 {
21 get => array[Convert.ToInt32(key) - lower];
22 set => array[Convert.ToInt32(key) - lower] = value;
23 }
24
25 public IEnumerator GetEnumerator()
26 {
27 return Enum.GetValues(typeof(TIndex)).Cast<TIndex>().Select(i => this[i]).GetEnumerator();
28 }
29
30 public void Assign(EnumIndexedArray<T, TIndex> source)
31 {
32 for (int i = 0; i < source.array.Length; i++)
33 {
34 array[i] = source.array[i];
35 }
36 }
37 }
38}
Note: See TracBrowser for help on using the repository browser.