Enumerable.cs 74 KB

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