Client LuaCsForBarotrauma
ListDictionary.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Collections.Immutable;
5 using System.Linq;
6 
7 namespace Barotrauma
8 {
9  public class ListDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
10  {
11  private readonly ImmutableDictionary<TKey, int> keyToIndex;
12  private readonly IReadOnlyList<TValue> list;
13 
14  public ListDictionary(IReadOnlyList<TValue> list, int len, Func<int, TKey> keyFunc)
15  {
16  this.list = list;
17  var keyToIndex = new Dictionary<TKey, int>();
18  for (int i = 0; i < len; i++)
19  {
20  keyToIndex.Add(keyFunc(i), i);
21  }
22 
23  this.keyToIndex = keyToIndex.ToImmutableDictionary();
24  }
25 
26  public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
27  {
28  foreach (var kvp in keyToIndex)
29  {
30  yield return new KeyValuePair<TKey, TValue>(kvp.Key, list[kvp.Value]);
31  }
32  }
33 
34  IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
35 
36  public int Count => keyToIndex.Count;
37  public bool ContainsKey(TKey key) => keyToIndex.ContainsKey(key);
38 
39  public bool TryGetValue(TKey key, out TValue value)
40  {
41  if (keyToIndex.TryGetValue(key, out int index))
42  {
43  value = list[index];
44  return true;
45  }
46  value = default(TValue);
47  return false;
48  }
49 
50  public TValue this[TKey key] => list[keyToIndex[key]];
51 
52  public IEnumerable<TKey> Keys => keyToIndex.Keys;
53  public IEnumerable<TValue> Values => keyToIndex.Values.Select(i => list[i]);
54  }
55 }
ListDictionary(IReadOnlyList< TValue > list, int len, Func< int, TKey > keyFunc)
IEnumerator< KeyValuePair< TKey, TValue > > GetEnumerator()
IEnumerable< TKey > Keys
IEnumerable< TValue > Values
bool ContainsKey(TKey key)
bool TryGetValue(TKey key, out TValue value)