diff --git a/StgSharp.Common/Collections/CapacityFixedList.cs b/StgSharp.Common/Collections/CapacityFixedList.cs index bfc0a732..fb9dcd9f 100644 --- a/StgSharp.Common/Collections/CapacityFixedList.cs +++ b/StgSharp.Common/Collections/CapacityFixedList.cs @@ -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 Create( ReadOnlySpan values ) + internal static CapacityFixedList Create(ReadOnlySpan values) { - return new CapacityFixedList( values ); + return new CapacityFixedList(values); } } - [CollectionBuilder( - typeof( CapacityFixedListBuilder ), - nameof( CapacityFixedListBuilder.Create ) )] + [CollectionBuilder(typeof(CapacityFixedListBuilder), nameof(CapacityFixedListBuilder.Create))] public class CapacityFixedList : IList { private T[] _values; private int capacity, _index; - public CapacityFixedList( int size ) + public CapacityFixedList(int size) { _values = new T[size]; } - public CapacityFixedList( ReadOnlySpan values ) + public CapacityFixedList(ReadOnlySpan 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 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 predicate ) + public void RemoveAll(Predicate 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.Insert( int index, T item ) + void IList.Insert(int index, T item) { throw new NotSupportedException(); } diff --git a/StgSharp.Common/Collections/CapacityFixedStack.cs b/StgSharp.Common/Collections/CapacityFixedStack.cs new file mode 100644 index 00000000..e6c8ddc9 --- /dev/null +++ b/StgSharp.Common/Collections/CapacityFixedStack.cs @@ -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 : IEnumerable + { + + 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 AsSpan() + { + return _values.AsSpan(0, _index + 1); + } + + public void Clear() + { + Array.Clear(_values, 0, _values.Length); + _index = -1; + } + + public IEnumerator 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 span) + { + if (_index + span.Length >= _values.Length) { + throw new InvalidOperationException("Not enough space in the stack to push the entire span."); + } + + Span sp = _values.AsSpan(_index + 1, span.Length); + span.CopyTo(sp); + _index += span.Length; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal Span GetValueLayout() + { + return _values.AsSpan(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + } + + public static class CapacityFixedStackBuilder + { + + public static CapacityFixedStack Create(ReadOnlySpan values) + { + CapacityFixedStack stack = new(values.Length); + Span sp = stack.GetValueLayout(); + values.CopyTo(sp); + return stack; + } + + } +} \ No newline at end of file diff --git a/StgSharp.Common/Collections/PartialDictionaryValues.cs b/StgSharp.Common/Collections/PartialDictionaryValues.cs index 322225fb..d516b8f1 100644 --- a/StgSharp.Common/Collections/PartialDictionaryValues.cs +++ b/StgSharp.Common/Collections/PartialDictionaryValues.cs @@ -1,33 +1,30 @@ //----------------------------------------------------------------------- -//----------------------------------------------------------------------- -// file="PartialDictionaryValues.cs" -// Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// ----------------------------------------------------------------------- +// 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: +// 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; @@ -43,7 +40,7 @@ namespace StgSharp.Collections private readonly PartialDictionary _values; - public PartialDictionaryValues( PartialDictionary value ) + public PartialDictionaryValues(PartialDictionary value) { _values = value; } @@ -52,34 +49,34 @@ namespace StgSharp.Collections public int Count => _values.Count; - public bool Contains( TValue item ) + public bool Contains(TValue item) { - foreach( KeyValuePair kvp in _values ) + foreach (KeyValuePair kvp in _values) { - if( kvp.Value != null && kvp.Value.Equals( item ) ) { + if (kvp.Value != null && kvp.Value.Equals(item)) { return true; } } return false; } - public void CopyTo( TValue[] array, int arrayIndex ) + public void CopyTo(TValue[] array, int arrayIndex) { - foreach( KeyValuePair kvp in _values ) + foreach (KeyValuePair kvp in _values) { - array[ arrayIndex ] = kvp.Value; + array[arrayIndex] = kvp.Value; arrayIndex++; } } public IEnumerator GetEnumerator() { - foreach( TKey key in _values._keys ) { - yield return _values._source[ key ]; + foreach (TKey key in _values._keys) { + yield return _values._source[key]; } } - void ICollection.Add( TValue item ) + void ICollection.Add(TValue item) { return; } @@ -89,7 +86,7 @@ namespace StgSharp.Collections return; } - bool ICollection.Remove( TValue item ) + bool ICollection.Remove(TValue item) { throw new NotSupportedException(); } diff --git a/StgSharp.Common/Mathematics/Graphic/Vec4.cs b/StgSharp.Common/Mathematics/Graphic/Vec4.cs index 1d02900d..b4186d61 100644 --- a/StgSharp.Common/Mathematics/Graphic/Vec4.cs +++ b/StgSharp.Common/Mathematics/Graphic/Vec4.cs @@ -1,30 +1,27 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="Vec4.cs" +// file="Vec4" // Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// 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. +// 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. // // ----------------------------------------------------------------------- // ----------------------------------------------------------------------- @@ -114,11 +111,13 @@ namespace StgSharp.Mathematics.Graphic public static Vec4 One { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new Vec4(1, 1, 1, 1); } public static Vec4 Zero { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get => new Vec4(0, 0, 0, 0); } @@ -130,7 +129,7 @@ namespace StgSharp.Mathematics.Graphic 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() diff --git a/StgSharp.Mathematics/AssemblyInfo.cs b/StgSharp.Mathematics/AssemblyInfo.cs new file mode 100644 index 00000000..b7896aec --- /dev/null +++ b/StgSharp.Mathematics/AssemblyInfo.cs @@ -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")] \ No newline at end of file diff --git a/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs index b5cfdd1d..f7edfbcd 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.Publish.cs @@ -1,33 +1,31 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="MatrixParallel.Publish.cs" +// file="MatrixParallel.Publish" // Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// 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. +// 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 StgSharp.Commom.Collections; using StgSharp.HighPerformance; using System; @@ -822,11 +820,24 @@ namespace StgSharp.Mathematics.Numeric int tileWidth = (1024 / secCount) + 1; int tileCount = (primCount / tileWidth) + 1; - int tilePerThreadLess = tileCount / Wraps.Length; - int tileToSlice = tileCount % Wraps.Length; + CapacityFixedStack wraps = LeadParallel(); - // TODO 这个方法依然是独占,需要一个新的缓存类型用来辅助复用空线程束 - for (int i = 0; i < Wraps.Length; i++) { } + int tilePerThreadLess = tileCount / Wraps.Count; + int tileToSlice = tileCount % Wraps.Count; + int slicingPerWrap = tileToSlice / Wraps.Count; + int extraTiles = tileToSlice % Wraps.Count; + + int total = tileCount; + List w = new List(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); + } } } diff --git a/StgSharp.Mathematics/Numeric/MatrixParallel.cs b/StgSharp.Mathematics/Numeric/MatrixParallel.cs index 54e1cbf9..f4e2d47c 100644 --- a/StgSharp.Mathematics/Numeric/MatrixParallel.cs +++ b/StgSharp.Mathematics/Numeric/MatrixParallel.cs @@ -1,33 +1,31 @@ //----------------------------------------------------------------------- // ----------------------------------------------------------------------- -// file="MatrixParallel.cs" +// file="MatrixParallel" // Project: StgSharp -// AuthorGroup: Nitload Space -// Copyright (c) Nitload Space. All rights reserved. +// 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. +// 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 StgSharp.Commom.Collections; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; @@ -130,13 +128,13 @@ namespace StgSharp.Mathematics.Numeric { Wraps = [ new MatrixParallelWrap(0, count, true) ]; (LargeWrapCount, SmallWrapCount) = (0, 1); - LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); + LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps.Peek()); } else { count = 4; Wraps = [ new MatrixParallelWrap(0, count, true) ]; (LargeWrapCount, SmallWrapCount) = (1, 0); - LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); + LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps.Peek()); } } else if (count == 6) { @@ -145,19 +143,19 @@ namespace StgSharp.Mathematics.Numeric new MatrixParallelWrap(3, 3, false) ]; (LargeWrapCount, SmallWrapCount) = (0, 2); - LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps[0]); + LeaderHandle = new MatrixParallelWrap.Handle(0, Wraps.Peek()); } else { (int _3wrap, int _4wrap) = FastDecompose(count); - Wraps = new MatrixParallelWrap[_4wrap + _3wrap]; - int id = 0,idx = 0; + Wraps = new(_4wrap + _3wrap); + int id = 0, idx = 0; 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) { - 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); IsInitialized = true; @@ -170,12 +168,6 @@ namespace StgSharp.Mathematics.Numeric SetAllWrap(wraps); } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void LeadParallel() - { - Monitor.Enter(_lock); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe void MatrixParallelLeaderWork() { @@ -271,6 +263,17 @@ namespace StgSharp.Mathematics.Numeric 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 private static unsafe void MatrixParallelWorker([AllowNull] object obj) @@ -342,21 +345,39 @@ namespace StgSharp.Mathematics.Numeric #endregion } + public enum SleepMode + { + + QuickWakeup = ThreadPriority.AboveNormal, + NormalSleep = ThreadPriority.BelowNormal, + DeepSleep = ThreadPriority.Lowest, + + } + #region wrap field - private static MatrixParallelWrap[] Wraps; + private static CapacityFixedStack Wraps; private static int LargeWrapCount; private static int SmallWrapCount; - #endregion - } + public static CapacityFixedStack LeadParallel() + { +#if NET9_0_OR_GREATER +#else + Monitor.Enter(_lock); +#endif + return Wraps; + } - public enum SleepMode - { - - QuickWakeup = ThreadPriority.AboveNormal, - NormalSleep = ThreadPriority.BelowNormal, - DeepSleep = ThreadPriority.Lowest, + public static void ReleaseParallelLeadership(ref CapacityFixedStack wraps) + { +#if NET9_0_OR_GREATER +#else + Monitor.Exit(_lock); +#endif + wraps = null!; + } + #endregion } } \ No newline at end of file