Advertisement
Guest User

MathNet.Numerics .Net 3.5 requirements

a guest
Nov 29th, 2013
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace MathNet.Numerics
  5. {
  6.     public static class Zipper
  7.     {
  8.         public static IEnumerable<T> Zip<A, B, T>(
  9.             this IEnumerable<A> seqA, IEnumerable<B> seqB, Func<A, B, T> func)
  10.         {
  11.             if (seqA == null) throw new ArgumentNullException("seqA");
  12.             if (seqB == null) throw new ArgumentNullException("seqB");
  13.  
  14.             return Zip35Deferred(seqA, seqB, func);
  15.         }
  16.  
  17.         private static IEnumerable<T> Zip35Deferred<A, B, T>(
  18.             this IEnumerable<A> seqA, IEnumerable<B> seqB, Func<A, B, T> func)
  19.         {
  20.             using (var iteratorA = seqA.GetEnumerator())
  21.             using (var iteratorB = seqB.GetEnumerator())
  22.             {
  23.                 while (iteratorA.MoveNext() && iteratorB.MoveNext())
  24.                 {
  25.                     yield return func(iteratorA.Current, iteratorB.Current);
  26.                 }
  27.             }
  28.         }
  29.     }
  30.  
  31.     public class Tuple<T1, T2>
  32.     {
  33.         public T1 Item1 { get; set; }
  34.         public T2 Item2 { get; set; }
  35.  
  36.         public Tuple(T1 Item1, T2 Item2)
  37.         {
  38.             this.Item1 = Item1;
  39.             this.Item2 = Item2;
  40.         }
  41.     }
  42.  
  43.     public class Tuple<T1, T2, T3>
  44.     {
  45.         public T1 Item1 { get; set; }
  46.         public T2 Item2 { get; set; }
  47.         public T3 Item3 { get; set; }
  48.  
  49.         public Tuple(T1 Item1, T2 Item2, T3 Item3)
  50.         {
  51.             this.Item1 = Item1;
  52.             this.Item2 = Item2;
  53.             this.Item3 = Item3;
  54.         }
  55.     }
  56.  
  57.     /// <summary>
  58.     /// Provides support for lazy initialization.
  59.     /// </summary>
  60.     /// <typeparam name="T">Specifies the type of object that is being lazily initialized.</typeparam>
  61.     public sealed class Lazy<T>
  62.     {
  63.         private readonly object padlock = new object();
  64.         private readonly Func<T> createValue;
  65.         private bool isValueCreated;
  66.         private T value;
  67.  
  68.         /// <summary>
  69.         /// Gets the lazily initialized value of the current Lazy{T} instance.
  70.         /// </summary>
  71.         public T Value
  72.         {
  73.             get
  74.             {
  75.                 if (!isValueCreated)
  76.                 {
  77.                     lock (padlock)
  78.                     {
  79.                         if (!isValueCreated)
  80.                         {
  81.                             value = createValue();
  82.                             isValueCreated = true;
  83.                         }
  84.                     }
  85.                 }
  86.                 return value;
  87.             }
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Gets a value that indicates whether a value has been created for this Lazy{T} instance.
  92.         /// </summary>
  93.         public bool IsValueCreated
  94.         {
  95.             get
  96.             {
  97.                 lock (padlock)
  98.                 {
  99.                     return isValueCreated;
  100.                 }
  101.             }
  102.         }
  103.  
  104.  
  105.         /// <summary>
  106.         /// Initializes a new instance of the Lazy{T} class.
  107.         /// </summary>
  108.         /// <param name="createValue">The delegate that produces the value when it is needed.</param>
  109.         public Lazy(Func<T> createValue)
  110.         {
  111.             if (createValue == null) throw new ArgumentNullException("createValue");
  112.  
  113.             this.createValue = createValue;
  114.         }
  115.  
  116.  
  117.         /// <summary>
  118.         /// Creates and returns a string representation of the Lazy{T}.Value.
  119.         /// </summary>
  120.         /// <returns>The string representation of the Lazy{T}.Value property.</returns>
  121.         public override string ToString()
  122.         {
  123.             return Value.ToString();
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement