new manage of wraps

This commit is contained in:
Youmu Liu
2025-10-15 20:29:02 +08:00
parent e283490525
commit d3e0b5d005
7 changed files with 398 additions and 202 deletions
@@ -1,33 +1,30 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="CapacityFixedList.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// -----------------------------------------------------------------------
// file="CapacityFixedList"
// 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:
// 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.
//
// 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;
@@ -40,44 +37,41 @@ namespace StgSharp.Commom.Collections
public static class CapacityFixedListBuilder
{
internal static CapacityFixedList<T> Create<T>( ReadOnlySpan<T> values )
internal static CapacityFixedList<T> Create<T>(ReadOnlySpan<T> values)
{
return new CapacityFixedList<T>( values );
return new CapacityFixedList<T>(values);
}
}
[CollectionBuilder(
typeof( CapacityFixedListBuilder ),
nameof( CapacityFixedListBuilder.Create ) )]
[CollectionBuilder(typeof(CapacityFixedListBuilder), nameof(CapacityFixedListBuilder.Create))]
public class CapacityFixedList<T> : IList<T>
{
private T[] _values;
private int capacity, _index;
public CapacityFixedList( int size )
public CapacityFixedList(int size)
{
_values = new T[size];
}
public CapacityFixedList( ReadOnlySpan<T> values )
public CapacityFixedList(ReadOnlySpan<T> values)
{
_values = values.ToArray();
}
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 )
if (index < _index)
{
_values[ index ] = value;
_values[index] = value;
} else
{
throw new ArgumentOutOfRangeException( nameof( index ) );
throw new ArgumentOutOfRangeException(nameof(index));
}
}
}
@@ -86,12 +80,12 @@ namespace StgSharp.Commom.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;
}
}
@@ -101,49 +95,49 @@ namespace StgSharp.Commom.Collections
_index = 0;
}
public bool Contains( T item )
public bool Contains(T item)
{
for( int i = 0; i <= _index; i++ )
for (int i = 0; i <= _index; i++)
{
if( item.Equals( _values[ i ] ) ) {
if (item.Equals(_values[i])) {
return true;
}
}
return false;
}
public void CopyTo( T[] array, int arrayIndex )
public void CopyTo(T[] array, int arrayIndex)
{
_values.CopyTo( array, arrayIndex );
_values.CopyTo(array, arrayIndex);
}
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)
{
T v;
for( int i = 0; i <= _index; i++ )
for (int i = 0; i <= _index; i++)
{
if( ( v = _values[ i ] ) != null && v.Equals( item ) ) {
if ((v = _values[i]) != null && v.Equals(item)) {
return i;
}
}
return -1;
}
public bool Remove( T item )
public bool Remove(T item)
{
T v;
for( int i = 0; i <= _index; i++ )
for (int i = 0; i <= _index; i++)
{
if( ( v = _values[ i ] ) != null && v.Equals( item ) )
if ((v = _values[i]) != null && v.Equals(item))
{
_values[ i ] = _values[ _index ];
_values[i] = _values[_index];
_index--;
return true;
}
@@ -151,30 +145,30 @@ namespace StgSharp.Commom.Collections
return false;
}
public void RemoveAll( Predicate<T> predicate )
public void RemoveAll(Predicate<T> predicate)
{
for( int i = _index; i > 0; i-- )
for (int i = _index; i > 0; i--)
{
if( predicate( _values[ i ] ) )
if (predicate(_values[i]))
{
_values[ i ] = _values[ _index ];
_values[i] = _values[_index];
_index--;
}
}
}
public void RemoveAt( int index )
public void RemoveAt(int index)
{
if( index <= _index )
if (index <= _index)
{
_values[ index ] = _values[ _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()
@@ -182,7 +176,7 @@ namespace StgSharp.Commom.Collections
yield return GetEnumerator();
}
void IList<T>.Insert( int index, T item )
void IList<T>.Insert(int index, T item)
{
throw new NotSupportedException();
}