12 [__DynamicallyInvokable]
18 [__DynamicallyInvokable]
23 public DependentHandle depHnd;
30 private int[] _buckets;
32 private Entry[] _entries;
34 private int _freeList;
36 private const int _initialCapacity = 5;
38 private readonly
object _lock;
40 private bool _invalid;
44 [SecuritySafeCritical]
50 for (
int i = 0; i < _buckets.Length; i++)
52 for (
int num = _buckets[i]; num != -1; num = _entries[num].next)
54 TKey val = (TKey)_entries[num].depHnd.GetPrimary();
68 [SecuritySafeCritical]
74 for (
int i = 0; i < _buckets.Length; i++)
76 for (
int num = _buckets[i]; num != -1; num = _entries[num].next)
78 object primary =
null;
79 object secondary =
null;
80 _entries[num].depHnd.GetPrimaryAndSecondary(out primary, out secondary);
83 list.Add((TValue)secondary);
93 [SecuritySafeCritical]
94 [__DynamicallyInvokable]
97 _buckets =
new int[0];
98 _entries =
new Entry[0];
100 _lock =
new object();
111 [SecuritySafeCritical]
112 [__DynamicallyInvokable]
117 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
122 return TryGetValueWorker(key, out value);
133 [SecuritySafeCritical]
134 [__DynamicallyInvokable]
135 public void Add(TKey key, TValue value)
139 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
145 int num = FindEntry(key);
149 ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
151 CreateEntry(key, value);
162 [SecuritySafeCritical]
163 [__DynamicallyInvokable]
168 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
175 int num2 = num % _buckets.Length;
177 for (
int num4 = _buckets[num2]; num4 != -1; num4 = _entries[num4].next)
179 if (_entries[num4].hashCode == num && _entries[num4].depHnd.GetPrimary() == key)
183 _buckets[num2] = _entries[num4].next;
187 _entries[num3].next = _entries[num4].next;
189 _entries[num4].depHnd.Free();
190 _entries[num4].next = _freeList;
208 [SecuritySafeCritical]
209 [__DynamicallyInvokable]
212 if (createValueCallback ==
null)
220 TValue val = createValueCallback(key);
225 if (TryGetValueWorker(key, out value))
230 CreateEntry(key, val);
243 [__DynamicallyInvokable]
249 [SecuritySafeCritical]
250 [FriendAccessAllowed]
251 internal TKey FindEquivalentKeyUnsafe(TKey key, out TValue value)
255 for (
int i = 0; i < _buckets.Length; i++)
257 for (
int num = _buckets[i]; num != -1; num = _entries[num].next)
259 _entries[num].depHnd.GetPrimaryAndSecondary(out
object primary, out
object secondary);
260 if (
object.Equals(primary, key))
262 value = (TValue)secondary;
263 return (TKey)primary;
272 [SecuritySafeCritical]
273 internal void Clear()
277 for (
int i = 0; i < _buckets.Length; i++)
282 for (j = 0; j < _entries.Length; j++)
284 if (_entries[j].depHnd.IsAllocated)
286 _entries[j].depHnd.Free();
288 _entries[j].next = j - 1;
295 private bool TryGetValueWorker(TKey key, out TValue value)
297 int num = FindEntry(key);
300 object primary =
null;
301 object secondary =
null;
302 _entries[num].depHnd.GetPrimaryAndSecondary(out primary, out secondary);
305 value = (TValue)secondary;
314 private void CreateEntry(TKey key, TValue value)
320 int num = RuntimeHelpers.GetHashCode(key) &
int.MaxValue;
321 int num2 = num % _buckets.Length;
322 int freeList = _freeList;
323 _freeList = _entries[freeList].next;
324 _entries[freeList].hashCode = num;
325 _entries[freeList].depHnd =
new DependentHandle(key, value);
326 _entries[freeList].next = _buckets[num2];
327 _buckets[num2] = freeList;
331 private void Resize()
333 int num = _buckets.Length;
336 for (i = 0; i < _entries.Length; i++)
338 if (_entries[i].depHnd.IsAllocated && _entries[i].depHnd.GetPrimary() ==
null)
346 num = HashHelpers.GetPrime((_buckets.Length == 0) ? 6 : (_buckets.Length * 2));
349 int[] array =
new int[num];
350 for (
int j = 0; j < num; j++)
354 Entry[] array2 =
new Entry[num];
355 for (i = 0; i < _entries.Length; i++)
357 DependentHandle depHnd = _entries[i].depHnd;
358 if (depHnd.IsAllocated && depHnd.GetPrimary() !=
null)
360 int num3 = _entries[i].hashCode % num;
361 array2[i].depHnd = depHnd;
362 array2[i].hashCode = _entries[i].hashCode;
363 array2[i].next = array[num3];
368 _entries[i].depHnd.Free();
369 array2[i].depHnd =
default(DependentHandle);
370 array2[i].next = num2;
374 for (; i != array2.Length; i++)
376 array2[i].depHnd =
default(DependentHandle);
377 array2[i].next = num2;
386 private int FindEntry(TKey key)
388 int num = RuntimeHelpers.GetHashCode(key) &
int.MaxValue;
389 for (
int num2 = _buckets[num % _buckets.Length]; num2 != -1; num2 = _entries[num2].next)
391 if (_entries[num2].hashCode == num && _entries[num2].depHnd.GetPrimary() == key)
399 private void VerifyIntegrity()
403 throw new InvalidOperationException(Environment.GetResourceString(
"CollectionCorrupted"));
408 [SecuritySafeCritical]
411 if (!Environment.HasShutdownStarted && _lock !=
null)
417 Entry[] entries = _entries;
422 for (; i < entries.Length; i++)
424 entries[i].depHnd.Free();
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
void Add(TKey key, TValue value)
Adds a key to the table.
TValue GetOrCreateValue(TKey key)
Atomically searches for a specified key in the table and returns the corresponding value....
static int GetHashCode(object o)
Serves as a hash function for a particular object, and is suitable for use in algorithms and data str...
ConditionalWeakTable()
Initializes a new instance of the T:System.Runtime.CompilerServices.ConditionalWeakTable`2 class.
Contains methods to create types of objects locally or remotely, or obtain references to existing rem...
Enables compilers to dynamically attach object fields to managed objects.
delegate TValue CreateValueCallback(TKey key)
Represents a method that creates a non-default value to add as part of a key/value pair to a T:System...
static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture)
Creates an instance of the specified type using the constructor that best matches the specified param...
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
TValue GetValue(TKey key, CreateValueCallback createValueCallback)
Atomically searches for a specified key in the table and returns the corresponding value....
bool TryGetValue(TKey key, out TValue value)
Gets the value of the specified key.
bool Remove(TKey key)
Removes a key and its value from the table.
Provides a set of static methods and properties that provide support for compilers....