Files
ThingsGateway/src/Admin/ThingsGateway.NewLife.X/Collections/NonBlockingDictionary/ConcurrentDictionary/DictionaryImpl`2.cs
2025-10-15 17:40:33 +08:00

82 lines
2.3 KiB
C#

// 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<TKey, TValue>
: DictionaryImpl
{
private readonly bool _valueIsValueType = typeof(TValue).IsValueType;
internal IEqualityComparer<TKey> _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<TKey, TValue> 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<TKey, TValue> Current
{
get
{
return new KeyValuePair<TKey, TValue>(this._curKey, _curValue);
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected TValue FromObjectValue(object obj)
{
// regular value type
if (default(TValue) != null)
{
return Unsafe.As<Boxed<TValue>>(obj).Value;
}
// null
if (obj == NULLVALUE)
{
return default(TValue);
}
// ref type
if (!_valueIsValueType)
{
return Unsafe.As<object, TValue>(ref obj);
}
// nullable
return (TValue)obj;
}
}
}