#if PORTABLE namespace MathNet.Numerics { using System; using System.Threading; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class SerializableAttribute : Attribute { } [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class SpecialNameAttribute : Attribute { } internal static class Partitioner { public static IEnumerable> Create(int fromInclusive, int toExclusive) { var rangeSize = Math.Max(1, (toExclusive - fromInclusive) / Control.MaxDegreeOfParallelism); return Create(fromInclusive, toExclusive, rangeSize); } public static IEnumerable> Create(int fromInclusive, int toExclusive, int rangeSize) { if (toExclusive <= fromInclusive) throw new ArgumentOutOfRangeException("toExclusive"); if (rangeSize <= 0) throw new ArgumentOutOfRangeException("rangeSize"); return CreateRanges(fromInclusive, toExclusive, rangeSize); } private static IEnumerable> CreateRanges(int fromInclusive, int toExclusive, int rangeSize) { bool flag = false; int num = fromInclusive; while (num < toExclusive && !flag) { int item = num; int num2; try { num2 = checked(num + rangeSize); } catch (OverflowException) { num2 = toExclusive; flag = true; } if (num2 > toExclusive) { num2 = toExclusive; } yield return new Tuple(item, num2); num += rangeSize; } } } internal class ParallelOptions { public TaskScheduler TaskScheduler { get; set; } public int MaxDegreeOfParallelism { get; set; } public CancellationToken CancellationToken { get; set; } public ParallelOptions() { TaskScheduler = TaskScheduler.Default; MaxDegreeOfParallelism = -1; CancellationToken = CancellationToken.None; } } internal class ParallelLoopState { } internal static class Parallel { public static void ForEach(IEnumerable source, ParallelOptions parallelOptions, Action body) { var chunks = source.ToArray(); var tasks = new Task[chunks.Length]; for (var i = 0; i < tasks.Length; i++) { var chunk = chunks[i]; tasks[i] = Task.Factory.StartNew(() => body(chunk), parallelOptions.CancellationToken, TaskCreationOptions.None, parallelOptions.TaskScheduler); } Task.WaitAll(tasks, parallelOptions.CancellationToken); } public static void Invoke(ParallelOptions parallelOptions, params Action[] actions) { var tasks = new Task[actions.Length]; for (var i = 0; i < tasks.Length; i++) { var action = actions[i]; if (action == null) { throw new ArgumentException(String.Format(Properties.Resources.ArgumentItemNull, "actions"), "actions"); } tasks[i] = Task.Factory.StartNew(action, parallelOptions.CancellationToken, TaskCreationOptions.None, parallelOptions.TaskScheduler); } Task.WaitAll(tasks, parallelOptions.CancellationToken); } public static void ForEach( IEnumerable source, ParallelOptions parallelOptions, Func localInit, Func body, Action localFinally) { var chunks = source.ToArray(); var tasks = new Task[chunks.Length]; var loopState = new ParallelLoopState(); for (var i = 0; i < tasks.Length; i++) { var chunk = chunks[i]; tasks[i] = Task.Factory.StartNew(() => { var local = localInit(); local = body(chunk, loopState, local); localFinally(local); }, parallelOptions.CancellationToken, TaskCreationOptions.None, parallelOptions.TaskScheduler); } Task.WaitAll(tasks, parallelOptions.CancellationToken); } } } #endif #if (PORTABLE || NET35) namespace MathNet.Numerics { using System; [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class TargetedPatchingOptOutAttribute : Attribute { public string Reason { get; private set; } public TargetedPatchingOptOutAttribute(string reason) { Reason = reason; } } } #endif #if NET35 namespace MathNet.Numerics { using System; using System.Collections.Concurrent; using System.Collections.Generic; internal static class ObjectComparer { internal static int Compare(T a, T b) { if (ReferenceEquals(a, null)) return -1; if (ReferenceEquals(b, null)) return 1; if (Equals(a, b)) return 0; return Comparer.Default.Compare(a, b); } } public class Tuple : IComparable, IComparable> { public T1 Item1 { get; set; } public T2 Item2 { get; set; } public Tuple(T1 item1, T2 item2) { Item1 = item1; Item2 = item2; } public int CompareTo(object obj) { if (obj == null) return 1; var other = obj as Tuple; if (other == null) throw new ArgumentException(); return CompareTo(other); } public int CompareTo(Tuple other) { if (other == null) return 1; int a = ObjectComparer.Compare(Item1, other.Item1); return a != 0 ? a : ObjectComparer.Compare(Item2, other.Item2); } } public class Tuple : IComparable, IComparable> { public T1 Item1 { get; set; } public T2 Item2 { get; set; } public T3 Item3 { get; set; } public Tuple(T1 item1, T2 item2, T3 item3) { Item1 = item1; Item2 = item2; Item3 = item3; } public int CompareTo(object obj) { if (obj == null) return 1; var other = obj as Tuple; if (other == null) throw new ArgumentException(); return CompareTo(other); } public int CompareTo(Tuple other) { if (other == null) return 1; int a = ObjectComparer.Compare(Item1, other.Item1); if (a != 0) return a; int b = ObjectComparer.Compare(Item2, other.Item2); return b != 0 ? b : ObjectComparer.Compare(Item3, other.Item3); } } public static class EnumerableExtensions { public static IEnumerable Zip(this IEnumerable seqA, IEnumerable seqB, Func func) { if (seqA == null) throw new ArgumentNullException("seqA"); if (seqB == null) throw new ArgumentNullException("seqB"); return Zip35Deferred(seqA, seqB, func); } private static IEnumerable Zip35Deferred(IEnumerable seqA, IEnumerable seqB, Func func) { using (var iteratorA = seqA.GetEnumerator()) using (var iteratorB = seqB.GetEnumerator()) { while (iteratorA.MoveNext() && iteratorB.MoveNext()) { yield return func(iteratorA.Current, iteratorB.Current); } } } } internal static class Partitioner { public static OrderablePartitioner> Create(int fromInclusive, int toExclusive) { var rangeSize = Math.Max(1, (toExclusive - fromInclusive) / Control.MaxDegreeOfParallelism); return Create(fromInclusive, toExclusive, rangeSize); } public static OrderablePartitioner> Create(int fromInclusive, int toExclusive, int rangeSize) { if (toExclusive <= fromInclusive) throw new ArgumentOutOfRangeException("toExclusive"); if (rangeSize <= 0) throw new ArgumentOutOfRangeException("rangeSize"); return System.Collections.Concurrent.Partitioner.Create(CreateRanges(fromInclusive, toExclusive, rangeSize)); } private static IEnumerable> CreateRanges(int fromInclusive, int toExclusive, int rangeSize) { bool flag = false; int num = fromInclusive; while (num < toExclusive && !flag) { int item = num; int num2; try { num2 = checked(num + rangeSize); } catch (OverflowException) { num2 = toExclusive; flag = true; } if (num2 > toExclusive) { num2 = toExclusive; } yield return new Tuple(item, num2); num += rangeSize; } } } } #endif