// Copyright (c) Vladimir Sadov. All rights reserved. // // This file is distributed under the MIT License. See LICENSE.md for details. #nullable disable using System.Collections; using System.Runtime.CompilerServices; namespace System.Collections.Concurrent { internal abstract class DictionaryImpl : DictionaryImpl { private readonly bool _valueIsValueType = typeof(TValue).IsValueType; internal IEqualityComparer _keyComparer; internal abstract void Clear(); internal abstract int Count { get; } internal abstract object TryGetValue(TKey key); internal abstract bool PutIfMatch(TKey key, TValue newVal, ref TValue oldValue, ValueMatch match); internal abstract bool RemoveIfMatch(TKey key, ref TValue oldValue, ValueMatch match); internal abstract TValue GetOrAdd(TKey key, Func valueFactory); internal abstract Snapshot GetSnapshot(); internal abstract class Snapshot { protected int _idx; protected TKey _curKey; protected TValue _curValue; public abstract int Count { get; } public abstract bool MoveNext(); public abstract void Reset(); internal DictionaryEntry Entry { get { return new DictionaryEntry(_curKey, _curValue); } } internal KeyValuePair Current { get { return new KeyValuePair(this._curKey, _curValue); } } } [MethodImpl(MethodImplOptions.AggressiveInlining)] protected TValue FromObjectValue(object obj) { // regular value type if (default(TValue) != null) { return Unsafe.As>(obj).Value; } // null if (obj == NULLVALUE) { return default(TValue); } // ref type if (!_valueIsValueType) { return Unsafe.As(ref obj); } // nullable return (TValue)obj; } } }