Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WeakHashtable.cs
Go to the documentation of this file.
3
5
6internal sealed class WeakHashtable : Hashtable
7{
8 private sealed class WeakKeyComparer : IEqualityComparer
9 {
10 bool IEqualityComparer.Equals(object x, object y)
11 {
12 if (x == null)
13 {
14 return y == null;
15 }
16 if (y != null && x.GetHashCode() == y.GetHashCode())
17 {
19 {
20 if (!weakReference.IsAlive)
21 {
22 return false;
23 }
24 x = weakReference.Target;
25 }
27 {
28 if (!weakReference2.IsAlive)
29 {
30 return false;
31 }
32 y = weakReference2.Target;
33 }
34 return x == y;
35 }
36 return false;
37 }
38
40 {
41 return obj.GetHashCode();
42 }
43 }
44
45 private sealed class EqualityWeakReference : WeakReference
46 {
47 private readonly int _hashCode;
48
49 internal EqualityWeakReference(object o)
50 : base(o)
51 {
52 _hashCode = o.GetHashCode();
53 }
54
55 public override bool Equals(object o)
56 {
57 if (o?.GetHashCode() != _hashCode)
58 {
59 return false;
60 }
61 if (o == this || (IsAlive && o == Target))
62 {
63 return true;
64 }
65 return false;
66 }
67
68 public override int GetHashCode()
69 {
70 return _hashCode;
71 }
72 }
73
74 private static readonly IEqualityComparer s_comparer = new WeakKeyComparer();
75
76 private long _lastGlobalMem;
77
78 private int _lastHashCount;
79
80 internal WeakHashtable()
82 {
83 }
84
85 public void SetWeak(object key, object value)
86 {
88 this[new EqualityWeakReference(key)] = value;
89 }
90
91 private void ScavengeKeys()
92 {
93 int count = Count;
94 if (count == 0)
95 {
96 return;
97 }
98 if (_lastHashCount == 0)
99 {
101 return;
102 }
104 if (_lastGlobalMem == 0L)
105 {
107 return;
108 }
109 float num = (float)(totalMemory - _lastGlobalMem) / (float)_lastGlobalMem;
110 float num2 = (float)(count - _lastHashCount) / (float)_lastHashCount;
111 if (num < 0f && num2 >= 0f)
112 {
113 List<object> list = null;
114 foreach (object key in Keys)
115 {
116 if (key is WeakReference { IsAlive: false } weakReference)
117 {
118 if (list == null)
119 {
120 list = new List<object>();
121 }
122 list.Add(weakReference);
123 }
124 }
125 if (list != null)
126 {
127 foreach (object item in list)
128 {
129 Remove(item);
130 }
131 }
132 }
135 }
136}
virtual ICollection Keys
Definition Hashtable.cs:532
void SetWeak(object key, object value)
static readonly IEqualityComparer s_comparer
static long GetTotalMemory()
Definition GC.cs:8
virtual ? object Target