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" // file="CapacityFixedList"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person obtaining a copy
// obtaining a copy of this software and associated documentation // of this software and associated documentation files (the "Software"), to deal
// files (the “Software”), to deal in the Software without restriction, // in the Software without restriction, including without limitation the rights
// including without limitation the rights to use, copy, modify, merge, // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// publish, distribute, sublicense, and/or sell copies of the Software, // copies of the Software, and to permit persons to whom the Software is
// and to permit persons to whom the Software is furnished to do so, // furnished to do so, subject to the following conditions:
// 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;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@@ -40,44 +37,41 @@ namespace StgSharp.Commom.Collections
public static class CapacityFixedListBuilder 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( [CollectionBuilder(typeof(CapacityFixedListBuilder), nameof(CapacityFixedListBuilder.Create))]
typeof( CapacityFixedListBuilder ),
nameof( CapacityFixedListBuilder.Create ) )]
public class CapacityFixedList<T> : IList<T> public class CapacityFixedList<T> : IList<T>
{ {
private T[] _values; private T[] _values;
private int capacity, _index; private int capacity, _index;
public CapacityFixedList( int size ) public CapacityFixedList(int size)
{ {
_values = new T[size]; _values = new T[size];
} }
public CapacityFixedList( ReadOnlySpan<T> values ) public CapacityFixedList(ReadOnlySpan<T> values)
{ {
_values = values.ToArray(); _values = values.ToArray();
} }
public T this[ int index ] public T this[int index]
{ {
get => index < _index ? get => index < _index ? _values[index] : throw new ArgumentOutOfRangeException(nameof(index));
_values[ index ] : throw new ArgumentOutOfRangeException( nameof( index ) );
set set
{ {
if( index < _index ) if (index < _index)
{ {
_values[ index ] = value; _values[index] = value;
} else } 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 int Count => _index + 1;
public void Add( T item ) public void Add(T item)
{ {
if( _index < capacity - 1 ) if (_index < capacity - 1)
{ {
_index++; _index++;
this[ _index ] = item; this[_index] = item;
} }
} }
@@ -101,49 +95,49 @@ namespace StgSharp.Commom.Collections
_index = 0; _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 true;
} }
} }
return false; 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() public IEnumerator<T> GetEnumerator()
{ {
for( int i = 0; i <= _index; i++ ) { for (int i = 0; i <= _index; i++) {
yield return _values[ i ]; yield return _values[i];
} }
} }
public int IndexOf( T item ) public int IndexOf(T item)
{ {
T v; 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 i;
} }
} }
return -1; return -1;
} }
public bool Remove( T item ) public bool Remove(T item)
{ {
T v; 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--; _index--;
return true; return true;
} }
@@ -151,30 +145,30 @@ namespace StgSharp.Commom.Collections
return false; 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--; _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--; _index--;
} }
} }
public void Resize( int newSize ) public void Resize(int newSize)
{ {
Array.Resize( ref _values, newSize ); Array.Resize(ref _values, newSize);
} }
IEnumerator IEnumerable.GetEnumerator() IEnumerator IEnumerable.GetEnumerator()
@@ -182,7 +176,7 @@ namespace StgSharp.Commom.Collections
yield return GetEnumerator(); yield return GetEnumerator();
} }
void IList<T>.Insert( int index, T item ) void IList<T>.Insert(int index, T item)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
@@ -0,0 +1,143 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="CapacityFixedStack"
// 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.Linq;
using System.Runtime.CompilerServices;
namespace StgSharp.Commom.Collections
{
[CollectionBuilder(typeof(CapacityFixedStackBuilder), nameof(CapacityFixedStackBuilder.Create))]
public class CapacityFixedStack<T> : IEnumerable<T>
{
private readonly T[] _values;
private int _index;
public CapacityFixedStack(int size)
{
_values = new T[size];
_index = -1;
}
public bool IsEmpty => _index == -1;
public int Count => _index + 1;
public int Capacity => _values.Length;
public ReadOnlySpan<T> AsSpan()
{
return _values.AsSpan(0, _index + 1);
}
public void Clear()
{
Array.Clear(_values, 0, _values.Length);
_index = -1;
}
public IEnumerator<T> GetEnumerator()
{
for (int i = _index; i >= 0; i--) {
yield return _values[i];
}
}
public T Peek()
{
if (_index >= 0)
{
return _values[_index];
} else
{
throw new InvalidOperationException("Stack is empty.");
}
}
public T Pop()
{
if (_index >= 0)
{
T item = _values[_index];
return item;
} else
{
throw new InvalidOperationException("Stack is empty.");
}
}
public void Push(T item)
{
if (_index < _values.Length - 1)
{
_values[++_index] = item;
} else
{
throw new InvalidOperationException("Stack is full.");
}
}
public void PushRange(ReadOnlySpan<T> span)
{
if (_index + span.Length >= _values.Length) {
throw new InvalidOperationException("Not enough space in the stack to push the entire span.");
}
Span<T> sp = _values.AsSpan(_index + 1, span.Length);
span.CopyTo(sp);
_index += span.Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Span<T> GetValueLayout()
{
return _values.AsSpan();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public static class CapacityFixedStackBuilder
{
public static CapacityFixedStack<T> Create<T>(ReadOnlySpan<T> values)
{
CapacityFixedStack<T> stack = new(values.Length);
Span<T> sp = stack.GetValueLayout();
values.CopyTo(sp);
return stack;
}
}
}
@@ -1,33 +1,30 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
//----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="PartialDictionaryValues.cs" // file="PartialDictionaryValues"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person obtaining a copy
// obtaining a copy of this software and associated documentation // of this software and associated documentation files (the "Software"), to deal
// files (the “Software”), to deal in the Software without restriction, // in the Software without restriction, including without limitation the rights
// including without limitation the rights to use, copy, modify, merge, // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// publish, distribute, sublicense, and/or sell copies of the Software, // copies of the Software, and to permit persons to whom the Software is
// and to permit persons to whom the Software is furnished to do so, // furnished to do so, subject to the following conditions:
// 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;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@@ -43,7 +40,7 @@ namespace StgSharp.Collections
private readonly PartialDictionary<TKey, TValue> _values; private readonly PartialDictionary<TKey, TValue> _values;
public PartialDictionaryValues( PartialDictionary<TKey, TValue> value ) public PartialDictionaryValues(PartialDictionary<TKey, TValue> value)
{ {
_values = value; _values = value;
} }
@@ -52,34 +49,34 @@ namespace StgSharp.Collections
public int Count => _values.Count; public int Count => _values.Count;
public bool Contains( TValue item ) public bool Contains(TValue item)
{ {
foreach( KeyValuePair<TKey, TValue> kvp in _values ) foreach (KeyValuePair<TKey, TValue> kvp in _values)
{ {
if( kvp.Value != null && kvp.Value.Equals( item ) ) { if (kvp.Value != null && kvp.Value.Equals(item)) {
return true; return true;
} }
} }
return false; return false;
} }
public void CopyTo( TValue[] array, int arrayIndex ) public void CopyTo(TValue[] array, int arrayIndex)
{ {
foreach( KeyValuePair<TKey, TValue> kvp in _values ) foreach (KeyValuePair<TKey, TValue> kvp in _values)
{ {
array[ arrayIndex ] = kvp.Value; array[arrayIndex] = kvp.Value;
arrayIndex++; arrayIndex++;
} }
} }
public IEnumerator<TValue> GetEnumerator() public IEnumerator<TValue> GetEnumerator()
{ {
foreach( TKey key in _values._keys ) { foreach (TKey key in _values._keys) {
yield return _values._source[ key ]; yield return _values._source[key];
} }
} }
void ICollection<TValue>.Add( TValue item ) void ICollection<TValue>.Add(TValue item)
{ {
return; return;
} }
@@ -89,7 +86,7 @@ namespace StgSharp.Collections
return; return;
} }
bool ICollection<TValue>.Remove( TValue item ) bool ICollection<TValue>.Remove(TValue item)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
+23 -24
View File
@@ -1,30 +1,27 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="Vec4.cs" // file="Vec4"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person obtaining a copy
// obtaining a copy of this software and associated documentation // of this software and associated documentation files (the "Software"), to deal
// files (the “Software”), to deal in the Software without restriction, // in the Software without restriction, including without limitation the rights
// including without limitation the rights to use, copy, modify, merge, // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// publish, distribute, sublicense, and/or sell copies of the Software, // copies of the Software, and to permit persons to whom the Software is
// and to permit persons to whom the Software is furnished to do so, // furnished to do so, subject to the following conditions:
// subject to the following conditions: //
// // The above copyright notice and this permission notice shall be included in all
// The above copyright notice and // copies or substantial portions of the Software.
// 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,
// THE SOFTWARE IS PROVIDED “AS IS”, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // SOFTWARE.
// 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.
// //
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@@ -114,11 +111,13 @@ namespace StgSharp.Mathematics.Graphic
public static Vec4 One public static Vec4 One
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(1, 1, 1, 1); get => new Vec4(1, 1, 1, 1);
} }
public static Vec4 Zero public static Vec4 Zero
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new Vec4(0, 0, 0, 0); get => new Vec4(0, 0, 0, 0);
} }
@@ -130,7 +129,7 @@ namespace StgSharp.Mathematics.Graphic
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return obj is Vec4 v ? v == this : false; return obj is Vec4 v && v == this;
} }
public override int GetHashCode() public override int GetHashCode()
+31
View File
@@ -0,0 +1,31 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="AssemblyInfo"
// 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.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("StgSharpDebug")]
@@ -1,33 +1,31 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallel.Publish.cs" // file="MatrixParallel.Publish"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person obtaining a copy
// obtaining a copy of this software and associated documentation // of this software and associated documentation files (the "Software"), to deal
// files (the “Software”), to deal in the Software without restriction, // in the Software without restriction, including without limitation the rights
// including without limitation the rights to use, copy, modify, merge, // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// publish, distribute, sublicense, and/or sell copies of the Software, // copies of the Software, and to permit persons to whom the Software is
// and to permit persons to whom the Software is furnished to do so, // furnished to do so, subject to the following conditions:
// subject to the following conditions: //
// // The above copyright notice and this permission notice shall be included in all
// The above copyright notice and // copies or substantial portions of the Software.
// 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,
// THE SOFTWARE IS PROVIDED “AS IS”, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // SOFTWARE.
// 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 StgSharp.Commom.Collections;
using StgSharp.HighPerformance; using StgSharp.HighPerformance;
using System; using System;
@@ -822,11 +820,24 @@ namespace StgSharp.Mathematics.Numeric
int tileWidth = (1024 / secCount) + 1; int tileWidth = (1024 / secCount) + 1;
int tileCount = (primCount / tileWidth) + 1; int tileCount = (primCount / tileWidth) + 1;
int tilePerThreadLess = tileCount / Wraps.Length; CapacityFixedStack<MatrixParallelWrap> wraps = LeadParallel();
int tileToSlice = tileCount % Wraps.Length;
// TODO 这个方法依然是独占,需要一个新的缓存类型用来辅助复用空线程束 int tilePerThreadLess = tileCount / Wraps.Count;
for (int i = 0; i < Wraps.Length; i++) { } int tileToSlice = tileCount % Wraps.Count;
int slicingPerWrap = tileToSlice / Wraps.Count;
int extraTiles = tileToSlice % Wraps.Count;
int total = tileCount;
List<MatrixParallelWrap> w = new List<MatrixParallelWrap>(Wraps.Count);
for (int i = 0; i < Wraps.Count; i++)
{
MatrixParallelWrap wrap = Wraps.Pop();
wrap.WrapTask = (MatrixParallelTaskPackageNonGeneric*)package;
wrap.WrapTaskCapacity = ;
wrap.WrapTaskSliceCount = ;
w.Add(wrap);
}
} }
} }
+67 -46
View File
@@ -1,33 +1,31 @@
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// file="MatrixParallel.cs" // file="MatrixParallel"
// Project: StgSharp // Project: StgSharp
// AuthorGroup: Nitload Space // AuthorGroup: Nitload
// Copyright (c) Nitload Space. All rights reserved. // Copyright (c) Nitload. All rights reserved.
// //
// Permission is hereby granted, free of charge, to any person // Permission is hereby granted, free of charge, to any person obtaining a copy
// obtaining a copy of this software and associated documentation // of this software and associated documentation files (the "Software"), to deal
// files (the “Software”), to deal in the Software without restriction, // in the Software without restriction, including without limitation the rights
// including without limitation the rights to use, copy, modify, merge, // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// publish, distribute, sublicense, and/or sell copies of the Software, // copies of the Software, and to permit persons to whom the Software is
// and to permit persons to whom the Software is furnished to do so, // furnished to do so, subject to the following conditions:
// subject to the following conditions: //
// // The above copyright notice and this permission notice shall be included in all
// The above copyright notice and // copies or substantial portions of the Software.
// 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,
// THE SOFTWARE IS PROVIDED “AS IS”, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // SOFTWARE.
// 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 StgSharp.Commom.Collections;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
@@ -130,13 +128,13 @@ namespace StgSharp.Mathematics.Numeric
{ {
Wraps = [ new MatrixParallelWrap(0, count, true) ]; Wraps = [ new MatrixParallelWrap(0, count, true) ];
(LargeWrapCount, SmallWrapCount) = (0, 1); (LargeWrapCount, SmallWrapCount) = (0, 1);
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps.Peek());
} else } else
{ {
count = 4; count = 4;
Wraps = [ new MatrixParallelWrap(0, count, true) ]; Wraps = [ new MatrixParallelWrap(0, count, true) ];
(LargeWrapCount, SmallWrapCount) = (1, 0); (LargeWrapCount, SmallWrapCount) = (1, 0);
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps.Peek());
} }
} else if (count == 6) } else if (count == 6)
{ {
@@ -145,19 +143,19 @@ namespace StgSharp.Mathematics.Numeric
new MatrixParallelWrap(3, 3, false) new MatrixParallelWrap(3, 3, false)
]; ];
(LargeWrapCount, SmallWrapCount) = (0, 2); (LargeWrapCount, SmallWrapCount) = (0, 2);
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps.Peek());
} else } else
{ {
(int _3wrap, int _4wrap) = FastDecompose(count); (int _3wrap, int _4wrap) = FastDecompose(count);
Wraps = new MatrixParallelWrap[_4wrap + _3wrap]; Wraps = new(_4wrap + _3wrap);
int id = 0,idx = 0; int id = 0, idx = 0;
for (; idx < _3wrap; idx++, id += 3) { for (; idx < _3wrap; idx++, id += 3) {
Wraps[idx] = new MatrixParallelWrap(idx, 3, id == 0); Wraps.Push(new MatrixParallelWrap(idx, 3, id == 0));
} }
for (; idx < _3wrap + _4wrap; idx++, id += 4) { for (; idx < _3wrap + _4wrap; idx++, id += 4) {
Wraps[idx] = new MatrixParallelWrap(id, 4, id == 0); Wraps.Push(new MatrixParallelWrap(id, 4, id == 0));
} }
LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps.Peek());
} }
LeaderTask = LeaderHandle.Wrap.GetWorkerTaskQueue(0); LeaderTask = LeaderHandle.Wrap.GetWorkerTaskQueue(0);
IsInitialized = true; IsInitialized = true;
@@ -170,12 +168,6 @@ namespace StgSharp.Mathematics.Numeric
SetAllWrap(wraps); SetAllWrap(wraps);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void LeadParallel()
{
Monitor.Enter(_lock);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void MatrixParallelLeaderWork() public static unsafe void MatrixParallelLeaderWork()
{ {
@@ -271,6 +263,17 @@ namespace StgSharp.Mathematics.Numeric
public record class Handle(int InWrapIndex, MatrixParallelWrap Wrap); public record class Handle(int InWrapIndex, MatrixParallelWrap Wrap);
#region wrap task
internal MatrixParallelTaskPackageNonGeneric* WrapTask { get; set; }
internal int WrapTaskSliceCount { get; set; }
internal int WrapTaskCapacity { get; set; }
#endregion
#region ThreadOperation #region ThreadOperation
private static unsafe void MatrixParallelWorker([AllowNull] object obj) private static unsafe void MatrixParallelWorker([AllowNull] object obj)
@@ -342,21 +345,39 @@ namespace StgSharp.Mathematics.Numeric
#endregion #endregion
} }
public enum SleepMode
{
QuickWakeup = ThreadPriority.AboveNormal,
NormalSleep = ThreadPriority.BelowNormal,
DeepSleep = ThreadPriority.Lowest,
}
#region wrap field #region wrap field
private static MatrixParallelWrap[] Wraps; private static CapacityFixedStack<MatrixParallelWrap> Wraps;
private static int LargeWrapCount; private static int LargeWrapCount;
private static int SmallWrapCount; private static int SmallWrapCount;
#endregion public static CapacityFixedStack<MatrixParallelWrap> LeadParallel()
} {
#if NET9_0_OR_GREATER
#else
Monitor.Enter(_lock);
#endif
return Wraps;
}
public enum SleepMode public static void ReleaseParallelLeadership(ref CapacityFixedStack<MatrixParallelWrap> wraps)
{ {
#if NET9_0_OR_GREATER
QuickWakeup = ThreadPriority.AboveNormal, #else
NormalSleep = ThreadPriority.BelowNormal, Monitor.Exit(_lock);
DeepSleep = ThreadPriority.Lowest, #endif
wraps = null!;
}
#endregion
} }
} }