-
Notifications
You must be signed in to change notification settings - Fork 1
/
Util.cs
30 lines (28 loc) · 1.14 KB
/
Util.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
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace SimdDictionary {
public static class CollectionsMarshal_ {
public static ref readonly V GetValueRefOrNullRef<K, V> (this VectorizedDictionary<K, V> self, K key)
where K : notnull
{
ref var pair = ref self.FindKey(key);
if (Unsafe.IsNullRef(ref pair))
return ref Unsafe.NullRef<V>();
return ref pair.Value;
}
public static ref readonly V GetValueRefOrAddDefault<K, V> (this VectorizedDictionary<K, V> self, K key, V defaultValue = default!)
where K : notnull
{
retry:
ref var pair = ref self.TryInsert(key, defaultValue, VectorizedDictionary<K, V>.InsertMode.EnsureUnique, out var result);
if (result == VectorizedDictionary<K, V>.InsertResult.NeedToGrow) {
self.EnsureCapacity(self.Count + 1);
goto retry;
}
if (Unsafe.IsNullRef(ref pair))
throw new Exception("Corrupted internal state");
return ref pair.Value;
}
}
}