Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ObjectIDGenerator.cs
Go to the documentation of this file.
3
5
6public class ObjectIDGenerator
7{
8 internal int _currentCount;
9
10 private int _currentSize;
11
12 private long[] _ids;
13
14 private object[] _objs;
15
17 {
18 _currentCount = 1;
19 _currentSize = 3;
20 _ids = new long[_currentSize * 4];
21 _objs = new object[_currentSize * 4];
22 }
23
24 private int FindElement(object obj, out bool found)
25 {
27 int num2 = 1 + (num & 0x7FFFFFFF) % (_currentSize - 2);
28 while (true)
29 {
30 int num3 = (num & 0x7FFFFFFF) % _currentSize * 4;
31 for (int i = num3; i < num3 + 4; i++)
32 {
33 if (_objs[i] == null)
34 {
35 found = false;
36 return i;
37 }
38 if (_objs[i] == obj)
39 {
40 found = true;
41 return i;
42 }
43 }
44 num += num2;
45 }
46 }
47
48 public virtual long GetId(object obj, out bool firstTime)
49 {
50 if (obj == null)
51 {
52 throw new ArgumentNullException("obj");
53 }
54 bool found;
55 int num = FindElement(obj, out found);
56 long result;
57 if (!found)
58 {
59 _objs[num] = obj;
60 _ids[num] = _currentCount++;
61 result = _ids[num];
62 if (_currentCount > _currentSize * 4 / 2)
63 {
64 Rehash();
65 }
66 }
67 else
68 {
69 result = _ids[num];
70 }
72 return result;
73 }
74
75 public virtual long HasId(object obj, out bool firstTime)
76 {
77 if (obj == null)
78 {
79 throw new ArgumentNullException("obj");
80 }
81 bool found;
82 int num = FindElement(obj, out found);
83 if (found)
84 {
85 firstTime = false;
86 return _ids[num];
87 }
88 firstTime = true;
89 return 0L;
90 }
91
92 private void Rehash()
93 {
96 if (num == currentSize)
97 {
99 }
100 _currentSize = num;
101 long[] ids = new long[_currentSize * 4];
102 object[] objs = new object[_currentSize * 4];
103 long[] ids2 = _ids;
104 object[] objs2 = _objs;
105 _ids = ids;
106 _objs = objs;
107 for (int i = 0; i < objs2.Length; i++)
108 {
109 if (objs2[i] != null)
110 {
111 bool found;
112 int num2 = FindElement(objs2[i], out found);
113 _objs[num2] = objs2[i];
114 _ids[num2] = ids2[i];
115 }
116 }
117 }
118}
static int ExpandPrime(int oldSize)
virtual long GetId(object obj, out bool firstTime)
virtual long HasId(object obj, out bool firstTime)
int FindElement(object obj, out bool found)
static string Serialization_TooManyElements
Definition SR.cs:26
Definition SR.cs:7