Enumerable.cs 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045
  1. //
  2. // Enumerable.cs
  3. //
  4. // Authors:
  5. // Marek Safar ([email protected])
  6. // Antonello Provenzano <[email protected]>
  7. // Alejandro Serrano "Serras" ([email protected])
  8. // Jb Evain ([email protected])
  9. //
  10. // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. // precious: http://www.hookedonlinq.com
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Collections.ObjectModel;
  36. namespace System.Linq
  37. {
  38. public static class Enumerable
  39. {
  40. enum Fallback {
  41. Default,
  42. Throw
  43. }
  44. static class PredicateOf<T> {
  45. public static readonly Func<T, bool> Always = (t) => true;
  46. }
  47. static class Function<T> {
  48. public static readonly Func<T, T> Identity = (t) => t;
  49. }
  50. static class EmptyOf<T> {
  51. public static readonly T[] Instance = new T [0];
  52. }
  53. static class ReadOnlyCollectionOf<T> {
  54. public static readonly ReadOnlyCollection<T> Empty = new ReadOnlyCollection<T> (EmptyOf<T>.Instance);
  55. }
  56. #region Aggregate
  57. public static TSource Aggregate<TSource> (this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
  58. {
  59. Check.SourceAndFunc (source, func);
  60. // custom foreach so that we can efficiently throw an exception
  61. // if zero elements and treat the first element differently
  62. using (var enumerator = source.GetEnumerator ()) {
  63. if (!enumerator.MoveNext ())
  64. throw new InvalidOperationException ("No elements in source list");
  65. TSource folded = enumerator.Current;
  66. while (enumerator.MoveNext ())
  67. folded = func (folded, enumerator.Current);
  68. return folded;
  69. }
  70. }
  71. public static TAccumulate Aggregate<TSource, TAccumulate> (this IEnumerable<TSource> source,
  72. TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func)
  73. {
  74. Check.SourceAndFunc (source, func);
  75. TAccumulate folded = seed;
  76. foreach (TSource element in source)
  77. folded = func (folded, element);
  78. return folded;
  79. }
  80. public static TResult Aggregate<TSource, TAccumulate, TResult> (this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
  81. {
  82. Check.SourceAndFunc (source, func);
  83. if (resultSelector == null)
  84. throw new ArgumentNullException ("resultSelector");
  85. var result = seed;
  86. foreach (var e in source)
  87. result = func (result, e);
  88. return resultSelector (result);
  89. }
  90. #endregion
  91. #region All
  92. public static bool All<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  93. {
  94. Check.SourceAndPredicate (source, predicate);
  95. foreach (var element in source)
  96. if (!predicate (element))
  97. return false;
  98. return true;
  99. }
  100. #endregion
  101. #region Any
  102. public static bool Any<TSource> (this IEnumerable<TSource> source)
  103. {
  104. Check.Source (source);
  105. var collection = source as ICollection<TSource>;
  106. if (collection != null)
  107. return collection.Count > 0;
  108. using (var enumerator = source.GetEnumerator ())
  109. return enumerator.MoveNext ();
  110. }
  111. public static bool Any<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  112. {
  113. Check.SourceAndPredicate (source, predicate);
  114. foreach (TSource element in source)
  115. if (predicate (element))
  116. return true;
  117. return false;
  118. }
  119. #endregion
  120. #region AsEnumerable
  121. public static IEnumerable<TSource> AsEnumerable<TSource> (this IEnumerable<TSource> source)
  122. {
  123. return source;
  124. }
  125. #endregion
  126. #region Average
  127. public static double Average (this IEnumerable<int> source)
  128. {
  129. Check.Source (source);
  130. long total = 0;
  131. int count = 0;
  132. foreach (var element in source){
  133. total = checked (total + element);
  134. count++;
  135. }
  136. if (count == 0)
  137. throw new InvalidOperationException ();
  138. return total / (double) count;
  139. }
  140. public static double Average (this IEnumerable<long> source)
  141. {
  142. Check.Source (source);
  143. long total = 0;
  144. long count = 0;
  145. foreach (var element in source){
  146. total += element;
  147. count++;
  148. }
  149. if (count == 0)
  150. throw new InvalidOperationException ();
  151. return total / (double) count;
  152. }
  153. public static double Average (this IEnumerable<double> source)
  154. {
  155. Check.Source (source);
  156. double total = 0;
  157. long count = 0;
  158. foreach (var element in source){
  159. total += element;
  160. count++;
  161. }
  162. if (count == 0)
  163. throw new InvalidOperationException ();
  164. return total / count;
  165. }
  166. public static float Average (this IEnumerable<float> source)
  167. {
  168. Check.Source (source);
  169. float total = 0;
  170. long count = 0;
  171. foreach (var element in source){
  172. total += element;
  173. count++;
  174. }
  175. if (count == 0)
  176. throw new InvalidOperationException ();
  177. return total / count;
  178. }
  179. public static decimal Average (this IEnumerable<decimal> source)
  180. {
  181. Check.Source (source);
  182. decimal total = 0;
  183. long count = 0;
  184. foreach (var element in source){
  185. total += element;
  186. count++;
  187. }
  188. if (count == 0)
  189. throw new InvalidOperationException ();
  190. return total / count;
  191. }
  192. static TResult? AverageNullable<TElement, TAggregate, TResult> (this IEnumerable<TElement?> source,
  193. Func<TAggregate, TElement, TAggregate> func, Func<TAggregate, long, TResult> result)
  194. where TElement : struct
  195. where TAggregate : struct
  196. where TResult : struct
  197. {
  198. Check.Source (source);
  199. var total = default (TAggregate);
  200. long counter = 0;
  201. foreach (var element in source) {
  202. if (!element.HasValue)
  203. continue;
  204. total = func (total, element.Value);
  205. counter++;
  206. }
  207. if (counter == 0)
  208. return null;
  209. return new TResult? (result (total, counter));
  210. }
  211. public static double? Average (this IEnumerable<int?> source)
  212. {
  213. Check.Source (source);
  214. long total = 0;
  215. long counter = 0;
  216. foreach (var element in source) {
  217. if (!element.HasValue)
  218. continue;
  219. total = total + element.Value;
  220. counter++;
  221. }
  222. if (counter == 0)
  223. return null;
  224. return new double? (total / (double) counter);
  225. }
  226. public static double? Average (this IEnumerable<long?> source)
  227. {
  228. Check.Source (source);
  229. long total = 0;
  230. long counter = 0;
  231. foreach (var element in source) {
  232. if (!element.HasValue)
  233. continue;
  234. total = checked (total + element.Value);
  235. counter++;
  236. }
  237. if (counter == 0)
  238. return null;
  239. return new double? (total / (double) counter);
  240. }
  241. public static double? Average (this IEnumerable<double?> source)
  242. {
  243. Check.Source (source);
  244. double total = 0;
  245. long counter = 0;
  246. foreach (var element in source) {
  247. if (!element.HasValue)
  248. continue;
  249. total = total + element.Value;
  250. counter++;
  251. }
  252. if (counter == 0)
  253. return null;
  254. return new double? (total / counter);
  255. }
  256. public static decimal? Average (this IEnumerable<decimal?> source)
  257. {
  258. Check.Source (source);
  259. decimal total = 0;
  260. long counter = 0;
  261. foreach (var element in source) {
  262. if (!element.HasValue)
  263. continue;
  264. total = total + element.Value;
  265. counter++;
  266. }
  267. if (counter == 0)
  268. return null;
  269. return new decimal? (total / counter);
  270. }
  271. public static float? Average (this IEnumerable<float?> source)
  272. {
  273. Check.Source (source);
  274. float total = 0;
  275. long counter = 0;
  276. foreach (var element in source) {
  277. if (!element.HasValue)
  278. continue;
  279. total = total + element.Value;
  280. counter++;
  281. }
  282. if (counter == 0)
  283. return null;
  284. return new float? (total / counter);
  285. }
  286. public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
  287. {
  288. Check.SourceAndSelector (source, selector);
  289. long total = 0;
  290. long count = 0;
  291. foreach (var element in source){
  292. total += selector (element);
  293. count++;
  294. }
  295. if (count == 0)
  296. throw new InvalidOperationException ();
  297. return total / (double) count;
  298. }
  299. public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
  300. {
  301. Check.SourceAndSelector (source, selector);
  302. long total = 0;
  303. long counter = 0;
  304. foreach (var element in source) {
  305. var value = selector (element);
  306. if (!value.HasValue)
  307. continue;
  308. total = total + value.Value;
  309. counter++;
  310. }
  311. if (counter == 0)
  312. return null;
  313. return new double? (total / (double) counter);
  314. }
  315. public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
  316. {
  317. Check.SourceAndSelector (source, selector);
  318. long total = 0;
  319. long count = 0;
  320. foreach (var element in source){
  321. total = checked (total + selector (element));
  322. count++;
  323. }
  324. if (count == 0)
  325. throw new InvalidOperationException ();
  326. return total / (double) count;
  327. }
  328. public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
  329. {
  330. Check.SourceAndSelector (source, selector);
  331. long total = 0;
  332. long counter = 0;
  333. foreach (var element in source) {
  334. var value = selector (element);
  335. if (!value.HasValue)
  336. continue;
  337. total = checked (total + value.Value);
  338. counter++;
  339. }
  340. if (counter == 0)
  341. return null;
  342. return new double? (total / (double) counter);
  343. }
  344. public static double Average<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
  345. {
  346. Check.SourceAndSelector (source, selector);
  347. double total = 0;
  348. long count = 0;
  349. foreach (var element in source){
  350. total += selector (element);
  351. count++;
  352. }
  353. if (count == 0)
  354. throw new InvalidOperationException ();
  355. return total / count;
  356. }
  357. public static double? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
  358. {
  359. Check.SourceAndSelector (source, selector);
  360. double total = 0;
  361. long counter = 0;
  362. foreach (var element in source) {
  363. var value = selector (element);
  364. if (!value.HasValue)
  365. continue;
  366. total = total + value.Value;
  367. counter++;
  368. }
  369. if (counter == 0)
  370. return null;
  371. return new double? (total / counter);
  372. }
  373. public static float Average<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
  374. {
  375. Check.SourceAndSelector (source, selector);
  376. float total = 0;
  377. long count = 0;
  378. foreach (var element in source){
  379. total += selector (element);
  380. count++;
  381. }
  382. if (count == 0)
  383. throw new InvalidOperationException ();
  384. return total / count;
  385. }
  386. public static float? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
  387. {
  388. Check.SourceAndSelector (source, selector);
  389. float total = 0;
  390. long counter = 0;
  391. foreach (var element in source) {
  392. var value = selector (element);
  393. if (!value.HasValue)
  394. continue;
  395. total = total + value.Value;
  396. counter++;
  397. }
  398. if (counter == 0)
  399. return null;
  400. return new float? (total / counter);
  401. }
  402. public static decimal Average<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
  403. {
  404. Check.SourceAndSelector (source, selector);
  405. decimal total = 0;
  406. long count = 0;
  407. foreach (var element in source){
  408. total += selector (element);
  409. count++;
  410. }
  411. if (count == 0)
  412. throw new InvalidOperationException ();
  413. return total / count;
  414. }
  415. public static decimal? Average<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
  416. {
  417. Check.SourceAndSelector (source, selector);
  418. decimal total = 0;
  419. long counter = 0;
  420. foreach (var element in source) {
  421. var value = selector (element);
  422. if (!value.HasValue)
  423. continue;
  424. total = total + value.Value;
  425. counter++;
  426. }
  427. if (counter == 0)
  428. return null;
  429. return new decimal? (total / counter);
  430. }
  431. #endregion
  432. #region Cast
  433. public static IEnumerable<TResult> Cast<TResult> (this IEnumerable source)
  434. {
  435. Check.Source (source);
  436. var actual = source as IEnumerable<TResult>;
  437. if (actual != null)
  438. return actual;
  439. return CreateCastIterator<TResult> (source);
  440. }
  441. static IEnumerable<TResult> CreateCastIterator<TResult> (IEnumerable source)
  442. {
  443. foreach (TResult element in source)
  444. yield return element;
  445. }
  446. #endregion
  447. #region Concat
  448. public static IEnumerable<TSource> Concat<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  449. {
  450. Check.FirstAndSecond (first, second);
  451. return CreateConcatIterator (first, second);
  452. }
  453. static IEnumerable<TSource> CreateConcatIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second)
  454. {
  455. foreach (TSource element in first)
  456. yield return element;
  457. foreach (TSource element in second)
  458. yield return element;
  459. }
  460. #endregion
  461. #region Contains
  462. public static bool Contains<TSource> (this IEnumerable<TSource> source, TSource value)
  463. {
  464. var collection = source as ICollection<TSource>;
  465. if (collection != null)
  466. return collection.Contains (value);
  467. return Contains<TSource> (source, value, null);
  468. }
  469. public static bool Contains<TSource> (this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
  470. {
  471. Check.Source (source);
  472. if (comparer == null)
  473. comparer = EqualityComparer<TSource>.Default;
  474. foreach (var element in source)
  475. if (comparer.Equals (element, value))
  476. return true;
  477. return false;
  478. }
  479. #endregion
  480. #region Count
  481. public static int Count<TSource> (this IEnumerable<TSource> source)
  482. {
  483. Check.Source (source);
  484. var collection = source as ICollection<TSource>;
  485. if (collection != null)
  486. return collection.Count;
  487. int counter = 0;
  488. using (var enumerator = source.GetEnumerator ())
  489. while (enumerator.MoveNext ())
  490. checked { counter++; }
  491. return counter;
  492. }
  493. public static int Count<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  494. {
  495. Check.SourceAndSelector (source, predicate);
  496. int counter = 0;
  497. foreach (var element in source)
  498. if (predicate (element))
  499. checked { counter++; }
  500. return counter;
  501. }
  502. #endregion
  503. #region DefaultIfEmpty
  504. public static IEnumerable<TSource> DefaultIfEmpty<TSource> (this IEnumerable<TSource> source)
  505. {
  506. return DefaultIfEmpty (source, default (TSource));
  507. }
  508. public static IEnumerable<TSource> DefaultIfEmpty<TSource> (this IEnumerable<TSource> source, TSource defaultValue)
  509. {
  510. Check.Source (source);
  511. return CreateDefaultIfEmptyIterator (source, defaultValue);
  512. }
  513. static IEnumerable<TSource> CreateDefaultIfEmptyIterator<TSource> (IEnumerable<TSource> source, TSource defaultValue)
  514. {
  515. bool empty = true;
  516. foreach (TSource item in source) {
  517. empty = false;
  518. yield return item;
  519. }
  520. if (empty)
  521. yield return defaultValue;
  522. }
  523. #endregion
  524. #region Distinct
  525. public static IEnumerable<TSource> Distinct<TSource> (this IEnumerable<TSource> source)
  526. {
  527. return Distinct<TSource> (source, null);
  528. }
  529. public static IEnumerable<TSource> Distinct<TSource> (this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  530. {
  531. Check.Source (source);
  532. if (comparer == null)
  533. comparer = EqualityComparer<TSource>.Default;
  534. return CreateDistinctIterator (source, comparer);
  535. }
  536. static IEnumerable<TSource> CreateDistinctIterator<TSource> (IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
  537. {
  538. var items = new HashSet<TSource> (comparer);
  539. foreach (var element in source) {
  540. if (! items.Contains (element)) {
  541. items.Add (element);
  542. yield return element;
  543. }
  544. }
  545. }
  546. #endregion
  547. #region ElementAt
  548. static TSource ElementAt<TSource> (this IEnumerable<TSource> source, int index, Fallback fallback)
  549. {
  550. long counter = 0L;
  551. foreach (var element in source) {
  552. if (index == counter++)
  553. return element;
  554. }
  555. if (fallback == Fallback.Throw)
  556. throw new ArgumentOutOfRangeException ();
  557. return default (TSource);
  558. }
  559. public static TSource ElementAt<TSource> (this IEnumerable<TSource> source, int index)
  560. {
  561. Check.Source (source);
  562. if (index < 0)
  563. throw new ArgumentOutOfRangeException ();
  564. var list = source as IList<TSource>;
  565. if (list != null)
  566. return list [index];
  567. return source.ElementAt (index, Fallback.Throw);
  568. }
  569. #endregion
  570. #region ElementAtOrDefault
  571. public static TSource ElementAtOrDefault<TSource> (this IEnumerable<TSource> source, int index)
  572. {
  573. Check.Source (source);
  574. if (index < 0)
  575. return default (TSource);
  576. var list = source as IList<TSource>;
  577. if (list != null)
  578. return index < list.Count ? list [index] : default (TSource);
  579. return source.ElementAt (index, Fallback.Default);
  580. }
  581. #endregion
  582. #region Empty
  583. public static IEnumerable<TResult> Empty<TResult> ()
  584. {
  585. return EmptyOf<TResult>.Instance;
  586. }
  587. #endregion
  588. #region Except
  589. public static IEnumerable<TSource> Except<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  590. {
  591. return Except (first, second, null);
  592. }
  593. public static IEnumerable<TSource> Except<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  594. {
  595. Check.FirstAndSecond (first, second);
  596. if (comparer == null)
  597. comparer = EqualityComparer<TSource>.Default;
  598. return CreateExceptIterator (first, second, comparer);
  599. }
  600. static IEnumerable<TSource> CreateExceptIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  601. {
  602. var items = new HashSet<TSource> (second, comparer);
  603. foreach (var element in first) {
  604. if (items.Add (element))
  605. yield return element;
  606. }
  607. }
  608. #endregion
  609. #region First
  610. static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate, Fallback fallback)
  611. {
  612. foreach (var element in source)
  613. if (predicate (element))
  614. return element;
  615. if (fallback == Fallback.Throw)
  616. throw new InvalidOperationException ();
  617. return default (TSource);
  618. }
  619. public static TSource First<TSource> (this IEnumerable<TSource> source)
  620. {
  621. Check.Source (source);
  622. var list = source as IList<TSource>;
  623. if (list != null) {
  624. if (list.Count != 0)
  625. return list [0];
  626. } else {
  627. using (var enumerator = source.GetEnumerator ()) {
  628. if (enumerator.MoveNext ())
  629. return enumerator.Current;
  630. }
  631. }
  632. throw new InvalidOperationException ("The source sequence is empty");
  633. }
  634. public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  635. {
  636. Check.SourceAndPredicate (source, predicate);
  637. return source.First (predicate, Fallback.Throw);
  638. }
  639. #endregion
  640. #region FirstOrDefault
  641. public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source)
  642. {
  643. Check.Source (source);
  644. return source.First (PredicateOf<TSource>.Always, Fallback.Default);
  645. }
  646. public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  647. {
  648. Check.SourceAndPredicate (source, predicate);
  649. return source.First (predicate, Fallback.Default);
  650. }
  651. #endregion
  652. #region GroupBy
  653. private static List<T> ContainsGroup<K, T> (
  654. Dictionary<K, List<T>> items, K key, IEqualityComparer<K> comparer)
  655. {
  656. IEqualityComparer<K> comparerInUse = (comparer ?? EqualityComparer<K>.Default);
  657. foreach (KeyValuePair<K, List<T>> value in items) {
  658. if (comparerInUse.Equals (value.Key, key))
  659. return value.Value;
  660. }
  661. return null;
  662. }
  663. public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IEnumerable<TSource> source,
  664. Func<TSource, TKey> keySelector)
  665. {
  666. return GroupBy<TSource, TKey> (source, keySelector, null);
  667. }
  668. public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IEnumerable<TSource> source,
  669. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  670. {
  671. Check.SourceAndKeySelector (source, keySelector);
  672. return CreateGroupByIterator (source, keySelector, comparer);
  673. }
  674. static IEnumerable<IGrouping<TKey, TSource>> CreateGroupByIterator<TSource, TKey> (this IEnumerable<TSource> source,
  675. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  676. {
  677. var groups = new Dictionary<TKey, List<TSource>> ();
  678. var nullList = new List<TSource> ();
  679. int counter = 0;
  680. int nullCounter = -1;
  681. foreach (TSource element in source) {
  682. TKey key = keySelector (element);
  683. if (key == null) {
  684. nullList.Add (element);
  685. if (nullCounter == -1) {
  686. nullCounter = counter;
  687. counter++;
  688. }
  689. } else {
  690. List<TSource> group = ContainsGroup<TKey, TSource> (groups, key, comparer);
  691. if (group == null) {
  692. group = new List<TSource> ();
  693. groups.Add (key, group);
  694. counter++;
  695. }
  696. group.Add (element);
  697. }
  698. }
  699. counter = 0;
  700. foreach (var group in groups) {
  701. if (counter == nullCounter) {
  702. yield return new Grouping<TKey, TSource> (default (TKey), nullList);
  703. counter++;
  704. }
  705. yield return new Grouping<TKey, TSource> (group.Key, group.Value);
  706. counter++;
  707. }
  708. if (counter == nullCounter) {
  709. yield return new Grouping<TKey, TSource> (default (TKey), nullList);
  710. counter++;
  711. }
  712. }
  713. public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  714. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  715. {
  716. return GroupBy<TSource, TKey, TElement> (source, keySelector, elementSelector, null);
  717. }
  718. public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  719. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  720. {
  721. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  722. return CreateGroupByIterator (source, keySelector, elementSelector, comparer);
  723. }
  724. static IEnumerable<IGrouping<TKey, TElement>> CreateGroupByIterator<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  725. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  726. {
  727. var groups = new Dictionary<TKey, List<TElement>> ();
  728. var nullList = new List<TElement> ();
  729. int counter = 0;
  730. int nullCounter = -1;
  731. foreach (TSource item in source) {
  732. TKey key = keySelector (item);
  733. TElement element = elementSelector (item);
  734. if (key == null) {
  735. nullList.Add (element);
  736. if (nullCounter == -1) {
  737. nullCounter = counter;
  738. counter++;
  739. }
  740. } else {
  741. List<TElement> group = ContainsGroup<TKey, TElement> (groups, key, comparer);
  742. if (group == null) {
  743. group = new List<TElement> ();
  744. groups.Add (key, group);
  745. counter++;
  746. }
  747. group.Add (element);
  748. }
  749. }
  750. counter = 0;
  751. foreach (var group in groups) {
  752. if (counter == nullCounter) {
  753. yield return new Grouping<TKey, TElement> (default (TKey), nullList);
  754. counter++;
  755. }
  756. yield return new Grouping<TKey, TElement> (group.Key, group.Value);
  757. counter++;
  758. }
  759. if (counter == nullCounter) {
  760. yield return new Grouping<TKey, TElement> (default (TKey), nullList);
  761. counter++;
  762. }
  763. }
  764. public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
  765. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
  766. Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
  767. {
  768. return GroupBy (source, keySelector, elementSelector, resultSelector, null);
  769. }
  770. public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
  771. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
  772. Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
  773. IEqualityComparer<TKey> comparer)
  774. {
  775. Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
  776. return CreateGroupByIterator (source, keySelector, elementSelector, resultSelector, comparer);
  777. }
  778. static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
  779. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
  780. Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
  781. IEqualityComparer<TKey> comparer)
  782. {
  783. IEnumerable<IGrouping<TKey, TElement>> groups = GroupBy<TSource, TKey, TElement> (
  784. source, keySelector, elementSelector, comparer);
  785. foreach (IGrouping<TKey, TElement> group in groups)
  786. yield return resultSelector (group.Key, group);
  787. }
  788. public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult> (this IEnumerable<TSource> source,
  789. Func<TSource, TKey> keySelector,
  790. Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
  791. {
  792. return GroupBy (source, keySelector, resultSelector, null);
  793. }
  794. public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult> (this IEnumerable<TSource> source,
  795. Func<TSource, TKey> keySelector,
  796. Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
  797. IEqualityComparer<TKey> comparer)
  798. {
  799. Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
  800. return CreateGroupByIterator (source, keySelector, resultSelector, comparer);
  801. }
  802. static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TResult> (this IEnumerable<TSource> source,
  803. Func<TSource, TKey> keySelector,
  804. Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
  805. IEqualityComparer<TKey> comparer)
  806. {
  807. IEnumerable<IGrouping<TKey,TSource>> groups = GroupBy<TSource, TKey> (source, keySelector, comparer);
  808. foreach (IGrouping<TKey, TSource> group in groups)
  809. yield return resultSelector (group.Key, group);
  810. }
  811. #endregion
  812. # region GroupJoin
  813. public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  814. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  815. Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
  816. {
  817. return GroupJoin (outer, inner, outerKeySelector, innerKeySelector, resultSelector, null);
  818. }
  819. public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  820. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  821. Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
  822. IEqualityComparer<TKey> comparer)
  823. {
  824. Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
  825. if (comparer == null)
  826. comparer = EqualityComparer<TKey>.Default;
  827. return CreateGroupJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  828. }
  829. static IEnumerable<TResult> CreateGroupJoinIterator<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  830. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  831. Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
  832. IEqualityComparer<TKey> comparer)
  833. {
  834. ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
  835. /*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
  836. foreach (U element in inner)
  837. {
  838. K innerKey = innerKeySelector (element);
  839. if (!innerKeys.ContainsKey (innerKey))
  840. innerKeys.Add (innerKey, new List<U> ());
  841. innerKeys[innerKey].Add (element);
  842. }*/
  843. foreach (TOuter element in outer) {
  844. TKey outerKey = outerKeySelector (element);
  845. if (innerKeys.Contains (outerKey))
  846. yield return resultSelector (element, innerKeys [outerKey]);
  847. else
  848. yield return resultSelector (element, Empty<TInner> ());
  849. }
  850. }
  851. #endregion
  852. #region Intersect
  853. public static IEnumerable<TSource> Intersect<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  854. {
  855. return Intersect (first, second, null);
  856. }
  857. public static IEnumerable<TSource> Intersect<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  858. {
  859. Check.FirstAndSecond (first, second);
  860. if (comparer == null)
  861. comparer = EqualityComparer<TSource>.Default;
  862. return CreateIntersectIterator (first, second, comparer);
  863. }
  864. static IEnumerable<TSource> CreateIntersectIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  865. {
  866. var items = new HashSet<TSource> (second, comparer);
  867. foreach (TSource element in first) {
  868. if (items.Remove (element))
  869. yield return element;
  870. }
  871. }
  872. #endregion
  873. # region Join
  874. public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  875. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  876. Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
  877. {
  878. Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
  879. if (comparer == null)
  880. comparer = EqualityComparer<TKey>.Default;
  881. return CreateJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  882. }
  883. static IEnumerable<TResult> CreateJoinIterator<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  884. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  885. Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
  886. {
  887. ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
  888. /*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
  889. foreach (U element in inner)
  890. {
  891. K innerKey = innerKeySelector (element);
  892. if (!innerKeys.ContainsKey (innerKey))
  893. innerKeys.Add (innerKey, new List<U> ());
  894. innerKeys[innerKey].Add (element);
  895. }*/
  896. foreach (TOuter element in outer) {
  897. TKey outerKey = outerKeySelector (element);
  898. if (innerKeys.Contains (outerKey)) {
  899. foreach (TInner innerElement in innerKeys [outerKey])
  900. yield return resultSelector (element, innerElement);
  901. }
  902. }
  903. }
  904. public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  905. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  906. Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector)
  907. {
  908. return outer.Join (inner, outerKeySelector, innerKeySelector, resultSelector, null);
  909. }
  910. #endregion
  911. #region Last
  912. static TSource Last<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate, Fallback fallback)
  913. {
  914. var empty = true;
  915. var item = default (TSource);
  916. foreach (var element in source) {
  917. if (!predicate (element))
  918. continue;
  919. item = element;
  920. empty = false;
  921. }
  922. if (!empty)
  923. return item;
  924. if (fallback == Fallback.Throw)
  925. throw new InvalidOperationException ();
  926. return item;
  927. }
  928. public static TSource Last<TSource> (this IEnumerable<TSource> source)
  929. {
  930. Check.Source (source);
  931. var collection = source as ICollection<TSource>;
  932. if (collection != null && collection.Count == 0)
  933. throw new InvalidOperationException ();
  934. var list = source as IList<TSource>;
  935. if (list != null)
  936. return list [list.Count - 1];
  937. return source.Last (PredicateOf<TSource>.Always, Fallback.Throw);
  938. }
  939. public static TSource Last<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  940. {
  941. Check.SourceAndPredicate (source, predicate);
  942. return source.Last (predicate, Fallback.Throw);
  943. }
  944. #endregion
  945. #region LastOrDefault
  946. public static TSource LastOrDefault<TSource> (this IEnumerable<TSource> source)
  947. {
  948. Check.Source (source);
  949. var list = source as IList<TSource>;
  950. if (list != null)
  951. return list.Count > 0 ? list [list.Count - 1] : default (TSource);
  952. return source.Last (PredicateOf<TSource>.Always, Fallback.Default);
  953. }
  954. public static TSource LastOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  955. {
  956. Check.SourceAndPredicate (source, predicate);
  957. return source.Last (predicate, Fallback.Default);
  958. }
  959. #endregion
  960. #region LongCount
  961. public static long LongCount<TSource> (this IEnumerable<TSource> source)
  962. {
  963. Check.Source (source);
  964. #if !NET_2_1
  965. var array = source as TSource [];
  966. if (array != null)
  967. return array.LongLength;
  968. #endif
  969. long counter = 0;
  970. using (var enumerator = source.GetEnumerator ())
  971. while (enumerator.MoveNext ())
  972. counter++;
  973. return counter;
  974. }
  975. public static long LongCount<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  976. {
  977. Check.SourceAndSelector (source, predicate);
  978. long counter = 0;
  979. foreach (TSource element in source)
  980. if (predicate (element))
  981. counter++;
  982. return counter;
  983. }
  984. #endregion
  985. #region Max
  986. public static int Max (this IEnumerable<int> source)
  987. {
  988. Check.Source (source);
  989. bool empty = true;
  990. var max = int.MinValue;
  991. foreach (var element in source){
  992. max = Math.Max (element, max);
  993. empty = false;
  994. }
  995. if (empty)
  996. throw new InvalidOperationException ();
  997. return max;
  998. }
  999. public static long Max (this IEnumerable<long> source)
  1000. {
  1001. Check.Source (source);
  1002. bool empty = true;
  1003. var max = long.MinValue;
  1004. foreach (var element in source){
  1005. max = Math.Max (element, max);
  1006. empty = false;
  1007. }
  1008. if (empty)
  1009. throw new InvalidOperationException ();
  1010. return max;
  1011. }
  1012. public static double Max (this IEnumerable<double> source)
  1013. {
  1014. Check.Source (source);
  1015. bool empty = true;
  1016. var max = double.MinValue;
  1017. foreach (var element in source){
  1018. max = Math.Max (element, max);
  1019. empty = false;
  1020. }
  1021. if (empty)
  1022. throw new InvalidOperationException ();
  1023. return max;
  1024. }
  1025. public static float Max (this IEnumerable<float> source)
  1026. {
  1027. Check.Source (source);
  1028. bool empty = true;
  1029. var max = float.MinValue;
  1030. foreach (var element in source){
  1031. max = Math.Max (element, max);
  1032. empty = false;
  1033. }
  1034. if (empty)
  1035. throw new InvalidOperationException ();
  1036. return max;
  1037. }
  1038. public static decimal Max (this IEnumerable<decimal> source)
  1039. {
  1040. Check.Source (source);
  1041. bool empty = true;
  1042. var max = decimal.MinValue;
  1043. foreach (var element in source){
  1044. max = Math.Max (element, max);
  1045. empty = false;
  1046. }
  1047. if (empty)
  1048. throw new InvalidOperationException ();
  1049. return max;
  1050. }
  1051. public static int? Max (this IEnumerable<int?> source)
  1052. {
  1053. Check.Source (source);
  1054. bool empty = true;
  1055. var max = int.MinValue;
  1056. foreach (var element in source) {
  1057. if (!element.HasValue)
  1058. continue;
  1059. max = Math.Max (element.Value, max);
  1060. empty = false;
  1061. }
  1062. if (empty)
  1063. return null;
  1064. return max;
  1065. }
  1066. public static long? Max (this IEnumerable<long?> source)
  1067. {
  1068. Check.Source (source);
  1069. bool empty = true;
  1070. var max = long.MinValue;
  1071. foreach (var element in source) {
  1072. if (!element.HasValue)
  1073. continue;
  1074. max = Math.Max (element.Value, max);
  1075. empty = false;
  1076. }
  1077. if (empty)
  1078. return null;
  1079. return max;
  1080. }
  1081. public static double? Max (this IEnumerable<double?> source)
  1082. {
  1083. Check.Source (source);
  1084. bool empty = true;
  1085. var max = double.MinValue;
  1086. foreach (var element in source) {
  1087. if (!element.HasValue)
  1088. continue;
  1089. max = Math.Max (element.Value, max);
  1090. empty = false;
  1091. }
  1092. if (empty)
  1093. return null;
  1094. return max;
  1095. }
  1096. public static float? Max (this IEnumerable<float?> source)
  1097. {
  1098. Check.Source (source);
  1099. bool empty = true;
  1100. var max = float.MinValue;
  1101. foreach (var element in source) {
  1102. if (!element.HasValue)
  1103. continue;
  1104. max = Math.Max (element.Value, max);
  1105. empty = false;
  1106. }
  1107. if (empty)
  1108. return null;
  1109. return max;
  1110. }
  1111. public static decimal? Max (this IEnumerable<decimal?> source)
  1112. {
  1113. Check.Source (source);
  1114. bool empty = true;
  1115. var max = decimal.MinValue;
  1116. foreach (var element in source) {
  1117. if (!element.HasValue)
  1118. continue;
  1119. max = Math.Max (element.Value, max);
  1120. empty = false;
  1121. }
  1122. if (empty)
  1123. return null;
  1124. return max;
  1125. }
  1126. // TODO: test nullable and non-nullable
  1127. public static TSource Max<TSource> (this IEnumerable<TSource> source)
  1128. {
  1129. Check.Source (source);
  1130. var comparer = Comparer<TSource>.Default;
  1131. TSource max = default (TSource);
  1132. if (default (TSource) == null){
  1133. foreach (var element in source) {
  1134. if (element == null)
  1135. continue;
  1136. if (max == null || comparer.Compare (element, max) > 0)
  1137. max = element;
  1138. }
  1139. } else {
  1140. bool empty = true;
  1141. foreach (var element in source) {
  1142. if (empty){
  1143. max = element;
  1144. empty = false;
  1145. continue;
  1146. }
  1147. if (comparer.Compare (element, max) > 0)
  1148. max = element;
  1149. }
  1150. if (empty)
  1151. throw new InvalidOperationException ();
  1152. }
  1153. return max;
  1154. }
  1155. public static int Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
  1156. {
  1157. Check.SourceAndSelector (source, selector);
  1158. bool empty = true;
  1159. var max = int.MinValue;
  1160. foreach (var element in source){
  1161. max = Math.Max (selector (element), max);
  1162. empty = false;
  1163. }
  1164. if (empty)
  1165. throw new InvalidOperationException ();
  1166. return max;
  1167. }
  1168. public static long Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
  1169. {
  1170. Check.SourceAndSelector (source, selector);
  1171. bool empty = true;
  1172. var max = long.MinValue;
  1173. foreach (var element in source){
  1174. max = Math.Max (selector (element), max);
  1175. empty = false;
  1176. }
  1177. if (empty)
  1178. throw new InvalidOperationException ();
  1179. return max;
  1180. }
  1181. public static double Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
  1182. {
  1183. Check.SourceAndSelector (source, selector);
  1184. bool empty = true;
  1185. var max = double.MinValue;
  1186. foreach (var element in source){
  1187. max = Math.Max (selector (element), max);
  1188. empty = false;
  1189. }
  1190. if (empty)
  1191. throw new InvalidOperationException ();
  1192. return max;
  1193. }
  1194. public static float Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
  1195. {
  1196. Check.SourceAndSelector (source, selector);
  1197. bool empty = true;
  1198. var max = float.MinValue;
  1199. foreach (var element in source){
  1200. max = Math.Max (selector (element), max);
  1201. empty = false;
  1202. }
  1203. if (empty)
  1204. throw new InvalidOperationException ();
  1205. return max;
  1206. }
  1207. public static decimal Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
  1208. {
  1209. Check.SourceAndSelector (source, selector);
  1210. bool empty = true;
  1211. var max = decimal.MinValue;
  1212. foreach (var element in source){
  1213. max = Math.Max (selector (element), max);
  1214. empty = false;
  1215. }
  1216. if (empty)
  1217. throw new InvalidOperationException ();
  1218. return max;
  1219. }
  1220. static U Iterate<T, U> (IEnumerable<T> source, U initValue, Func<T, U, U> selector)
  1221. {
  1222. bool empty = true;
  1223. foreach (var element in source) {
  1224. initValue = selector (element, initValue);
  1225. empty = false;
  1226. }
  1227. if (empty)
  1228. throw new InvalidOperationException ();
  1229. return initValue;
  1230. }
  1231. public static int? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
  1232. {
  1233. Check.SourceAndSelector (source, selector);
  1234. bool empty = true;
  1235. int? max = null;
  1236. foreach (var element in source) {
  1237. int? item = selector (element);
  1238. if (!max.HasValue)
  1239. max = item;
  1240. else if (item > max)
  1241. max = item;
  1242. empty = false;
  1243. }
  1244. if (empty)
  1245. return null;
  1246. return max;
  1247. }
  1248. public static long? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
  1249. {
  1250. Check.SourceAndSelector (source, selector);
  1251. bool empty = true;
  1252. long? max = null;
  1253. foreach (var element in source) {
  1254. long? item = selector (element);
  1255. if (!max.HasValue)
  1256. max = item;
  1257. else if (item > max)
  1258. max = item;
  1259. empty = false;
  1260. }
  1261. if (empty)
  1262. return null;
  1263. return max;
  1264. }
  1265. public static double? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
  1266. {
  1267. Check.SourceAndSelector (source, selector);
  1268. bool empty = true;
  1269. double? max = null;
  1270. foreach (var element in source) {
  1271. double? item = selector (element);
  1272. if (!max.HasValue)
  1273. max = item;
  1274. else if (item > max)
  1275. max = item;
  1276. empty = false;
  1277. }
  1278. if (empty)
  1279. return null;
  1280. return max;
  1281. }
  1282. public static float? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
  1283. {
  1284. Check.SourceAndSelector (source, selector);
  1285. bool empty = true;
  1286. float? max = null;
  1287. foreach (var element in source) {
  1288. float? item = selector (element);
  1289. if (!max.HasValue)
  1290. max = item;
  1291. else if (item > max)
  1292. max = item;
  1293. empty = false;
  1294. }
  1295. if (empty)
  1296. return null;
  1297. return max;
  1298. }
  1299. public static decimal? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
  1300. {
  1301. Check.SourceAndSelector (source, selector);
  1302. bool empty = true;
  1303. decimal? max = null;
  1304. foreach (var element in source) {
  1305. decimal? item = selector (element);
  1306. if (!max.HasValue)
  1307. max = item;
  1308. else if (item > max)
  1309. max = item;
  1310. empty = false;
  1311. }
  1312. if (empty)
  1313. return null;
  1314. return max;
  1315. }
  1316. public static TResult Max<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1317. {
  1318. Check.SourceAndSelector (source, selector);
  1319. // TODO: inline
  1320. return source.Select (selector).Max ();
  1321. }
  1322. #endregion
  1323. #region Min
  1324. public static int Min (this IEnumerable<int> source)
  1325. {
  1326. Check.Source (source);
  1327. bool empty = true;
  1328. var min = int.MaxValue;
  1329. foreach (var element in source){
  1330. min = Math.Min (element, min);
  1331. empty = false;
  1332. }
  1333. if (empty)
  1334. throw new InvalidOperationException ();
  1335. return min;
  1336. }
  1337. public static long Min (this IEnumerable<long> source)
  1338. {
  1339. Check.Source (source);
  1340. bool empty = true;
  1341. var min = long.MaxValue;
  1342. foreach (var element in source){
  1343. min = Math.Min (element, min);
  1344. empty = false;
  1345. }
  1346. if (empty)
  1347. throw new InvalidOperationException ();
  1348. return min;
  1349. }
  1350. public static double Min (this IEnumerable<double> source)
  1351. {
  1352. Check.Source (source);
  1353. bool empty = true;
  1354. var min = double.MaxValue;
  1355. foreach (var element in source){
  1356. min = Math.Min (element, min);
  1357. empty = false;
  1358. }
  1359. if (empty)
  1360. throw new InvalidOperationException ();
  1361. return min;
  1362. }
  1363. public static float Min (this IEnumerable<float> source)
  1364. {
  1365. Check.Source (source);
  1366. bool empty = true;
  1367. var min = float.MaxValue;
  1368. foreach (var element in source){
  1369. min = Math.Min (element, min);
  1370. empty = false;
  1371. }
  1372. if (empty)
  1373. throw new InvalidOperationException ();
  1374. return min;
  1375. }
  1376. public static decimal Min (this IEnumerable<decimal> source)
  1377. {
  1378. Check.Source (source);
  1379. bool empty = true;
  1380. var min = decimal.MaxValue;
  1381. foreach (var element in source){
  1382. min = Math.Min (element, min);
  1383. empty = false;
  1384. }
  1385. if (empty)
  1386. throw new InvalidOperationException ();
  1387. return min;
  1388. }
  1389. public static int? Min (this IEnumerable<int?> source)
  1390. {
  1391. Check.Source (source);
  1392. bool empty = true;
  1393. var min = int.MaxValue;
  1394. foreach (var element in source) {
  1395. if (!element.HasValue)
  1396. continue;
  1397. min = Math.Min (element.Value, min);
  1398. empty = false;
  1399. }
  1400. if (empty)
  1401. return null;
  1402. return min;
  1403. }
  1404. public static long? Min (this IEnumerable<long?> source)
  1405. {
  1406. Check.Source (source);
  1407. bool empty = true;
  1408. var min = long.MaxValue;
  1409. foreach (var element in source) {
  1410. if (!element.HasValue)
  1411. continue;
  1412. min = Math.Min (element.Value, min);
  1413. empty = false;
  1414. }
  1415. if (empty)
  1416. return null;
  1417. return min;
  1418. }
  1419. public static double? Min (this IEnumerable<double?> source)
  1420. {
  1421. Check.Source (source);
  1422. bool empty = true;
  1423. var min = double.MaxValue;
  1424. foreach (var element in source) {
  1425. if (!element.HasValue)
  1426. continue;
  1427. min = Math.Min (element.Value, min);
  1428. empty = false;
  1429. }
  1430. if (empty)
  1431. return null;
  1432. return min;
  1433. }
  1434. public static float? Min (this IEnumerable<float?> source)
  1435. {
  1436. Check.Source (source);
  1437. bool empty = true;
  1438. var min = float.MaxValue;
  1439. foreach (var element in source) {
  1440. if (!element.HasValue)
  1441. continue;
  1442. min = Math.Min (element.Value, min);
  1443. empty = false;
  1444. }
  1445. if (empty)
  1446. return null;
  1447. return min;
  1448. }
  1449. public static decimal? Min (this IEnumerable<decimal?> source)
  1450. {
  1451. Check.Source (source);
  1452. bool empty = true;
  1453. var min = decimal.MaxValue;
  1454. foreach (var element in source) {
  1455. if (!element.HasValue)
  1456. continue;
  1457. min = Math.Min (element.Value, min);
  1458. empty = false;
  1459. }
  1460. if (empty)
  1461. return null;
  1462. return min;
  1463. }
  1464. public static TSource Min<TSource> (this IEnumerable<TSource> source)
  1465. {
  1466. Check.Source (source);
  1467. var comparer = Comparer<TSource>.Default;
  1468. TSource min = default (TSource);
  1469. if (default (TSource) == null){
  1470. foreach (var element in source) {
  1471. if (element == null)
  1472. continue;
  1473. if (min == null || comparer.Compare (element, min) < 0)
  1474. min = element;
  1475. }
  1476. } else {
  1477. bool empty = true;
  1478. foreach (var element in source) {
  1479. if (empty){
  1480. min = element;
  1481. empty = false;
  1482. continue;
  1483. }
  1484. if (comparer.Compare (element, min) < 0)
  1485. min = element;
  1486. }
  1487. if (empty)
  1488. throw new InvalidOperationException ();
  1489. }
  1490. return min;
  1491. }
  1492. public static int Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
  1493. {
  1494. Check.SourceAndSelector (source, selector);
  1495. bool empty = true;
  1496. var min = int.MaxValue;
  1497. foreach (var element in source){
  1498. min = Math.Min (selector (element), min);
  1499. empty = false;
  1500. }
  1501. if (empty)
  1502. throw new InvalidOperationException ();
  1503. return min;
  1504. }
  1505. public static long Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
  1506. {
  1507. Check.SourceAndSelector (source, selector);
  1508. bool empty = true;
  1509. var min = long.MaxValue;
  1510. foreach (var element in source){
  1511. min = Math.Min (selector (element), min);
  1512. empty = false;
  1513. }
  1514. if (empty)
  1515. throw new InvalidOperationException ();
  1516. return min;
  1517. }
  1518. public static double Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
  1519. {
  1520. Check.SourceAndSelector (source, selector);
  1521. bool empty = true;
  1522. var min = double.MaxValue;
  1523. foreach (var element in source){
  1524. min = Math.Min (selector (element), min);
  1525. empty = false;
  1526. }
  1527. if (empty)
  1528. throw new InvalidOperationException ();
  1529. return min;
  1530. }
  1531. public static float Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
  1532. {
  1533. Check.SourceAndSelector (source, selector);
  1534. bool empty = true;
  1535. var min = float.MaxValue;
  1536. foreach (var element in source){
  1537. min = Math.Min (selector (element), min);
  1538. empty = false;
  1539. }
  1540. if (empty)
  1541. throw new InvalidOperationException ();
  1542. return min;
  1543. }
  1544. public static decimal Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
  1545. {
  1546. Check.SourceAndSelector (source, selector);
  1547. bool empty = true;
  1548. var min = decimal.MaxValue;
  1549. foreach (var element in source){
  1550. min = Math.Min (selector (element), min);
  1551. empty = false;
  1552. }
  1553. if (empty)
  1554. throw new InvalidOperationException ();
  1555. return min;
  1556. }
  1557. public static int? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
  1558. {
  1559. Check.SourceAndSelector (source, selector);
  1560. bool empty = true;
  1561. int? min = null;
  1562. foreach (var element in source) {
  1563. int? item = selector (element);
  1564. if (!min.HasValue)
  1565. min = item;
  1566. else if (item < min)
  1567. min = item;
  1568. empty = false;
  1569. }
  1570. if (empty)
  1571. return null;
  1572. return min;
  1573. }
  1574. public static long? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
  1575. {
  1576. Check.SourceAndSelector (source, selector);
  1577. bool empty = true;
  1578. long? min = null;
  1579. foreach (var element in source) {
  1580. long? item = selector (element);
  1581. if (!min.HasValue)
  1582. min = item;
  1583. else if (item < min)
  1584. min = item;
  1585. empty = false;
  1586. }
  1587. if (empty)
  1588. return null;
  1589. return min;
  1590. }
  1591. public static float? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
  1592. {
  1593. Check.SourceAndSelector (source, selector);
  1594. bool empty = true;
  1595. float? min = null;
  1596. foreach (var element in source) {
  1597. float? item = selector (element);
  1598. if (!min.HasValue)
  1599. min = item;
  1600. else if (item < min)
  1601. min = item;
  1602. empty = false;
  1603. }
  1604. if (empty)
  1605. return null;
  1606. return min;
  1607. }
  1608. public static double? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
  1609. {
  1610. Check.SourceAndSelector (source, selector);
  1611. bool empty = true;
  1612. double? min = null;
  1613. foreach (var element in source) {
  1614. double? item = selector (element);
  1615. if (!min.HasValue)
  1616. min = item;
  1617. else if (item < min)
  1618. min = item;
  1619. empty = false;
  1620. }
  1621. if (empty)
  1622. return null;
  1623. return min;
  1624. }
  1625. public static decimal? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
  1626. {
  1627. Check.SourceAndSelector (source, selector);
  1628. bool empty = true;
  1629. decimal? min = null;
  1630. foreach (var element in source) {
  1631. decimal? item = selector (element);
  1632. if (!min.HasValue)
  1633. min = item;
  1634. else if (item < min)
  1635. min = item;
  1636. empty = false;
  1637. }
  1638. if (empty)
  1639. return null;
  1640. return min;
  1641. }
  1642. public static TResult Min<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1643. {
  1644. Check.SourceAndSelector (source, selector);
  1645. // TODO: inline
  1646. return source.Select (selector).Min ();
  1647. }
  1648. #endregion
  1649. #region OfType
  1650. public static IEnumerable<TResult> OfType<TResult> (this IEnumerable source)
  1651. {
  1652. Check.Source (source);
  1653. return CreateOfTypeIterator<TResult> (source);
  1654. }
  1655. static IEnumerable<TResult> CreateOfTypeIterator<TResult> (IEnumerable source)
  1656. {
  1657. foreach (object element in source)
  1658. if (element is TResult)
  1659. yield return (TResult) element;
  1660. }
  1661. #endregion
  1662. #region OrderBy
  1663. public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey> (this IEnumerable<TSource> source,
  1664. Func<TSource, TKey> keySelector)
  1665. {
  1666. return OrderBy<TSource, TKey> (source, keySelector, null);
  1667. }
  1668. public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey> (this IEnumerable<TSource> source,
  1669. Func<TSource, TKey> keySelector,
  1670. IComparer<TKey> comparer)
  1671. {
  1672. Check.SourceAndKeySelector (source, keySelector);
  1673. return new OrderedSequence<TSource, TKey> (source, keySelector, comparer, SortDirection.Ascending);
  1674. }
  1675. #endregion
  1676. #region OrderByDescending
  1677. public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey> (this IEnumerable<TSource> source,
  1678. Func<TSource, TKey> keySelector)
  1679. {
  1680. return OrderByDescending<TSource, TKey> (source, keySelector, null);
  1681. }
  1682. public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey> (this IEnumerable<TSource> source,
  1683. Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  1684. {
  1685. Check.SourceAndKeySelector (source, keySelector);
  1686. return new OrderedSequence<TSource, TKey> (source, keySelector, comparer, SortDirection.Descending);
  1687. }
  1688. #endregion
  1689. #region Range
  1690. public static IEnumerable<int> Range (int start, int count)
  1691. {
  1692. if (count < 0)
  1693. throw new ArgumentOutOfRangeException ("count");
  1694. if (((long) start + count) - 1L > int.MaxValue)
  1695. throw new ArgumentOutOfRangeException ();
  1696. return CreateRangeIterator (start, count);
  1697. }
  1698. static IEnumerable<int> CreateRangeIterator (int start, int count)
  1699. {
  1700. for (int i = 0; i < count; i++)
  1701. yield return start + i;
  1702. }
  1703. #endregion
  1704. #region Repeat
  1705. public static IEnumerable<TResult> Repeat<TResult> (TResult element, int count)
  1706. {
  1707. if (count < 0)
  1708. throw new ArgumentOutOfRangeException ();
  1709. return CreateRepeatIterator (element, count);
  1710. }
  1711. static IEnumerable<TResult> CreateRepeatIterator<TResult> (TResult element, int count)
  1712. {
  1713. for (int i = 0; i < count; i++)
  1714. yield return element;
  1715. }
  1716. #endregion
  1717. #region Reverse
  1718. public static IEnumerable<TSource> Reverse<TSource> (this IEnumerable<TSource> source)
  1719. {
  1720. Check.Source (source);
  1721. return CreateReverseIterator (source);
  1722. }
  1723. static IEnumerable<TSource> CreateReverseIterator<TSource> (IEnumerable<TSource> source)
  1724. {
  1725. var array = source.ToArray ();
  1726. for (int i = array.Length - 1; i >= 0; i--)
  1727. yield return array [i];
  1728. }
  1729. #endregion
  1730. #region Select
  1731. public static IEnumerable<TResult> Select<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1732. {
  1733. Check.SourceAndSelector (source, selector);
  1734. return CreateSelectIterator (source, selector);
  1735. }
  1736. static IEnumerable<TResult> CreateSelectIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1737. {
  1738. foreach (var element in source)
  1739. yield return selector (element);
  1740. }
  1741. public static IEnumerable<TResult> Select<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
  1742. {
  1743. Check.SourceAndSelector (source, selector);
  1744. return CreateSelectIterator (source, selector);
  1745. }
  1746. static IEnumerable<TResult> CreateSelectIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
  1747. {
  1748. int counter = 0;
  1749. foreach (TSource element in source) {
  1750. yield return selector (element, counter);
  1751. counter++;
  1752. }
  1753. }
  1754. #endregion
  1755. #region SelectMany
  1756. public static IEnumerable<TResult> SelectMany<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
  1757. {
  1758. Check.SourceAndSelector (source, selector);
  1759. return CreateSelectManyIterator (source, selector);
  1760. }
  1761. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
  1762. {
  1763. foreach (TSource element in source)
  1764. foreach (TResult item in selector (element))
  1765. yield return item;
  1766. }
  1767. public static IEnumerable<TResult> SelectMany<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector)
  1768. {
  1769. Check.SourceAndSelector (source, selector);
  1770. return CreateSelectManyIterator (source, selector);
  1771. }
  1772. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector)
  1773. {
  1774. int counter = 0;
  1775. foreach (TSource element in source) {
  1776. foreach (TResult item in selector (element, counter))
  1777. yield return item;
  1778. counter++;
  1779. }
  1780. }
  1781. public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
  1782. Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
  1783. {
  1784. Check.SourceAndCollectionSelectors (source, collectionSelector, resultSelector);
  1785. return CreateSelectManyIterator (source, collectionSelector, resultSelector);
  1786. }
  1787. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
  1788. Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
  1789. {
  1790. foreach (TSource element in source)
  1791. foreach (TCollection collection in collectionSelector (element))
  1792. yield return selector (element, collection);
  1793. }
  1794. public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
  1795. Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
  1796. {
  1797. Check.SourceAndCollectionSelectors (source, collectionSelector, resultSelector);
  1798. return CreateSelectManyIterator (source, collectionSelector, resultSelector);
  1799. }
  1800. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
  1801. Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
  1802. {
  1803. int counter = 0;
  1804. foreach (TSource element in source)
  1805. foreach (TCollection collection in collectionSelector (element, counter++))
  1806. yield return selector (element, collection);
  1807. }
  1808. #endregion
  1809. #region Single
  1810. static TSource Single<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate, Fallback fallback)
  1811. {
  1812. var found = false;
  1813. var item = default (TSource);
  1814. foreach (var element in source) {
  1815. if (!predicate (element))
  1816. continue;
  1817. if (found)
  1818. throw new InvalidOperationException ();
  1819. found = true;
  1820. item = element;
  1821. }
  1822. if (!found && fallback == Fallback.Throw)
  1823. throw new InvalidOperationException ();
  1824. return item;
  1825. }
  1826. public static TSource Single<TSource> (this IEnumerable<TSource> source)
  1827. {
  1828. Check.Source (source);
  1829. return source.Single (PredicateOf<TSource>.Always, Fallback.Throw);
  1830. }
  1831. public static TSource Single<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1832. {
  1833. Check.SourceAndPredicate (source, predicate);
  1834. return source.Single (predicate, Fallback.Throw);
  1835. }
  1836. #endregion
  1837. #region SingleOrDefault
  1838. public static TSource SingleOrDefault<TSource> (this IEnumerable<TSource> source)
  1839. {
  1840. Check.Source (source);
  1841. return source.Single (PredicateOf<TSource>.Always, Fallback.Default);
  1842. }
  1843. public static TSource SingleOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1844. {
  1845. Check.SourceAndPredicate (source, predicate);
  1846. return source.Single (predicate, Fallback.Default);
  1847. }
  1848. #endregion
  1849. #region Skip
  1850. public static IEnumerable<TSource> Skip<TSource> (this IEnumerable<TSource> source, int count)
  1851. {
  1852. Check.Source (source);
  1853. return CreateSkipIterator (source, count);
  1854. }
  1855. static IEnumerable<TSource> CreateSkipIterator<TSource> (IEnumerable<TSource> source, int count)
  1856. {
  1857. var enumerator = source.GetEnumerator ();
  1858. try {
  1859. while (count-- > 0)
  1860. if (!enumerator.MoveNext ())
  1861. yield break;
  1862. while (enumerator.MoveNext ())
  1863. yield return enumerator.Current;
  1864. } finally {
  1865. enumerator.Dispose ();
  1866. }
  1867. }
  1868. #endregion
  1869. #region SkipWhile
  1870. public static IEnumerable<TSource> SkipWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1871. {
  1872. Check.SourceAndPredicate (source, predicate);
  1873. return CreateSkipWhileIterator (source, predicate);
  1874. }
  1875. static IEnumerable<TSource> CreateSkipWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1876. {
  1877. bool yield = false;
  1878. foreach (TSource element in source) {
  1879. if (yield)
  1880. yield return element;
  1881. else
  1882. if (!predicate (element)) {
  1883. yield return element;
  1884. yield = true;
  1885. }
  1886. }
  1887. }
  1888. public static IEnumerable<TSource> SkipWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  1889. {
  1890. Check.SourceAndPredicate (source, predicate);
  1891. return CreateSkipWhileIterator (source, predicate);
  1892. }
  1893. static IEnumerable<TSource> CreateSkipWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  1894. {
  1895. int counter = 0;
  1896. bool yield = false;
  1897. foreach (TSource element in source) {
  1898. if (yield)
  1899. yield return element;
  1900. else
  1901. if (!predicate (element, counter)) {
  1902. yield return element;
  1903. yield = true;
  1904. }
  1905. counter++;
  1906. }
  1907. }
  1908. #endregion
  1909. #region Sum
  1910. public static int Sum (this IEnumerable<int> source)
  1911. {
  1912. Check.Source (source);
  1913. int total = 0;
  1914. foreach (var element in source)
  1915. total = checked (total + element);
  1916. return total;
  1917. }
  1918. public static int? Sum (this IEnumerable<int?> source)
  1919. {
  1920. Check.Source (source);
  1921. int total = 0;
  1922. foreach (var element in source) {
  1923. if (element.HasValue)
  1924. total = checked (total + element.Value);
  1925. }
  1926. return total;
  1927. }
  1928. public static int Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
  1929. {
  1930. Check.SourceAndSelector (source, selector);
  1931. int total = 0;
  1932. foreach (var element in source)
  1933. total = checked (total + selector (element));
  1934. return total;
  1935. }
  1936. public static int? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
  1937. {
  1938. Check.SourceAndSelector (source, selector);
  1939. int total = 0;
  1940. foreach (var element in source) {
  1941. var value = selector (element);
  1942. if (value.HasValue)
  1943. total = checked (total + value.Value);
  1944. }
  1945. return total;
  1946. }
  1947. public static long Sum (this IEnumerable<long> source)
  1948. {
  1949. Check.Source (source);
  1950. long total = 0;
  1951. foreach (var element in source)
  1952. total = checked (total + element);
  1953. return total;
  1954. }
  1955. public static long? Sum (this IEnumerable<long?> source)
  1956. {
  1957. Check.Source (source);
  1958. long total = 0;
  1959. foreach (var element in source) {
  1960. if (element.HasValue)
  1961. total = checked (total + element.Value);
  1962. }
  1963. return total;
  1964. }
  1965. public static long Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
  1966. {
  1967. Check.SourceAndSelector (source, selector);
  1968. long total = 0;
  1969. foreach (var element in source)
  1970. total = checked (total + selector (element));
  1971. return total;
  1972. }
  1973. public static long? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
  1974. {
  1975. Check.SourceAndSelector (source, selector);
  1976. long total = 0;
  1977. foreach (var element in source) {
  1978. var value = selector (element);
  1979. if (value.HasValue)
  1980. total = checked (total + value.Value);
  1981. }
  1982. return total;
  1983. }
  1984. public static double Sum (this IEnumerable<double> source)
  1985. {
  1986. Check.Source (source);
  1987. double total = 0;
  1988. foreach (var element in source)
  1989. total = checked (total + element);
  1990. return total;
  1991. }
  1992. public static double? Sum (this IEnumerable<double?> source)
  1993. {
  1994. Check.Source (source);
  1995. double total = 0;
  1996. foreach (var element in source) {
  1997. if (element.HasValue)
  1998. total = checked (total + element.Value);
  1999. }
  2000. return total;
  2001. }
  2002. public static double Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
  2003. {
  2004. Check.SourceAndSelector (source, selector);
  2005. double total = 0;
  2006. foreach (var element in source)
  2007. total = checked (total + selector (element));
  2008. return total;
  2009. }
  2010. public static double? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
  2011. {
  2012. Check.SourceAndSelector (source, selector);
  2013. double total = 0;
  2014. foreach (var element in source) {
  2015. var value = selector (element);
  2016. if (value.HasValue)
  2017. total = checked (total + value.Value);
  2018. }
  2019. return total;
  2020. }
  2021. public static float Sum (this IEnumerable<float> source)
  2022. {
  2023. Check.Source (source);
  2024. float total = 0;
  2025. foreach (var element in source)
  2026. total = checked (total + element);
  2027. return total;
  2028. }
  2029. public static float? Sum (this IEnumerable<float?> source)
  2030. {
  2031. Check.Source (source);
  2032. float total = 0;
  2033. foreach (var element in source) {
  2034. if (element.HasValue)
  2035. total = checked (total + element.Value);
  2036. }
  2037. return total;
  2038. }
  2039. public static float Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
  2040. {
  2041. Check.SourceAndSelector (source, selector);
  2042. float total = 0;
  2043. foreach (var element in source)
  2044. total = checked (total + selector (element));
  2045. return total;
  2046. }
  2047. public static float? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
  2048. {
  2049. Check.SourceAndSelector (source, selector);
  2050. float total = 0;
  2051. foreach (var element in source) {
  2052. var value = selector (element);
  2053. if (value.HasValue)
  2054. total = checked (total + value.Value);
  2055. }
  2056. return total;
  2057. }
  2058. public static decimal Sum (this IEnumerable<decimal> source)
  2059. {
  2060. Check.Source (source);
  2061. decimal total = 0;
  2062. foreach (var element in source)
  2063. total = checked (total + element);
  2064. return total;
  2065. }
  2066. public static decimal? Sum (this IEnumerable<decimal?> source)
  2067. {
  2068. Check.Source (source);
  2069. decimal total = 0;
  2070. foreach (var element in source) {
  2071. if (element.HasValue)
  2072. total = checked (total + element.Value);
  2073. }
  2074. return total;
  2075. }
  2076. public static decimal Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
  2077. {
  2078. Check.SourceAndSelector (source, selector);
  2079. decimal total = 0;
  2080. foreach (var element in source)
  2081. total = checked (total + selector (element));
  2082. return total;
  2083. }
  2084. public static decimal? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
  2085. {
  2086. Check.SourceAndSelector (source, selector);
  2087. decimal total = 0;
  2088. foreach (var element in source) {
  2089. var value = selector (element);
  2090. if (value.HasValue)
  2091. total = checked (total + value.Value);
  2092. }
  2093. return total;
  2094. }
  2095. #endregion
  2096. #region Take
  2097. public static IEnumerable<TSource> Take<TSource> (this IEnumerable<TSource> source, int count)
  2098. {
  2099. Check.Source (source);
  2100. return CreateTakeIterator (source, count);
  2101. }
  2102. static IEnumerable<TSource> CreateTakeIterator<TSource> (IEnumerable<TSource> source, int count)
  2103. {
  2104. if (count <= 0)
  2105. yield break;
  2106. int counter = 0;
  2107. foreach (TSource element in source) {
  2108. yield return element;
  2109. if (++counter == count)
  2110. yield break;
  2111. }
  2112. }
  2113. #endregion
  2114. #region TakeWhile
  2115. public static IEnumerable<TSource> TakeWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2116. {
  2117. Check.SourceAndPredicate (source, predicate);
  2118. return CreateTakeWhileIterator (source, predicate);
  2119. }
  2120. static IEnumerable<TSource> CreateTakeWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2121. {
  2122. foreach (var element in source) {
  2123. if (!predicate (element))
  2124. yield break;
  2125. yield return element;
  2126. }
  2127. }
  2128. public static IEnumerable<TSource> TakeWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2129. {
  2130. Check.SourceAndPredicate (source, predicate);
  2131. return CreateTakeWhileIterator (source, predicate);
  2132. }
  2133. static IEnumerable<TSource> CreateTakeWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2134. {
  2135. int counter = 0;
  2136. foreach (var element in source) {
  2137. if (!predicate (element, counter))
  2138. yield break;
  2139. yield return element;
  2140. counter++;
  2141. }
  2142. }
  2143. #endregion
  2144. #region ThenBy
  2145. public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey> (this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  2146. {
  2147. return ThenBy<TSource, TKey> (source, keySelector, null);
  2148. }
  2149. public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey> (this IOrderedEnumerable<TSource> source,
  2150. Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  2151. {
  2152. Check.SourceAndKeySelector (source, keySelector);
  2153. return source.CreateOrderedEnumerable (keySelector, comparer, false);
  2154. }
  2155. #endregion
  2156. #region ThenByDescending
  2157. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey> (this IOrderedEnumerable<TSource> source,
  2158. Func<TSource, TKey> keySelector)
  2159. {
  2160. return ThenByDescending<TSource, TKey> (source, keySelector, null);
  2161. }
  2162. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey> (this IOrderedEnumerable<TSource> source,
  2163. Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  2164. {
  2165. Check.SourceAndKeySelector (source, keySelector);
  2166. return source.CreateOrderedEnumerable (keySelector, comparer, true);
  2167. }
  2168. #endregion
  2169. #region ToArray
  2170. public static TSource [] ToArray<TSource> (this IEnumerable<TSource> source)
  2171. {
  2172. Check.Source (source);
  2173. var collection = source as ICollection<TSource>;
  2174. if (collection != null) {
  2175. var array = new TSource [collection.Count];
  2176. collection.CopyTo (array, 0);
  2177. return array;
  2178. }
  2179. return new List<TSource> (source).ToArray ();
  2180. }
  2181. #endregion
  2182. #region ToDictionary
  2183. public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2184. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  2185. {
  2186. return ToDictionary<TSource, TKey, TElement> (source, keySelector, elementSelector, null);
  2187. }
  2188. public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2189. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  2190. {
  2191. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  2192. if (comparer == null)
  2193. comparer = EqualityComparer<TKey>.Default;
  2194. var dict = new Dictionary<TKey, TElement> (comparer);
  2195. foreach (var e in source)
  2196. dict.Add (keySelector (e), elementSelector (e));
  2197. return dict;
  2198. }
  2199. public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this IEnumerable<TSource> source,
  2200. Func<TSource, TKey> keySelector)
  2201. {
  2202. return ToDictionary (source, keySelector, null);
  2203. }
  2204. public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this IEnumerable<TSource> source,
  2205. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  2206. {
  2207. return ToDictionary<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, comparer);
  2208. }
  2209. #endregion
  2210. #region ToList
  2211. public static List<TSource> ToList<TSource> (this IEnumerable<TSource> source)
  2212. {
  2213. Check.Source (source);
  2214. return new List<TSource> (source);
  2215. }
  2216. #endregion
  2217. #region ToLookup
  2218. public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  2219. {
  2220. return ToLookup<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, null);
  2221. }
  2222. public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source,
  2223. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  2224. {
  2225. return ToLookup<TSource, TKey, TSource> (source, keySelector, element => element, comparer);
  2226. }
  2227. public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2228. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  2229. {
  2230. return ToLookup<TSource, TKey, TElement> (source, keySelector, elementSelector, null);
  2231. }
  2232. public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2233. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  2234. {
  2235. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  2236. List<TElement> nullKeyElements = null;
  2237. var dictionary = new Dictionary<TKey, List<TElement>> (comparer ?? EqualityComparer<TKey>.Default);
  2238. foreach (var element in source) {
  2239. var key = keySelector (element);
  2240. List<TElement> list;
  2241. if (key == null) {
  2242. if (nullKeyElements == null)
  2243. nullKeyElements = new List<TElement> ();
  2244. list = nullKeyElements;
  2245. } else if (!dictionary.TryGetValue (key, out list)) {
  2246. list = new List<TElement> ();
  2247. dictionary.Add (key, list);
  2248. }
  2249. list.Add (elementSelector (element));
  2250. }
  2251. return new Lookup<TKey, TElement> (dictionary, nullKeyElements);
  2252. }
  2253. #endregion
  2254. #region SequenceEqual
  2255. public static bool SequenceEqual<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  2256. {
  2257. return first.SequenceEqual (second, null);
  2258. }
  2259. public static bool SequenceEqual<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  2260. {
  2261. Check.FirstAndSecond (first, second);
  2262. if (comparer == null)
  2263. comparer = EqualityComparer<TSource>.Default;
  2264. using (IEnumerator<TSource> first_enumerator = first.GetEnumerator (),
  2265. second_enumerator = second.GetEnumerator ()) {
  2266. while (first_enumerator.MoveNext ()) {
  2267. if (!second_enumerator.MoveNext ())
  2268. return false;
  2269. if (!comparer.Equals (first_enumerator.Current, second_enumerator.Current))
  2270. return false;
  2271. }
  2272. return !second_enumerator.MoveNext ();
  2273. }
  2274. }
  2275. #endregion
  2276. #region Union
  2277. public static IEnumerable<TSource> Union<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  2278. {
  2279. Check.FirstAndSecond (first, second);
  2280. return first.Union (second, null);
  2281. }
  2282. public static IEnumerable<TSource> Union<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  2283. {
  2284. Check.FirstAndSecond (first, second);
  2285. if (comparer == null)
  2286. comparer = EqualityComparer<TSource>.Default;
  2287. return CreateUnionIterator (first, second, comparer);
  2288. }
  2289. static IEnumerable<TSource> CreateUnionIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  2290. {
  2291. var items = new HashSet<TSource> (comparer);
  2292. foreach (var element in first) {
  2293. if (! items.Contains (element)) {
  2294. items.Add (element);
  2295. yield return element;
  2296. }
  2297. }
  2298. foreach (var element in second) {
  2299. if (! items.Contains (element)) {
  2300. items.Add (element);
  2301. yield return element;
  2302. }
  2303. }
  2304. }
  2305. #endregion
  2306. #if NET_4_0 || MOONLIGHT
  2307. #region Zip
  2308. public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult> (this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
  2309. {
  2310. Check.FirstAndSecond (first, second);
  2311. if (resultSelector == null)
  2312. throw new ArgumentNullException ("resultSelector");
  2313. return CreateZipIterator (first, second, resultSelector);
  2314. }
  2315. static IEnumerable<TResult> CreateZipIterator<TFirst, TSecond, TResult> (IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  2316. {
  2317. using (IEnumerator<TFirst> first_enumerator = first.GetEnumerator ()) {
  2318. using (IEnumerator<TSecond> second_enumerator = second.GetEnumerator ()) {
  2319. while (first_enumerator.MoveNext () && second_enumerator.MoveNext ()) {
  2320. yield return selector (first_enumerator.Current, second_enumerator.Current);
  2321. }
  2322. }
  2323. }
  2324. }
  2325. #endregion
  2326. #endif
  2327. #region Where
  2328. public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2329. {
  2330. Check.SourceAndPredicate (source, predicate);
  2331. return CreateWhereIterator (source, predicate);
  2332. }
  2333. static IEnumerable<TSource> CreateWhereIterator<TSource> (IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2334. {
  2335. foreach (TSource element in source)
  2336. if (predicate (element))
  2337. yield return element;
  2338. }
  2339. public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2340. {
  2341. Check.SourceAndPredicate (source, predicate);
  2342. return CreateWhereIterator (source, predicate);
  2343. }
  2344. static IEnumerable<TSource> CreateWhereIterator<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2345. {
  2346. int counter = 0;
  2347. foreach (TSource element in source) {
  2348. if (predicate (element, counter))
  2349. yield return element;
  2350. counter++;
  2351. }
  2352. }
  2353. #endregion
  2354. internal static ReadOnlyCollection<TSource> ToReadOnlyCollection<TSource> (this IEnumerable<TSource> source)
  2355. {
  2356. if (source == null)
  2357. return ReadOnlyCollectionOf<TSource>.Empty;
  2358. var ro = source as ReadOnlyCollection<TSource>;
  2359. if (ro != null)
  2360. return ro;
  2361. return new ReadOnlyCollection<TSource> (source.ToArray<TSource> ());
  2362. }
  2363. }
  2364. }