Line | |
---|
1 | using System;
|
---|
2 | using System.Collections;
|
---|
3 | using System.Linq;
|
---|
4 |
|
---|
5 | namespace 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.