-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlatDictionary.cs
169 lines (143 loc) · 3.68 KB
/
FlatDictionary.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System.Collections;
namespace FlatDictionary;
public partial class FlatDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TKey : unmanaged
{
public FlatDictionary() { }
public FlatDictionary(IDictionary dictionary) => AddDictionary(dictionary);
public int Count => _pairs.Count;
public int Capacity => _pairs.Capacity;
public bool IsReadOnly => false;
[EditorBrowsable(EditorBrowsableState.Never)]
public ICollection<TKey> Keys => throw new NotSupportedException("FlatDictionary objects do not support acquiring key collections.");
public ICollection<TValue> Values
{
get
{
TValue[] Values = new TValue[_pairs.Count];
for (int i = 0; i < Values.Length; i++) Values[i] = _pairs[i].Value;
return Values;
}
}
public TValue this[TKey key]
{
get
{
if (TryGetKeyIndex(key, out int index)) return _pairs[index].Value;
throw new KeyNotFoundException($"The given key '{key}' was not found in the dictionary.");
}
set
{
if (TryGetKeyIndex(key, out int index))
{
_pairs[index] = new(_pairs[index].Key, value);
}
else
{
_pairs.Add(new(GetHash(key), value));
}
}
}
public TValue this[ReadOnlySpan<TKey> keys]
{
get
{
if (TryGetKeyIndex(keys, out int index)) return _pairs[index].Value;
throw new KeyNotFoundException("The given key set was not found in the dictionary.");
}
set
{
if (TryGetKeyIndex(keys, out int index))
{
_pairs[index] = new(_pairs[index].Key, value);
}
else
{
_pairs.Add(new(GetHash(keys), value));
}
}
}
public void Add(TKey key, TValue value) => _pairs.Add(new(GetHash(key), value));
public void Add(ReadOnlySpan<TKey> keys, TValue value) => _pairs.Add(new(GetHash(keys), value));
public void AddDictionary(IDictionary dictionary, ReadOnlySpan<TKey> baseKeys)
{
_nestedKeys.AddRange(baseKeys);
AddDictionary(dictionary);
_nestedKeys.Clear();
}
public void Clear() => _pairs.Clear();
public bool ContainsKey(TKey key)
{
ulong hash = GetHash(key);
foreach (KeyValuePair<ulong, TValue> pair in _pairs)
{
if (pair.Key == hash) return true;
}
return false;
}
public bool ContainsKey(ReadOnlySpan<TKey> keys)
{
ulong hash = GetHash(keys);
foreach (KeyValuePair<ulong, TValue> pair in _pairs)
{
if (pair.Key == hash) return true;
}
return false;
}
public bool ContainsValue(TValue value)
{
foreach (KeyValuePair<ulong, TValue> pair in _pairs)
{
if (EqualityComparer<TValue>.Default.Equals(pair.Value, value)) return true;
}
return false;
}
public int EnsureCapacity(int capacity) => _pairs.EnsureCapacity(capacity);
public IEnumerator GetEnumerator() => _pairs.GetEnumerator();
public bool Remove(TKey key)
{
if (TryGetKeyIndex(key, out int index))
{
_pairs.RemoveAt(index);
return true;
}
return false;
}
public bool Remove(ReadOnlySpan<TKey> keys)
{
if (TryGetKeyIndex(keys, out int index))
{
_pairs.RemoveAt(index);
return true;
}
return false;
}
public void TrimExcess() => _pairs.TrimExcess();
public void TrimExcess(int capacity) => _pairs.Capacity = capacity;
public bool TryAdd(ReadOnlySpan<TKey> keys, TValue value)
{
ulong hash = GetHash(keys);
if (TryGetKeyIndex(hash, out int _)) return false;
_pairs.Add(new(hash, value));
return true;
}
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{
if (TryGetKeyIndex(key, out int index))
{
value = _pairs[index].Value;
return true;
}
value = default;
return false;
}
public bool TryGetValue(ReadOnlySpan<TKey> keys, [MaybeNullWhen(false)] out TValue value)
{
if (TryGetKeyIndex(keys, out int index))
{
value = _pairs[index].Value;
return true;
}
value = default;
return false;
}
}