Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ObjectToIdCache.cs
Go to the documentation of this file.
2
4
5internal sealed class ObjectToIdCache
6{
7 internal int m_currentCount;
8
9 internal int[] m_ids;
10
11 internal object[] m_objs;
12
13 internal bool[] m_isWrapped;
14
15 internal static readonly int[] primes = new int[30]
16 {
17 3, 7, 17, 37, 89, 197, 431, 919, 1931, 4049,
18 8419, 17519, 36353, 75431, 156437, 324449, 672827, 1395263, 2893249, 5999471,
19 11998949, 23997907, 47995853, 95991737, 191983481, 383966977, 767933981, 1535867969, 2146435069, 2147483591
20 };
21
23 {
25 m_ids = new int[GetPrime(1)];
26 m_objs = new object[m_ids.Length];
27 m_isWrapped = new bool[m_ids.Length];
28 }
29
30 public int GetId(object obj, ref bool newId)
31 {
32 bool isEmpty;
33 bool isWrapped;
35 if (!isEmpty)
36 {
37 newId = false;
38 return m_ids[num];
39 }
40 if (!newId)
41 {
42 return -1;
43 }
44 int num2 = m_currentCount++;
45 m_objs[num] = obj;
46 m_ids[num] = num2;
48 if (m_currentCount >= m_objs.Length - 1)
49 {
50 Rehash();
51 }
52 return num2;
53 }
54
55 public int ReassignId(int oldObjId, object oldObj, object newObj)
56 {
58 if (isEmpty)
59 {
60 return 0;
61 }
62 int num2 = m_ids[num];
63 if (oldObjId > 0)
64 {
65 m_ids[num] = oldObjId;
66 }
67 else
68 {
69 RemoveAt(num);
70 }
72 int result = 0;
73 if (!isEmpty)
74 {
75 result = m_ids[num];
76 }
77 m_objs[num] = newObj;
78 m_ids[num] = num2;
80 return result;
81 }
82
83 private int FindElement(object obj, out bool isEmpty, out bool isWrapped)
84 {
85 isWrapped = false;
86 int num = ComputeStartPosition(obj);
87 for (int i = num; i != num - 1; i++)
88 {
89 if (m_objs[i] == null)
90 {
91 isEmpty = true;
92 return i;
93 }
94 if (m_objs[i] == obj)
95 {
96 isEmpty = false;
97 return i;
98 }
99 if (i == m_objs.Length - 1)
100 {
101 isWrapped = true;
102 i = -1;
103 }
104 }
106 }
107
108 private void RemoveAt(int position)
109 {
110 int num = m_objs.Length;
111 int num2 = position;
112 for (int i = ((position != num - 1) ? (position + 1) : 0); i != position; i++)
113 {
114 if (m_objs[i] == null)
115 {
116 m_objs[num2] = null;
117 m_ids[num2] = 0;
118 m_isWrapped[num2] = false;
119 return;
120 }
122 bool flag = i < position && !m_isWrapped[i];
123 bool flag2 = num2 < position;
124 if ((num3 <= num2 && (!flag || flag2)) || (flag2 && !flag))
125 {
126 m_objs[num2] = m_objs[i];
127 m_ids[num2] = m_ids[i];
128 m_isWrapped[num2] = m_isWrapped[i] && i > num2;
129 num2 = i;
130 }
131 if (i == num - 1)
132 {
133 i = -1;
134 }
135 }
137 }
138
139 private int ComputeStartPosition(object o)
140 {
141 return (RuntimeHelpers.GetHashCode(o) & 0x7FFFFFFF) % m_objs.Length;
142 }
143
144 private void Rehash()
145 {
146 int prime = GetPrime(m_objs.Length + 1);
147 int[] ids = m_ids;
148 object[] objs = m_objs;
149 m_ids = new int[prime];
150 m_objs = new object[prime];
151 m_isWrapped = new bool[prime];
152 for (int i = 0; i < objs.Length; i++)
153 {
154 object obj = objs[i];
155 if (obj != null)
156 {
157 bool isEmpty;
158 bool isWrapped;
159 int num = FindElement(obj, out isEmpty, out isWrapped);
160 m_objs[num] = obj;
161 m_ids[num] = ids[i];
162 m_isWrapped[num] = isWrapped;
163 }
164 }
165 }
166
167 private static int GetPrime(int min)
168 {
169 for (int i = 0; i < primes.Length; i++)
170 {
171 int num = primes[i];
172 if (num >= min)
173 {
174 return num;
175 }
176 }
177 return min;
178 }
179}
int FindElement(object obj, out bool isEmpty, out bool isWrapped)
int GetId(object obj, ref bool newId)
int ReassignId(int oldObjId, object oldObj, object newObj)
static SerializationException CreateSerializationException(string errorMessage)
static string ObjectTableOverflow
Definition SR.cs:224
Definition SR.cs:7