Advertisement
Guest User

Implementação Where

a guest
Sep 16th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 125.87 KB | None | 0 0
  1.  
  2. Enumerable.cs source code in C# .NET
  3. Source code for the .NET framework in C#
  4.  
  5.                        
  6. Code:
  7.  
  8.                          / 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Linq / Enumerable.cs / 1407647 / Enumerable.cs
  9.                        
  10.  
  11.  
  12.                        
  13.  
  14.                             using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Threading;
  18.  
  19. // Include Silverlight's managed resources
  20. #if SILVERLIGHT
  21. using System.Core;
  22. #endif //SILVERLIGHT
  23.  
  24. namespace System.Linq
  25. {
  26.     public static class Enumerable
  27.     {
  28.         public static IEnumerable Where(this IEnumerable source, Func predicate) {
  29.             if (source == null) throw Error.ArgumentNull("source");
  30.             if (predicate == null) throw Error.ArgumentNull("predicate");
  31.             if (source is Iterator) return ((Iterator)source).Where(predicate);
  32.             if (source is TSource[]) return new WhereArrayIterator((TSource[])source, predicate);
  33.             if (source is List) return new WhereListIterator((List)source, predicate);
  34.             return new WhereEnumerableIterator(source, predicate);
  35.         }
  36.  
  37.         public static IEnumerable Where(this IEnumerable source, Func predicate) {
  38.             if (source == null) throw Error.ArgumentNull("source");
  39.             if (predicate == null) throw Error.ArgumentNull("predicate");
  40.             return WhereIterator(source, predicate);
  41.         }
  42.  
  43.         static IEnumerable WhereIterator(IEnumerable source, Func predicate) {
  44.             int index = -1;
  45.             foreach (TSource element in source) {
  46.                 checked { index++; }
  47.                 if (predicate(element, index)) yield return element;
  48.             }
  49.         }
  50.  
  51.         public static IEnumerable Select(this IEnumerable source, Func selector) {
  52.             if (source == null) throw Error.ArgumentNull("source");
  53.             if (selector == null) throw Error.ArgumentNull("selector");
  54.             if (source is Iterator) return ((Iterator)source).Select(selector);
  55.             if (source is TSource[]) return new WhereSelectArrayIterator((TSource[])source, null, selector);
  56.             if (source is List) return new WhereSelectListIterator((List)source, null, selector);
  57.             return new WhereSelectEnumerableIterator(source, null, selector);
  58.         }
  59.  
  60.         public static IEnumerable Select(this IEnumerable source, Func selector) {
  61.             if (source == null) throw Error.ArgumentNull("source");
  62.             if (selector == null) throw Error.ArgumentNull("selector");
  63.             return SelectIterator(source, selector);
  64.         }
  65.  
  66.         static IEnumerable SelectIterator(IEnumerable source, Func selector) {
  67.             int index = -1;
  68.             foreach (TSource element in source) {
  69.                 checked { index++; }
  70.                 yield return selector(element, index);
  71.             }
  72.         }
  73.  
  74.         static Func CombinePredicates(Func predicate1, Func predicate2) {
  75.             return x => predicate1(x) && predicate2(x);
  76.         }
  77.  
  78.         static Func CombineSelectors(Func selector1, Func selector2) {
  79.             return x => selector2(selector1(x));
  80.         }
  81.  
  82.         abstract class Iterator : IEnumerable, IEnumerator
  83.         {
  84.             int threadId;
  85.             internal int state;
  86.             internal TSource current;
  87.  
  88.             public Iterator() {
  89.                 threadId = Thread.CurrentThread.ManagedThreadId;
  90.             }
  91.  
  92.             public TSource Current {
  93.                 get { return current; }
  94.             }
  95.  
  96.             public abstract Iterator Clone();
  97.  
  98.             public virtual void Dispose() {
  99.                 current = default(TSource);
  100.                 state = -1;
  101.             }
  102.  
  103.             public IEnumerator GetEnumerator() {
  104.                 if (threadId == Thread.CurrentThread.ManagedThreadId && state == 0) {
  105.                     state = 1;
  106.                     return this;
  107.                 }
  108.                 Iterator duplicate = Clone();
  109.                 duplicate.state = 1;
  110.                 return duplicate;
  111.             }
  112.  
  113.             public abstract bool MoveNext();
  114.  
  115.             public abstract IEnumerable Select(Func selector);
  116.  
  117.             public abstract IEnumerable Where(Func predicate);
  118.  
  119.             object IEnumerator.Current {
  120.                 get { return Current; }
  121.             }
  122.  
  123.             IEnumerator IEnumerable.GetEnumerator() {
  124.                 return GetEnumerator();
  125.             }
  126.  
  127.             void IEnumerator.Reset() {
  128.                 throw new NotImplementedException();
  129.             }
  130.         }
  131.  
  132.         class WhereEnumerableIterator : Iterator
  133.         {
  134.             IEnumerable source;
  135.             Func predicate;
  136.             IEnumerator enumerator;
  137.  
  138.             public WhereEnumerableIterator(IEnumerable source, Func predicate) {
  139.                 this.source = source;
  140.                 this.predicate = predicate;
  141.             }
  142.  
  143.             public override Iterator Clone() {
  144.                 return new WhereEnumerableIterator(source, predicate);
  145.             }
  146.  
  147.             public override void Dispose() {
  148.                 if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose();
  149.                 enumerator = null;
  150.                 base.Dispose();
  151.             }
  152.  
  153.             public override bool MoveNext() {
  154.                 switch (state) {
  155.                     case 1:
  156.                         enumerator = source.GetEnumerator();
  157.                         state = 2;
  158.                         goto case 2;
  159.                     case 2:
  160.                         while (enumerator.MoveNext()) {
  161.                             TSource item = enumerator.Current;
  162.                             if (predicate(item)) {
  163.                                 current = item;
  164.                                 return true;
  165.                             }
  166.                         }
  167.                         Dispose();
  168.                         break;
  169.                 }
  170.                 return false;
  171.             }
  172.  
  173.             public override IEnumerable Select(Func selector) {
  174.                 return new WhereSelectEnumerableIterator(source, predicate, selector);
  175.             }
  176.  
  177.             public override IEnumerable Where(Func predicate) {
  178.                 return new WhereEnumerableIterator(source, CombinePredicates(this.predicate, predicate));
  179.             }
  180.         }
  181.  
  182.         class WhereArrayIterator : Iterator
  183.         {
  184.             TSource[] source;
  185.             Func predicate;
  186.             int index;
  187.  
  188.             public WhereArrayIterator(TSource[] source, Func predicate) {
  189.                 this.source = source;
  190.                 this.predicate = predicate;
  191.             }
  192.  
  193.             public override Iterator Clone() {
  194.                 return new WhereArrayIterator(source, predicate);
  195.             }
  196.  
  197.             public override bool MoveNext() {
  198.                 if (state == 1) {
  199.                     while (index < source.Length) {
  200.                         TSource item = source[index];
  201.                         index++;
  202.                         if (predicate(item)) {
  203.                             current = item;
  204.                             return true;
  205.                         }
  206.                     }
  207.                     Dispose();
  208.                 }
  209.                 return false;
  210.             }
  211.  
  212.             public override IEnumerable Select(Func selector) {
  213.                 return new WhereSelectArrayIterator(source, predicate, selector);
  214.             }
  215.  
  216.             public override IEnumerable Where(Func predicate) {
  217.                 return new WhereArrayIterator(source, CombinePredicates(this.predicate, predicate));
  218.             }
  219.         }
  220.  
  221.         class WhereListIterator : Iterator
  222.         {
  223.             List source;
  224.             Func predicate;
  225.             List.Enumerator enumerator;
  226.  
  227.             public WhereListIterator(List source, Func predicate) {
  228.                 this.source = source;
  229.                 this.predicate = predicate;
  230.             }
  231.  
  232.             public override Iterator Clone() {
  233.                 return new WhereListIterator(source, predicate);
  234.             }
  235.  
  236.             public override bool MoveNext() {
  237.                 switch (state) {
  238.                     case 1:
  239.                         enumerator = source.GetEnumerator();
  240.                         state = 2;
  241.                         goto case 2;
  242.                     case 2:
  243.                         while (enumerator.MoveNext()) {
  244.                             TSource item = enumerator.Current;
  245.                             if (predicate(item)) {
  246.                                 current = item;
  247.                                 return true;
  248.                             }
  249.                         }
  250.                         Dispose();
  251.                         break;
  252.                 }
  253.                 return false;
  254.             }
  255.  
  256.             public override IEnumerable Select(Func selector) {
  257.                 return new WhereSelectListIterator(source, predicate, selector);
  258.             }
  259.  
  260.             public override IEnumerable Where(Func predicate) {
  261.                 return new WhereListIterator(source, CombinePredicates(this.predicate, predicate));
  262.             }
  263.         }
  264.  
  265.         class WhereSelectEnumerableIterator : Iterator
  266.         {
  267.             IEnumerable source;
  268.             Func predicate;
  269.             Func selector;
  270.             IEnumerator enumerator;
  271.  
  272.             public WhereSelectEnumerableIterator(IEnumerable source, Func predicate, Func selector) {
  273.                 this.source = source;
  274.                 this.predicate = predicate;
  275.                 this.selector = selector;
  276.             }
  277.  
  278.             public override Iterator Clone() {
  279.                 return new WhereSelectEnumerableIterator(source, predicate, selector);
  280.             }
  281.  
  282.             public override void Dispose() {
  283.                 if (enumerator is IDisposable) ((IDisposable)enumerator).Dispose();
  284.                 enumerator = null;
  285.                 base.Dispose();
  286.             }
  287.  
  288.             public override bool MoveNext() {
  289.                 switch (state) {
  290.                     case 1:
  291.                         enumerator = source.GetEnumerator();
  292.                         state = 2;
  293.                         goto case 2;
  294.                     case 2:
  295.                         while (enumerator.MoveNext()) {
  296.                             TSource item = enumerator.Current;
  297.                             if (predicate == null || predicate(item)) {
  298.                                 current = selector(item);
  299.                                 return true;
  300.                             }
  301.                         }
  302.                         Dispose();
  303.                         break;
  304.                 }
  305.                 return false;
  306.             }
  307.  
  308.             public override IEnumerable Select(Func selector) {
  309.                 return new WhereSelectEnumerableIterator(source, predicate, CombineSelectors(this.selector, selector));
  310.             }
  311.  
  312.             public override IEnumerable Where(Func predicate) {
  313.                 return new WhereEnumerableIterator(this, predicate);
  314.             }
  315.         }
  316.  
  317.         class WhereSelectArrayIterator : Iterator
  318.         {
  319.             TSource[] source;
  320.             Func predicate;
  321.             Func selector;
  322.             int index;
  323.  
  324.             public WhereSelectArrayIterator(TSource[] source, Func predicate, Func selector) {
  325.                 this.source = source;
  326.                 this.predicate = predicate;
  327.                 this.selector = selector;
  328.             }
  329.  
  330.             public override Iterator Clone() {
  331.                 return new WhereSelectArrayIterator(source, predicate, selector);
  332.             }
  333.  
  334.             public override bool MoveNext() {
  335.                 if (state == 1) {
  336.                     while (index < source.Length) {
  337.                         TSource item = source[index];
  338.                         index++;
  339.                         if (predicate == null || predicate(item)) {
  340.                             current = selector(item);
  341.                             return true;
  342.                         }
  343.                     }
  344.                     Dispose();
  345.                 }
  346.                 return false;
  347.             }
  348.  
  349.             public override IEnumerable Select(Func selector) {
  350.                 return new WhereSelectArrayIterator(source, predicate, CombineSelectors(this.selector, selector));
  351.             }
  352.  
  353.             public override IEnumerable Where(Func predicate) {
  354.                 return new WhereEnumerableIterator(this, predicate);
  355.             }
  356.         }
  357.  
  358.         class WhereSelectListIterator : Iterator
  359.         {
  360.             List source;
  361.             Func predicate;
  362.             Func selector;
  363.             List.Enumerator enumerator;
  364.  
  365.             public WhereSelectListIterator(List source, Func predicate, Func selector) {
  366.                 this.source = source;
  367.                 this.predicate = predicate;
  368.                 this.selector = selector;
  369.             }
  370.  
  371.             public override Iterator Clone() {
  372.                 return new WhereSelectListIterator(source, predicate, selector);
  373.             }
  374.  
  375.             public override bool MoveNext() {
  376.                 switch (state) {
  377.                     case 1:
  378.                         enumerator = source.GetEnumerator();
  379.                         state = 2;
  380.                         goto case 2;
  381.                     case 2:
  382.                         while (enumerator.MoveNext()) {
  383.                             TSource item = enumerator.Current;
  384.                             if (predicate == null || predicate(item)) {
  385.                                 current = selector(item);
  386.                                 return true;
  387.                             }
  388.                         }
  389.                         Dispose();
  390.                         break;
  391.                 }
  392.                 return false;
  393.             }
  394.  
  395.             public override IEnumerable Select(Func selector) {
  396.                 return new WhereSelectListIterator(source, predicate, CombineSelectors(this.selector, selector));
  397.             }
  398.  
  399.             public override IEnumerable Where(Func predicate) {
  400.                 return new WhereEnumerableIterator(this, predicate);
  401.             }
  402.         }
  403.  
  404.         //public static IEnumerable Where(this IEnumerable source, Func predicate) {
  405.         //    if (source == null) throw Error.ArgumentNull("source");
  406.         //    if (predicate == null) throw Error.ArgumentNull("predicate");
  407.         //    return WhereIterator(source, predicate);
  408.         //}
  409.  
  410.         //static IEnumerable WhereIterator(IEnumerable source, Func predicate) {
  411.         //    foreach (TSource element in source) {
  412.         //        if (predicate(element)) yield return element;
  413.         //    }
  414.         //}
  415.  
  416.         //public static IEnumerable Select(this IEnumerable source, Func selector) {
  417.         //    if (source == null) throw Error.ArgumentNull("source");
  418.         //    if (selector == null) throw Error.ArgumentNull("selector");
  419.         //    return SelectIterator(source, selector);
  420.         //}
  421.  
  422.         //static IEnumerable SelectIterator(IEnumerable source, Func selector) {
  423.         //    foreach (TSource element in source) {
  424.         //        yield return selector(element);
  425.         //    }
  426.         //}
  427.  
  428.         public static IEnumerable SelectMany(this IEnumerable source, Func> selector) {
  429.             if (source == null) throw Error.ArgumentNull("source");
  430.             if (selector == null) throw Error.ArgumentNull("selector");
  431.             return SelectManyIterator(source, selector);
  432.         }
  433.  
  434.         static IEnumerable SelectManyIterator(IEnumerable source, Func> selector) {
  435.             foreach (TSource element in source) {
  436.                 foreach (TResult subElement in selector(element)) {
  437.                     yield return subElement;
  438.                 }
  439.             }
  440.         }
  441.  
  442.         public static IEnumerable SelectMany(this IEnumerable source, Func> selector) {
  443.             if (source == null) throw Error.ArgumentNull("source");
  444.             if (selector == null) throw Error.ArgumentNull("selector");
  445.             return SelectManyIterator(source, selector);
  446.         }
  447.  
  448.         static IEnumerable SelectManyIterator(IEnumerable source, Func> selector) {
  449.             int index = -1;
  450.             foreach (TSource element in source) {
  451.                 checked { index++; }
  452.                 foreach (TResult subElement in selector(element, index)) {
  453.                     yield return subElement;
  454.                 }
  455.             }
  456.         }
  457.         public static IEnumerable SelectMany(this IEnumerable source, Func> collectionSelector, Func resultSelector)
  458.         {
  459.             if (source == null) throw Error.ArgumentNull("source");
  460.             if (collectionSelector == null) throw Error.ArgumentNull("collectionSelector");
  461.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  462.             return SelectManyIterator(source, collectionSelector, resultSelector);
  463.         }
  464.  
  465.         static IEnumerable SelectManyIterator(IEnumerable source, Func> collectionSelector, Func resultSelector){
  466.             int index = -1;
  467.             foreach (TSource element in source){
  468.                 checked { index++; }
  469.                 foreach (TCollection subElement in collectionSelector(element, index)){
  470.                     yield return resultSelector(element, subElement);
  471.                 }
  472.             }
  473.         }
  474.  
  475.         public static IEnumerable SelectMany(this IEnumerable source, Func> collectionSelector, Func resultSelector) {
  476.             if (source == null) throw Error.ArgumentNull("source");
  477.             if (collectionSelector == null) throw Error.ArgumentNull("collectionSelector");
  478.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  479.             return SelectManyIterator(source, collectionSelector, resultSelector);
  480.         }
  481.  
  482.         static IEnumerable SelectManyIterator(IEnumerable source, Func> collectionSelector, Func resultSelector) {
  483.             foreach (TSource element in source) {
  484.                 foreach (TCollection subElement in collectionSelector(element)) {
  485.                     yield return resultSelector(element, subElement);
  486.                 }
  487.             }
  488.         }
  489.  
  490.         public static IEnumerable Take(this IEnumerable source, int count) {
  491.             if (source == null) throw Error.ArgumentNull("source");
  492.             return TakeIterator(source, count);
  493.         }
  494.  
  495.         static IEnumerable TakeIterator(IEnumerable source, int count) {
  496.             if (count > 0) {
  497.                 foreach (TSource element in source) {
  498.                     yield return element;
  499.                     if (--count == 0) break;
  500.                 }
  501.             }
  502.         }
  503.  
  504.         public static IEnumerable TakeWhile(this IEnumerable source, Func predicate) {
  505.             if (source == null) throw Error.ArgumentNull("source");
  506.             if (predicate == null) throw Error.ArgumentNull("predicate");
  507.             return TakeWhileIterator(source, predicate);
  508.         }
  509.  
  510.         static IEnumerable TakeWhileIterator(IEnumerable source, Func predicate) {
  511.             foreach (TSource element in source) {
  512.                 if (!predicate(element)) break;
  513.                 yield return element;
  514.             }
  515.         }
  516.  
  517.         public static IEnumerable TakeWhile(this IEnumerable source, Func predicate) {
  518.             if (source == null) throw Error.ArgumentNull("source");
  519.             if (predicate == null) throw Error.ArgumentNull("predicate");
  520.             return TakeWhileIterator(source, predicate);
  521.         }
  522.  
  523.         static IEnumerable TakeWhileIterator(IEnumerable source, Func predicate) {
  524.             int index = -1;
  525.             foreach (TSource element in source) {
  526.                 checked { index++; }
  527.                 if (!predicate(element, index)) break;
  528.                 yield return element;
  529.             }
  530.         }
  531.  
  532.         public static IEnumerable Skip(this IEnumerable source, int count) {
  533.             if (source == null) throw Error.ArgumentNull("source");
  534.             return SkipIterator(source, count);
  535.         }
  536.  
  537.         static IEnumerable SkipIterator(IEnumerable source, int count) {
  538.             using (IEnumerator e = source.GetEnumerator()) {
  539.                 while (count > 0 && e.MoveNext()) count--;
  540.                 if (count <= 0) {
  541.                     while (e.MoveNext()) yield return e.Current;
  542.                 }
  543.             }
  544.         }
  545.  
  546.         public static IEnumerable SkipWhile(this IEnumerable source, Func predicate) {
  547.             if (source == null) throw Error.ArgumentNull("source");
  548.             if (predicate == null) throw Error.ArgumentNull("predicate");
  549.             return SkipWhileIterator(source, predicate);
  550.         }
  551.  
  552.         static IEnumerable SkipWhileIterator(IEnumerable source, Func predicate) {
  553.             bool yielding = false;
  554.             foreach (TSource element in source) {
  555.                 if (!yielding && !predicate(element)) yielding = true;
  556.                 if (yielding) yield return element;
  557.             }
  558.         }
  559.  
  560.         public static IEnumerable SkipWhile(this IEnumerable source, Func predicate) {
  561.             if (source == null) throw Error.ArgumentNull("source");
  562.             if (predicate == null) throw Error.ArgumentNull("predicate");
  563.             return SkipWhileIterator(source, predicate);
  564.         }
  565.  
  566.         static IEnumerable SkipWhileIterator(IEnumerable source, Func predicate) {
  567.             int index = -1;
  568.             bool yielding = false;
  569.             foreach (TSource element in source) {
  570.                 checked { index++; }
  571.                 if (!yielding && !predicate(element, index)) yielding = true;
  572.                 if (yielding) yield return element;
  573.             }
  574.         }
  575.  
  576.         public static IEnumerable Join(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector) {
  577.             if (outer == null) throw Error.ArgumentNull("outer");
  578.             if (inner == null) throw Error.ArgumentNull("inner");
  579.             if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector");
  580.             if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector");
  581.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  582.             return JoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, null);
  583.         }
  584.  
  585.         public static IEnumerable Join(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer) {
  586.             if (outer == null) throw Error.ArgumentNull("outer");
  587.             if (inner == null) throw Error.ArgumentNull("inner");
  588.             if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector");
  589.             if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector");
  590.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  591.             return JoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  592.         }
  593.  
  594.         static IEnumerable JoinIterator(IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func resultSelector, IEqualityComparer comparer) {
  595.             Lookup lookup = Lookup.CreateForJoin(inner, innerKeySelector, comparer);
  596.             foreach (TOuter item in outer) {
  597.                 Lookup.Grouping g = lookup.GetGrouping(outerKeySelector(item), false);
  598.                 if (g != null) {
  599.                     for (int i = 0; i < g.count; i++) {
  600.                         yield return resultSelector(item, g.elements[i]);
  601.                     }
  602.                 }
  603.             }
  604.         }
  605.  
  606.         public static IEnumerable GroupJoin(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector) {
  607.             if (outer == null) throw Error.ArgumentNull("outer");
  608.             if (inner == null) throw Error.ArgumentNull("inner");
  609.             if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector");
  610.             if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector");
  611.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  612.             return GroupJoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, null);
  613.         }
  614.  
  615.         public static IEnumerable GroupJoin(this IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector, IEqualityComparer comparer) {
  616.             if (outer == null) throw Error.ArgumentNull("outer");
  617.             if (inner == null) throw Error.ArgumentNull("inner");
  618.             if (outerKeySelector == null) throw Error.ArgumentNull("outerKeySelector");
  619.             if (innerKeySelector == null) throw Error.ArgumentNull("innerKeySelector");
  620.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  621.             return GroupJoinIterator(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  622.         }
  623.  
  624.         static IEnumerable GroupJoinIterator(IEnumerable outer, IEnumerable inner, Func outerKeySelector, Func innerKeySelector, Func, TResult> resultSelector, IEqualityComparer comparer) {
  625.             Lookup lookup = Lookup.CreateForJoin(inner, innerKeySelector, comparer);
  626.             foreach (TOuter item in outer) {
  627.                 yield return resultSelector(item, lookup[outerKeySelector(item)]);
  628.             }
  629.         }
  630.  
  631.         public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector) {
  632.             return new OrderedEnumerable(source, keySelector, null, false);
  633.         }
  634.  
  635.         public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer comparer) {
  636.             return new OrderedEnumerable(source, keySelector, comparer, false);
  637.         }
  638.  
  639.         public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector) {
  640.             return new OrderedEnumerable(source, keySelector, null, true);
  641.         }
  642.  
  643.         public static IOrderedEnumerable OrderByDescending(this IEnumerable source, Func keySelector, IComparer comparer) {
  644.             return new OrderedEnumerable(source, keySelector, comparer, true);
  645.         }
  646.  
  647.         public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector) {
  648.             if (source == null) throw Error.ArgumentNull("source");
  649.             return source.CreateOrderedEnumerable(keySelector, null, false);
  650.         }
  651.  
  652.         public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector, IComparer comparer) {
  653.             if (source == null) throw Error.ArgumentNull("source");
  654.             return source.CreateOrderedEnumerable(keySelector, comparer, false);
  655.         }
  656.  
  657.         public static IOrderedEnumerable ThenByDescending(this IOrderedEnumerable source, Func keySelector) {
  658.             if (source == null) throw Error.ArgumentNull("source");
  659.             return source.CreateOrderedEnumerable(keySelector, null, true);
  660.         }
  661.  
  662.         public static IOrderedEnumerable ThenByDescending(this IOrderedEnumerable source, Func keySelector, IComparer comparer) {
  663.             if (source == null) throw Error.ArgumentNull("source");
  664.             return source.CreateOrderedEnumerable(keySelector, comparer, true);
  665.         }
  666.  
  667.         public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector) {
  668.             return new GroupedEnumerable(source, keySelector, IdentityFunction.Instance, null);
  669.         }
  670.  
  671.         public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector, IEqualityComparer comparer) {
  672.             return new GroupedEnumerable(source, keySelector, IdentityFunction.Instance, comparer);
  673.         }
  674.  
  675.         public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector, Func elementSelector) {
  676.             return new GroupedEnumerable(source, keySelector, elementSelector, null);
  677.         }
  678.  
  679.         public static IEnumerable> GroupBy(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) {
  680.             return new GroupedEnumerable(source, keySelector, elementSelector, comparer);
  681.         }
  682.  
  683.        public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func, TResult> resultSelector){
  684.            return  new GroupedEnumerable(source, keySelector, IdentityFunction.Instance, resultSelector, null);
  685.         }
  686.  
  687.         public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector){
  688.            return new GroupedEnumerable(source, keySelector, elementSelector, resultSelector, null);
  689.         }
  690.  
  691.         public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func, TResult> resultSelector, IEqualityComparer comparer){
  692.             return  new GroupedEnumerable(source, keySelector, IdentityFunction.Instance, resultSelector, comparer);
  693.         }
  694.  
  695.         public static IEnumerable GroupBy(this IEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector, IEqualityComparer comparer){
  696.             return  new GroupedEnumerable(source, keySelector, elementSelector, resultSelector, comparer);
  697.         }
  698.  
  699.         public static IEnumerable Concat(this IEnumerable first, IEnumerable second) {
  700.             if (first == null) throw Error.ArgumentNull("first");
  701.             if (second == null) throw Error.ArgumentNull("second");
  702.             return ConcatIterator(first, second);
  703.         }
  704.  
  705.         static IEnumerable ConcatIterator(IEnumerable first, IEnumerable second) {
  706.             foreach (TSource element in first) yield return element;
  707.             foreach (TSource element in second) yield return element;
  708.         }
  709.  
  710.         public static IEnumerable Zip(this IEnumerable first, IEnumerable second, Func resultSelector) {
  711.             if (first == null) throw Error.ArgumentNull("first");
  712.             if (second == null) throw Error.ArgumentNull("second");
  713.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  714.             return ZipIterator(first, second, resultSelector);
  715.         }
  716.  
  717.         static IEnumerable ZipIterator(IEnumerable first, IEnumerable second, Func resultSelector) {
  718.             using (IEnumerator e1 = first.GetEnumerator())
  719.                 using (IEnumerator e2 = second.GetEnumerator())
  720.                     while (e1.MoveNext() && e2.MoveNext())
  721.                         yield return resultSelector(e1.Current, e2.Current);
  722.         }
  723.  
  724.  
  725.         public static IEnumerable Distinct(this IEnumerable source) {
  726.             if (source == null) throw Error.ArgumentNull("source");
  727.             return DistinctIterator(source, null);
  728.         }
  729.  
  730.         public static IEnumerable Distinct(this IEnumerable source, IEqualityComparer comparer) {
  731.             if (source == null) throw Error.ArgumentNull("source");
  732.             return DistinctIterator(source, comparer);
  733.         }
  734.  
  735.         static IEnumerable DistinctIterator(IEnumerable source, IEqualityComparer comparer) {
  736.             Set set = new Set(comparer);
  737.             foreach (TSource element in source)
  738.                 if (set.Add(element)) yield return element;
  739.         }
  740.  
  741.         public static IEnumerable Union(this IEnumerable first, IEnumerable second) {
  742.             if (first == null) throw Error.ArgumentNull("first");
  743.             if (second == null) throw Error.ArgumentNull("second");
  744.             return UnionIterator(first, second, null);
  745.         }
  746.  
  747.         public static IEnumerable Union(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
  748.         {
  749.             if (first == null) throw Error.ArgumentNull("first");
  750.             if (second == null) throw Error.ArgumentNull("second");
  751.             return UnionIterator(first, second, comparer);
  752.         }
  753.  
  754.         static IEnumerable UnionIterator(IEnumerable first, IEnumerable second, IEqualityComparer comparer)
  755.         {
  756.             Set set = new Set(comparer);
  757.             foreach (TSource element in first)
  758.                 if (set.Add(element)) yield return element;
  759.             foreach (TSource element in second)
  760.                 if (set.Add(element)) yield return element;
  761.         }
  762.  
  763.         public static IEnumerable Intersect(this IEnumerable first, IEnumerable second) {
  764.             if (first == null) throw Error.ArgumentNull("first");
  765.             if (second == null) throw Error.ArgumentNull("second");
  766.             return IntersectIterator(first, second, null);
  767.         }
  768.  
  769.         public static IEnumerable Intersect(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
  770.         {
  771.             if (first == null) throw Error.ArgumentNull("first");
  772.             if (second == null) throw Error.ArgumentNull("second");
  773.             return IntersectIterator(first, second, comparer);
  774.         }
  775.  
  776.         static IEnumerable IntersectIterator(IEnumerable first, IEnumerable second, IEqualityComparer comparer)
  777.         {
  778.             Set set = new Set(comparer);
  779.             foreach (TSource element in second) set.Add(element);
  780.             foreach (TSource element in first)
  781.                 if (set.Remove(element)) yield return element;
  782.         }
  783.  
  784.         public static IEnumerable Except(this IEnumerable first, IEnumerable second)
  785.         {
  786.             if (first == null) throw Error.ArgumentNull("first");
  787.             if (second == null) throw Error.ArgumentNull("second");
  788.             return ExceptIterator(first, second, null);
  789.         }
  790.  
  791.         public static IEnumerable Except(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
  792.         {
  793.             if (first == null) throw Error.ArgumentNull("first");
  794.             if (second == null) throw Error.ArgumentNull("second");
  795.             return ExceptIterator(first, second, comparer);
  796.         }
  797.  
  798.         static IEnumerable ExceptIterator(IEnumerable first, IEnumerable second, IEqualityComparer comparer) {
  799.             Set set = new Set(comparer);
  800.             foreach (TSource element in second) set.Add(element);
  801.             foreach (TSource element in first)
  802.                 if (set.Add(element)) yield return element;
  803.         }
  804.  
  805.         public static IEnumerable Reverse(this IEnumerable source) {
  806.             if (source == null) throw Error.ArgumentNull("source");
  807.             return ReverseIterator(source);
  808.         }
  809.  
  810.         static IEnumerable ReverseIterator(IEnumerable source) {
  811.             Buffer buffer = new Buffer(source);
  812.             for (int i = buffer.count - 1; i >= 0; i--) yield return buffer.items[i];
  813.         }
  814.  
  815.         public static bool SequenceEqual(this IEnumerable first, IEnumerable second) {
  816.             return SequenceEqual(first, second, null);
  817.         }
  818.  
  819.         public static bool SequenceEqual(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
  820.         {
  821.             if (comparer == null) comparer = EqualityComparer.Default;
  822.             if (first == null) throw Error.ArgumentNull("first");
  823.             if (second == null) throw Error.ArgumentNull("second");
  824.             using (IEnumerator e1 = first.GetEnumerator())
  825.             using (IEnumerator e2 = second.GetEnumerator())
  826.             {
  827.                 while (e1.MoveNext())
  828.                 {
  829.                     if (!(e2.MoveNext() && comparer.Equals(e1.Current, e2.Current))) return false;
  830.                 }
  831.                 if (e2.MoveNext()) return false;
  832.             }
  833.             return true;
  834.         }
  835.  
  836.         public static IEnumerable AsEnumerable(this IEnumerable source)
  837.         {
  838.             return source;
  839.         }
  840.  
  841.         public static TSource[] ToArray(this IEnumerable source) {
  842.             if (source == null) throw Error.ArgumentNull("source");
  843.             return new Buffer(source).ToArray();
  844.         }
  845.  
  846.         public static List ToList(this IEnumerable source) {
  847.             if (source == null) throw Error.ArgumentNull("source");
  848.             return new List(source);
  849.         }
  850.  
  851.         public static Dictionary ToDictionary(this IEnumerable source, Func keySelector) {
  852.             return ToDictionary(source, keySelector, IdentityFunction.Instance, null);
  853.         }
  854.  
  855.         public static Dictionary ToDictionary(this IEnumerable source, Func keySelector, IEqualityComparer comparer) {
  856.             return ToDictionary(source, keySelector, IdentityFunction.Instance, comparer);
  857.         }
  858.  
  859.         public static Dictionary ToDictionary(this IEnumerable source, Func keySelector, Func elementSelector) {
  860.             return ToDictionary(source, keySelector, elementSelector, null);
  861.         }
  862.  
  863.         public static Dictionary ToDictionary(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) {
  864.             if (source == null) throw Error.ArgumentNull("source");
  865.             if (keySelector == null) throw Error.ArgumentNull("keySelector");
  866.             if (elementSelector == null) throw Error.ArgumentNull("elementSelector");
  867.             Dictionary d = new Dictionary(comparer);
  868.             foreach (TSource element in source) d.Add(keySelector(element), elementSelector(element));
  869.             return d;
  870.         }
  871.  
  872.         public static ILookup ToLookup(this IEnumerable source, Func keySelector) {
  873.             return Lookup.Create(source, keySelector, IdentityFunction.Instance, null);
  874.         }
  875.  
  876.         public static ILookup ToLookup(this IEnumerable source, Func keySelector, IEqualityComparer comparer) {
  877.             return Lookup.Create(source, keySelector, IdentityFunction.Instance, comparer);
  878.         }
  879.  
  880.         public static ILookup ToLookup(this IEnumerable source, Func keySelector, Func elementSelector) {
  881.             return Lookup.Create(source, keySelector, elementSelector, null);
  882.         }
  883.  
  884.         public static ILookup ToLookup(this IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) {
  885.             return Lookup.Create(source, keySelector, elementSelector, comparer);
  886.         }
  887.  
  888.         public static IEnumerable DefaultIfEmpty(this IEnumerable source) {
  889.             return DefaultIfEmpty(source, default(TSource));
  890.         }
  891.  
  892.         public static IEnumerable DefaultIfEmpty(this IEnumerable source, TSource defaultValue) {
  893.             if (source == null) throw Error.ArgumentNull("source");
  894.             return DefaultIfEmptyIterator(source, defaultValue);
  895.         }
  896.  
  897.         static IEnumerable DefaultIfEmptyIterator(IEnumerable source, TSource defaultValue) {
  898.             using (IEnumerator e = source.GetEnumerator()) {
  899.                 if (e.MoveNext()) {
  900.                     do {
  901.                         yield return e.Current;
  902.                     } while (e.MoveNext());
  903.                 }
  904.                 else {
  905.                     yield return defaultValue;
  906.                 }
  907.             }
  908.         }
  909.  
  910.         public static IEnumerable OfType(this IEnumerable source) {
  911.             if (source == null) throw Error.ArgumentNull("source");
  912.             return OfTypeIterator(source);
  913.         }
  914.  
  915.         static IEnumerable OfTypeIterator(IEnumerable source) {
  916.             foreach (object obj in source) {
  917.                 if (obj is TResult) yield return (TResult)obj;
  918.             }
  919.         }
  920.  
  921.         public static IEnumerable Cast(this IEnumerable source) {
  922.             IEnumerable typedSource = source as IEnumerable;
  923.             if (typedSource != null) return typedSource;
  924.             if (source == null) throw Error.ArgumentNull("source");
  925.             return CastIterator(source);
  926.         }
  927.  
  928.         static IEnumerable CastIterator(IEnumerable source) {
  929.             foreach (object obj in source) yield return (TResult)obj;
  930.         }
  931.  
  932.         public static TSource First(this IEnumerable source) {
  933.             if (source == null) throw Error.ArgumentNull("source");
  934.             IList list = source as IList;
  935.             if (list != null) {
  936.                 if (list.Count > 0) return list[0];
  937.             }
  938.             else {
  939.                 using (IEnumerator e = source.GetEnumerator()) {
  940.                     if (e.MoveNext()) return e.Current;
  941.                 }
  942.             }
  943.             throw Error.NoElements();
  944.         }
  945.  
  946.         public static TSource First(this IEnumerable source, Func predicate) {
  947.             if (source == null) throw Error.ArgumentNull("source");
  948.             if (predicate == null) throw Error.ArgumentNull("predicate");
  949.             foreach (TSource element in source) {
  950.                 if (predicate(element)) return element;
  951.             }
  952.             throw Error.NoMatch();
  953.         }
  954.  
  955.         public static TSource FirstOrDefault(this IEnumerable source) {
  956.             if (source == null) throw Error.ArgumentNull("source");
  957.             IList list = source as IList;
  958.             if (list != null) {
  959.                 if (list.Count > 0) return list[0];
  960.             }
  961.             else {
  962.                 using (IEnumerator e = source.GetEnumerator()) {
  963.                     if (e.MoveNext()) return e.Current;
  964.                 }
  965.             }
  966.             return default(TSource);
  967.         }
  968.  
  969.         public static TSource FirstOrDefault(this IEnumerable source, Func predicate) {
  970.             if (source == null) throw Error.ArgumentNull("source");
  971.             if (predicate == null) throw Error.ArgumentNull("predicate");
  972.             foreach (TSource element in source) {
  973.                 if (predicate(element)) return element;
  974.             }
  975.             return default(TSource);
  976.         }
  977.  
  978.         public static TSource Last(this IEnumerable source) {
  979.             if (source == null) throw Error.ArgumentNull("source");
  980.             IList list = source as IList;
  981.             if (list != null) {
  982.                 int count = list.Count;
  983.                 if (count > 0) return list[count - 1];
  984.             }
  985.             else {
  986.                 using (IEnumerator e = source.GetEnumerator()) {
  987.                     if (e.MoveNext()) {
  988.                         TSource result;
  989.                         do {
  990.                             result = e.Current;
  991.                         } while (e.MoveNext());
  992.                         return result;
  993.                     }
  994.                 }
  995.             }
  996.             throw Error.NoElements();
  997.         }
  998.  
  999.         public static TSource Last(this IEnumerable source, Func predicate) {
  1000.             if (source == null) throw Error.ArgumentNull("source");
  1001.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1002.             TSource result = default(TSource);
  1003.             bool found = false;
  1004.             foreach (TSource element in source) {
  1005.                 if (predicate(element)) {
  1006.                     result = element;
  1007.                     found = true;
  1008.                 }
  1009.             }
  1010.             if (found) return result;
  1011.             throw Error.NoMatch();
  1012.         }
  1013.  
  1014.         public static TSource LastOrDefault(this IEnumerable source) {
  1015.             if (source == null) throw Error.ArgumentNull("source");
  1016.             IList list = source as IList;
  1017.             if (list != null) {
  1018.                 int count = list.Count;
  1019.                 if (count > 0) return list[count - 1];
  1020.             }
  1021.             else {
  1022.                 using (IEnumerator e = source.GetEnumerator()) {
  1023.                     if (e.MoveNext()) {
  1024.                         TSource result;
  1025.                         do {
  1026.                             result = e.Current;
  1027.                         } while (e.MoveNext());
  1028.                         return result;
  1029.                     }
  1030.                 }
  1031.             }
  1032.             return default(TSource);
  1033.         }
  1034.  
  1035.         public static TSource LastOrDefault(this IEnumerable source, Func predicate) {
  1036.             if (source == null) throw Error.ArgumentNull("source");
  1037.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1038.             TSource result = default(TSource);
  1039.             foreach (TSource element in source) {
  1040.                 if (predicate(element)) {
  1041.                     result = element;
  1042.                 }
  1043.             }
  1044.             return result;
  1045.         }
  1046.  
  1047.         public static TSource Single(this IEnumerable source) {
  1048.             if (source == null) throw Error.ArgumentNull("source");
  1049.             IList list = source as IList;
  1050.             if (list != null) {
  1051.                 switch (list.Count) {
  1052.                     case 0: throw Error.NoElements();
  1053.                     case 1: return list[0];
  1054.                 }
  1055.             }
  1056.             else {
  1057.                 using (IEnumerator e = source.GetEnumerator()) {
  1058.                     if (!e.MoveNext()) throw Error.NoElements();
  1059.                     TSource result = e.Current;
  1060.                     if (!e.MoveNext()) return result;
  1061.                 }
  1062.             }
  1063.             throw Error.MoreThanOneElement();
  1064.         }
  1065.  
  1066.         public static TSource Single(this IEnumerable source, Func predicate) {
  1067.             if (source == null) throw Error.ArgumentNull("source");
  1068.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1069.             TSource result = default(TSource);
  1070.             long count = 0;
  1071.             foreach (TSource element in source) {
  1072.                 if (predicate(element)) {
  1073.                     result = element;
  1074.                     checked { count++; }
  1075.                 }
  1076.             }
  1077.             switch (count) {
  1078.                 case 0: throw Error.NoMatch();
  1079.                 case 1: return result;
  1080.             }
  1081.             throw Error.MoreThanOneMatch();
  1082.         }
  1083.  
  1084.         public static TSource SingleOrDefault(this IEnumerable source) {
  1085.             if (source == null) throw Error.ArgumentNull("source");
  1086.             IList list = source as IList;
  1087.             if (list != null) {
  1088.                 switch (list.Count) {
  1089.                     case 0: return default(TSource);
  1090.                     case 1: return list[0];
  1091.                 }
  1092.             }
  1093.             else {
  1094.                 using (IEnumerator e = source.GetEnumerator()) {
  1095.                     if (!e.MoveNext()) return default(TSource);
  1096.                     TSource result = e.Current;
  1097.                     if (!e.MoveNext()) return result;
  1098.                 }
  1099.             }
  1100.             throw Error.MoreThanOneElement();
  1101.         }
  1102.  
  1103.         public static TSource SingleOrDefault(this IEnumerable source, Func predicate) {
  1104.             if (source == null) throw Error.ArgumentNull("source");
  1105.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1106.             TSource result = default(TSource);
  1107.             long count = 0;
  1108.             foreach (TSource element in source) {
  1109.                 if (predicate(element)) {
  1110.                     result = element;
  1111.                     checked { count++; }
  1112.                 }
  1113.             }
  1114.             switch (count) {
  1115.                 case 0: return default(TSource);
  1116.                 case 1: return result;
  1117.             }
  1118.             throw Error.MoreThanOneMatch();
  1119.         }
  1120.  
  1121.         public static TSource ElementAt(this IEnumerable source, int index) {
  1122.             if (source == null) throw Error.ArgumentNull("source");
  1123.             IList list = source as IList;
  1124.             if (list != null) return list[index];
  1125.             if (index < 0) throw Error.ArgumentOutOfRange("index");
  1126.             using (IEnumerator e = source.GetEnumerator()) {
  1127.                 while (true) {
  1128.                     if (!e.MoveNext()) throw Error.ArgumentOutOfRange("index");
  1129.                     if (index == 0) return e.Current;
  1130.                     index--;
  1131.                 }
  1132.             }
  1133.         }
  1134.  
  1135.         public static TSource ElementAtOrDefault(this IEnumerable source, int index) {
  1136.             if (source == null) throw Error.ArgumentNull("source");
  1137.             if (index >= 0) {
  1138.                 IList list = source as IList;
  1139.                 if (list != null) {
  1140.                     if (index < list.Count) return list[index];
  1141.                 }
  1142.                 else {
  1143.                     using (IEnumerator e = source.GetEnumerator()) {
  1144.                         while (true) {
  1145.                             if (!e.MoveNext()) break;
  1146.                             if (index == 0) return e.Current;
  1147.                             index--;
  1148.                         }
  1149.                     }
  1150.                 }
  1151.             }
  1152.             return default(TSource);
  1153.         }
  1154.  
  1155.         public static IEnumerable Range(int start, int count) {
  1156.             long max = ((long)start) + count - 1;
  1157.             if (count < 0 || max > Int32.MaxValue) throw Error.ArgumentOutOfRange("count");
  1158.             return RangeIterator(start, count);
  1159.         }
  1160.  
  1161.         static IEnumerable RangeIterator(int start, int count) {
  1162.             for (int i = 0; i < count; i++) yield return start + i;
  1163.         }
  1164.  
  1165.         public static IEnumerable Repeat(TResult element, int count) {
  1166.             if (count < 0) throw Error.ArgumentOutOfRange("count");
  1167.             return RepeatIterator(element, count);
  1168.         }
  1169.  
  1170.         static IEnumerable RepeatIterator(TResult element, int count) {
  1171.             for (int i = 0; i < count; i++) yield return element;
  1172.         }
  1173.  
  1174.         public static IEnumerable Empty() {
  1175.             return EmptyEnumerable.Instance;
  1176.         }
  1177.  
  1178.         public static bool Any(this IEnumerable source) {
  1179.             if (source == null) throw Error.ArgumentNull("source");
  1180.             using (IEnumerator e = source.GetEnumerator()) {
  1181.                 if (e.MoveNext()) return true;
  1182.             }
  1183.             return false;
  1184.         }
  1185.  
  1186.         public static bool Any(this IEnumerable source, Func predicate) {
  1187.             if (source == null) throw Error.ArgumentNull("source");
  1188.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1189.             foreach (TSource element in source) {
  1190.                 if (predicate(element)) return true;
  1191.             }
  1192.             return false;
  1193.         }
  1194.  
  1195.         public static bool All(this IEnumerable source, Func predicate) {
  1196.             if (source == null) throw Error.ArgumentNull("source");
  1197.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1198.             foreach (TSource element in source) {
  1199.                 if (!predicate(element)) return false;
  1200.             }
  1201.             return true;
  1202.         }
  1203.  
  1204.         public static int Count(this IEnumerable source) {
  1205.             if (source == null) throw Error.ArgumentNull("source");
  1206.             ICollection collectionoft = source as ICollection;
  1207.             if (collectionoft != null) return collectionoft.Count;
  1208.             ICollection collection = source as ICollection;
  1209.             if (collection != null) return collection.Count;
  1210.             int count = 0;
  1211.             using (IEnumerator e = source.GetEnumerator()) {
  1212.                 checked {
  1213.                     while (e.MoveNext()) count++;
  1214.                 }
  1215.             }
  1216.             return count;
  1217.         }
  1218.  
  1219.         public static int Count(this IEnumerable source, Func predicate) {
  1220.             if (source == null) throw Error.ArgumentNull("source");
  1221.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1222.             int count = 0;
  1223.             foreach (TSource element in source) {
  1224.                 checked {
  1225.                     if (predicate(element)) count++;
  1226.                 }
  1227.             }
  1228.             return count;
  1229.         }
  1230.  
  1231.         public static long LongCount(this IEnumerable source) {
  1232.             if (source == null) throw Error.ArgumentNull("source");
  1233.             long count = 0;
  1234.             using (IEnumerator e = source.GetEnumerator()) {
  1235.                 checked {
  1236.                     while (e.MoveNext()) count++;
  1237.                 }
  1238.             }
  1239.             return count;
  1240.         }
  1241.  
  1242.         public static long LongCount(this IEnumerable source, Func predicate) {
  1243.             if (source == null) throw Error.ArgumentNull("source");
  1244.             if (predicate == null) throw Error.ArgumentNull("predicate");
  1245.             long count = 0;
  1246.             foreach (TSource element in source) {
  1247.                 checked {
  1248.                     if (predicate(element)) count++;
  1249.                 }
  1250.             }
  1251.             return count;
  1252.         }
  1253.  
  1254.         public static bool Contains(this IEnumerable source, TSource value) {
  1255.             ICollection collection = source as ICollection;
  1256.             if (collection != null) return collection.Contains(value);
  1257.             return Contains(source, value, null);
  1258.         }
  1259.  
  1260.         public static bool Contains(this IEnumerable source, TSource value, IEqualityComparer comparer)
  1261.         {
  1262.             if (comparer == null) comparer = EqualityComparer.Default;
  1263.             if (source == null) throw Error.ArgumentNull("source");
  1264.             foreach (TSource element in source)
  1265.                 if (comparer.Equals(element, value)) return true;
  1266.             return false;
  1267.         }
  1268.  
  1269.         public static TSource Aggregate(this IEnumerable source, Func func)
  1270.         {
  1271.             if (source == null) throw Error.ArgumentNull("source");
  1272.             if (func == null) throw Error.ArgumentNull("func");
  1273.             using (IEnumerator e = source.GetEnumerator()) {
  1274.                 if (!e.MoveNext()) throw Error.NoElements();
  1275.                 TSource result = e.Current;
  1276.                 while (e.MoveNext()) result = func(result, e.Current);
  1277.                 return result;
  1278.             }
  1279.         }
  1280.  
  1281.         public static TAccumulate Aggregate(this IEnumerable source, TAccumulate seed, Func func) {
  1282.             if (source == null) throw Error.ArgumentNull("source");
  1283.             if (func == null) throw Error.ArgumentNull("func");
  1284.             TAccumulate result = seed;
  1285.             foreach (TSource element in source) result = func(result, element);
  1286.             return result;
  1287.         }
  1288.  
  1289.         public static TResult Aggregate(this IEnumerable source, TAccumulate seed, Func func, Func resultSelector) {
  1290.             if (source == null) throw Error.ArgumentNull("source");
  1291.             if (func == null) throw Error.ArgumentNull("func");
  1292.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  1293.             TAccumulate result = seed;
  1294.             foreach (TSource element in source) result = func(result, element);
  1295.             return resultSelector(result);
  1296.         }
  1297.  
  1298.         public static int Sum(this IEnumerable source) {
  1299.             if (source == null) throw Error.ArgumentNull("source");
  1300.             int sum = 0;
  1301.             checked {
  1302.                 foreach (int v in source) sum += v;
  1303.             }
  1304.             return sum;
  1305.         }
  1306.  
  1307.         public static int? Sum(this IEnumerable source) {
  1308.             if (source == null) throw Error.ArgumentNull("source");
  1309.             int sum = 0;
  1310.             checked {
  1311.                 foreach (int? v in source) {
  1312.                     if (v != null) sum += v.GetValueOrDefault();
  1313.                 }
  1314.             }
  1315.             return sum;
  1316.         }
  1317.  
  1318.         public static long Sum(this IEnumerable source) {
  1319.             if (source == null) throw Error.ArgumentNull("source");
  1320.             long sum = 0;
  1321.             checked {
  1322.                 foreach (long v in source) sum += v;
  1323.             }
  1324.             return sum;
  1325.         }
  1326.  
  1327.         public static long? Sum(this IEnumerable source) {
  1328.             if (source == null) throw Error.ArgumentNull("source");
  1329.             long sum = 0;
  1330.             checked {
  1331.                 foreach (long? v in source) {
  1332.                     if (v != null) sum += v.GetValueOrDefault();
  1333.                 }
  1334.             }
  1335.             return sum;
  1336.         }
  1337.  
  1338.         public static float Sum(this IEnumerable source) {
  1339.             if (source == null) throw Error.ArgumentNull("source");
  1340.             double sum = 0;
  1341.             foreach (float v in source) sum += v;
  1342.             return (float)sum;
  1343.         }
  1344.  
  1345.         public static float? Sum(this IEnumerable source) {
  1346.             if (source == null) throw Error.ArgumentNull("source");
  1347.             double sum = 0;
  1348.             foreach (float? v in source) {
  1349.                 if (v != null) sum += v.GetValueOrDefault();
  1350.             }
  1351.             return (float)sum;
  1352.         }
  1353.  
  1354.         public static double Sum(this IEnumerable source) {
  1355.             if (source == null) throw Error.ArgumentNull("source");
  1356.             double sum = 0;
  1357.             foreach (double v in source) sum += v;
  1358.             return sum;
  1359.         }
  1360.  
  1361.         public static double? Sum(this IEnumerable source) {
  1362.             if (source == null) throw Error.ArgumentNull("source");
  1363.             double sum = 0;
  1364.             foreach (double? v in source) {
  1365.                 if (v != null) sum += v.GetValueOrDefault();
  1366.             }
  1367.             return sum;
  1368.         }
  1369.  
  1370.         public static decimal Sum(this IEnumerable source) {
  1371.             if (source == null) throw Error.ArgumentNull("source");
  1372.             decimal sum = 0;
  1373.             foreach (decimal v in source) sum += v;
  1374.             return sum;
  1375.         }
  1376.  
  1377.         public static decimal? Sum(this IEnumerable source) {
  1378.             if (source == null) throw Error.ArgumentNull("source");
  1379.             decimal sum = 0;
  1380.             foreach (decimal? v in source) {
  1381.                 if (v != null) sum += v.GetValueOrDefault();
  1382.             }
  1383.             return sum;
  1384.         }
  1385.  
  1386.         public static int Sum(this IEnumerable source, Func selector) {
  1387.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1388.         }
  1389.  
  1390.         public static int? Sum(this IEnumerable source, Func selector) {
  1391.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1392.         }
  1393.  
  1394.         public static long Sum(this IEnumerable source, Func selector) {
  1395.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1396.         }
  1397.  
  1398.         public static long? Sum(this IEnumerable source, Func selector) {
  1399.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1400.         }
  1401.  
  1402.         public static float Sum(this IEnumerable source, Func selector) {
  1403.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1404.         }
  1405.  
  1406.         public static float? Sum(this IEnumerable source, Func selector) {
  1407.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1408.         }
  1409.  
  1410.         public static double Sum(this IEnumerable source, Func selector) {
  1411.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1412.         }
  1413.  
  1414.         public static double? Sum(this IEnumerable source, Func selector) {
  1415.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1416.         }
  1417.  
  1418.         public static decimal Sum(this IEnumerable source, Func selector) {
  1419.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1420.         }
  1421.  
  1422.         public static decimal? Sum(this IEnumerable source, Func selector) {
  1423.             return Enumerable.Sum(Enumerable.Select(source, selector));
  1424.         }
  1425.  
  1426.         public static int Min(this IEnumerable source) {
  1427.             if (source == null) throw Error.ArgumentNull("source");
  1428.             int value = 0;
  1429.             bool hasValue = false;
  1430.             foreach (int x in source) {
  1431.                 if (hasValue) {
  1432.                     if (x < value) value = x;
  1433.                 }
  1434.                 else {
  1435.                     value = x;
  1436.                     hasValue = true;
  1437.                 }
  1438.             }
  1439.             if (hasValue) return value;
  1440.             throw Error.NoElements();
  1441.         }
  1442.  
  1443.         public static int? Min(this IEnumerable source) {
  1444.             if (source == null) throw Error.ArgumentNull("source");
  1445.             int? value = null;
  1446.             foreach (int? x in source) {
  1447.                 if (value == null || x < value)
  1448.                     value = x;
  1449.             }
  1450.             return value;
  1451.         }
  1452.  
  1453.         public static long Min(this IEnumerable source) {
  1454.             if (source == null) throw Error.ArgumentNull("source");
  1455.             long value = 0;
  1456.             bool hasValue = false;
  1457.             foreach (long x in source) {
  1458.                 if (hasValue) {
  1459.                     if (x < value) value = x;
  1460.                 }
  1461.                 else {
  1462.                     value = x;
  1463.                     hasValue = true;
  1464.                 }
  1465.             }
  1466.             if (hasValue) return value;
  1467.             throw Error.NoElements();
  1468.         }
  1469.  
  1470.         public static long? Min(this IEnumerable source) {
  1471.             if (source == null) throw Error.ArgumentNull("source");
  1472.             long? value = null;
  1473.             foreach (long? x in source) {
  1474.                 if (value == null || x < value) value = x;
  1475.             }
  1476.             return value;
  1477.         }
  1478.  
  1479.         public static float Min(this IEnumerable source) {
  1480.             if (source == null) throw Error.ArgumentNull("source");
  1481.             float value = 0;
  1482.             bool hasValue = false;
  1483.             foreach (float x in source) {
  1484.                 if (hasValue) {
  1485.                     // Normally NaN < anything is false, as is anything < NaN
  1486.                     // However, this leads to some irksome outcomes in Min and Max.
  1487.                     // If we use those semantics then Min(NaN, 5.0) is NaN, but
  1488.                     // Min(5.0, NaN) is 5.0!  To fix this, we impose a total
  1489.                     // ordering where NaN is smaller than every value, including
  1490.                     // negative infinity.
  1491.                     if (x < value || System.Single.IsNaN(x)) value = x;
  1492.                 }
  1493.                 else {
  1494.                     value = x;
  1495.                     hasValue = true;
  1496.                 }
  1497.             }
  1498.             if (hasValue) return value;
  1499.             throw Error.NoElements();
  1500.         }
  1501.  
  1502.         public static float? Min(this IEnumerable source) {
  1503.             if (source == null) throw Error.ArgumentNull("source");
  1504.             float? value = null;
  1505.             foreach (float? x in source) {
  1506.                 if (x == null) continue;
  1507.                 if (value == null || x < value || System.Single.IsNaN((float)x)) value = x;
  1508.             }
  1509.             return value;
  1510.         }
  1511.  
  1512.         public static double Min(this IEnumerable source) {
  1513.             if (source == null) throw Error.ArgumentNull("source");
  1514.             double value = 0;
  1515.             bool hasValue = false;
  1516.             foreach (double x in source) {
  1517.                 if (hasValue) {
  1518.                     if (x < value || System.Double.IsNaN(x)) value = x;
  1519.                 }
  1520.                 else {
  1521.                     value = x;
  1522.                     hasValue = true;
  1523.                 }
  1524.             }
  1525.             if (hasValue) return value;
  1526.             throw Error.NoElements();
  1527.         }
  1528.  
  1529.         public static double? Min(this IEnumerable source) {
  1530.             if (source == null) throw Error.ArgumentNull("source");
  1531.             double? value = null;
  1532.             foreach (double? x in source) {
  1533.                 if (x == null) continue;
  1534.                 if (value == null || x < value || System.Double.IsNaN((double)x)) value = x;
  1535.             }
  1536.             return value;
  1537.         }
  1538.  
  1539.         public static decimal Min(this IEnumerable source) {
  1540.             if (source == null) throw Error.ArgumentNull("source");
  1541.             decimal value = 0;
  1542.             bool hasValue = false;
  1543.             foreach (decimal x in source) {
  1544.                 if (hasValue) {
  1545.                     if (x < value) value = x;
  1546.                 }
  1547.                 else {
  1548.                     value = x;
  1549.                     hasValue = true;
  1550.                 }
  1551.             }
  1552.             if (hasValue) return value;
  1553.             throw Error.NoElements();
  1554.         }
  1555.  
  1556.         public static decimal? Min(this IEnumerable source) {
  1557.             if (source == null) throw Error.ArgumentNull("source");
  1558.             decimal? value = null;
  1559.             foreach (decimal? x in source) {
  1560.                 if (value == null || x < value) value = x;
  1561.             }
  1562.             return value;
  1563.         }
  1564.  
  1565.         public static TSource Min(this IEnumerable source) {
  1566.             if (source == null) throw Error.ArgumentNull("source");
  1567.             Comparer comparer = Comparer.Default;
  1568.             TSource value = default(TSource);
  1569.             if (value == null) {
  1570.                 foreach (TSource x in source) {
  1571.                     if (x != null && (value == null || comparer.Compare(x, value) < 0))
  1572.                         value = x;
  1573.                 }
  1574.                 return value;
  1575.             }
  1576.             else {
  1577.                 bool hasValue = false;
  1578.                 foreach (TSource x in source) {
  1579.                     if (hasValue) {
  1580.                         if (comparer.Compare(x, value) < 0)
  1581.                             value = x;
  1582.                     }
  1583.                     else {
  1584.                         value = x;
  1585.                         hasValue = true;
  1586.                     }
  1587.                 }
  1588.                 if (hasValue) return value;
  1589.                 throw Error.NoElements();
  1590.             }
  1591.         }
  1592.  
  1593.         public static int Min(this IEnumerable source, Func selector) {
  1594.             return Enumerable.Min(Enumerable.Select(source, selector));
  1595.         }
  1596.  
  1597.         public static int? Min(this IEnumerable source, Func selector) {
  1598.             return Enumerable.Min(Enumerable.Select(source, selector));
  1599.         }
  1600.  
  1601.         public static long Min(this IEnumerable source, Func selector) {
  1602.             return Enumerable.Min(Enumerable.Select(source, selector));
  1603.         }
  1604.  
  1605.         public static long? Min(this IEnumerable source, Func selector) {
  1606.             return Enumerable.Min(Enumerable.Select(source, selector));
  1607.         }
  1608.  
  1609.         public static float Min(this IEnumerable source, Func selector) {
  1610.             return Enumerable.Min(Enumerable.Select(source, selector));
  1611.         }
  1612.  
  1613.         public static float? Min(this IEnumerable source, Func selector) {
  1614.             return Enumerable.Min(Enumerable.Select(source, selector));
  1615.         }
  1616.  
  1617.         public static double Min(this IEnumerable source, Func selector) {
  1618.             return Enumerable.Min(Enumerable.Select(source, selector));
  1619.         }
  1620.  
  1621.         public static double? Min(this IEnumerable source, Func selector) {
  1622.             return Enumerable.Min(Enumerable.Select(source, selector));
  1623.         }
  1624.  
  1625.         public static decimal Min(this IEnumerable source, Func selector) {
  1626.             return Enumerable.Min(Enumerable.Select(source, selector));
  1627.         }
  1628.  
  1629.         public static decimal? Min(this IEnumerable source, Func selector) {
  1630.             return Enumerable.Min(Enumerable.Select(source, selector));
  1631.         }
  1632.  
  1633.         public static TResult Min(this IEnumerable source, Func selector) {
  1634.             return Enumerable.Min(Enumerable.Select(source, selector));
  1635.         }
  1636.  
  1637.         public static int Max(this IEnumerable source) {
  1638.             if (source == null) throw Error.ArgumentNull("source");
  1639.             int value = 0;
  1640.             bool hasValue = false;
  1641.             foreach (int x in source) {
  1642.                 if (hasValue) {
  1643.                     if (x > value) value = x;
  1644.                 }
  1645.                 else {
  1646.                     value = x;
  1647.                     hasValue = true;
  1648.                 }
  1649.             }
  1650.             if (hasValue) return value;
  1651.             throw Error.NoElements();
  1652.         }
  1653.  
  1654.         public static int? Max(this IEnumerable source) {
  1655.             if (source == null) throw Error.ArgumentNull("source");
  1656.             int? value = null;
  1657.             foreach (int? x in source) {
  1658.                 if (value == null || x > value) value = x;
  1659.             }
  1660.             return value;
  1661.         }
  1662.  
  1663.         public static long Max(this IEnumerable source) {
  1664.             if (source == null) throw Error.ArgumentNull("source");
  1665.             long value = 0;
  1666.             bool hasValue = false;
  1667.             foreach (long x in source) {
  1668.                 if (hasValue) {
  1669.                     if (x > value) value = x;
  1670.                 }
  1671.                 else {
  1672.                     value = x;
  1673.                     hasValue = true;
  1674.                 }
  1675.             }
  1676.             if (hasValue) return value;
  1677.             throw Error.NoElements();
  1678.         }
  1679.  
  1680.         public static long? Max(this IEnumerable source) {
  1681.             if (source == null) throw Error.ArgumentNull("source");
  1682.             long? value = null;
  1683.             foreach (long? x in source) {
  1684.                 if (value == null || x > value) value = x;
  1685.             }
  1686.             return value;
  1687.         }
  1688.  
  1689.         public static double Max(this IEnumerable source) {
  1690.             if (source == null) throw Error.ArgumentNull("source");
  1691.             double value = 0;
  1692.             bool hasValue = false;
  1693.             foreach (double x in source) {
  1694.                 if (hasValue) {
  1695.                     if (x > value || System.Double.IsNaN(value)) value = x;
  1696.                 }
  1697.                 else {
  1698.                     value = x;
  1699.                     hasValue = true;
  1700.                 }
  1701.             }
  1702.             if (hasValue) return value;
  1703.             throw Error.NoElements();
  1704.         }
  1705.  
  1706.         public static double? Max(this IEnumerable source) {
  1707.             if (source == null) throw Error.ArgumentNull("source");
  1708.             double? value = null;
  1709.             foreach (double? x in source) {
  1710.                 if (x == null) continue;
  1711.                 if (value == null || x > value || System.Double.IsNaN((double)value)) value = x;
  1712.             }
  1713.             return value;
  1714.         }
  1715.  
  1716.         public static float Max(this IEnumerable source) {
  1717.             if (source == null) throw Error.ArgumentNull("source");
  1718.             float value = 0;
  1719.             bool hasValue = false;
  1720.             foreach (float x in source) {
  1721.                 if (hasValue) {
  1722.                     if (x > value || System.Double.IsNaN(value)) value = x;
  1723.                 }
  1724.                 else {
  1725.                     value = x;
  1726.                     hasValue = true;
  1727.                 }
  1728.             }
  1729.             if (hasValue) return value;
  1730.             throw Error.NoElements();
  1731.         }
  1732.  
  1733.         public static float? Max(this IEnumerable source) {
  1734.             if (source == null) throw Error.ArgumentNull("source");
  1735.             float? value = null;
  1736.             foreach (float? x in source) {
  1737.                 if (x == null) continue;
  1738.                 if (value == null || x > value || System.Single.IsNaN((float)value)) value = x;
  1739.             }
  1740.             return value;
  1741.         }
  1742.  
  1743.         public static decimal Max(this IEnumerable source) {
  1744.             if (source == null) throw Error.ArgumentNull("source");
  1745.             decimal value = 0;
  1746.             bool hasValue = false;
  1747.             foreach (decimal x in source) {
  1748.                 if (hasValue) {
  1749.                     if (x > value) value = x;
  1750.                 }
  1751.                 else {
  1752.                     value = x;
  1753.                     hasValue = true;
  1754.                 }
  1755.             }
  1756.             if (hasValue) return value;
  1757.             throw Error.NoElements();
  1758.         }
  1759.  
  1760.         public static decimal? Max(this IEnumerable source) {
  1761.             if (source == null) throw Error.ArgumentNull("source");
  1762.             decimal? value = null;
  1763.             foreach (decimal? x in source) {
  1764.                 if (value == null || x > value) value = x;
  1765.             }
  1766.             return value;
  1767.         }
  1768.  
  1769.         public static TSource Max(this IEnumerable source) {
  1770.             if (source == null) throw Error.ArgumentNull("source");
  1771.             Comparer comparer = Comparer.Default;
  1772.             TSource value = default(TSource);
  1773.             if (value == null) {
  1774.                 foreach (TSource x in source) {
  1775.                     if (x != null && (value == null || comparer.Compare(x, value) > 0))
  1776.                         value = x;
  1777.                 }
  1778.                 return value;
  1779.             }
  1780.             else {
  1781.                 bool hasValue = false;
  1782.                 foreach (TSource x in source) {
  1783.                     if (hasValue) {
  1784.                         if (comparer.Compare(x, value) > 0)
  1785.                             value = x;
  1786.                     }
  1787.                     else {
  1788.                         value = x;
  1789.                         hasValue = true;
  1790.                     }
  1791.                 }
  1792.                 if (hasValue) return value;
  1793.                 throw Error.NoElements();
  1794.             }
  1795.         }
  1796.  
  1797.         public static int Max(this IEnumerable source, Func selector) {
  1798.             return Enumerable.Max(Enumerable.Select(source, selector));
  1799.         }
  1800.  
  1801.         public static int? Max(this IEnumerable source, Func selector) {
  1802.             return Enumerable.Max(Enumerable.Select(source, selector));
  1803.         }
  1804.  
  1805.         public static long Max(this IEnumerable source, Func selector) {
  1806.             return Enumerable.Max(Enumerable.Select(source, selector));
  1807.         }
  1808.  
  1809.         public static long? Max(this IEnumerable source, Func selector) {
  1810.             return Enumerable.Max(Enumerable.Select(source, selector));
  1811.         }
  1812.  
  1813.         public static float Max(this IEnumerable source, Func selector) {
  1814.             return Enumerable.Max(Enumerable.Select(source, selector));
  1815.         }
  1816.  
  1817.         public static float? Max(this IEnumerable source, Func selector) {
  1818.             return Enumerable.Max(Enumerable.Select(source, selector));
  1819.         }
  1820.  
  1821.         public static double Max(this IEnumerable source, Func selector) {
  1822.             return Enumerable.Max(Enumerable.Select(source, selector));
  1823.         }
  1824.  
  1825.         public static double? Max(this IEnumerable source, Func selector) {
  1826.             return Enumerable.Max(Enumerable.Select(source, selector));
  1827.         }
  1828.  
  1829.         public static decimal Max(this IEnumerable source, Func selector) {
  1830.             return Enumerable.Max(Enumerable.Select(source, selector));
  1831.         }
  1832.  
  1833.         public static decimal? Max(this IEnumerable source, Func selector) {
  1834.             return Enumerable.Max(Enumerable.Select(source, selector));
  1835.         }
  1836.  
  1837.         public static TResult Max(this IEnumerable source, Func selector) {
  1838.             return Enumerable.Max(Enumerable.Select(source, selector));
  1839.         }
  1840.  
  1841.         public static double Average(this IEnumerable source) {
  1842.             if (source == null) throw Error.ArgumentNull("source");
  1843.             long sum = 0;
  1844.             long count = 0;
  1845.             checked {
  1846.                 foreach (int v in source) {
  1847.                     sum += v;
  1848.                     count++;
  1849.                 }
  1850.             }
  1851.             if (count > 0) return (double)sum / count;
  1852.             throw Error.NoElements();
  1853.         }
  1854.  
  1855.         public static double? Average(this IEnumerable source) {
  1856.             if (source == null) throw Error.ArgumentNull("source");
  1857.             long sum = 0;
  1858.             long count = 0;
  1859.             checked {
  1860.                 foreach (int? v in source) {
  1861.                     if (v != null) {
  1862.                         sum += v.GetValueOrDefault();
  1863.                         count++;
  1864.                     }
  1865.                 }
  1866.             }
  1867.             if (count > 0) return (double)sum / count;
  1868.             return null;
  1869.         }
  1870.  
  1871.         public static double Average(this IEnumerable source) {
  1872.             if (source == null) throw Error.ArgumentNull("source");
  1873.             long sum = 0;
  1874.             long count = 0;
  1875.             checked {
  1876.                 foreach (long v in source) {
  1877.                     sum += v;
  1878.                     count++;
  1879.                 }
  1880.             }
  1881.             if (count > 0) return (double)sum / count;
  1882.             throw Error.NoElements();
  1883.         }
  1884.  
  1885.         public static double? Average(this IEnumerable source) {
  1886.             if (source == null) throw Error.ArgumentNull("source");
  1887.             long sum = 0;
  1888.             long count = 0;
  1889.             checked {
  1890.                 foreach (long? v in source) {
  1891.                     if (v != null) {
  1892.                         sum += v.GetValueOrDefault();
  1893.                         count++;
  1894.                     }
  1895.                 }
  1896.             }
  1897.             if (count > 0) return (double)sum / count;
  1898.             return null;
  1899.         }
  1900.  
  1901.         public static float Average(this IEnumerable source) {
  1902.             if (source == null) throw Error.ArgumentNull("source");
  1903.             double sum = 0;
  1904.             long count = 0;
  1905.             checked {
  1906.                 foreach (float v in source) {
  1907.                     sum += v;
  1908.                     count++;
  1909.                 }
  1910.             }
  1911.             if (count > 0) return (float)(sum / count);
  1912.             throw Error.NoElements();
  1913.         }
  1914.  
  1915.         public static float? Average(this IEnumerable source) {
  1916.             if (source == null) throw Error.ArgumentNull("source");
  1917.             double sum = 0;
  1918.             long count = 0;
  1919.             checked {
  1920.                 foreach (float? v in source) {
  1921.                     if (v != null) {
  1922.                         sum += v.GetValueOrDefault();
  1923.                         count++;
  1924.                     }
  1925.                 }
  1926.             }
  1927.             if (count > 0) return (float)(sum / count);
  1928.             return null;
  1929.         }
  1930.  
  1931.         public static double Average(this IEnumerable source) {
  1932.             if (source == null) throw Error.ArgumentNull("source");
  1933.             double sum = 0;
  1934.             long count = 0;
  1935.             checked {
  1936.                 foreach (double v in source) {
  1937.                     sum += v;
  1938.                     count++;
  1939.                 }
  1940.             }
  1941.             if (count > 0) return sum / count;
  1942.             throw Error.NoElements();
  1943.         }
  1944.  
  1945.         public static double? Average(this IEnumerable source) {
  1946.             if (source == null) throw Error.ArgumentNull("source");
  1947.             double sum = 0;
  1948.             long count = 0;
  1949.             checked {
  1950.                 foreach (double? v in source) {
  1951.                     if (v != null) {
  1952.                         sum += v.GetValueOrDefault();
  1953.                         count++;
  1954.                     }
  1955.                 }
  1956.             }
  1957.             if (count > 0) return sum / count;
  1958.             return null;
  1959.         }
  1960.  
  1961.         public static decimal Average(this IEnumerable source) {
  1962.             if (source == null) throw Error.ArgumentNull("source");
  1963.             decimal sum = 0;
  1964.             long count = 0;
  1965.             checked {
  1966.                 foreach (decimal v in source) {
  1967.                     sum += v;
  1968.                     count++;
  1969.                 }
  1970.             }
  1971.             if (count > 0) return sum / count;
  1972.             throw Error.NoElements();
  1973.         }
  1974.  
  1975.         public static decimal? Average(this IEnumerable source) {
  1976.             if (source == null) throw Error.ArgumentNull("source");
  1977.             decimal sum = 0;
  1978.             long count = 0;
  1979.             checked {
  1980.                 foreach (decimal? v in source) {
  1981.                     if (v != null) {
  1982.                         sum += v.GetValueOrDefault();
  1983.                         count++;
  1984.                     }
  1985.                 }
  1986.             }
  1987.             if (count > 0) return sum / count;
  1988.             return null;
  1989.         }
  1990.  
  1991.         public static double Average(this IEnumerable source, Func selector) {
  1992.             return Enumerable.Average(Enumerable.Select(source, selector));
  1993.         }
  1994.  
  1995.         public static double? Average(this IEnumerable source, Func selector) {
  1996.             return Enumerable.Average(Enumerable.Select(source, selector));
  1997.         }
  1998.  
  1999.         public static double Average(this IEnumerable source, Func selector) {
  2000.             return Enumerable.Average(Enumerable.Select(source, selector));
  2001.         }
  2002.  
  2003.         public static double? Average(this IEnumerable source, Func selector) {
  2004.             return Enumerable.Average(Enumerable.Select(source, selector));
  2005.         }
  2006.  
  2007.         public static float Average(this IEnumerable source, Func selector) {
  2008.             return Enumerable.Average(Enumerable.Select(source, selector));
  2009.         }
  2010.  
  2011.         public static float? Average(this IEnumerable source, Func selector) {
  2012.             return Enumerable.Average(Enumerable.Select(source, selector));
  2013.         }
  2014.  
  2015.         public static double Average(this IEnumerable source, Func selector) {
  2016.             return Enumerable.Average(Enumerable.Select(source, selector));
  2017.         }
  2018.  
  2019.         public static double? Average(this IEnumerable source, Func selector) {
  2020.             return Enumerable.Average(Enumerable.Select(source, selector));
  2021.         }
  2022.  
  2023.         public static decimal Average(this IEnumerable source, Func selector) {
  2024.             return Enumerable.Average(Enumerable.Select(source, selector));
  2025.         }
  2026.  
  2027.         public static decimal? Average(this IEnumerable source, Func selector) {
  2028.             return Enumerable.Average(Enumerable.Select(source, selector));
  2029.         }
  2030.     }
  2031.  
  2032.     internal class EmptyEnumerable
  2033.     {
  2034.         static TElement[] instance;
  2035.  
  2036.         public static IEnumerable Instance {
  2037.             get {
  2038.                 if (instance == null) instance = new TElement[0];
  2039.                 return instance;
  2040.             }
  2041.         }
  2042.     }
  2043.  
  2044.     internal class IdentityFunction
  2045.     {
  2046.         public static Func Instance {
  2047.             get { return x => x; }
  2048.         }
  2049.     }
  2050.  
  2051.     public interface IOrderedEnumerable : IEnumerable
  2052.     {
  2053.         IOrderedEnumerable CreateOrderedEnumerable(Func keySelector, IComparer comparer, bool descending);
  2054.     }
  2055.  
  2056. #if SILVERLIGHT
  2057.     public interface IGrouping : IEnumerable
  2058. #else
  2059.     public interface IGrouping : IEnumerable
  2060. #endif
  2061.     {
  2062.         TKey Key { get; }
  2063.     }
  2064.  
  2065.     public interface ILookup : IEnumerable>{
  2066.         int Count { get; }
  2067.         IEnumerable this[TKey key] { get; }
  2068.         bool Contains(TKey key);
  2069.     }
  2070.  
  2071.     public class Lookup : IEnumerable>, ILookup{
  2072.         IEqualityComparer comparer;
  2073.         Grouping[] groupings;
  2074.         Grouping lastGrouping;
  2075.         int count;
  2076.  
  2077.         internal static Lookup Create(IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) {
  2078.             if (source == null) throw Error.ArgumentNull("source");
  2079.             if (keySelector == null) throw Error.ArgumentNull("keySelector");
  2080.             if (elementSelector == null) throw Error.ArgumentNull("elementSelector");
  2081.             Lookup lookup = new Lookup(comparer);
  2082.             foreach (TSource item in source) {
  2083.                 lookup.GetGrouping(keySelector(item), true).Add(elementSelector(item));
  2084.             }
  2085.             return lookup;
  2086.         }
  2087.  
  2088.         internal static Lookup CreateForJoin(IEnumerable source, Func keySelector, IEqualityComparer comparer) {
  2089.             Lookup lookup = new Lookup(comparer);
  2090.             foreach (TElement item in source) {
  2091.                 TKey key = keySelector(item);
  2092.                 if (key != null) lookup.GetGrouping(key, true).Add(item);
  2093.             }
  2094.             return lookup;
  2095.         }
  2096.  
  2097.         Lookup(IEqualityComparer comparer) {
  2098.             if (comparer == null) comparer = EqualityComparer.Default;
  2099.             this.comparer = comparer;
  2100.             groupings = new Grouping[7];
  2101.         }
  2102.  
  2103.         public int Count {
  2104.             get { return count; }
  2105.         }
  2106.  
  2107.         public IEnumerable this[TKey key] {
  2108.             get {
  2109.                 Grouping grouping = GetGrouping(key, false);
  2110.                 if (grouping != null) return grouping;
  2111.                 return EmptyEnumerable.Instance;
  2112.             }
  2113.         }
  2114.  
  2115.         public bool Contains(TKey key) {
  2116.             return GetGrouping(key, false) != null;
  2117.         }
  2118.  
  2119.         public IEnumerator> GetEnumerator() {
  2120.             Grouping g = lastGrouping;
  2121.             if (g != null) {
  2122.                 do {
  2123.                     g = g.next;
  2124.                     yield return g;
  2125.                 } while (g != lastGrouping);
  2126.             }
  2127.         }
  2128.  
  2129.         public IEnumerable ApplyResultSelector(Func, TResult> resultSelector){
  2130.             Grouping g = lastGrouping;
  2131.             if (g != null) {
  2132.                 do {
  2133.                     g = g.next;
  2134.                     if (g.count != g.elements.Length) { Array.Resize(ref g.elements, g.count); }
  2135.                     yield return resultSelector(g.key, g.elements);
  2136.                 }while (g != lastGrouping);
  2137.             }
  2138.         }
  2139.  
  2140.         IEnumerator IEnumerable.GetEnumerator() {
  2141.             return GetEnumerator();
  2142.         }
  2143.  
  2144.         internal int InternalGetHashCode(TKey key)
  2145.         {
  2146.             //[....] DevDivBugs 171937. work around comparer implementations that throw when passed null
  2147.             return (key == null) ? 0 : comparer.GetHashCode(key) & 0x7FFFFFFF;
  2148.         }
  2149.  
  2150.         internal Grouping GetGrouping(TKey key, bool create) {
  2151.             int hashCode = InternalGetHashCode(key);
  2152.             for (Grouping g = groupings[hashCode % groupings.Length]; g != null; g = g.hashNext)
  2153.                 if (g.hashCode == hashCode && comparer.Equals(g.key, key)) return g;
  2154.             if (create) {
  2155.                 if (count == groupings.Length) Resize();
  2156.                 int index = hashCode % groupings.Length;
  2157.                 Grouping g = new Grouping();
  2158.                 g.key = key;
  2159.                 g.hashCode = hashCode;
  2160.                 g.elements = new TElement[1];
  2161.                 g.hashNext = groupings[index];
  2162.                 groupings[index] = g;
  2163.                 if (lastGrouping == null) {
  2164.                     g.next = g;
  2165.                 }
  2166.                 else {
  2167.                     g.next = lastGrouping.next;
  2168.                     lastGrouping.next = g;
  2169.                 }
  2170.                 lastGrouping = g;
  2171.                 count++;
  2172.                 return g;
  2173.             }
  2174.             return null;
  2175.         }
  2176.  
  2177.         void Resize() {
  2178.             int newSize = checked(count * 2 + 1);
  2179.             Grouping[] newGroupings = new Grouping[newSize];
  2180.             Grouping g = lastGrouping;
  2181.             do {
  2182.                 g = g.next;
  2183.                 int index = g.hashCode % newSize;
  2184.                 g.hashNext = newGroupings[index];
  2185.                 newGroupings[index] = g;
  2186.             } while (g != lastGrouping);
  2187.             groupings = newGroupings;
  2188.         }
  2189.  
  2190.         internal class Grouping : IGrouping, IList
  2191.         {
  2192.             internal TKey key;
  2193.             internal int hashCode;
  2194.             internal TElement[] elements;
  2195.             internal int count;
  2196.             internal Grouping hashNext;
  2197.             internal Grouping next;
  2198.  
  2199.             internal void Add(TElement element) {
  2200.                 if (elements.Length == count) Array.Resize(ref elements, checked(count * 2));
  2201.                 elements[count] = element;
  2202.                 count++;
  2203.             }
  2204.  
  2205.             public IEnumerator GetEnumerator() {
  2206.                 for (int i = 0; i < count; i++) yield return elements[i];
  2207.             }
  2208.  
  2209.             IEnumerator IEnumerable.GetEnumerator() {
  2210.                 return GetEnumerator();
  2211.             }
  2212.  
  2213.             // DDB195907: implement IGrouping<>.Key implicitly
  2214.             // so that WPF binding works on this property.
  2215.             public TKey Key {
  2216.                 get { return key; }
  2217.             }
  2218.  
  2219.             int ICollection.Count {
  2220.                 get { return count; }
  2221.             }
  2222.  
  2223.             bool ICollection.IsReadOnly {
  2224.                 get { return true; }
  2225.             }
  2226.  
  2227.             void ICollection.Add(TElement item) {
  2228.                 throw Error.NotSupported();
  2229.             }
  2230.  
  2231.             void ICollection.Clear() {
  2232.                 throw Error.NotSupported();
  2233.             }
  2234.  
  2235.             bool ICollection.Contains(TElement item) {
  2236.                 return Array.IndexOf(elements, item, 0, count) >= 0;
  2237.             }
  2238.  
  2239.             void ICollection.CopyTo(TElement[] array, int arrayIndex) {
  2240.                 Array.Copy(elements, 0, array, arrayIndex, count);
  2241.             }
  2242.  
  2243.             bool ICollection.Remove(TElement item) {
  2244.                 throw Error.NotSupported();
  2245.             }
  2246.  
  2247.             int IList.IndexOf(TElement item) {
  2248.                 return Array.IndexOf(elements, item, 0, count);
  2249.             }
  2250.  
  2251.             void IList.Insert(int index, TElement item) {
  2252.                 throw Error.NotSupported();
  2253.             }
  2254.  
  2255.             void IList.RemoveAt(int index) {
  2256.                 throw Error.NotSupported();
  2257.             }
  2258.  
  2259.             TElement IList.this[int index] {
  2260.                 get {
  2261.                     if (index < 0 || index >= count) throw Error.ArgumentOutOfRange("index");
  2262.                     return elements[index];
  2263.                 }
  2264.                 set {
  2265.                     throw Error.NotSupported();
  2266.                 }
  2267.             }
  2268.         }
  2269.     }
  2270.  
  2271.     internal class Set
  2272.     {
  2273.         int[] buckets;
  2274.         Slot[] slots;
  2275.         int count;
  2276.         int freeList;
  2277.         IEqualityComparer comparer;
  2278.  
  2279.         public Set() : this(null) { }
  2280.  
  2281.         public Set(IEqualityComparer comparer) {
  2282.             if (comparer == null) comparer = EqualityComparer.Default;
  2283.             this.comparer = comparer;
  2284.             buckets = new int[7];
  2285.             slots = new Slot[7];
  2286.             freeList = -1;
  2287.         }
  2288.  
  2289.         // If value is not in set, add it and return true; otherwise return false
  2290.         public bool Add(TElement value) {
  2291.             return !Find(value, true);
  2292.         }
  2293.  
  2294.         // Check whether value is in set
  2295.         public bool Contains(TElement value) {
  2296.             return Find(value, false);
  2297.         }
  2298.  
  2299.         // If value is in set, remove it and return true; otherwise return false
  2300.         public bool Remove(TElement value) {
  2301.             int hashCode = InternalGetHashCode(value);
  2302.             int bucket = hashCode % buckets.Length;
  2303.             int last = -1;
  2304.             for (int i = buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next) {
  2305.                 if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, value)) {
  2306.                     if (last < 0) {
  2307.                         buckets[bucket] = slots[i].next + 1;
  2308.                     }
  2309.                     else {
  2310.                         slots[last].next = slots[i].next;
  2311.                     }
  2312.                     slots[i].hashCode = -1;
  2313.                     slots[i].value = default(TElement);
  2314.                     slots[i].next = freeList;
  2315.                     freeList = i;
  2316.                     return true;
  2317.                 }
  2318.             }
  2319.             return false;
  2320.         }
  2321.  
  2322.         bool Find(TElement value, bool add) {
  2323.             int hashCode = InternalGetHashCode(value);
  2324.             for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next) {
  2325.                 if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, value)) return true;
  2326.             }
  2327.             if (add) {
  2328.                 int index;
  2329.                 if (freeList >= 0) {
  2330.                     index = freeList;
  2331.                     freeList = slots[index].next;
  2332.                 }
  2333.                 else {
  2334.                     if (count == slots.Length) Resize();
  2335.                     index = count;
  2336.                     count++;
  2337.                 }
  2338.                 int bucket = hashCode % buckets.Length;
  2339.                 slots[index].hashCode = hashCode;
  2340.                 slots[index].value = value;
  2341.                 slots[index].next = buckets[bucket] - 1;
  2342.                 buckets[bucket] = index + 1;
  2343.             }
  2344.             return false;
  2345.         }
  2346.  
  2347.         void Resize() {
  2348.             int newSize = checked(count * 2 + 1);
  2349.             int[] newBuckets = new int[newSize];
  2350.             Slot[] newSlots = new Slot[newSize];
  2351.             Array.Copy(slots, 0, newSlots, 0, count);
  2352.             for (int i = 0; i < count; i++) {
  2353.                 int bucket = newSlots[i].hashCode % newSize;
  2354.                 newSlots[i].next = newBuckets[bucket] - 1;
  2355.                 newBuckets[bucket] = i + 1;
  2356.             }
  2357.             buckets = newBuckets;
  2358.             slots = newSlots;
  2359.         }
  2360.  
  2361.         internal int InternalGetHashCode(TElement value)
  2362.         {
  2363.             //[....] DevDivBugs 171937. work around comparer implementations that throw when passed null
  2364.             return (value == null) ? 0 : comparer.GetHashCode(value) & 0x7FFFFFFF;
  2365.         }
  2366.  
  2367.         internal struct Slot
  2368.         {
  2369.             internal int hashCode;
  2370.             internal TElement value;
  2371.             internal int next;
  2372.         }
  2373.     }
  2374.  
  2375.     internal class GroupedEnumerable : IEnumerable{
  2376.         IEnumerable source;
  2377.         Func keySelector;
  2378.         Func elementSelector;
  2379.         IEqualityComparer comparer;
  2380.         Func, TResult> resultSelector;
  2381.  
  2382.         public GroupedEnumerable(IEnumerable source, Func keySelector, Func elementSelector, Func, TResult> resultSelector, IEqualityComparer comparer){
  2383.             if (source == null) throw Error.ArgumentNull("source");
  2384.             if (keySelector == null) throw Error.ArgumentNull("keySelector");
  2385.             if (elementSelector == null) throw Error.ArgumentNull("elementSelector");
  2386.             if (resultSelector == null) throw Error.ArgumentNull("resultSelector");
  2387.             this.source = source;
  2388.             this.keySelector = keySelector;
  2389.             this.elementSelector = elementSelector;
  2390.             this.comparer = comparer;
  2391.             this.resultSelector = resultSelector;
  2392.         }
  2393.  
  2394.         public IEnumerator GetEnumerator(){
  2395.             Lookup lookup = Lookup.Create(source, keySelector, elementSelector, comparer);
  2396.             return lookup.ApplyResultSelector(resultSelector).GetEnumerator();
  2397.         }
  2398.  
  2399.         IEnumerator IEnumerable.GetEnumerator(){
  2400.             return GetEnumerator();
  2401.         }
  2402.     }
  2403.  
  2404.     internal class GroupedEnumerable : IEnumerable>
  2405.     {
  2406.         IEnumerable source;
  2407.         Func keySelector;
  2408.         Func elementSelector;
  2409.         IEqualityComparer comparer;
  2410.  
  2411.         public GroupedEnumerable(IEnumerable source, Func keySelector, Func elementSelector, IEqualityComparer comparer) {
  2412.             if (source == null) throw Error.ArgumentNull("source");
  2413.             if (keySelector == null) throw Error.ArgumentNull("keySelector");
  2414.             if (elementSelector == null) throw Error.ArgumentNull("elementSelector");
  2415.             this.source = source;
  2416.             this.keySelector = keySelector;
  2417.             this.elementSelector = elementSelector;
  2418.             this.comparer = comparer;
  2419.         }
  2420.  
  2421.         public IEnumerator> GetEnumerator() {
  2422.             return Lookup.Create(source, keySelector, elementSelector, comparer).GetEnumerator();
  2423.         }
  2424.  
  2425.         IEnumerator IEnumerable.GetEnumerator() {
  2426.             return GetEnumerator();
  2427.         }
  2428.     }
  2429.  
  2430.     internal abstract class OrderedEnumerable : IOrderedEnumerable
  2431.     {
  2432.         internal IEnumerable source;
  2433.  
  2434.         public IEnumerator GetEnumerator() {
  2435.             Buffer buffer = new Buffer(source);
  2436.             if (buffer.count > 0) {
  2437.                 EnumerableSorter sorter = GetEnumerableSorter(null);
  2438.                 int[] map = sorter.Sort(buffer.items, buffer.count);
  2439.                 sorter = null;
  2440.                 for (int i = 0; i < buffer.count; i++) yield return buffer.items[map[i]];
  2441.             }
  2442.         }
  2443.  
  2444.         internal abstract EnumerableSorter GetEnumerableSorter(EnumerableSorter next);
  2445.  
  2446.         IEnumerator IEnumerable.GetEnumerator() {
  2447.             return GetEnumerator();
  2448.         }
  2449.  
  2450.         IOrderedEnumerable IOrderedEnumerable.CreateOrderedEnumerable(Func keySelector, IComparer comparer, bool descending) {
  2451.             OrderedEnumerable result = new OrderedEnumerable(source, keySelector, comparer, descending);
  2452.             result.parent = this;
  2453.             return result;
  2454.         }
  2455.     }
  2456.  
  2457.     internal class OrderedEnumerable : OrderedEnumerable
  2458.     {
  2459.         internal OrderedEnumerable parent;
  2460.         internal Func keySelector;
  2461.         internal IComparer comparer;
  2462.         internal bool descending;
  2463.  
  2464.         internal OrderedEnumerable(IEnumerable source, Func keySelector, IComparer comparer, bool descending) {
  2465.             if (source == null) throw Error.ArgumentNull("source");
  2466.             if (keySelector == null) throw Error.ArgumentNull("keySelector");
  2467.             this.source = source;
  2468.             this.parent = null;
  2469.             this.keySelector = keySelector;
  2470.             this.comparer = comparer != null ? comparer : Comparer.Default;
  2471.             this.descending = descending;
  2472.         }
  2473.  
  2474.         internal override EnumerableSorter GetEnumerableSorter(EnumerableSorter next) {
  2475.             EnumerableSorter sorter = new EnumerableSorter(keySelector, comparer, descending, next);
  2476.             if (parent != null) sorter = parent.GetEnumerableSorter(sorter);
  2477.             return sorter;
  2478.         }
  2479.     }
  2480.  
  2481.     internal abstract class EnumerableSorter
  2482.     {
  2483.         internal abstract void ComputeKeys(TElement[] elements, int count);
  2484.  
  2485.         internal abstract int CompareKeys(int index1, int index2);
  2486.  
  2487.         internal int[] Sort(TElement[] elements, int count) {
  2488.             ComputeKeys(elements, count);
  2489.             int[] map = new int[count];
  2490.             for (int i = 0; i < count; i++) map[i] = i;
  2491.             QuickSort(map, 0, count - 1);
  2492.             return map;
  2493.         }
  2494.  
  2495.         void QuickSort(int[] map, int left, int right) {
  2496.             do {
  2497.                 int i = left;
  2498.                 int j = right;
  2499.                 int x = map[i + ((j - i) >> 1)];
  2500.                 do {
  2501.                     while (i < map.Length && CompareKeys(x, map[i]) > 0) i++;
  2502.                     while (j >= 0 && CompareKeys(x, map[j]) < 0) j--;
  2503.                     if (i > j) break;
  2504.                     if (i < j) {
  2505.                         int temp = map[i];
  2506.                         map[i] = map[j];
  2507.                         map[j] = temp;
  2508.                     }
  2509.                     i++;
  2510.                     j--;
  2511.                 } while (i <= j);
  2512.                 if (j - left <= right - i) {
  2513.                     if (left < j) QuickSort(map, left, j);
  2514.                     left = i;
  2515.                 }
  2516.                 else {
  2517.                     if (i < right) QuickSort(map, i, right);
  2518.                     right = j;
  2519.                 }
  2520.             } while (left < right);
  2521.         }
  2522.     }
  2523.  
  2524.     internal class EnumerableSorter : EnumerableSorter
  2525.     {
  2526.         internal Func keySelector;
  2527.         internal IComparer comparer;
  2528.         internal bool descending;
  2529.         internal EnumerableSorter next;
  2530.         internal TKey[] keys;
  2531.  
  2532.         internal EnumerableSorter(Func keySelector, IComparer comparer, bool descending, EnumerableSorter next) {
  2533.             this.keySelector = keySelector;
  2534.             this.comparer = comparer;
  2535.             this.descending = descending;
  2536.             this.next = next;
  2537.         }
  2538.  
  2539.         internal override void ComputeKeys(TElement[] elements, int count) {
  2540.             keys = new TKey[count];
  2541.             for (int i = 0; i < count; i++) keys[i] = keySelector(elements[i]);
  2542.             if (next != null) next.ComputeKeys(elements, count);
  2543.         }
  2544.  
  2545.         internal override int CompareKeys(int index1, int index2) {
  2546.             int c = comparer.Compare(keys[index1], keys[index2]);
  2547.             if (c == 0) {
  2548.                 if (next == null) return index1 - index2;
  2549.                 return next.CompareKeys(index1, index2);
  2550.             }
  2551.             return descending ? -c : c;
  2552.         }
  2553.     }
  2554.  
  2555.     struct Buffer
  2556.     {
  2557.         internal TElement[] items;
  2558.         internal int count;
  2559.  
  2560.         internal Buffer(IEnumerable source) {
  2561.             TElement[] items = null;
  2562.             int count = 0;
  2563.             ICollection collection = source as ICollection;
  2564.             if (collection != null) {
  2565.                 count = collection.Count;
  2566.                 if (count > 0) {
  2567.                     items = new TElement[count];
  2568.                     collection.CopyTo(items, 0);
  2569.                 }
  2570.             }
  2571.             else {
  2572.                 foreach (TElement item in source) {
  2573.                     if (items == null) {
  2574.                         items = new TElement[4];
  2575.                     }
  2576.                     else if (items.Length == count) {
  2577.                         TElement[] newItems = new TElement[checked(count * 2)];
  2578.                         Array.Copy(items, 0, newItems, 0, count);
  2579.                         items = newItems;
  2580.                     }
  2581.                     items[count] = item;
  2582.                     count++;
  2583.                 }
  2584.             }
  2585.             this.items = items;
  2586.             this.count = count;
  2587.         }
  2588.  
  2589.         internal TElement[] ToArray() {
  2590.             if (count == 0) return new TElement[0];
  2591.             if (items.Length == count) return items;
  2592.             TElement[] result = new TElement[count];
  2593.             Array.Copy(items, 0, result, 0, count);
  2594.             return result;
  2595.         }
  2596.     }
  2597.  
  2598.     ///
  2599.     /// This class provides the items view for the Enumerable
  2600.     ///
  2601.     ///  
  2602.     internal sealed class SystemCore_EnumerableDebugView
  2603.     {
  2604.         public SystemCore_EnumerableDebugView(IEnumerable enumerable)
  2605.         {
  2606.             if (enumerable == null)
  2607.             {
  2608.                 throw new ArgumentNullException("enumerable");
  2609.             }
  2610.  
  2611.             this.enumerable = enumerable;
  2612.         }
  2613.  
  2614.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  2615.         public T[] Items
  2616.         {
  2617.             get
  2618.             {
  2619.                 List tempList = new List();
  2620.                 IEnumerator currentEnumerator = this.enumerable.GetEnumerator();
  2621.  
  2622.                 if (currentEnumerator != null)
  2623.                 {
  2624.                     for(count = 0; currentEnumerator.MoveNext(); count++)
  2625.                     {
  2626.                         tempList.Add(currentEnumerator.Current);
  2627.                     }
  2628.                 }
  2629.                 if (count == 0)
  2630.                 {
  2631.                     throw new SystemCore_EnumerableDebugViewEmptyException();
  2632.                 }
  2633.                 cachedCollection = new T[this.count];
  2634.                 tempList.CopyTo(cachedCollection, 0);
  2635.                 return cachedCollection;
  2636.             }
  2637.         }
  2638.  
  2639.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  2640.         private IEnumerable enumerable;
  2641.  
  2642.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  2643.         private T[] cachedCollection;
  2644.  
  2645.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  2646.         private int count;
  2647.     }
  2648.  
  2649.     internal sealed class SystemCore_EnumerableDebugViewEmptyException : Exception
  2650.     {
  2651.         public string Empty
  2652.         {
  2653.             get
  2654.             {
  2655.                 return Strings.EmptyEnumerable;
  2656.             }
  2657.         }
  2658.     }
  2659.  
  2660.     internal sealed class SystemCore_EnumerableDebugView
  2661.     {
  2662.         public SystemCore_EnumerableDebugView(IEnumerable enumerable)
  2663.         {
  2664.             if (enumerable == null)
  2665.             {
  2666.                 throw new ArgumentNullException("enumerable");
  2667.             }
  2668.  
  2669.             this.enumerable = enumerable;
  2670.             count = 0;
  2671.             cachedCollection = null;
  2672.         }
  2673.  
  2674.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.RootHidden)]
  2675.         public object[] Items
  2676.         {
  2677.             get
  2678.             {
  2679.                 List tempList = new List();
  2680.                 IEnumerator currentEnumerator = this.enumerable.GetEnumerator();
  2681.  
  2682.                 if (currentEnumerator != null)
  2683.                 {
  2684.                     for (count = 0; currentEnumerator.MoveNext(); count++)
  2685.                     {
  2686.                         tempList.Add(currentEnumerator.Current);
  2687.                     }
  2688.                 }
  2689.                 if (count == 0)
  2690.                 {
  2691.                     throw new SystemCore_EnumerableDebugViewEmptyException();
  2692.                 }
  2693.                 cachedCollection = new object[this.count];
  2694.                 tempList.CopyTo(cachedCollection, 0);
  2695.                 return cachedCollection;
  2696.             }
  2697.         }
  2698.  
  2699.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  2700.         private IEnumerable enumerable;
  2701.  
  2702.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  2703.         private object[] cachedCollection;
  2704.  
  2705.         [System.Diagnostics.DebuggerBrowsable(System.Diagnostics.DebuggerBrowsableState.Never)]
  2706.         private int count;
  2707.     }
  2708. }
  2709.  
  2710. // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
  2711. // Copyright (c) Microsoft Corporation. All rights reserved.
  2712.  
  2713.  
  2714.                        
  2715.  
  2716.                        
  2717.                        
  2718.                             SyntaxHighlighter.all()
  2719.                        
  2720.                        
  2721.                    
  2722.                    
  2723.                         Link Menu
  2724.                        
  2725.                            
  2726.                        
  2727.  
  2728.                             This book is available now!
  2729.  
  2730.                              Buy at Amazon US or
  2731.  
  2732.                              Buy at Amazon UK
  2733.                            
  2734.  
  2735.                             <!--
  2736.                                 google_ad_client = "pub-6435000594396515";
  2737.                                 /* network.programming-in.net */
  2738.                                 google_ad_slot = "3902760999";
  2739.                                 google_ad_width = 160;
  2740.                                 google_ad_height = 600;
  2741.                                 //-->
  2742.                            
  2743.                            
  2744.                            
  2745.                            
  2746.                                
  2747.                                        
  2748.                                                 CTreeGenerator.cs
  2749.  
  2750.                                            
  2751.  
  2752.                                    
  2753.                                        
  2754.                                                 SecurityResources.cs
  2755.  
  2756.                                            
  2757.  
  2758.                                    
  2759.                                        
  2760.                                                 ParsedRoute.cs
  2761.  
  2762.                                            
  2763.  
  2764.                                    
  2765.                                        
  2766.                                                 NetworkCredential.cs
  2767.  
  2768.                                            
  2769.  
  2770.                                    
  2771.                                        
  2772.                                                 TCPClient.cs
  2773.  
  2774.                                            
  2775.  
  2776.                                    
  2777.                                        
  2778.                                                 StorageSetMapping.cs
  2779.  
  2780.                                            
  2781.  
  2782.                                    
  2783.                                        
  2784.                                                 PropertyStore.cs
  2785.  
  2786.                                            
  2787.  
  2788.                                    
  2789.                                        
  2790.                                                 Triangle.cs
  2791.  
  2792.                                            
  2793.  
  2794.                                    
  2795.                                        
  2796.                                                 SuppressMessageAttribute.cs
  2797.  
  2798.                                            
  2799.  
  2800.                                    
  2801.                                        
  2802.                                                 ResourceDescriptionAttribute.cs
  2803.  
  2804.                                            
  2805.  
  2806.                                    
  2807.                                        
  2808.                                                 DataGridViewCellMouseEventArgs.cs
  2809.  
  2810.                                            
  2811.  
  2812.                                    
  2813.                                        
  2814.                                                 QueryableDataSourceView.cs
  2815.  
  2816.                                            
  2817.  
  2818.                                    
  2819.                                        
  2820.                                                 DefaultHttpHandler.cs
  2821.  
  2822.                                            
  2823.  
  2824.                                    
  2825.                                        
  2826.                                                 LambdaCompiler.Lambda.cs
  2827.  
  2828.                                            
  2829.  
  2830.                                    
  2831.                                        
  2832.                                                 SendingRequestEventArgs.cs
  2833.  
  2834.                                            
  2835.  
  2836.                                    
  2837.                                        
  2838.                                                 HttpClientProtocol.cs
  2839.  
  2840.                                            
  2841.  
  2842.                                    
  2843.                                        
  2844.                                                 FieldInfo.cs
  2845.  
  2846.                                            
  2847.  
  2848.                                    
  2849.                                        
  2850.                                                 NavigationProgressEventArgs.cs
  2851.  
  2852.                                            
  2853.  
  2854.                                    
  2855.                                        
  2856.                                                 CheckBoxRenderer.cs
  2857.  
  2858.                                            
  2859.  
  2860.                                    
  2861.                                        
  2862.                                                 HttpConfigurationSystem.cs
  2863.  
  2864.                                            
  2865.  
  2866.                                    
  2867.                                        
  2868.                                                 JsonFormatMapping.cs
  2869.  
  2870.                                            
  2871.  
  2872.                                    
  2873.                                        
  2874.                                                 HandledEventArgs.cs
  2875.  
  2876.                                            
  2877.  
  2878.                                    
  2879.                                        
  2880.                                                 FixedPageAutomationPeer.cs
  2881.  
  2882.                                            
  2883.  
  2884.                                    
  2885.                                        
  2886.                                                 XPathScanner.cs
  2887.  
  2888.                                            
  2889.  
  2890.                                    
  2891.                                        
  2892.                                                 CodeIdentifiers.cs
  2893.  
  2894.                                            
  2895.  
  2896.                                    
  2897.                                        
  2898.                                                 ChildrenQuery.cs
  2899.  
  2900.                                            
  2901.  
  2902.                                    
  2903.                                        
  2904.                                                 UpdateManifestForBrowserApplication.cs
  2905.  
  2906.                                            
  2907.  
  2908.                                    
  2909.                                        
  2910.                                                 RootAction.cs
  2911.  
  2912.                                            
  2913.  
  2914.                                    
  2915.                                        
  2916.                                                 DLinqAssociationProvider.cs
  2917.  
  2918.                                            
  2919.  
  2920.                                    
  2921.                                        
  2922.                                                 TypeElementCollection.cs
  2923.  
  2924.                                            
  2925.  
  2926.                                    
  2927.                                        
  2928.                                                 ErrorTableItemStyle.cs
  2929.  
  2930.                                            
  2931.  
  2932.                                    
  2933.                                        
  2934.                                                 RootBrowserWindow.cs
  2935.  
  2936.                                            
  2937.  
  2938.                                    
  2939.                                        
  2940.                                                 WebPartTransformerCollection.cs
  2941.  
  2942.                                            
  2943.  
  2944.                                    
  2945.                                        
  2946.                                                 Label.cs
  2947.  
  2948.                                            
  2949.  
  2950.                                    
  2951.                                        
  2952.                                                 TraceInternal.cs
  2953.  
  2954.                                            
  2955.  
  2956.                                    
  2957.                                        
  2958.                                                 SchemaNotation.cs
  2959.  
  2960.                                            
  2961.  
  2962.                                    
  2963.                                        
  2964.                                                 regiisutil.cs
  2965.  
  2966.                                            
  2967.  
  2968.                                    
  2969.                                        
  2970.                                                 XmlWellformedWriter.cs
  2971.  
  2972.                                            
  2973.  
  2974.                                    
  2975.                                        
  2976.                                                 EventDescriptor.cs
  2977.  
  2978.                                            
  2979.  
  2980.                                    
  2981.                                        
  2982.                                                 HostProtectionException.cs
  2983.  
  2984.                                            
  2985.  
  2986.                                    
  2987.                                        
  2988.                                                 FlowDocumentView.cs
  2989.  
  2990.                                            
  2991.  
  2992.                                    
  2993.                                        
  2994.                                                 CroppedBitmap.cs
  2995.  
  2996.                                            
  2997.  
  2998.                                    
  2999.                                        
  3000.                                                 ChannelSinkStacks.cs
  3001.  
  3002.                                            
  3003.  
  3004.                                    
  3005.                                        
  3006.                                                 XslCompiledTransform.cs
  3007.  
  3008.                                            
  3009.  
  3010.                                    
  3011.                                        
  3012.                                                 ObjectParameter.cs
  3013.  
  3014.                                            
  3015.  
  3016.                                    
  3017.                                        
  3018.                                                 CertificateManager.cs
  3019.  
  3020.                                            
  3021.  
  3022.                                    
  3023.                                        
  3024.                                                 FocusTracker.cs
  3025.  
  3026.                                            
  3027.  
  3028.                                    
  3029.                                        
  3030.                                                 TimeSpanOrInfiniteValidator.cs
  3031.  
  3032.                                            
  3033.  
  3034.                                    
  3035.                                        
  3036.                                                 HMACSHA512.cs
  3037.  
  3038.                                            
  3039.  
  3040.                                    
  3041.                                        
  3042.                                                 DataColumnChangeEvent.cs
  3043.  
  3044.                                            
  3045.  
  3046.                                    
  3047.                                        
  3048.                                                 PrinterUnitConvert.cs
  3049.  
  3050.                                            
  3051.  
  3052.                                    
  3053.                                        
  3054.                                                 StringTraceRecord.cs
  3055.  
  3056.                                            
  3057.  
  3058.                                    
  3059.                                        
  3060.                                                 DataGridViewTextBoxEditingControl.cs
  3061.  
  3062.                                            
  3063.  
  3064.                                    
  3065.                                        
  3066.                                                 DataGridViewRowsRemovedEventArgs.cs
  3067.  
  3068.                                            
  3069.  
  3070.                                    
  3071.                                        
  3072.                                                 XsltLibrary.cs
  3073.  
  3074.                                            
  3075.  
  3076.                                    
  3077.                                        
  3078.                                                 IBuiltInEvidence.cs
  3079.  
  3080.                                            
  3081.  
  3082.                                    
  3083.                                        
  3084.                                                 RNGCryptoServiceProvider.cs
  3085.  
  3086.                                            
  3087.  
  3088.                                    
  3089.                                        
  3090.                                                 BypassElement.cs
  3091.  
  3092.                                            
  3093.  
  3094.                                    
  3095.                                        
  3096.                                                 UnmanagedMemoryStream.cs
  3097.  
  3098.                                            
  3099.  
  3100.                                    
  3101.                                        
  3102.                                                 XsltOutput.cs
  3103.  
  3104.                                            
  3105.  
  3106.                                    
  3107.                                        
  3108.                                                 LowerCaseStringConverter.cs
  3109.  
  3110.                                            
  3111.  
  3112.                                    
  3113.                                        
  3114.                                                 StrokeCollection2.cs
  3115.  
  3116.                                            
  3117.  
  3118.                                    
  3119.                                        
  3120.                                                 MessageQueueAccessControlEntry.cs
  3121.  
  3122.                                            
  3123.  
  3124.                                    
  3125.                                        
  3126.                                                 ReadOnlyNameValueCollection.cs
  3127.  
  3128.                                            
  3129.  
  3130.                                    
  3131.                                        
  3132.                                                 ProfessionalColors.cs
  3133.  
  3134.                                            
  3135.  
  3136.                                    
  3137.                                        
  3138.                                                 DuplicateWaitObjectException.cs
  3139.  
  3140.                                            
  3141.  
  3142.                                    
  3143.                                        
  3144.                                                 ManagedFilter.cs
  3145.  
  3146.                                            
  3147.  
  3148.                                    
  3149.                                        
  3150.                                                 FormViewPageEventArgs.cs
  3151.  
  3152.                                            
  3153.  
  3154.                                    
  3155.                                        
  3156.                                                 RequestResizeEvent.cs
  3157.  
  3158.                                            
  3159.  
  3160.                                    
  3161.                                        
  3162.                                                 PrtCap_Base.cs
  3163.  
  3164.                                            
  3165.  
  3166.                                    
  3167.                                        
  3168.                                                 MediaElementAutomationPeer.cs
  3169.  
  3170.                                            
  3171.  
  3172.                                    
  3173.                                        
  3174.                                                 CategoryAttribute.cs
  3175.  
  3176.                                            
  3177.  
  3178.                                    
  3179.                                        
  3180.                                                 XmlDocument.cs
  3181.  
  3182.                                            
  3183.  
  3184.                                    
  3185.                                        
  3186.                                                 RoleBoolean.cs
  3187.  
  3188.                                            
  3189.  
  3190.                                    
  3191.                                        
  3192.                                                 SpellerError.cs
  3193.  
  3194.                                            
  3195.  
  3196.                                    
  3197.                                        
  3198.                                                 _AutoWebProxyScriptWrapper.cs
  3199.  
  3200.                                            
  3201.  
  3202.                                    
  3203.                                        
  3204.                                                 QueryStringParameter.cs
  3205.  
  3206.                                            
  3207.  
  3208.                                    
  3209.                                        
  3210.                                                 DisposableCollectionWrapper.cs
  3211.  
  3212.                                            
  3213.  
  3214.                                    
  3215.                                        
  3216.                                                 DocumentXmlWriter.cs
  3217.  
  3218.                                            
  3219.  
  3220.                                    
  3221.                                        
  3222.                                                 PrintPageEvent.cs
  3223.  
  3224.                                            
  3225.  
  3226.                                    
  3227.                                        
  3228.                                                 Int64Storage.cs
  3229.  
  3230.                                            
  3231.  
  3232.                                    
  3233.                                        
  3234.                                                 BaseCAMarshaler.cs
  3235.  
  3236.                                            
  3237.  
  3238.                                    
  3239.                                        
  3240.                                                 WindowCollection.cs
  3241.  
  3242.                                            
  3243.  
  3244.                                    
  3245.                                        
  3246.                                                 HighContrastHelper.cs
  3247.  
  3248.                                            
  3249.  
  3250.                                    
  3251.                                        
  3252.                                                 DirectoryRootQuery.cs
  3253.  
  3254.                                            
  3255.  
  3256.                                    
  3257.                                        
  3258.                                                 SoapExtensionTypeElementCollection.cs
  3259.  
  3260.                                            
  3261.  
  3262.                                    
  3263.                                        
  3264.                                                 Partitioner.cs
  3265.  
  3266.                                            
  3267.  
  3268.                                    
  3269.                                        
  3270.                                                 BitmapDecoder.cs
  3271.  
  3272.                                            
  3273.  
  3274.                                    
  3275.                                        
  3276.                                                 ToolStripSeparator.cs
  3277.  
  3278.                                            
  3279.  
  3280.                                    
  3281.                                        
  3282.                                                 ConfigurationProperty.cs
  3283.  
  3284.                                            
  3285.  
  3286.                                    
  3287.                                        
  3288.                                                 AspCompat.cs
  3289.  
  3290.                                            
  3291.  
  3292.                                    
  3293.                                        
  3294.                                                 loginstatus.cs
  3295.  
  3296.                                            
  3297.  
  3298.                                    
  3299.                                        
  3300.                                                 XhtmlBasicPhoneCallAdapter.cs
  3301.  
  3302.                                            
  3303.  
  3304.                                    
  3305.                                        
  3306.                                                 x509store.cs
  3307.  
  3308.                                            
  3309.  
  3310.                                    
  3311.                                        
  3312.                                                 DataGridViewImageColumn.cs
  3313.  
  3314.                                            
  3315.  
  3316.                                    
  3317.                                        
  3318.                                                 PeerConnector.cs
  3319.  
  3320.                                            
  3321.  
  3322.                                    
  3323.                                        
  3324.                                                 SslStream.cs
  3325.  
  3326.                                            
  3327.  
  3328.                                    
  3329.                                        
  3330.                                                 ApplicationInfo.cs
  3331.  
  3332.                                            
  3333.  
  3334.                                    
  3335.                                        
  3336.                                                 ScrollEventArgs.cs
  3337.  
  3338.                                            
  3339.  
  3340.                                    
  3341.                                        
  3342.                                                 BuildResultCache.cs
  3343.  
  3344.                                            
  3345.  
  3346.                                    
  3347.                            
  3348.                    
  3349.                
  3350.            
  3351.            
  3352.                
  3353.                     Copyright © 2010-2015 Open Merchant Account Ltd
  3354.                     | DNS provided by Free Name Servers
  3355.                
  3356.  
  3357.            
  3358.            
  3359.                 var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  3360.                 document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
  3361.            
  3362.            
  3363.                 var pageTracker = _gat._getTracker("UA-3658396-9");
  3364.                 pageTracker._trackPageview();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement