DictionaryBase.cs 1013 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Runtime.CompilerServices;
  3. namespace Jint.Collections;
  4. internal abstract class DictionaryBase<TValue> : IEngineDictionary<Key, TValue>
  5. {
  6. public ref TValue this[Key key]
  7. {
  8. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  9. get => ref GetValueRefOrAddDefault(key, out _);
  10. }
  11. public bool TryGetValue(Key key, [NotNullWhen(true)] out TValue? value)
  12. {
  13. value = default;
  14. ref var temp = ref GetValueRefOrNullRef(key);
  15. if (Unsafe.IsNullRef(ref temp))
  16. {
  17. return false;
  18. }
  19. value = temp!;
  20. return true;
  21. }
  22. public bool ContainsKey(Key key)
  23. {
  24. ref var valueRefOrNullRef = ref GetValueRefOrNullRef(key);
  25. return !Unsafe.IsNullRef(ref valueRefOrNullRef);
  26. }
  27. public abstract int Count { get; }
  28. public abstract ref TValue GetValueRefOrNullRef(Key key);
  29. public abstract ref TValue GetValueRefOrAddDefault(Key key, out bool exists);
  30. }