daily update on 20241002 by nitload youmu, intrinsic function rebuild, syntax style arrange

This commit is contained in:
Nitload
2024-10-03 04:03:06 +08:00
parent 0979123ac0
commit 6e2f93b053
265 changed files with 26732 additions and 19058 deletions
@@ -49,53 +49,47 @@ namespace StgSharp.Collections
_reverse = new Dictionary<TSecond, TFirst>();
}
public TFirst this[TSecond key]
public TFirst this[ TSecond key ]
{
get
{
if (_reverse.TryGetValue(key,out TFirst value))
{
if( _reverse.TryGetValue( key, out TFirst value ) ) {
return value;
}
throw new ArgumentException($"Cannot find key {key!.ToString()} in dictionary.");
throw new ArgumentException(
$"Cannot find key {key!.ToString()} in dictionary." );
}
set
{
if (_reverse.TryGetValue(key,out TFirst origin))
{
_reverse[key] = value;
_forward.Remove(origin);
if( _reverse.TryGetValue( key, out TFirst origin ) ) {
_reverse[ key ] = value;
_forward.Remove( origin );
} else {
_reverse.Add( key, value );
}
else
{
_reverse.Add(key, value);
}
_forward.Add(value, key);
_forward.Add( value, key );
}
}
public TSecond this[TFirst key]
public TSecond this[ TFirst key ]
{
get
{
if (_forward.TryGetValue(key, out TSecond value))
{
if( _forward.TryGetValue( key, out TSecond value ) ) {
return value;
}
throw new ArgumentException($"Cannot find key {key!.ToString()} in dictionary.");
throw new ArgumentException(
$"Cannot find key {key!.ToString()} in dictionary." );
}
set
{
if (_forward.TryGetValue(key, out TSecond origin))
{
_forward[key] = value;
_reverse.Remove(origin);
if( _forward.TryGetValue( key, out TSecond origin ) ) {
_forward[ key ] = value;
_reverse.Remove( origin );
} else {
_forward.Add( key, value );
}
else
{
_forward.Add(key, value);
}
_reverse.Add(value, key);
_reverse.Add( value, key );
}
}
@@ -107,28 +101,29 @@ namespace StgSharp.Collections
public int Count => _forward.Count;
public IReadOnlyDictionary<TFirst, TSecond> Forward => new ReadOnlyDictionary<TFirst,TSecond>(_forward);
public IReadOnlyDictionary<TFirst, TSecond> Forward => new ReadOnlyDictionary<TFirst, TSecond>(
_forward );
public IReadOnlyDictionary<TSecond, TFirst> Reverse => new ReadOnlyDictionary<TSecond,TFirst>(_reverse);
public IReadOnlyDictionary<TSecond, TFirst> Reverse => new ReadOnlyDictionary<TSecond, TFirst>(
_reverse );
public void Add( KeyValuePair<TFirst, TSecond> item )
{
if (_forward.ContainsKey(item.Key) || _reverse.ContainsKey(item.Value))
{
throw new ArgumentException("Duplicate key or value.");
if( _forward.ContainsKey( item.Key ) || _reverse.ContainsKey(
item.Value ) ) {
throw new ArgumentException( "Duplicate key or value." );
}
_forward.Add(item.Key,item.Value);
_reverse.Add(item.Value, item.Key);
_forward.Add( item.Key, item.Value );
_reverse.Add( item.Value, item.Key );
}
public void Add( TFirst key, TSecond value )
public void Add( TFirst key, TSecond value )
{
if (_forward.ContainsKey(key) || _reverse.ContainsKey(value))
{
throw new ArgumentException("Duplicate key or value.");
if( _forward.ContainsKey( key ) || _reverse.ContainsKey( value ) ) {
throw new ArgumentException( "Duplicate key or value." );
}
_forward.Add(key, value);
_reverse.Add(value, key);
_forward.Add( key, value );
_reverse.Add( value, key );
}
public void Clear()
@@ -139,75 +134,74 @@ namespace StgSharp.Collections
public bool Contains( KeyValuePair<TFirst, TSecond> item )
{
return _forward.Contains(item) && _reverse.TryGetValue(item.Value, out TFirst var) && var!.Equals(item.Key) ;
return _forward.Contains( item ) && _reverse.TryGetValue(
item.Value, out TFirst var ) && var!.Equals( item.Key );
}
public bool Contains(TSecond key)
public bool Contains( TSecond key )
{
return _reverse.ContainsKey(key);
return _reverse.ContainsKey( key );
}
public bool Contains(TFirst key)
public bool Contains( TFirst key )
{
return _forward.ContainsKey(key);
return _forward.ContainsKey( key );
}
public void CopyTo( KeyValuePair<TFirst, TSecond>[] array, int arrayIndex )
public void CopyTo(
KeyValuePair<TFirst, TSecond>[] array,
int arrayIndex )
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
if( array == null ) {
throw new ArgumentNullException( nameof( array ) );
}
if (arrayIndex < 0 || arrayIndex > array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if( arrayIndex < 0 || arrayIndex > array.Length ) {
throw new ArgumentOutOfRangeException( nameof( arrayIndex ) );
}
if (array.Length - arrayIndex < _forward.Count)
{
throw new ArgumentException("The array is too small to copy the elements.");
if( array.Length - arrayIndex < _forward.Count ) {
throw new ArgumentException(
"The array is too small to copy the elements." );
}
foreach (var pair in _forward)
{
array[arrayIndex ++] = pair;
foreach( KeyValuePair<TFirst, TSecond> pair in _forward ) {
array[ arrayIndex++ ] = pair;
}
}
public IEnumerator<KeyValuePair<TFirst, TSecond>> GetEnumerator()
{
foreach (var pair in _forward)
{
foreach( KeyValuePair<TFirst, TSecond> pair in _forward ) {
yield return pair;
}
}
public bool Remove( TFirst key ) { throw new NotImplementedException(); }
public bool Remove( TFirst key )
{
throw new NotImplementedException();
}
public bool Remove( KeyValuePair<TFirst, TSecond> item )
{
if (_forward.Remove(item.Key))
{
return _reverse.Remove(item.Value);
if( _forward.Remove( item.Key ) ) {
return _reverse.Remove( item.Value );
}
return false;
}
public bool TryGetValue( TFirst key, out TSecond value )
{
return _forward.TryGetValue(key, out value);
return _forward.TryGetValue( key, out value );
}
public bool TryGetValue(TSecond key, out TFirst value)
public bool TryGetValue( TSecond key, out TFirst value )
{
return _reverse.TryGetValue(key, out value);
return _reverse.TryGetValue( key, out value );
}
IEnumerator IEnumerable.GetEnumerator()
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
@@ -1,3 +1,33 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="IBidrirectionalDictionary.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;
using System.Collections.Generic;
@@ -5,46 +35,66 @@ using System.Threading.Tasks;
namespace StgSharp.Collections
{
public interface IBidirectionalDictionary<TFirst, TSecond> :
IDictionary<TFirst, TSecond>
public interface IBidirectionalDictionary<TFirst, TSecond> : IDictionary<TFirst, TSecond>
{
#pragma warning disable CA1033
public TFirst this[ TSecond key ]
{
get;
set;
}
public ICollection<TFirst> FirstIndex
{
get;
}
public ICollection<TSecond> SecondIndex
{
get;
}
public IReadOnlyDictionary<TFirst, TSecond> Forward
{
get;
}
public IReadOnlyDictionary<TSecond, TFirst> Reverse
{
get;
}
/// <summary>
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains
/// the specified key.
/// </summary>
/// <param name="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.
/// </returns>
public bool Contains( TSecond key );
/// <summary>
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains
/// the specified key.
/// </summary>
/// <param name="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.
/// </returns>
public bool Contains( TFirst key );
public 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);
bool IDictionary<TFirst, TSecond>.ContainsKey( TFirst key ) => Contains(
key );
#pragma warning restore CA1033
public IReadOnlyDictionary<TFirst, TSecond> Forward { get; }
public IReadOnlyDictionary<TSecond, TFirst> Reverse { get; }
public ICollection<TFirst> FirstIndex { get;}
public ICollection<TSecond> SecondIndex { get; }
public TFirst this[TSecond key] { get;set; }
/// <summary>
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
/// specified key.
/// </summary>
/// <param name="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.
/// </returns>
public bool Contains(TSecond key);
/// <summary>
/// Determines whether the <see cref="IBidirectionalDictionary{TFirst, TSecond}" /> contains the
/// specified key.
/// </summary>
/// <param name="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.
/// </returns>
public bool Contains(TFirst key);
public bool TryGetValue(TSecond key, out TFirst value);
}
}
@@ -42,23 +42,22 @@ namespace StgSharp.Collections
private T[] values;
private int capacity, _index;
public SizeFixedList(int size)
public SizeFixedList( int size )
{
values = new T[size];
}
public T this[int index]
public T this[ int index ]
{
get => (index < _index) ? values[index] : throw new ArgumentOutOfRangeException(nameof(index));
get => ( index < _index ) ?
values[ index ] : throw new ArgumentOutOfRangeException(
nameof( index ) );
set
{
if (index < _index)
{
values[index] = value;
}
else
{
throw new ArgumentOutOfRangeException(nameof(index));
if( index < _index ) {
values[ index ] = value;
} else {
throw new ArgumentOutOfRangeException( nameof( index ) );
}
}
}
@@ -67,12 +66,11 @@ namespace StgSharp.Collections
public int Count => _index + 1;
public void Add(T item)
public void Add( T item )
{
if (_index < capacity - 1)
{
if( _index < capacity - 1 ) {
_index++;
this[_index] = item;
this[ _index ] = item;
}
}
@@ -82,56 +80,43 @@ namespace StgSharp.Collections
_index = 0;
}
public bool Contains(T item)
public bool Contains( T item )
{
for (int i = 0; i <= _index; i++)
{
if (item.Equals(values[i]))
{
for( int i = 0; i <= _index; i++ ) {
if( item.Equals( values[ i ] ) ) {
return true;
}
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
public void CopyTo( T[] array, int arrayIndex )
{
throw new NotImplementedException();
}
public IEnumerator<T> GetEnumerator()
{
for (int i = 0; i <= _index; i++)
{
yield return values[i];
for( int i = 0; i <= _index; i++ ) {
yield return values[ i ];
}
}
public int IndexOf(T item)
public int IndexOf( T item )
{
for (int i = 0; i <= _index; i++)
{
if (values[i].Equals(item))
{
for( int i = 0; i <= _index; i++ ) {
if( values[ i ].Equals( item ) ) {
return i;
}
}
return -1;
}
void IList<T>.Insert(int index, T item)
public bool Remove( T item )
{
throw new NotSupportedException();
}
public bool Remove(T item)
{
for (int i = 0; i <= _index; i++)
{
if (values[i].Equals(item))
{
values[i] = values[_index];
for( int i = 0; i <= _index; i++ ) {
if( values[ i ].Equals( item ) ) {
values[ i ] = values[ _index ];
_index--;
return true;
}
@@ -139,30 +124,27 @@ namespace StgSharp.Collections
return false;
}
public void RemoveAll(Predicate<T> predicate)
public void RemoveAll( Predicate<T> predicate )
{
for (int i = _index; i > 0; i--)
{
if (predicate(values[i]))
{
values[i] = values[_index];
for( int i = _index; i > 0; i-- ) {
if( predicate( values[ i ] ) ) {
values[ i ] = values[ _index ];
_index--;
}
}
}
public void RemoveAt(int index)
public void RemoveAt( int index )
{
if (index <= _index)
{
values[index] = values[_index];
if( index <= _index ) {
values[ index ] = values[ _index ];
_index--;
}
}
public void Resize(int newSize)
public void Resize( int newSize )
{
Array.Resize(ref values, newSize);
Array.Resize( ref values, newSize );
}
IEnumerator IEnumerable.GetEnumerator()
@@ -170,5 +152,10 @@ namespace StgSharp.Collections
yield return GetEnumerator();
}
void IList<T>.Insert( int index, T item )
{
throw new NotSupportedException();
}
}//------------------------------------- End of Class ------------------------------------------
}