internal static void ForEachInternal <T, U>(ParallelFuncContext <T, U> Context) { Int32 StartIndex = Context._StartIndex; Int32 EndIndex = Context._EndIndex; T[] Items = Context._Items; Func <T, U> Action = Context._Action; U[] Receiver = Context._Receiver; for (Int32 I = StartIndex; I < EndIndex; I++) { Receiver[I] = Action.Invoke(Items[I]); } }
/// <summary>Loops over each item in the specified array.</summary> /// <typeparam name="T1">The type inside the array.</typeparam> /// <typeparam name="T2">The type used for the additional information.</typeparam> /// <typeparam name="U">The type to be outputted</typeparam> /// <param name="Items">The items to loop over.</param> /// <param name="Argument1">The first argument1</param> /// <param name="action">The action to preform on each item.</param> /// <param name="TaskCount">The amount of tasks that should process over this array.</param> /// <returns>Loops over each item in the specified array.</returns> public static FuncResult <U> ForEach <T1, T2, U>(T1[] Items, T2 Argument1, Func <T1, T2, U> action, Int32 TaskCount) { if (TaskCount < 1) { throw new ArgumentOutOfRangeException(nameof(TaskCount)); } var Tasks = new Task[TaskCount]; Int32 Length = Items.Length; var Receiver = new U[Length]; Int32 Step = Length / TaskCount; Int32 Max = TaskCount - 1; //Creates sections and start for (Int32 I = 0; I < Max; I++) { //Creates the context for this section var Context = new ParallelFuncContext <T1, T2, U>() { _StartIndex = I * Step, _EndIndex = (I + 1) * Step, _Items = Items, _Action = action, _Argument1 = Argument1, _Receiver = Receiver }; Tasks[I] = Task.Factory.StartNew(Context.Invoke); } //Creates the last Int32 Start = Max * Step; var Context1 = new ParallelFuncContext <T1, T2, U>() { _StartIndex = Start, _EndIndex = Items.Length, _Items = Items, _Action = action, _Argument1 = Argument1, _Receiver = Receiver }; Tasks[Max] = Task.Factory.StartNew(Context1.Invoke); return(new FuncResult <U>(Tasks, Receiver)); }