daily update on 20250214 by nitload youmu, building syntax analysor, tough!

This commit is contained in:
Nitload
2025-02-14 13:30:17 +08:00
parent c23b640cae
commit ccef0de6f6
68 changed files with 2594 additions and 913 deletions
@@ -52,22 +52,22 @@ namespace StgSharp.Commom.Collections
{
private ConcurrentDictionary<int, T> _cache;
private Func<T> _defaultCreater;
private Func<T> _defaultCreator;
public CacheIndexPool( Func<T> defaultCreater )
public CacheIndexPool( Func<T> defaultCreator )
{
_cache = new ConcurrentDictionary<int, T>();
_defaultCreater = defaultCreater;
_defaultCreator = defaultCreator;
}
public T this[ int index ]
{
get
{
if( _cache.TryGetValue( index, out T value ) ) {
if( _cache.TryGetValue( index, out T? value ) ) {
return value;
}
T newValue = _defaultCreater();
T newValue = _defaultCreator();
_cache.TryAdd( index, newValue );
return newValue;
}
@@ -75,7 +75,7 @@ namespace StgSharp.Commom.Collections
public T CreateObject()
{
T _new = _defaultCreater();
T _new = _defaultCreator();
_cache.AddOrUpdate( 1, _new, static( _, origin ) => origin );
throw new NotImplementedException();
return _new;
@@ -32,11 +32,21 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
namespace StgSharp.Commom.Collections
{
internal class CapacityFixedList<T> : IList<T>
public static class CapacityFixedListBuilder
{
internal static CapacityFixedList<T> Create<T>(ReadOnlySpan<T> values)
{
return new CapacityFixedList<T>(values);
}
}
[CollectionBuilder(typeof(CapacityFixedListBuilder),nameof(CapacityFixedListBuilder.Create))]
public class CapacityFixedList<T> : IList<T>
{
private T[] _values;
@@ -47,6 +57,11 @@ namespace StgSharp.Commom.Collections
_values = new T[size];
}
public CapacityFixedList(ReadOnlySpan<T> values)
{
_values = values.ToArray();
}
public T this[ int index ]
{
get => index < _index ?
@@ -0,0 +1,52 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="IHashMultiplexer.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. 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.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace StgSharp.Collections
{
public interface IHashMultiplexer<T> : IEqualityComparer<T>
{
int RemainedMultiplexCount
{
get;
set;
}
void CacheHash( T obj, int count );
void ExtendCache( int count );
int ForceComputeHash( T count );
}
}
@@ -0,0 +1,90 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace StgSharp.Collections
{
/// <summary>
/// Part of a collection. This contains a direct reference to origin collection,
/// so all change to origin collection will be reflected in this object.
/// </summary>
/// <typeparam name="T"></typeparam>
public struct ListSegment<T> : ICollection<T>
{
private List<T> _collection;
private int _begin;
private int _length;
public ListSegment(List<T> collection, int begin, int length)
{
ArgumentNullException.ThrowIfNull(collection);
ArgumentOutOfRangeException.ThrowIfLessThan(begin, 0);
ArgumentOutOfRangeException.ThrowIfLessThan(length, 0);
ArgumentOutOfRangeException.ThrowIfLessThan(begin + length, collection.Count);
_collection = collection;
_begin = begin;
_length = length;
}
public int Count => _length;
public bool IsReadOnly => true;
public T this[int indexValue]
{
get {
ArgumentOutOfRangeException.ThrowIfLessThan(indexValue, 0);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(indexValue, _length);
return _collection[indexValue + _begin];
}
}
void ICollection<T>.Add(T item)
{
throw new NotSupportedException();
}
void ICollection<T>.Clear()
{
throw new NotSupportedException();
}
public bool Contains(T item)
{
var index= _collection.IndexOf(item);
return index >= _begin && index < _length + index;
}
public void CopyTo(T[] array, int arrayIndex)
{
ArgumentNullException.ThrowIfNull(array);
var span = CollectionsMarshal.AsSpan<T>(_collection).Slice(_begin, _length);
var target = new Span<T>(array, arrayIndex,_length);
span.CopyTo(target);
}
public IEnumerator<T> GetEnumerator()
{
for (int i = _begin; i < _length; i++)
{
yield return this[i];
}
}
bool ICollection<T>.Remove(T item)
{
throw new NotSupportedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
@@ -31,14 +31,15 @@
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;
@@ -71,6 +72,23 @@ namespace StgSharp.Collections
_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
@@ -0,0 +1,165 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="KeyMultiplexer.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. 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.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace StgSharp.Collections
{
public sealed class StringHashMultiplexer : IHashMultiplexer<string>
{
private int _initCount, _remainedCount, _hashCache, _hashLast;
private string _source, _sourceLast;
public int RemainedMultiplexCount
{
get => _remainedCount;
set => _initCount = value;
}
public void CacheHash( string obj, int count )
{
_hashCache = ForceComputeHash( obj );
_initCount = count;
_remainedCount = _initCount;
_source = obj;
}
public bool Equals( string x, string y )
{
return x == y;
}
public void ExtendCache( int count )
{
_initCount += count;
_remainedCount += count;
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public unsafe int ForceComputeHash( [NotNull] string obj )
{
int hash;
fixed( char* cPtr = obj.AsSpan() ) {
hash = InternalIO.Intrinsic
.city_hash_simplify( cPtr, obj.Length );
}
return hash;
}
public int GetHashCode( string obj )
{
if( _remainedCount > 0 && ReferenceEquals( _source, obj ) ) {
_remainedCount--;
return _hashCache;
}
if( ReferenceEquals( _sourceLast, obj ) ) {
return _hashLast;
}
_sourceLast = obj;
_hashLast = ForceComputeHash( obj );
return _hashLast;
}
}
public sealed class ConcurrentStringHashMultiplexer : IHashMultiplexer<string>, IDisposable
{
private bool disposedValue;
private readonly ThreadLocal<StringHashMultiplexer> _multiplexers =
new( () => new StringHashMultiplexer() );
public int RemainedMultiplexCount
{
get => _multiplexers.Value.RemainedMultiplexCount;
set => _multiplexers.Value.RemainedMultiplexCount = value;
}
public void CacheHash( string obj, int count )
{
throw new NotImplementedException();
}
// ~ConcurrentStringHashMultiplexer()
// {
// Dispose(disposing: false);
// }
public void Dispose()
{
Dispose( disposing: true );
GC.SuppressFinalize( this );
}
public bool Equals( string x, string y )
{
return x == y;
}
public void ExtendCache( int count )
{
_multiplexers.Value.ExtendCache( count );
}
public unsafe int ForceComputeHash( [NotNull] string obj )
{
int hash;
fixed( char* cPtr = obj.AsSpan() ) {
hash = InternalIO.Intrinsic
.city_hash_simplify( cPtr, obj.Length );
}
return hash;
}
public int GetHashCode( string obj )
{
return _multiplexers.Value.GetHashCode( obj );
}
private void Dispose( bool disposing )
{
if( !disposedValue ) {
if( disposing ) {
_multiplexers.Dispose();
}
disposedValue = true;
}
}
}
}
@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("StgSharp.Common")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ae94c265ae77870ea6498b624b6f71b78fb94e51")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+c23b640cae59b9ee7dcfd8f026b8970a084ddbd7")]
[assembly: System.Reflection.AssemblyProductAttribute("StgSharp.Common")]
[assembly: System.Reflection.AssemblyTitleAttribute("StgSharp.Common")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/ae94c265ae77870ea6498b624b6f71b78fb94e51/*"}}
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/c23b640cae59b9ee7dcfd8f026b8970a084ddbd7/*"}}