-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indexer.cs
37 lines (32 loc) · 1004 Bytes
/
Indexer.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
// Copyright (C) 2024 - Nordic Space Link
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace NordicSpaceLink.IIO
{
public class Indexer<TKey, TValue> : IEnumerable<TValue>
{
private readonly Dictionary<TKey, TValue> valueLookup;
private readonly List<TValue> values;
internal Indexer(List<(TValue value, TKey[] keys)> values)
{
this.valueLookup = values.SelectMany(v => v.keys.Select(key => (key, v.value))).ToDictionary(v => v.key, v => v.value);
this.values = values.Select(x => x.value).ToList();
}
public TValue this[TKey key]
{
get
{
return valueLookup[key];
}
}
IEnumerator<TValue> IEnumerable<TValue>.GetEnumerator()
{
return values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return values.GetEnumerator();
}
}
}