Enumerable.cs 76 KB

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