Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CacheDict.cs
Go to the documentation of this file.
1
using
System.Diagnostics.CodeAnalysis
;
2
using
System.Threading
;
3
4
namespace
System.Dynamic.Utils
;
5
6
internal
sealed
class
CacheDict
<TKey, TValue>
7
{
8
private
sealed
class
Entry
9
{
10
internal
readonly
int
_hash
;
11
12
internal
readonly TKey
_key
;
13
14
internal
readonly TValue
_value
;
15
16
internal
Entry
(
int
hash, TKey
key
, TValue
value
)
17
{
18
_hash
= hash;
19
_key
=
key
;
20
_value
=
value
;
21
}
22
}
23
24
private
readonly
int
_mask
;
25
26
private
readonly
Entry
[]
_entries
;
27
28
internal
TValue
this
[TKey
key
]
29
{
30
set
31
{
32
Add
(
key
,
value
);
33
}
34
}
35
36
internal
CacheDict
(
int
size)
37
{
38
int
num =
AlignSize
(size);
39
_mask
= num - 1;
40
_entries
=
new
Entry
[num];
41
}
42
43
private
static
int
AlignSize
(
int
size)
44
{
45
size--;
46
size |= size >> 1;
47
size |= size >> 2;
48
size |= size >> 4;
49
size |= size >> 8;
50
size |= size >> 16;
51
size++;
52
return
size;
53
}
54
55
internal
bool
TryGetValue
(TKey
key
, [MaybeNullWhen(
false
)] out TValue
value
)
56
{
57
int
hashCode =
key
.GetHashCode();
58
int
num = hashCode &
_mask
;
59
Entry
entry =
Volatile
.
Read
(ref
_entries
[num]);
60
if
(entry !=
null
&& entry.
_hash
== hashCode && entry.
_key
.Equals(
key
))
61
{
62
value
= entry.
_value
;
63
return
true
;
64
}
65
value
=
default
(TValue);
66
return
false
;
67
}
68
69
internal
void
Add
(TKey
key
, TValue
value
)
70
{
71
int
hashCode =
key
.GetHashCode();
72
int
num = hashCode &
_mask
;
73
Entry
entry =
Volatile
.
Read
(ref
_entries
[num]);
74
if
(entry ==
null
|| entry.
_hash
!= hashCode || !entry.
_key
.Equals(
key
))
75
{
76
Volatile
.
Write
(ref
_entries
[num],
new
Entry
(hashCode,
key
,
value
));
77
}
78
}
79
}
System.Dynamic.Utils.CacheDict.Entry.Entry
Entry(int hash, TKey key, TValue value)
Definition
CacheDict.cs:16
System.Dynamic.Utils.CacheDict.Entry._hash
readonly int _hash
Definition
CacheDict.cs:10
System.Dynamic.Utils.CacheDict.Entry._value
readonly TValue _value
Definition
CacheDict.cs:14
System.Dynamic.Utils.CacheDict.Entry._key
readonly TKey _key
Definition
CacheDict.cs:12
System.Dynamic.Utils.CacheDict.Entry
Definition
CacheDict.cs:9
System.Dynamic.Utils.CacheDict._entries
readonly Entry[] _entries
Definition
CacheDict.cs:26
System.Dynamic.Utils.CacheDict.TryGetValue
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
Definition
CacheDict.cs:55
System.Dynamic.Utils.CacheDict._mask
readonly int _mask
Definition
CacheDict.cs:24
System.Dynamic.Utils.CacheDict.AlignSize
static int AlignSize(int size)
Definition
CacheDict.cs:43
System.Dynamic.Utils.CacheDict.Add
void Add(TKey key, TValue value)
Definition
CacheDict.cs:69
System.Dynamic.Utils.CacheDict.CacheDict
CacheDict(int size)
Definition
CacheDict.cs:36
System.Dynamic.Utils.CacheDict
Definition
CacheDict.cs:7
System.Threading.Volatile.Read
static bool Read(ref bool location)
Definition
Volatile.cs:67
System.Threading.Volatile.Write
static void Write(ref bool location, bool value)
Definition
Volatile.cs:74
System.Threading.Volatile
Definition
Volatile.cs:9
System.Diagnostics.CodeAnalysis
Definition
AllowNullAttribute.cs:1
System.Dynamic.Utils
Definition
CacheDict.cs:4
System.Threading
Definition
TaskToApm.cs:3
System.ExceptionArgument.value
@ value
System.ExceptionArgument.key
@ key
System.ConsoleKey.Add
@ Add
source
System.Linq.Expressions
System.Dynamic.Utils
CacheDict.cs
Generated by
1.10.0