From 717116b2a6f16c5a6f4d46e85499e74014add66f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anta=CC=83o=20Almada?= Date: Mon, 4 Dec 2023 22:15:40 +0000 Subject: [PATCH] Add README --- .../ActionWrapper.cs | 27 +- .../AsyncFunctionWrapper.cs | 77 ++--- .../AsyncValueEnumerable.cs | 5 +- .../FunctionWrapper.cs | 83 ++--- NetFabric.Hyperlinq.Abstractions/IAction.cs | 325 +++++++++--------- .../IAsyncFunction.cs | 5 +- .../IValueEnumerable.cs | 44 ++- .../IVectorAction.cs | 50 ++- .../NetFabric.Hyperlinq.Abstractions.csproj | 4 +- NetFabric.Hyperlinq.Abstractions/README.md | 65 ++++ 10 files changed, 377 insertions(+), 308 deletions(-) create mode 100644 NetFabric.Hyperlinq.Abstractions/README.md diff --git a/NetFabric.Hyperlinq.Abstractions/ActionWrapper.cs b/NetFabric.Hyperlinq.Abstractions/ActionWrapper.cs index 61f950a8..9d985146 100644 --- a/NetFabric.Hyperlinq.Abstractions/ActionWrapper.cs +++ b/NetFabric.Hyperlinq.Abstractions/ActionWrapper.cs @@ -17,7 +17,8 @@ namespace NetFabric.Hyperlinq; public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T arg) @@ -42,7 +43,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2) @@ -68,7 +70,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3) @@ -95,7 +98,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4) @@ -123,7 +127,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5) @@ -152,7 +157,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6) @@ -182,7 +188,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7) @@ -213,7 +220,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8) @@ -245,7 +253,8 @@ void IAction.Invoke(ref readonly T arg) public readonly struct ActionWrapper(Action action) : IAction { - readonly Action action = action ?? Throw.ArgumentNullException>(nameof(action)); + readonly Action action + = action ?? Throw.ArgumentNullException>(nameof(action)); [MethodImpl(MethodImplOptions.AggressiveInlining)] void IAction.Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8, ref readonly T9 arg9) diff --git a/NetFabric.Hyperlinq.Abstractions/AsyncFunctionWrapper.cs b/NetFabric.Hyperlinq.Abstractions/AsyncFunctionWrapper.cs index 9d031d2c..ab9c8644 100644 --- a/NetFabric.Hyperlinq.Abstractions/AsyncFunctionWrapper.cs +++ b/NetFabric.Hyperlinq.Abstractions/AsyncFunctionWrapper.cs @@ -1,19 +1,14 @@ -using System; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Threading; -using System.Threading.Tasks; namespace NetFabric.Hyperlinq; [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T arg, CancellationToken cancellationToken) @@ -24,13 +19,11 @@ public ValueTask InvokeAsync(ref readonly T arg, CancellationToken canc } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, CancellationToken cancellationToken) @@ -41,13 +34,11 @@ public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2 } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, CancellationToken cancellationToken) @@ -58,13 +49,11 @@ public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2 } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, CancellationToken cancellationToken) @@ -75,13 +64,11 @@ public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2 } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, CancellationToken cancellationToken) @@ -92,13 +79,11 @@ public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2 } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, CancellationToken cancellationToken) @@ -109,13 +94,11 @@ public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2 } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, CancellationToken cancellationToken) @@ -126,13 +109,11 @@ public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2 } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8, CancellationToken cancellationToken) @@ -143,13 +124,11 @@ public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2 } [StructLayout(LayoutKind.Auto)] -public readonly struct AsyncFunctionWrapper +public readonly struct AsyncFunctionWrapper(Func> function) : IAsyncFunction { - readonly Func> function; - - public AsyncFunctionWrapper(Func> function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func> function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ValueTask InvokeAsync(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8, ref readonly T9 arg9, CancellationToken cancellationToken) diff --git a/NetFabric.Hyperlinq.Abstractions/AsyncValueEnumerable.cs b/NetFabric.Hyperlinq.Abstractions/AsyncValueEnumerable.cs index 554f6825..632693f1 100644 --- a/NetFabric.Hyperlinq.Abstractions/AsyncValueEnumerable.cs +++ b/NetFabric.Hyperlinq.Abstractions/AsyncValueEnumerable.cs @@ -1,7 +1,4 @@ -using System.Collections.Generic; -using System.Threading; - -namespace NetFabric.Hyperlinq; +namespace NetFabric.Hyperlinq; public interface IAsyncValueEnumerable : IAsyncEnumerable diff --git a/NetFabric.Hyperlinq.Abstractions/FunctionWrapper.cs b/NetFabric.Hyperlinq.Abstractions/FunctionWrapper.cs index 62716c9b..2960455b 100644 --- a/NetFabric.Hyperlinq.Abstractions/FunctionWrapper.cs +++ b/NetFabric.Hyperlinq.Abstractions/FunctionWrapper.cs @@ -1,17 +1,14 @@ -using System; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace NetFabric.Hyperlinq; [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke() @@ -22,13 +19,11 @@ public TResult Invoke() } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T arg) @@ -39,13 +34,11 @@ public TResult Invoke(T arg) } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2) @@ -56,13 +49,11 @@ public TResult Invoke(T1 arg1, T2 arg2) } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2, T3 arg3) @@ -73,13 +64,11 @@ public TResult Invoke(T1 arg1, T2 arg2, T3 arg3) } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4) @@ -90,13 +79,11 @@ public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4) } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) @@ -107,13 +94,11 @@ public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5) } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) @@ -124,13 +109,11 @@ public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6) } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7) @@ -141,13 +124,11 @@ public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 a } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8) @@ -158,13 +139,11 @@ public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 a } [StructLayout(LayoutKind.Auto)] -public readonly struct FunctionWrapper +public readonly struct FunctionWrapper(Func function) : IFunction { - readonly Func function; - - public FunctionWrapper(Func function) - => this.function = function ?? throw new ArgumentNullException(nameof(function)); + readonly Func function + = function ?? throw new ArgumentNullException(nameof(function)); [MethodImpl(MethodImplOptions.AggressiveInlining)] public TResult Invoke(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9) diff --git a/NetFabric.Hyperlinq.Abstractions/IAction.cs b/NetFabric.Hyperlinq.Abstractions/IAction.cs index d9a2b95d..3cdaa8d2 100644 --- a/NetFabric.Hyperlinq.Abstractions/IAction.cs +++ b/NetFabric.Hyperlinq.Abstractions/IAction.cs @@ -1,193 +1,190 @@ -using System.Numerics; +namespace NetFabric.Hyperlinq; -namespace NetFabric.Hyperlinq +/// +/// Defines an interface for a generic action that can be invoked with a readonly reference to an argument of type . +/// +/// The type of the argument to be passed when invoking the action. +public interface IAction { /// - /// Defines an interface for a generic action that can be invoked with a readonly reference to an argument of type . + /// Invokes the action with a readonly reference to the specified argument of type . /// - /// The type of the argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with a readonly reference to the specified argument of type . - /// - /// The readonly reference to the argument to be passed to the action. - void Invoke(ref readonly T arg); - } + /// The readonly reference to the argument to be passed to the action. + void Invoke(ref readonly T arg); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to two arguments of type and . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to two arguments of type and . + /// Invokes the action with readonly references to the specified arguments of type and . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of type and . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to three arguments of type , , and . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +/// The type of the third argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to three arguments of type , , and . + /// Invokes the action with readonly references to the specified arguments of type , , and . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - /// The type of the third argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of type , , and . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - /// The readonly reference to the third argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + /// The readonly reference to the third argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +/// The type of the third argument to be passed when invoking the action. +/// The type of the fourth argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . + /// Invokes the action with readonly references to the specified arguments of types to . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - /// The type of the third argument to be passed when invoking the action. - /// The type of the fourth argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of types to . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - /// The readonly reference to the third argument to be passed to the action. - /// The readonly reference to the fourth argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + /// The readonly reference to the third argument to be passed to the action. + /// The readonly reference to the fourth argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +/// The type of the third argument to be passed when invoking the action. +/// The type of the fourth argument to be passed when invoking the action. +/// The type of the fifth argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . + /// Invokes the action with readonly references to the specified arguments of types to . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - /// The type of the third argument to be passed when invoking the action. - /// The type of the fourth argument to be passed when invoking the action. - /// The type of the fifth argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of types to . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - /// The readonly reference to the third argument to be passed to the action. - /// The readonly reference to the fourth argument to be passed to the action. - /// The readonly reference to the fifth argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + /// The readonly reference to the third argument to be passed to the action. + /// The readonly reference to the fourth argument to be passed to the action. + /// The readonly reference to the fifth argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +/// The type of the third argument to be passed when invoking the action. +/// The type of the fourth argument to be passed when invoking the action. +/// The type of the fifth argument to be passed when invoking the action. +/// The type of the sixth argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . + /// Invokes the action with readonly references to the specified arguments of types to . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - /// The type of the third argument to be passed when invoking the action. - /// The type of the fourth argument to be passed when invoking the action. - /// The type of the fifth argument to be passed when invoking the action. - /// The type of the sixth argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of types to . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - /// The readonly reference to the third argument to be passed to the action. - /// The readonly reference to the fourth argument to be passed to the action. - /// The readonly reference to the fifth argument to be passed to the action. - /// The readonly reference to the sixth argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + /// The readonly reference to the third argument to be passed to the action. + /// The readonly reference to the fourth argument to be passed to the action. + /// The readonly reference to the fifth argument to be passed to the action. + /// The readonly reference to the sixth argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +/// The type of the third argument to be passed when invoking the action. +/// The type of the fourth argument to be passed when invoking the action. +/// The type of the fifth argument to be passed when invoking the action. +/// The type of the sixth argument to be passed when invoking the action. +/// The type of the seventh argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . + /// Invokes the action with readonly references to the specified arguments of types to . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - /// The type of the third argument to be passed when invoking the action. - /// The type of the fourth argument to be passed when invoking the action. - /// The type of the fifth argument to be passed when invoking the action. - /// The type of the sixth argument to be passed when invoking the action. - /// The type of the seventh argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of types to . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - /// The readonly reference to the third argument to be passed to the action. - /// The readonly reference to the fourth argument to be passed to the action. - /// The readonly reference to the fifth argument to be passed to the action. - /// The readonly reference to the sixth argument to be passed to the action. - /// The readonly reference to the seventh argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + /// The readonly reference to the third argument to be passed to the action. + /// The readonly reference to the fourth argument to be passed to the action. + /// The readonly reference to the fifth argument to be passed to the action. + /// The readonly reference to the sixth argument to be passed to the action. + /// The readonly reference to the seventh argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +/// The type of the third argument to be passed when invoking the action. +/// The type of the fourth argument to be passed when invoking the action. +/// The type of the fifth argument to be passed when invoking the action. +/// The type of the sixth argument to be passed when invoking the action. +/// The type of the seventh argument to be passed when invoking the action. +/// The type of the eighth argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . + /// Invokes the action with readonly references to the specified arguments of types to . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - /// The type of the third argument to be passed when invoking the action. - /// The type of the fourth argument to be passed when invoking the action. - /// The type of the fifth argument to be passed when invoking the action. - /// The type of the sixth argument to be passed when invoking the action. - /// The type of the seventh argument to be passed when invoking the action. - /// The type of the eighth argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of types to . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - /// The readonly reference to the third argument to be passed to the action. - /// The readonly reference to the fourth argument to be passed to the action. - /// The readonly reference to the fifth argument to be passed to the action. - /// The readonly reference to the sixth argument to be passed to the action. - /// The readonly reference to the seventh argument to be passed to the action. - /// The readonly reference to the eighth argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + /// The readonly reference to the third argument to be passed to the action. + /// The readonly reference to the fourth argument to be passed to the action. + /// The readonly reference to the fifth argument to be passed to the action. + /// The readonly reference to the sixth argument to be passed to the action. + /// The readonly reference to the seventh argument to be passed to the action. + /// The readonly reference to the eighth argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8); +} +/// +/// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . +/// +/// The type of the first argument to be passed when invoking the action. +/// The type of the second argument to be passed when invoking the action. +/// The type of the third argument to be passed when invoking the action. +/// The type of the fourth argument to be passed when invoking the action. +/// The type of the fifth argument to be passed when invoking the action. +/// The type of the sixth argument to be passed when invoking the action. +/// The type of the seventh argument to be passed when invoking the action. +/// The type of the eighth argument to be passed when invoking the action. +/// The type of the ninth argument to be passed when invoking the action. +public interface IAction +{ /// - /// Defines an interface for a generic action that can be invoked with readonly references to nine arguments of types to . + /// Invokes the action with readonly references to the specified arguments of types to . /// - /// The type of the first argument to be passed when invoking the action. - /// The type of the second argument to be passed when invoking the action. - /// The type of the third argument to be passed when invoking the action. - /// The type of the fourth argument to be passed when invoking the action. - /// The type of the fifth argument to be passed when invoking the action. - /// The type of the sixth argument to be passed when invoking the action. - /// The type of the seventh argument to be passed when invoking the action. - /// The type of the eighth argument to be passed when invoking the action. - /// The type of the ninth argument to be passed when invoking the action. - public interface IAction - { - /// - /// Invokes the action with readonly references to the specified arguments of types to . - /// - /// The readonly reference to the first argument to be passed to the action. - /// The readonly reference to the second argument to be passed to the action. - /// The readonly reference to the third argument to be passed to the action. - /// The readonly reference to the fourth argument to be passed to the action. - /// The readonly reference to the fifth argument to be passed to the action. - /// The readonly reference to the sixth argument to be passed to the action. - /// The readonly reference to the seventh argument to be passed to the action. - /// The readonly reference to the eighth argument to be passed to the action. - /// The readonly reference to the ninth argument to be passed to the action. - void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8, ref readonly T9 arg9); - } + /// The readonly reference to the first argument to be passed to the action. + /// The readonly reference to the second argument to be passed to the action. + /// The readonly reference to the third argument to be passed to the action. + /// The readonly reference to the fourth argument to be passed to the action. + /// The readonly reference to the fifth argument to be passed to the action. + /// The readonly reference to the sixth argument to be passed to the action. + /// The readonly reference to the seventh argument to be passed to the action. + /// The readonly reference to the eighth argument to be passed to the action. + /// The readonly reference to the ninth argument to be passed to the action. + void Invoke(ref readonly T1 arg1, ref readonly T2 arg2, ref readonly T3 arg3, ref readonly T4 arg4, ref readonly T5 arg5, ref readonly T6 arg6, ref readonly T7 arg7, ref readonly T8 arg8, ref readonly T9 arg9); } diff --git a/NetFabric.Hyperlinq.Abstractions/IAsyncFunction.cs b/NetFabric.Hyperlinq.Abstractions/IAsyncFunction.cs index 2fc0aa6e..2303ce80 100644 --- a/NetFabric.Hyperlinq.Abstractions/IAsyncFunction.cs +++ b/NetFabric.Hyperlinq.Abstractions/IAsyncFunction.cs @@ -1,7 +1,4 @@ -using System.Threading; -using System.Threading.Tasks; - -namespace NetFabric.Hyperlinq; +namespace NetFabric.Hyperlinq; public interface IAsyncFunction { diff --git a/NetFabric.Hyperlinq.Abstractions/IValueEnumerable.cs b/NetFabric.Hyperlinq.Abstractions/IValueEnumerable.cs index 92f6c3ae..18bc4b96 100644 --- a/NetFabric.Hyperlinq.Abstractions/IValueEnumerable.cs +++ b/NetFabric.Hyperlinq.Abstractions/IValueEnumerable.cs @@ -1,28 +1,26 @@ -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; -namespace NetFabric.Hyperlinq +namespace NetFabric.Hyperlinq; + +/// +/// Represents a read-only collection of values with a non-boxing enumerator, +/// providing an alternative to for improved performance. +/// +/// The type of elements in the collection. +/// The type of the enumerator. +/// +/// This interface is designed to offer improved performance over +/// by avoiding the boxing of the enumerator. It maintains backward compatibility by deriving from +/// . +/// +public interface IValueEnumerable + : IEnumerable + where TEnumerator : struct, IEnumerator { /// - /// Represents a read-only collection of values with a non-boxing enumerator, - /// providing an alternative to for improved performance. + /// Returns an enumerator instance. /// - /// The type of elements in the collection. - /// The type of the enumerator. - /// - /// This interface is designed to offer improved performance over - /// by avoiding the boxing of the enumerator. It maintains backward compatibility by deriving from - /// . - /// - public interface IValueEnumerable - : IEnumerable - where TEnumerator : struct, IEnumerator - { - /// - /// Returns an enumerator instance. - /// - /// An enumerator instance. - [return: NotNull] - new TEnumerator GetEnumerator(); - } + /// An enumerator instance. + [return: NotNull] + new TEnumerator GetEnumerator(); } diff --git a/NetFabric.Hyperlinq.Abstractions/IVectorAction.cs b/NetFabric.Hyperlinq.Abstractions/IVectorAction.cs index 9872806f..41b4742b 100644 --- a/NetFabric.Hyperlinq.Abstractions/IVectorAction.cs +++ b/NetFabric.Hyperlinq.Abstractions/IVectorAction.cs @@ -2,7 +2,7 @@ namespace NetFabric.Hyperlinq; -#if NETSTANDARD2_1_OR_GREATER +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP1_0_OR_GREATER /// /// Represents an interface for a generic action that can be invoked with a readonly reference to a argument of type . @@ -17,7 +17,8 @@ namespace NetFabric.Hyperlinq; /// The method is used instead in scenarios where vectorization is not available or on elements /// that don't fit in a . /// -public interface IVectorAction : IAction +public interface IVectorAction + : IAction where T : struct { /// @@ -98,4 +99,49 @@ public interface IVectorAction4D void Invoke(ref readonly Vector arg); } +// +/// Represents an interface for a generic 5D action that can be invoked with a readonly reference to a argument of type . +/// +/// The type of the elements in the argument. +/// +/// This interface extends the interface to support 2D vectorized operations. It allows actions to be invoked +/// with a readonly reference to a argument, making it suitable for scenarios where 2D vectorization can be applied +/// for improved performance in numerical or computational tasks. +/// +/// +/// The method is used instead in scenarios where vectorization is not available or on elements +/// that don't fit in a . +/// +public interface IVectorAction5D : IAction where T : struct +{ + /// + /// Invokes the action with a readonly reference to the specified argument of type . + /// + /// The readonly reference to the argument. + void Invoke(in Vector arg); +} + +// +/// Represents an interface for a generic 6D action that can be invoked with a readonly reference to a argument of type . +/// +/// The type of the elements in the argument. +/// +/// This interface extends the interface to support 2D vectorized operations. It allows actions to be invoked +/// with a readonly reference to a argument, making it suitable for scenarios where 2D vectorization can be applied +/// for improved performance in numerical or computational tasks. +/// +/// +/// The method is used instead in scenarios where vectorization is not available or on elements +/// that don't fit in a . +/// +public interface IVectorAction6D : IAction where T : struct +{ + /// + /// Invokes the action with a readonly reference to the specified argument of type . + /// + /// The readonly reference to the argument. + void Invoke(in Vector arg); +} + + #endif diff --git a/NetFabric.Hyperlinq.Abstractions/NetFabric.Hyperlinq.Abstractions.csproj b/NetFabric.Hyperlinq.Abstractions/NetFabric.Hyperlinq.Abstractions.csproj index 71f8966b..3cc482b1 100644 --- a/NetFabric.Hyperlinq.Abstractions/NetFabric.Hyperlinq.Abstractions.csproj +++ b/NetFabric.Hyperlinq.Abstractions/NetFabric.Hyperlinq.Abstractions.csproj @@ -8,6 +8,7 @@ 2.0.0 Icon.png LICENSE + README.md netfabric, hyperlinq, abstractions, linq, enumeration, performance true true @@ -18,10 +19,11 @@ + - + all runtime; build; native; contentfiles; analyzers diff --git a/NetFabric.Hyperlinq.Abstractions/README.md b/NetFabric.Hyperlinq.Abstractions/README.md new file mode 100644 index 00000000..d3e9863e --- /dev/null +++ b/NetFabric.Hyperlinq.Abstractions/README.md @@ -0,0 +1,65 @@ +# NetFabric.Hyperlinq.Abstractions + +This library provides abstractions for optimizing the performance of enumerations. + +## IValueEnumerable Interface + +In this package, you'll find the `IValueEnumerable` interface. It's designed for collections that want a value type enumerator for better performance, avoiding the enumerator boxing present in `IEnumerable` and `IEnumerable`. + +## Value Delegates + +In C#, the commonly used predefined delegates [Func](https://github.com/dotnet/dotnet/blob/57e423d62059914d1b92854b656abecdc120e538/src/runtime/src/libraries/System.Private.CoreLib/src/System/Function.cs#L6) and [Action](https://github.com/dotnet/dotnet/blob/57e423d62059914d1b92854b656abecdc120e538/src/runtime/src/libraries/System.Private.CoreLib/src/System/Action.cs#L9) can lead to performance issues due to heap allocation and virtual calls. Value delegates address this by providing optimized performance for resource-intensive scenarios. + +It's worth noting that value delegates aren't automatically supported by the compiler and need explicit implementation. + +The package introduces interfaces like `IAction`, `IFunction`, `IAsyncFunction`, and `IVectorAction`. These interfaces define a method `Invoke()` that can be implemented by a value type. Instances of this value type can replace traditional delegates as parameters and can also hold state, similar to the reference type generated by the compiler when using delegates. + +Here's an example of a value type, `SumValueAction`, implementing `IAction` for calculating the sum: + +```csharp +public struct SumValueAction : IAction + where T : struct, IAdditiveIdentity, IAdditionOperators +{ + T sum; + + public SumValueAction() + { + sum = T.AdditiveIdentity; + } + + public readonly T Result + => sum; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + void IAction.Invoke(ref readonly T item) + => sum += item; +} +``` + +Here's an example of a method implementation that's support the use of a value delegate: + +```csharp +public static void ForEachEx(this IEnumerable source, ref TAction action) + where TAction : struct, IAction +{ + foreach (var item in source) + action.Invoke(in item); +} +``` + +The use of the interface as a constraint avoids the boxing of the value delegate. + +These can be used as follow to calculate the sum of the elements of a collection: + +```csharp +var action = new SumValueAction(); +collection.ForEachEx(ref action); +console.WriteLine(action.Result); +``` + +The package also includes wrappers enabling the use of delegates, along with implicit cast operators converting a delegate to a value delegate. + +References: + +- ["An interface for value-type enumerators, a proposal"](https://www.linkedin.com/pulse/interface-value-type-enumerators-proposal-ant%25C3%25A3o-almada/) +- ["The Battle of Loops: foreach vs. ForEach in C#"](https://www.linkedin.com/pulse/battle-loops-foreach-vs-c-ant%25C3%25A3o-almada/) \ No newline at end of file