Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.86 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5.  
  6. namespace LinqToObjects
  7. {
  8.     public static class Enumerable
  9.     {
  10.         public static IEnumerable<int> Range(int from, int to)
  11.         {
  12.             for (int i = from; i <= to; i++)
  13.                 yield return i;
  14.         }
  15.  
  16.         public static T Aggregate<T>(this IEnumerable<T> source, Func<T, T, T> func)
  17.         {
  18.             Preconditions.CheckNotNull(source,"source");
  19.             Preconditions.CheckNotNull(func,"func");
  20.  
  21.             T current;
  22.             using (IEnumerator<T> iterator = source.GetEnumerator())
  23.             {
  24.                 iterator.MoveNext();
  25.                 current = iterator.Current;
  26.  
  27.                 while (iterator.MoveNext())
  28.                 {
  29.                     current = func(current, iterator.Current);
  30.                 }
  31.             }
  32.             return current;
  33.         }
  34.  
  35.         public static TAccumulate Aggregate<T, TAccumulate>(this IEnumerable<T> source, TAccumulate seed,
  36.             Func<TAccumulate, T, TAccumulate> func)
  37.         {
  38.             Preconditions.CheckNotNull(source, "source");
  39.             Preconditions.CheckNotNull(func, "func");
  40.  
  41.             TAccumulate current = seed;
  42.             using (IEnumerator<T> iterator = source.GetEnumerator())
  43.             {
  44.                 while (iterator.MoveNext())
  45.                 {
  46.                     current = func(current, iterator.Current);
  47.                 }
  48.             }
  49.             return current;
  50.         }
  51.  
  52.         public static TResult Aggregate<TSource, TAccumulate, TResult>(
  53.             this IEnumerable<TSource> source,
  54.             TAccumulate seed,
  55.             Func<TAccumulate, TSource, TAccumulate> func,
  56.             Func<TAccumulate, TResult> resultSelector)
  57.         {
  58.             Preconditions.CheckNotNull(source, "source");
  59.             Preconditions.CheckNotNull(func, "func");
  60.             Preconditions.CheckNotNull(resultSelector,"resultSelector");
  61.  
  62.             return resultSelector(source.Aggregate(seed, func));
  63.         }
  64.  
  65.         public static bool All<TSource>(
  66.             this IEnumerable<TSource> source,
  67.             Func<TSource, bool> predicate
  68.             )
  69.         {
  70.             foreach (var item in source)
  71.             {
  72.                 if (!predicate(item))
  73.                     return false;
  74.             }
  75.             return true;
  76.         }
  77.  
  78.         public static bool Any<TSource>(this IEnumerable<TSource> source)
  79.         {
  80.             return source.Count() != 0;
  81.         }
  82.  
  83.         public static bool Any<TSource>(this IEnumerable<TSource> source,Func<TSource, bool> predicate)
  84.         {
  85.             foreach (var item in source)
  86.             {
  87.                 if (predicate(item)) return true;
  88.             }
  89.             return false;
  90.         }
  91.  
  92.         public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
  93.         {
  94.             return source;
  95.         }
  96.  
  97.         public static int Count<TSource>(this IEnumerable<TSource> source)
  98.         {
  99.             if (source is IList<TSource>) return ((IList<TSource>) source).Count;
  100.             if (source is ICollection<TSource>) return ((ICollection<TSource>) source).Count;
  101.  
  102.             int count = 0;
  103.  
  104.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  105.             {
  106.                 while (iterator.MoveNext()) count++;
  107.             }
  108.  
  109.             return count;
  110.         }
  111.  
  112.         public static int Count<TSource>(
  113.             this IEnumerable<TSource> source,
  114.             Func<TSource, bool> predicate)
  115.         {
  116.             int count = 0;
  117.  
  118.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  119.             {
  120.                 while (iterator.MoveNext())
  121.                 {
  122.                     if (predicate(iterator.Current))
  123.                     {
  124.                         count++;
  125.                     }
  126.                 }
  127.             }
  128.  
  129.             return count;
  130.         }
  131.  
  132.         public static long LongCount<TSource>(this IEnumerable<TSource> source)
  133.         {
  134.             if (source is IList<TSource>) return ((IList<TSource>)source).Count;
  135.             if (source is ICollection<TSource>) return ((ICollection<TSource>)source).Count;
  136.  
  137.             long count = 0;
  138.  
  139.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  140.             {
  141.                 while (iterator.MoveNext()) count++;
  142.             }
  143.  
  144.             return count;
  145.         }
  146.  
  147.         public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
  148.         {
  149.             foreach (var item in source)
  150.                 yield return (TResult) item;
  151.         }
  152.  
  153.         public static IEnumerable<TSource> Concat<TSource>(
  154.             this IEnumerable<TSource> first,
  155.             IEnumerable<TSource> second)
  156.         {
  157.             foreach (var item in first)
  158.                 yield return item;
  159.  
  160.             foreach (var item in second)
  161.                 yield return item;
  162.         }
  163.  
  164.         public static bool Contains<TSource>(
  165.             this IEnumerable<TSource> source,
  166.             TSource value)
  167.         {
  168.             foreach (var item in source)
  169.             {
  170.                 if (item.Equals(value))
  171.                     return true;
  172.             }
  173.             return false;
  174.         }
  175.  
  176.         public static bool Contains<TSource>(
  177.             this IEnumerable<TSource> source,
  178.             TSource value,
  179.             IEqualityComparer<TSource> comparer)
  180.         {
  181.             foreach (var item in source)
  182.             {
  183.                 if (comparer.Equals(item, value))
  184.                     return true;
  185.             }
  186.             return false;
  187.         }
  188.  
  189.         public static IEnumerable<TSource> DefaultIfEmpty<TSource>(this IEnumerable<TSource> source)
  190.         {
  191.             return source.DefaultIfEmpty(default(TSource));
  192.         }
  193.  
  194.         public static IEnumerable<TSource> DefaultIfEmpty<TSource>(
  195.             this IEnumerable<TSource> source,
  196.             TSource defaultValue)
  197.         {
  198.             Preconditions.CheckNotNull(source,"source");
  199.  
  200.             if (!source.Any())
  201.             {
  202.                 return new[] {defaultValue};
  203.             }
  204.  
  205.             return source;
  206.         }
  207.  
  208.         public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source)
  209.         {
  210.             HashSet<TSource> hash = new HashSet<TSource>();
  211.  
  212.             foreach (var item in source)
  213.             {
  214.                 if (hash.Add(item)) yield return item;
  215.             }
  216.         }
  217.  
  218.         public static IEnumerable<TSource> Distinct<TSource>(
  219.             this IEnumerable<TSource> source,
  220.             IEqualityComparer<TSource> comparer)
  221.         {
  222.             HashSet<TSource> hash = new HashSet<TSource>(comparer);
  223.  
  224.             foreach (var item in source)
  225.             {
  226.                 if (hash.Add(item)) yield return item;
  227.             }
  228.         }
  229.  
  230.         public static TSource ElementAt<TSource>(
  231.             this IEnumerable<TSource> source, int index)
  232.         {
  233.             Preconditions.CheckNotNull(source, "source");
  234.             Preconditions.CheckNotOutOfBounds(index);
  235.  
  236.             if (source is IList<TSource>) return ((IList<TSource>) source)[index];
  237.  
  238.             int count = 0;
  239.  
  240.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  241.             {
  242.                 while (iterator.MoveNext())
  243.                 {
  244.                     if (count == index)
  245.                     {
  246.                         return iterator.Current;
  247.                     }
  248.  
  249.                     count++;
  250.                 }
  251.             }
  252.  
  253.             throw new IndexOutOfRangeException();
  254.         }
  255.  
  256.         public static TSource ElementAtOrDefault<TSource>(
  257.             this IEnumerable<TSource> source,
  258.             int index)
  259.         {
  260.             Preconditions.CheckNotNull(source, "source");
  261.             Preconditions.CheckNotOutOfBounds(index);
  262.  
  263.             if (source is IList<TSource>) return ((IList<TSource>)source)[index];
  264.  
  265.             int count = 0;
  266.  
  267.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  268.             {
  269.                 while (iterator.MoveNext())
  270.                 {
  271.                     if (count == index)
  272.                     {
  273.                         return iterator.Current;
  274.                     }
  275.  
  276.                     count++;
  277.                 }
  278.             }
  279.  
  280.             return default(TSource);
  281.         }
  282.  
  283.         public static IEnumerable<TSource> Except<TSource>(
  284.             this IEnumerable<TSource> first,
  285.             IEnumerable<TSource> second)
  286.         {
  287.             HashSet<TSource> hash = new HashSet<TSource>(second);
  288.  
  289.             foreach(var item in first)
  290.                 if (hash.Add(item)) yield return item;
  291.         }
  292.  
  293.         public static IEnumerable<TSource> Except<TSource>(
  294.             this IEnumerable<TSource> first,
  295.             IEnumerable<TSource> second,
  296.             IEqualityComparer<TSource> comparer)
  297.         {
  298.             HashSet<TSource> hash = new HashSet<TSource>(second,comparer);
  299.  
  300.             foreach(var item in first)
  301.                 if (hash.Add(item)) yield return item;
  302.         }
  303.  
  304.         public static TSource First<TSource>(this IEnumerable<TSource> source)
  305.         {
  306.             Preconditions.CheckNotNull(source, "source");
  307.  
  308.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  309.             {
  310.                 if (iterator.MoveNext())
  311.                 {
  312.                     return iterator.Current;
  313.                 }
  314.                 else
  315.                 {
  316.                     throw new InvalidOperationException("source sequence was empty");
  317.                 }
  318.             }
  319.         }
  320.  
  321.         public static TSource First<TSource>(
  322.             this IEnumerable<TSource> source,
  323.             Func<TSource, bool> predicate)
  324.         {
  325.             Preconditions.CheckNotNull(source, "source");
  326.             Preconditions.CheckNotNull(predicate, "predicate");
  327.             Preconditions.CheckNotEmpty(source);
  328.  
  329.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  330.             {
  331.                 while (iterator.MoveNext())
  332.                 {
  333.                     if (predicate(iterator.Current))
  334.                     {
  335.                         return iterator.Current;
  336.                     }
  337.                 }
  338.             }
  339.  
  340.             throw new InvalidOperationException("No element satisfies the condition in predicate");
  341.         }
  342.  
  343.         public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source)
  344.         {
  345.             Preconditions.CheckNotNull(source, "source");
  346.  
  347.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  348.             {
  349.                 if (iterator.MoveNext())
  350.                 {
  351.                     return iterator.Current;
  352.                 }
  353.                 else
  354.                 {
  355.                     return default(TSource);
  356.                 }
  357.             }
  358.         }
  359.  
  360.         public static TSource FirstOrDefault<TSource>(
  361.             this IEnumerable<TSource> source,
  362.             Func<TSource, bool> predicate)
  363.         {
  364.             Preconditions.CheckNotNull(source, "source");
  365.  
  366.             using (IEnumerator<TSource> iterator = source.GetEnumerator())
  367.             {
  368.                 while (iterator.MoveNext())
  369.                 {
  370.                     if (predicate(iterator.Current))
  371.                     {
  372.                         return iterator.Current;
  373.                     }
  374.                 }
  375.             }
  376.  
  377.             return default(TSource);
  378.         }
  379.  
  380.         public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
  381.             this IEnumerable<TSource> source,
  382.             Func<TSource, TKey> keySelector)
  383.         {
  384.             var dictionary = new Dictionary<TKey, Grouping<TKey, TSource>>();
  385.  
  386.             foreach (var item in source)
  387.             {
  388.                 TKey key = keySelector(item);
  389.                 if (!dictionary.ContainsKey(key))
  390.                 {
  391.                     var grouping = new Grouping<TKey, TSource>(key);
  392.                     grouping.Add(item);
  393.                     dictionary[key] = grouping;
  394.                 }
  395.                 else
  396.                 {
  397.                     dictionary[key].Add(item);
  398.                 }
  399.             }
  400.  
  401.             return dictionary.Values;
  402.         }
  403.  
  404.         public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
  405.             this IEnumerable<TSource> source,
  406.             Func<TSource, TKey> keySelector,
  407.             IEqualityComparer<TKey> comparer)
  408.         {
  409.             var dictionary = new Dictionary<TKey, Grouping<TKey, TSource>>(comparer);
  410.  
  411.             foreach (var item in source)
  412.             {
  413.                 TKey key = keySelector(item);
  414.                 if (!dictionary.ContainsKey(key))
  415.                 {
  416.                     var grouping = new Grouping<TKey, TSource>(key);
  417.                     grouping.Add(item);
  418.                     dictionary[key] = grouping;
  419.                 }
  420.                 else
  421.                 {
  422.                     dictionary[key].Add(item);
  423.                 }
  424.             }
  425.  
  426.             return dictionary.Values;
  427.         }
  428.  
  429.  
  430.         public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
  431.             this IEnumerable<TSource> source,
  432.             Func<TSource, TKey> keySelector,
  433.             Func<TSource, TElement> elementSelector)
  434.         {
  435.             var dictionary = new Dictionary<TKey, Grouping<TKey, TElement>>();
  436.  
  437.             foreach (var item in source)
  438.             {
  439.                 TKey key = keySelector(item);
  440.                 if (!dictionary.ContainsKey(key))
  441.                 {
  442.                     var grouping = new Grouping<TKey, TElement>(key);
  443.                     grouping.Add(elementSelector(item));
  444.                     dictionary[key] = grouping;
  445.                 }
  446.                 else
  447.                 {
  448.                     dictionary[key].Add(elementSelector(item));
  449.                 }
  450.             }
  451.  
  452.             return dictionary.Values;
  453.         }
  454.  
  455.         public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
  456.             this IEnumerable<TSource> source,
  457.             Func<TSource, TKey> keySelector,
  458.             Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
  459.         {
  460.             return source.GroupBy(keySelector).Select(x => resultSelector(x.Key,x));
  461.         }
  462.  
  463.         public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement>(
  464.             this IEnumerable<TSource> source,
  465.             Func<TSource, TKey> keySelector,
  466.             Func<TSource, TElement> elementSelector,
  467.             IEqualityComparer<TKey> comparer
  468.             )
  469.         {
  470.             var dictionary = new Dictionary<TKey, Grouping<TKey, TElement>>(comparer);
  471.  
  472.             foreach (var item in source)
  473.             {
  474.                 TKey key = keySelector(item);
  475.                 if (!dictionary.ContainsKey(key))
  476.                 {
  477.                     var grouping = new Grouping<TKey, TElement>(key);
  478.                     grouping.Add(elementSelector(item));
  479.                     dictionary[key] = grouping;
  480.                 }
  481.                 else
  482.                 {
  483.                     dictionary[key].Add(elementSelector(item));
  484.                 }
  485.             }
  486.  
  487.             return dictionary.Values;
  488.         }
  489.  
  490.         public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult>(
  491.             this IEnumerable<TSource> source,
  492.             Func<TSource, TKey> keySelector,
  493.             Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
  494.             IEqualityComparer<TKey> comparer)
  495.         {
  496.             return source.GroupBy(keySelector,comparer).Select(x => resultSelector(x.Key, x));
  497.         }
  498.  
  499.         public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
  500.             this IEnumerable<TSource> source,
  501.             Func<TSource, TKey> keySelector,
  502.             Func<TSource, TElement> elementSelector,
  503.             Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
  504.         {
  505.             return source.GroupBy(keySelector, elementSelector).Select(x => resultSelector(x.Key, x));
  506.         }
  507.  
  508.         public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult>(
  509.             this IEnumerable<TSource> source,
  510.             Func<TSource, TKey> keySelector,
  511.             Func<TSource, TElement> elementSelector,
  512.             Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
  513.             IEqualityComparer<TKey> comparer)
  514.         {
  515.             return source.GroupBy(keySelector, elementSelector,comparer).Select(x => resultSelector(x.Key, x));
  516.         }
  517.  
  518.  
  519.         public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source,
  520.             Func<TSource, TResult> selector)
  521.         {
  522.             foreach (var item in source)
  523.                 yield return selector(item);
  524.         }
  525.  
  526.         public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
  527.             Func<TSource, bool> predicate)
  528.         {
  529.             foreach (var item in source)
  530.                 if (predicate(item))
  531.                     yield return item;
  532.         }
  533.     }
  534.  
  535.     public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>,IEnumerable
  536.     {
  537.         TKey Key { get; }
  538.     }
  539.  
  540.     public class Grouping<TKey, TValue> : IGrouping<TKey, TValue>
  541.     {
  542.         private List<TValue> values = new List<TValue>();
  543.  
  544.         public Grouping(TKey key)
  545.         {
  546.             Key = key;
  547.         }
  548.  
  549.         public IEnumerator<TValue> GetEnumerator()
  550.         {
  551.             return values.GetEnumerator();
  552.         }
  553.  
  554.         public void Add(TValue value)
  555.         {
  556.             values.Add(value);
  557.         }
  558.  
  559.         IEnumerator IEnumerable.GetEnumerator()
  560.         {
  561.             return GetEnumerator();
  562.         }
  563.  
  564.         public TKey Key { get; private set; }
  565.     }
  566. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement