[rebuid]use dynamic wrapping instead of static wrapping
This commit is contained in:
@@ -29,12 +29,13 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
{
|
||||
public sealed class BidirectionalDictionary<TFirst, TSecond> : IBidirectionalDictionary<TFirst, TSecond>
|
||||
where TFirst: notnull
|
||||
where TSecond: notnull
|
||||
{
|
||||
|
||||
private readonly Dictionary<TFirst, TSecond> _forward;
|
||||
@@ -42,25 +43,21 @@ namespace StgSharp.Collections
|
||||
|
||||
public BidirectionalDictionary()
|
||||
{
|
||||
_forward = new Dictionary<TFirst, TSecond>();
|
||||
_reverse = new Dictionary<TSecond, TFirst>();
|
||||
_forward = [];
|
||||
_reverse = [];
|
||||
}
|
||||
|
||||
public TFirst this[TSecond key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_reverse.TryGetValue(key, out TFirst value)) {
|
||||
return value;
|
||||
}
|
||||
throw new ArgumentException($"Cannot find key {key!.ToString()} in dictionary.");
|
||||
}
|
||||
get => _reverse.TryGetValue(key, out TFirst value) ?
|
||||
value :
|
||||
throw new ArgumentException($"Cannot find key {key} in dictionary.");
|
||||
set
|
||||
{
|
||||
if (_reverse.TryGetValue(key, out TFirst origin))
|
||||
{
|
||||
_reverse[key] = value;
|
||||
_forward.Remove(origin);
|
||||
_ = _forward.Remove(origin);
|
||||
} else
|
||||
{
|
||||
_reverse.Add(key, value);
|
||||
@@ -71,19 +68,15 @@ namespace StgSharp.Collections
|
||||
|
||||
public TSecond this[TFirst key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_forward.TryGetValue(key, out TSecond value)) {
|
||||
return value;
|
||||
}
|
||||
throw new ArgumentException($"Cannot find key {key!.ToString()} in dictionary.");
|
||||
}
|
||||
get => _forward.TryGetValue(key, out TSecond value) ?
|
||||
value :
|
||||
throw new ArgumentException($"Cannot find key {key} in dictionary.");
|
||||
set
|
||||
{
|
||||
if (_forward.TryGetValue(key, out TSecond origin))
|
||||
{
|
||||
_forward[key] = value;
|
||||
_reverse.Remove(origin);
|
||||
_ = _reverse.Remove(origin);
|
||||
} else
|
||||
{
|
||||
_forward.Add(key, value);
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
@@ -88,6 +87,13 @@ namespace StgSharp.Collections
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ReadOnlySpan<T> PopRange(int count)
|
||||
{
|
||||
int i = int.Min(count, _index) - 1;
|
||||
return _values[0..i];
|
||||
}
|
||||
|
||||
public void Push(T item)
|
||||
{
|
||||
_values[++_index] = _index < _values.Length - 1 ?
|
||||
|
||||
@@ -25,60 +25,60 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
{
|
||||
public interface IBidirectionalDictionary<TFirst, TSecond> : IDictionary<TFirst, TSecond>
|
||||
{
|
||||
|
||||
public TFirst this[TSecond key] { get; set; }
|
||||
TFirst this[TSecond key] { get; set; }
|
||||
|
||||
public ICollection<TFirst> FirstIndex { get; }
|
||||
ICollection<TFirst> FirstIndex { get; }
|
||||
|
||||
public ICollection<TSecond> SecondIndex { get; }
|
||||
ICollection<TSecond> SecondIndex { get; }
|
||||
|
||||
public IReadOnlyDictionary<TFirst, TSecond> Forward { get; }
|
||||
IReadOnlyDictionary<TFirst, TSecond> Forward { get; }
|
||||
|
||||
public IReadOnlyDictionary<TSecond, TFirst> Reverse { get; }
|
||||
IReadOnlyDictionary<TSecond, TFirst> Reverse { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />
|
||||
/// contains the specified key.
|
||||
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
|
||||
/// specified key.
|
||||
/// </summary>
|
||||
/// <param _label="key">
|
||||
/// The value to locate in the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an
|
||||
/// element with the specified key; otherwise, false.
|
||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
||||
/// the specified key; otherwise, false.
|
||||
/// </returns>
|
||||
public bool Contains(TSecond key);
|
||||
bool Contains(TSecond key);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />
|
||||
/// contains the specified key.
|
||||
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
|
||||
/// specified key.
|
||||
/// </summary>
|
||||
/// <param _label="key">
|
||||
/// The value to locate in the <see cref="IBidirectionalDictionary{TFirst, TSecond}" />.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an
|
||||
/// element with the specified key; otherwise, false.
|
||||
/// true if the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains an element with
|
||||
/// the specified key; otherwise, false.
|
||||
/// </returns>
|
||||
public bool Contains(TFirst key);
|
||||
bool Contains(TFirst key);
|
||||
|
||||
public bool TryGetValue(TSecond key, out TFirst value);
|
||||
bool TryGetValue(TSecond key, out TFirst value);
|
||||
|
||||
#pragma warning disable CA1033
|
||||
ICollection<TFirst> IDictionary<TFirst, TSecond>.Keys => FirstIndex;
|
||||
|
||||
ICollection<TSecond> IDictionary<TFirst, TSecond>.Values => SecondIndex;
|
||||
|
||||
bool IDictionary<TFirst, TSecond>.ContainsKey(TFirst key) => Contains(key);
|
||||
#pragma warning restore CA1033
|
||||
bool IDictionary<TFirst, TSecond>.ContainsKey(TFirst key)
|
||||
{
|
||||
return Contains(key);
|
||||
}
|
||||
#pragma warning restore CA1033
|
||||
}
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="PartialDictionary"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
{
|
||||
public partial class PartialDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
|
||||
private readonly Dictionary<TKey, TValue> _source;
|
||||
private readonly HashSet<TKey> _keys;
|
||||
private readonly PartialDictionaryValues _values;
|
||||
|
||||
public PartialDictionary(Dictionary<TKey, TValue> source, TKey[] index)
|
||||
{
|
||||
_source = source;
|
||||
_keys = new HashSet<TKey>(index);
|
||||
foreach (TKey item in _keys)
|
||||
{
|
||||
if (!_source.ContainsKey(item)) {
|
||||
_source.Add(item, default!);
|
||||
}
|
||||
}
|
||||
_values = new PartialDictionaryValues(this);
|
||||
}
|
||||
|
||||
public PartialDictionary(Dictionary<TKey, TValue> source, HashSet<TKey> keys)
|
||||
{
|
||||
_source = source;
|
||||
_keys = keys;
|
||||
foreach (TKey item in _keys)
|
||||
{
|
||||
if (!_source.ContainsKey(item)) {
|
||||
_source.Add(item, default!);
|
||||
}
|
||||
}
|
||||
_values = new PartialDictionaryValues(this);
|
||||
}
|
||||
|
||||
public PartialDictionary(Dictionary<TKey, TValue> source, TKey[] index, IHashMultiplexer<TKey> multiplexer)
|
||||
{
|
||||
_source = source;
|
||||
_keys = new HashSet<TKey>(index);
|
||||
foreach (TKey item in _keys)
|
||||
{
|
||||
if (!_source.ContainsKey(item)) {
|
||||
_source.Add(item, default!);
|
||||
}
|
||||
}
|
||||
_values = new PartialDictionaryValues(this);
|
||||
}
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_keys.Contains(key)) {
|
||||
return _source[key];
|
||||
}
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_keys.Contains(key)) {
|
||||
_source[key] = value;
|
||||
}
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys => _keys;
|
||||
|
||||
public ICollection<TValue> Values => _values;
|
||||
|
||||
public int Count => _keys.Count;
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return _keys.Contains(item.Key) && _source.Contains(item);
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
return _keys.Contains(key);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
foreach (TKey item in _keys) {
|
||||
yield return new KeyValuePair<TKey, TValue>(item, _source[item]);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
if (_keys.Contains(key))
|
||||
{
|
||||
value = _source[key];
|
||||
return true;
|
||||
}
|
||||
value = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.Clear()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => true;
|
||||
|
||||
void IDictionary<TKey, TValue>.Add(TKey key, TValue value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool IDictionary<TKey, TValue>.Remove(TKey key)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="PartialDictionaryValues"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
{
|
||||
public partial class PartialDictionary<TKey, TValue>
|
||||
{
|
||||
|
||||
internal sealed class PartialDictionaryValues : ICollection<TValue>
|
||||
{
|
||||
|
||||
private readonly PartialDictionary<TKey, TValue> _values;
|
||||
|
||||
public PartialDictionaryValues(PartialDictionary<TKey, TValue> value)
|
||||
{
|
||||
_values = value;
|
||||
}
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
|
||||
public int Count => _values.Count;
|
||||
|
||||
public bool Contains(TValue item)
|
||||
{
|
||||
foreach (KeyValuePair<TKey, TValue> kvp in _values)
|
||||
{
|
||||
if (kvp.Value != null && kvp.Value.Equals(item)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void CopyTo(TValue[] array, int arrayIndex)
|
||||
{
|
||||
foreach (KeyValuePair<TKey, TValue> kvp in _values)
|
||||
{
|
||||
array[arrayIndex] = kvp.Value;
|
||||
arrayIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<TValue> GetEnumerator()
|
||||
{
|
||||
foreach (TKey key in _values._keys) {
|
||||
yield return _values._source[key];
|
||||
}
|
||||
}
|
||||
|
||||
void ICollection<TValue>.Add(TValue item)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void ICollection<TValue>.Clear()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool ICollection<TValue>.Remove(TValue item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -28,10 +28,6 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
{
|
||||
@@ -176,7 +172,10 @@ namespace StgSharp.Collections
|
||||
Array.Resize(ref _array, newCapacity);
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,8 +102,8 @@ namespace StgSharp.Collections
|
||||
|
||||
public int RemainedMultiplexCount
|
||||
{
|
||||
get => _multiplexers.Value.RemainedMultiplexCount;
|
||||
set => _multiplexers.Value.RemainedMultiplexCount = value;
|
||||
get => _multiplexers.Value!.RemainedMultiplexCount;
|
||||
set => _multiplexers.Value!.RemainedMultiplexCount = value;
|
||||
}
|
||||
|
||||
public void CacheHash(string obj, int count)
|
||||
|
||||
Reference in New Issue
Block a user