Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CerHashtable.cs
Go to the documentation of this file.
3
4namespace System.Reflection;
5
6internal struct CerHashtable<K, V> where K : class
7{
8 private sealed class Table
9 {
10 internal K[] m_keys;
11
12 internal V[] m_values;
13
14 internal int m_count;
15
16 internal Table(int size)
17 {
18 size = HashHelpers.GetPrime(size);
19 m_keys = new K[size];
20 m_values = new V[size];
21 }
22
23 internal void Insert(K key, V value)
24 {
26 if (num < 0)
27 {
28 num = ~num;
29 }
30 K[] keys = m_keys;
31 int num2 = num % keys.Length;
32 while (true)
33 {
34 K val = keys[num2];
35 if (val == null)
36 {
37 break;
38 }
39 num2++;
40 if (num2 >= keys.Length)
41 {
42 num2 -= keys.Length;
43 }
44 }
45 m_count++;
46 m_values[num2] = value;
47 Volatile.Write(ref keys[num2], key);
48 }
49 }
50
51 private Table m_Table;
52
53 internal V this[K key]
54 {
55 get
56 {
57 Table table = Volatile.Read(ref m_Table);
58 if (table == null)
59 {
60 return default(V);
61 }
62 int num = GetHashCodeHelper(key);
63 if (num < 0)
64 {
65 num = ~num;
66 }
67 K[] keys = table.m_keys;
68 int num2 = num % keys.Length;
69 while (true)
70 {
71 K val = Volatile.Read(ref keys[num2]);
72 if (val == null)
73 {
74 break;
75 }
76 if (val.Equals(key))
77 {
78 return table.m_values[num2];
79 }
80 num2++;
81 if (num2 >= keys.Length)
82 {
83 num2 -= keys.Length;
84 }
85 }
86 return default(V);
87 }
88 set
89 {
90 Table table = m_Table;
91 if (table != null)
92 {
93 int num = 2 * (table.m_count + 1);
94 if (num >= table.m_keys.Length)
95 {
96 Rehash(num);
97 }
98 }
99 else
100 {
101 Rehash(7);
102 }
104 }
105 }
106
107 private static int GetHashCodeHelper(K key)
108 {
109 if (!(key is string text))
110 {
111 return key.GetHashCode();
112 }
113 return text.GetNonRandomizedHashCode();
114 }
115
116 private void Rehash(int newSize)
117 {
118 Table table = new Table(newSize);
119 Table table2 = m_Table;
120 if (table2 != null)
121 {
122 K[] keys = table2.m_keys;
123 V[] values = table2.m_values;
124 for (int i = 0; i < keys.Length; i++)
125 {
126 K val = keys[i];
127 if (val != null)
128 {
129 table.Insert(val, values[i]);
130 }
131 }
132 }
133 Volatile.Write(ref m_Table, table);
134 }
135}
static int GetPrime(int min)
static bool Read(ref bool location)
Definition Volatile.cs:67
static void Write(ref bool location, bool value)
Definition Volatile.cs:74
static int GetHashCodeHelper(K key)