Enumerable.cs 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049
  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.Add (element))
  605. yield return element;
  606. }
  607. }
  608. #endregion
  609. #region First
  610. static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate, Fallback fallback)
  611. {
  612. foreach (var element in source)
  613. if (predicate (element))
  614. return element;
  615. if (fallback == Fallback.Throw)
  616. throw new InvalidOperationException ();
  617. return default (TSource);
  618. }
  619. public static TSource First<TSource> (this IEnumerable<TSource> source)
  620. {
  621. Check.Source (source);
  622. var list = source as IList<TSource>;
  623. if (list != null) {
  624. if (list.Count != 0)
  625. return list [0];
  626. } else {
  627. using (var enumerator = source.GetEnumerator ()) {
  628. if (enumerator.MoveNext ())
  629. return enumerator.Current;
  630. }
  631. }
  632. throw new InvalidOperationException ("The source sequence is empty");
  633. }
  634. public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  635. {
  636. Check.SourceAndPredicate (source, predicate);
  637. return source.First (predicate, Fallback.Throw);
  638. }
  639. #endregion
  640. #region FirstOrDefault
  641. public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source)
  642. {
  643. Check.Source (source);
  644. return source.First (PredicateOf<TSource>.Always, Fallback.Default);
  645. }
  646. public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  647. {
  648. Check.SourceAndPredicate (source, predicate);
  649. return source.First (predicate, Fallback.Default);
  650. }
  651. #endregion
  652. #region GroupBy
  653. private static List<T> ContainsGroup<K, T> (
  654. Dictionary<K, List<T>> items, K key, IEqualityComparer<K> comparer)
  655. {
  656. IEqualityComparer<K> comparerInUse = (comparer ?? EqualityComparer<K>.Default);
  657. foreach (KeyValuePair<K, List<T>> value in items) {
  658. if (comparerInUse.Equals (value.Key, key))
  659. return value.Value;
  660. }
  661. return null;
  662. }
  663. public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IEnumerable<TSource> source,
  664. Func<TSource, TKey> keySelector)
  665. {
  666. return GroupBy<TSource, TKey> (source, keySelector, null);
  667. }
  668. public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IEnumerable<TSource> source,
  669. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  670. {
  671. Check.SourceAndKeySelector (source, keySelector);
  672. return CreateGroupByIterator (source, keySelector, comparer);
  673. }
  674. static IEnumerable<IGrouping<TKey, TSource>> CreateGroupByIterator<TSource, TKey> (this IEnumerable<TSource> source,
  675. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  676. {
  677. var groups = new Dictionary<TKey, List<TSource>> ();
  678. var nullList = new List<TSource> ();
  679. int counter = 0;
  680. int nullCounter = -1;
  681. foreach (TSource element in source) {
  682. TKey key = keySelector (element);
  683. if (key == null) {
  684. nullList.Add (element);
  685. if (nullCounter == -1) {
  686. nullCounter = counter;
  687. counter++;
  688. }
  689. } else {
  690. List<TSource> group = ContainsGroup<TKey, TSource> (groups, key, comparer);
  691. if (group == null) {
  692. group = new List<TSource> ();
  693. groups.Add (key, group);
  694. counter++;
  695. }
  696. group.Add (element);
  697. }
  698. }
  699. counter = 0;
  700. foreach (var group in groups) {
  701. if (counter == nullCounter) {
  702. yield return new Grouping<TKey, TSource> (default (TKey), nullList);
  703. counter++;
  704. }
  705. yield return new Grouping<TKey, TSource> (group.Key, group.Value);
  706. counter++;
  707. }
  708. if (counter == nullCounter) {
  709. yield return new Grouping<TKey, TSource> (default (TKey), nullList);
  710. counter++;
  711. }
  712. }
  713. public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  714. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  715. {
  716. return GroupBy<TSource, TKey, TElement> (source, keySelector, elementSelector, null);
  717. }
  718. public static IEnumerable<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  719. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  720. {
  721. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  722. return CreateGroupByIterator (source, keySelector, elementSelector, comparer);
  723. }
  724. static IEnumerable<IGrouping<TKey, TElement>> CreateGroupByIterator<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  725. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  726. {
  727. var groups = new Dictionary<TKey, List<TElement>> ();
  728. var nullList = new List<TElement> ();
  729. int counter = 0;
  730. int nullCounter = -1;
  731. foreach (TSource item in source) {
  732. TKey key = keySelector (item);
  733. TElement element = elementSelector (item);
  734. if (key == null) {
  735. nullList.Add (element);
  736. if (nullCounter == -1) {
  737. nullCounter = counter;
  738. counter++;
  739. }
  740. } else {
  741. List<TElement> group = ContainsGroup<TKey, TElement> (groups, key, comparer);
  742. if (group == null) {
  743. group = new List<TElement> ();
  744. groups.Add (key, group);
  745. counter++;
  746. }
  747. group.Add (element);
  748. }
  749. }
  750. counter = 0;
  751. foreach (var group in groups) {
  752. if (counter == nullCounter) {
  753. yield return new Grouping<TKey, TElement> (default (TKey), nullList);
  754. counter++;
  755. }
  756. yield return new Grouping<TKey, TElement> (group.Key, group.Value);
  757. counter++;
  758. }
  759. if (counter == nullCounter) {
  760. yield return new Grouping<TKey, TElement> (default (TKey), nullList);
  761. counter++;
  762. }
  763. }
  764. public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
  765. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
  766. Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
  767. {
  768. return GroupBy (source, keySelector, elementSelector, resultSelector, null);
  769. }
  770. public static IEnumerable<TResult> GroupBy<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
  771. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
  772. Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
  773. IEqualityComparer<TKey> comparer)
  774. {
  775. Check.GroupBySelectors (source, keySelector, elementSelector, resultSelector);
  776. return CreateGroupByIterator (source, keySelector, elementSelector, resultSelector, comparer);
  777. }
  778. static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TElement, TResult> (this IEnumerable<TSource> source,
  779. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector,
  780. Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
  781. IEqualityComparer<TKey> comparer)
  782. {
  783. IEnumerable<IGrouping<TKey, TElement>> groups = GroupBy<TSource, TKey, TElement> (
  784. source, keySelector, elementSelector, comparer);
  785. foreach (IGrouping<TKey, TElement> group in groups)
  786. yield return resultSelector (group.Key, group);
  787. }
  788. public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult> (this IEnumerable<TSource> source,
  789. Func<TSource, TKey> keySelector,
  790. Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
  791. {
  792. return GroupBy (source, keySelector, resultSelector, null);
  793. }
  794. public static IEnumerable<TResult> GroupBy<TSource, TKey, TResult> (this IEnumerable<TSource> source,
  795. Func<TSource, TKey> keySelector,
  796. Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
  797. IEqualityComparer<TKey> comparer)
  798. {
  799. Check.SourceAndKeyResultSelectors (source, keySelector, resultSelector);
  800. return CreateGroupByIterator (source, keySelector, resultSelector, comparer);
  801. }
  802. static IEnumerable<TResult> CreateGroupByIterator<TSource, TKey, TResult> (this IEnumerable<TSource> source,
  803. Func<TSource, TKey> keySelector,
  804. Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
  805. IEqualityComparer<TKey> comparer)
  806. {
  807. IEnumerable<IGrouping<TKey,TSource>> groups = GroupBy<TSource, TKey> (source, keySelector, comparer);
  808. foreach (IGrouping<TKey, TSource> group in groups)
  809. yield return resultSelector (group.Key, group);
  810. }
  811. #endregion
  812. # region GroupJoin
  813. public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  814. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  815. Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
  816. {
  817. return GroupJoin (outer, inner, outerKeySelector, innerKeySelector, resultSelector, null);
  818. }
  819. public static IEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  820. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  821. Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
  822. IEqualityComparer<TKey> comparer)
  823. {
  824. Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
  825. if (comparer == null)
  826. comparer = EqualityComparer<TKey>.Default;
  827. return CreateGroupJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  828. }
  829. static IEnumerable<TResult> CreateGroupJoinIterator<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  830. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  831. Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
  832. IEqualityComparer<TKey> comparer)
  833. {
  834. ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
  835. /*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
  836. foreach (U element in inner)
  837. {
  838. K innerKey = innerKeySelector (element);
  839. if (!innerKeys.ContainsKey (innerKey))
  840. innerKeys.Add (innerKey, new List<U> ());
  841. innerKeys[innerKey].Add (element);
  842. }*/
  843. foreach (TOuter element in outer) {
  844. TKey outerKey = outerKeySelector (element);
  845. if (innerKeys.Contains (outerKey))
  846. yield return resultSelector (element, innerKeys [outerKey]);
  847. else
  848. yield return resultSelector (element, Empty<TInner> ());
  849. }
  850. }
  851. #endregion
  852. #region Intersect
  853. public static IEnumerable<TSource> Intersect<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  854. {
  855. return Intersect (first, second, null);
  856. }
  857. public static IEnumerable<TSource> Intersect<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  858. {
  859. Check.FirstAndSecond (first, second);
  860. if (comparer == null)
  861. comparer = EqualityComparer<TSource>.Default;
  862. return CreateIntersectIterator (first, second, comparer);
  863. }
  864. static IEnumerable<TSource> CreateIntersectIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  865. {
  866. var items = new HashSet<TSource> (second, comparer);
  867. foreach (TSource element in first) {
  868. if (items.Remove (element))
  869. yield return element;
  870. }
  871. }
  872. #endregion
  873. # region Join
  874. public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  875. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  876. Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
  877. {
  878. Check.JoinSelectors (outer, inner, outerKeySelector, innerKeySelector, resultSelector);
  879. if (comparer == null)
  880. comparer = EqualityComparer<TKey>.Default;
  881. return CreateJoinIterator (outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  882. }
  883. static IEnumerable<TResult> CreateJoinIterator<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  884. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  885. Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
  886. {
  887. ILookup<TKey, TInner> innerKeys = ToLookup<TInner, TKey> (inner, innerKeySelector, comparer);
  888. /*Dictionary<K, List<U>> innerKeys = new Dictionary<K, List<U>> ();
  889. foreach (U element in inner)
  890. {
  891. K innerKey = innerKeySelector (element);
  892. if (!innerKeys.ContainsKey (innerKey))
  893. innerKeys.Add (innerKey, new List<U> ());
  894. innerKeys[innerKey].Add (element);
  895. }*/
  896. foreach (TOuter element in outer) {
  897. TKey outerKey = outerKeySelector (element);
  898. if (innerKeys.Contains (outerKey)) {
  899. foreach (TInner innerElement in innerKeys [outerKey])
  900. yield return resultSelector (element, innerElement);
  901. }
  902. }
  903. }
  904. public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult> (this IEnumerable<TOuter> outer,
  905. IEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector,
  906. Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector)
  907. {
  908. return outer.Join (inner, outerKeySelector, innerKeySelector, resultSelector, null);
  909. }
  910. #endregion
  911. #region Last
  912. static TSource Last<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate, Fallback fallback)
  913. {
  914. var empty = true;
  915. var item = default (TSource);
  916. foreach (var element in source) {
  917. if (!predicate (element))
  918. continue;
  919. item = element;
  920. empty = false;
  921. }
  922. if (!empty)
  923. return item;
  924. if (fallback == Fallback.Throw)
  925. throw new InvalidOperationException ();
  926. return item;
  927. }
  928. public static TSource Last<TSource> (this IEnumerable<TSource> source)
  929. {
  930. Check.Source (source);
  931. var collection = source as ICollection<TSource>;
  932. if (collection != null && collection.Count == 0)
  933. throw new InvalidOperationException ();
  934. var list = source as IList<TSource>;
  935. if (list != null)
  936. return list [list.Count - 1];
  937. return source.Last (PredicateOf<TSource>.Always, Fallback.Throw);
  938. }
  939. public static TSource Last<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  940. {
  941. Check.SourceAndPredicate (source, predicate);
  942. return source.Last (predicate, Fallback.Throw);
  943. }
  944. #endregion
  945. #region LastOrDefault
  946. public static TSource LastOrDefault<TSource> (this IEnumerable<TSource> source)
  947. {
  948. Check.Source (source);
  949. var list = source as IList<TSource>;
  950. if (list != null)
  951. return list.Count > 0 ? list [list.Count - 1] : default (TSource);
  952. return source.Last (PredicateOf<TSource>.Always, Fallback.Default);
  953. }
  954. public static TSource LastOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  955. {
  956. Check.SourceAndPredicate (source, predicate);
  957. return source.Last (predicate, Fallback.Default);
  958. }
  959. #endregion
  960. #region LongCount
  961. public static long LongCount<TSource> (this IEnumerable<TSource> source)
  962. {
  963. Check.Source (source);
  964. #if !NET_2_1
  965. var array = source as TSource [];
  966. if (array != null)
  967. return array.LongLength;
  968. #endif
  969. long counter = 0;
  970. using (var enumerator = source.GetEnumerator ())
  971. while (enumerator.MoveNext ())
  972. counter++;
  973. return counter;
  974. }
  975. public static long LongCount<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> selector)
  976. {
  977. Check.SourceAndSelector (source, selector);
  978. long counter = 0;
  979. foreach (TSource element in source)
  980. if (selector (element))
  981. counter++;
  982. return counter;
  983. }
  984. #endregion
  985. #region Max
  986. public static int Max (this IEnumerable<int> source)
  987. {
  988. Check.Source (source);
  989. bool empty = true;
  990. var max = int.MinValue;
  991. foreach (var element in source){
  992. max = Math.Max (element, max);
  993. empty = false;
  994. }
  995. if (empty)
  996. throw new InvalidOperationException ();
  997. return max;
  998. }
  999. public static long Max (this IEnumerable<long> source)
  1000. {
  1001. Check.Source (source);
  1002. bool empty = true;
  1003. var max = long.MinValue;
  1004. foreach (var element in source){
  1005. max = Math.Max (element, max);
  1006. empty = false;
  1007. }
  1008. if (empty)
  1009. throw new InvalidOperationException ();
  1010. return max;
  1011. }
  1012. public static double Max (this IEnumerable<double> source)
  1013. {
  1014. Check.Source (source);
  1015. bool empty = true;
  1016. var max = double.MinValue;
  1017. foreach (var element in source){
  1018. max = Math.Max (element, max);
  1019. empty = false;
  1020. }
  1021. if (empty)
  1022. throw new InvalidOperationException ();
  1023. return max;
  1024. }
  1025. public static float Max (this IEnumerable<float> source)
  1026. {
  1027. Check.Source (source);
  1028. bool empty = true;
  1029. var max = float.MinValue;
  1030. foreach (var element in source){
  1031. max = Math.Max (element, max);
  1032. empty = false;
  1033. }
  1034. if (empty)
  1035. throw new InvalidOperationException ();
  1036. return max;
  1037. }
  1038. public static decimal Max (this IEnumerable<decimal> source)
  1039. {
  1040. Check.Source (source);
  1041. bool empty = true;
  1042. var max = decimal.MinValue;
  1043. foreach (var element in source){
  1044. max = Math.Max (element, max);
  1045. empty = false;
  1046. }
  1047. if (empty)
  1048. throw new InvalidOperationException ();
  1049. return max;
  1050. }
  1051. public static int? Max (this IEnumerable<int?> source)
  1052. {
  1053. Check.Source (source);
  1054. bool empty = true;
  1055. var max = int.MinValue;
  1056. foreach (var element in source) {
  1057. if (!element.HasValue)
  1058. continue;
  1059. max = Math.Max (element.Value, max);
  1060. empty = false;
  1061. }
  1062. if (empty)
  1063. return null;
  1064. return max;
  1065. }
  1066. public static long? Max (this IEnumerable<long?> source)
  1067. {
  1068. Check.Source (source);
  1069. bool empty = true;
  1070. var max = long.MinValue;
  1071. foreach (var element in source) {
  1072. if (!element.HasValue)
  1073. continue;
  1074. max = Math.Max (element.Value, max);
  1075. empty = false;
  1076. }
  1077. if (empty)
  1078. return null;
  1079. return max;
  1080. }
  1081. public static double? Max (this IEnumerable<double?> source)
  1082. {
  1083. Check.Source (source);
  1084. bool empty = true;
  1085. var max = double.MinValue;
  1086. foreach (var element in source) {
  1087. if (!element.HasValue)
  1088. continue;
  1089. max = Math.Max (element.Value, max);
  1090. empty = false;
  1091. }
  1092. if (empty)
  1093. return null;
  1094. return max;
  1095. }
  1096. public static float? Max (this IEnumerable<float?> source)
  1097. {
  1098. Check.Source (source);
  1099. bool empty = true;
  1100. var max = float.MinValue;
  1101. foreach (var element in source) {
  1102. if (!element.HasValue)
  1103. continue;
  1104. max = Math.Max (element.Value, max);
  1105. empty = false;
  1106. }
  1107. if (empty)
  1108. return null;
  1109. return max;
  1110. }
  1111. public static decimal? Max (this IEnumerable<decimal?> source)
  1112. {
  1113. Check.Source (source);
  1114. bool empty = true;
  1115. var max = decimal.MinValue;
  1116. foreach (var element in source) {
  1117. if (!element.HasValue)
  1118. continue;
  1119. max = Math.Max (element.Value, max);
  1120. empty = false;
  1121. }
  1122. if (empty)
  1123. return null;
  1124. return max;
  1125. }
  1126. // TODO: test nullable and non-nullable
  1127. public static TSource Max<TSource> (this IEnumerable<TSource> source)
  1128. {
  1129. Check.Source (source);
  1130. var comparer = Comparer<TSource>.Default;
  1131. TSource max = default (TSource);
  1132. if (default (TSource) == null){
  1133. foreach (var element in source) {
  1134. if (element == null)
  1135. continue;
  1136. if (max == null || comparer.Compare (element, max) > 0)
  1137. max = element;
  1138. }
  1139. } else {
  1140. bool empty = true;
  1141. foreach (var element in source) {
  1142. if (empty){
  1143. max = element;
  1144. empty = false;
  1145. continue;
  1146. }
  1147. if (comparer.Compare (element, max) > 0)
  1148. max = element;
  1149. }
  1150. if (empty)
  1151. throw new InvalidOperationException ();
  1152. }
  1153. return max;
  1154. }
  1155. public static int Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
  1156. {
  1157. Check.SourceAndSelector (source, selector);
  1158. bool empty = true;
  1159. var max = int.MinValue;
  1160. foreach (var element in source){
  1161. max = Math.Max (selector (element), max);
  1162. empty = false;
  1163. }
  1164. if (empty)
  1165. throw new InvalidOperationException ();
  1166. return max;
  1167. }
  1168. public static long Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
  1169. {
  1170. Check.SourceAndSelector (source, selector);
  1171. bool empty = true;
  1172. var max = long.MinValue;
  1173. foreach (var element in source){
  1174. max = Math.Max (selector (element), max);
  1175. empty = false;
  1176. }
  1177. if (empty)
  1178. throw new InvalidOperationException ();
  1179. return max;
  1180. }
  1181. public static double Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
  1182. {
  1183. Check.SourceAndSelector (source, selector);
  1184. bool empty = true;
  1185. var max = double.MinValue;
  1186. foreach (var element in source){
  1187. max = Math.Max (selector (element), max);
  1188. empty = false;
  1189. }
  1190. if (empty)
  1191. throw new InvalidOperationException ();
  1192. return max;
  1193. }
  1194. public static float Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
  1195. {
  1196. Check.SourceAndSelector (source, selector);
  1197. bool empty = true;
  1198. var max = float.MinValue;
  1199. foreach (var element in source){
  1200. max = Math.Max (selector (element), max);
  1201. empty = false;
  1202. }
  1203. if (empty)
  1204. throw new InvalidOperationException ();
  1205. return max;
  1206. }
  1207. public static decimal Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
  1208. {
  1209. Check.SourceAndSelector (source, selector);
  1210. bool empty = true;
  1211. var max = decimal.MinValue;
  1212. foreach (var element in source){
  1213. max = Math.Max (selector (element), max);
  1214. empty = false;
  1215. }
  1216. if (empty)
  1217. throw new InvalidOperationException ();
  1218. return max;
  1219. }
  1220. static U Iterate<T, U> (IEnumerable<T> source, U initValue, Func<T, U, U> selector)
  1221. {
  1222. bool empty = true;
  1223. foreach (var element in source) {
  1224. initValue = selector (element, initValue);
  1225. empty = false;
  1226. }
  1227. if (empty)
  1228. throw new InvalidOperationException ();
  1229. return initValue;
  1230. }
  1231. public static int? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
  1232. {
  1233. Check.SourceAndSelector (source, selector);
  1234. bool empty = true;
  1235. int? max = null;
  1236. foreach (var element in source) {
  1237. int? item = selector (element);
  1238. if (!max.HasValue)
  1239. max = item;
  1240. else if (item > max)
  1241. max = item;
  1242. empty = false;
  1243. }
  1244. if (empty)
  1245. return null;
  1246. return max;
  1247. }
  1248. public static long? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
  1249. {
  1250. Check.SourceAndSelector (source, selector);
  1251. bool empty = true;
  1252. long? max = null;
  1253. foreach (var element in source) {
  1254. long? item = selector (element);
  1255. if (!max.HasValue)
  1256. max = item;
  1257. else if (item > max)
  1258. max = item;
  1259. empty = false;
  1260. }
  1261. if (empty)
  1262. return null;
  1263. return max;
  1264. }
  1265. public static double? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
  1266. {
  1267. Check.SourceAndSelector (source, selector);
  1268. bool empty = true;
  1269. double? max = null;
  1270. foreach (var element in source) {
  1271. double? item = selector (element);
  1272. if (!max.HasValue)
  1273. max = item;
  1274. else if (item > max)
  1275. max = item;
  1276. empty = false;
  1277. }
  1278. if (empty)
  1279. return null;
  1280. return max;
  1281. }
  1282. public static float? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
  1283. {
  1284. Check.SourceAndSelector (source, selector);
  1285. bool empty = true;
  1286. float? max = null;
  1287. foreach (var element in source) {
  1288. float? item = selector (element);
  1289. if (!max.HasValue)
  1290. max = item;
  1291. else if (item > max)
  1292. max = item;
  1293. empty = false;
  1294. }
  1295. if (empty)
  1296. return null;
  1297. return max;
  1298. }
  1299. public static decimal? Max<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
  1300. {
  1301. Check.SourceAndSelector (source, selector);
  1302. bool empty = true;
  1303. decimal? max = null;
  1304. foreach (var element in source) {
  1305. decimal? item = selector (element);
  1306. if (!max.HasValue)
  1307. max = item;
  1308. else if (item > max)
  1309. max = item;
  1310. empty = false;
  1311. }
  1312. if (empty)
  1313. return null;
  1314. return max;
  1315. }
  1316. public static TResult Max<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1317. {
  1318. Check.SourceAndSelector (source, selector);
  1319. // TODO: inline
  1320. return source.Select (selector).Max ();
  1321. }
  1322. #endregion
  1323. #region Min
  1324. public static int Min (this IEnumerable<int> source)
  1325. {
  1326. Check.Source (source);
  1327. bool empty = true;
  1328. var min = int.MaxValue;
  1329. foreach (var element in source){
  1330. min = Math.Min (element, min);
  1331. empty = false;
  1332. }
  1333. if (empty)
  1334. throw new InvalidOperationException ();
  1335. return min;
  1336. }
  1337. public static long Min (this IEnumerable<long> source)
  1338. {
  1339. Check.Source (source);
  1340. bool empty = true;
  1341. var min = long.MaxValue;
  1342. foreach (var element in source){
  1343. min = Math.Min (element, min);
  1344. empty = false;
  1345. }
  1346. if (empty)
  1347. throw new InvalidOperationException ();
  1348. return min;
  1349. }
  1350. public static double Min (this IEnumerable<double> source)
  1351. {
  1352. Check.Source (source);
  1353. bool empty = true;
  1354. var min = double.MaxValue;
  1355. foreach (var element in source){
  1356. min = Math.Min (element, min);
  1357. empty = false;
  1358. }
  1359. if (empty)
  1360. throw new InvalidOperationException ();
  1361. return min;
  1362. }
  1363. public static float Min (this IEnumerable<float> source)
  1364. {
  1365. Check.Source (source);
  1366. bool empty = true;
  1367. var min = float.MaxValue;
  1368. foreach (var element in source){
  1369. min = Math.Min (element, min);
  1370. empty = false;
  1371. }
  1372. if (empty)
  1373. throw new InvalidOperationException ();
  1374. return min;
  1375. }
  1376. public static decimal Min (this IEnumerable<decimal> source)
  1377. {
  1378. Check.Source (source);
  1379. bool empty = true;
  1380. var min = decimal.MaxValue;
  1381. foreach (var element in source){
  1382. min = Math.Min (element, min);
  1383. empty = false;
  1384. }
  1385. if (empty)
  1386. throw new InvalidOperationException ();
  1387. return min;
  1388. }
  1389. public static int? Min (this IEnumerable<int?> source)
  1390. {
  1391. Check.Source (source);
  1392. bool empty = true;
  1393. var min = int.MaxValue;
  1394. foreach (var element in source) {
  1395. if (!element.HasValue)
  1396. continue;
  1397. min = Math.Min (element.Value, min);
  1398. empty = false;
  1399. }
  1400. if (empty)
  1401. return null;
  1402. return min;
  1403. }
  1404. public static long? Min (this IEnumerable<long?> source)
  1405. {
  1406. Check.Source (source);
  1407. bool empty = true;
  1408. var min = long.MaxValue;
  1409. foreach (var element in source) {
  1410. if (!element.HasValue)
  1411. continue;
  1412. min = Math.Min (element.Value, min);
  1413. empty = false;
  1414. }
  1415. if (empty)
  1416. return null;
  1417. return min;
  1418. }
  1419. public static double? Min (this IEnumerable<double?> source)
  1420. {
  1421. Check.Source (source);
  1422. bool empty = true;
  1423. var min = double.MaxValue;
  1424. foreach (var element in source) {
  1425. if (!element.HasValue)
  1426. continue;
  1427. min = Math.Min (element.Value, min);
  1428. empty = false;
  1429. }
  1430. if (empty)
  1431. return null;
  1432. return min;
  1433. }
  1434. public static float? Min (this IEnumerable<float?> source)
  1435. {
  1436. Check.Source (source);
  1437. bool empty = true;
  1438. var min = float.MaxValue;
  1439. foreach (var element in source) {
  1440. if (!element.HasValue)
  1441. continue;
  1442. min = Math.Min (element.Value, min);
  1443. empty = false;
  1444. }
  1445. if (empty)
  1446. return null;
  1447. return min;
  1448. }
  1449. public static decimal? Min (this IEnumerable<decimal?> source)
  1450. {
  1451. Check.Source (source);
  1452. bool empty = true;
  1453. var min = decimal.MaxValue;
  1454. foreach (var element in source) {
  1455. if (!element.HasValue)
  1456. continue;
  1457. min = Math.Min (element.Value, min);
  1458. empty = false;
  1459. }
  1460. if (empty)
  1461. return null;
  1462. return min;
  1463. }
  1464. public static TSource Min<TSource> (this IEnumerable<TSource> source)
  1465. {
  1466. Check.Source (source);
  1467. var comparer = Comparer<TSource>.Default;
  1468. TSource min = default (TSource);
  1469. if (default (TSource) == null){
  1470. foreach (var element in source) {
  1471. if (element == null)
  1472. continue;
  1473. if (min == null || comparer.Compare (element, min) < 0)
  1474. min = element;
  1475. }
  1476. } else {
  1477. bool empty = true;
  1478. foreach (var element in source) {
  1479. if (empty){
  1480. min = element;
  1481. empty = false;
  1482. continue;
  1483. }
  1484. if (comparer.Compare (element, min) < 0)
  1485. min = element;
  1486. }
  1487. if (empty)
  1488. throw new InvalidOperationException ();
  1489. }
  1490. return min;
  1491. }
  1492. public static int Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
  1493. {
  1494. Check.SourceAndSelector (source, selector);
  1495. bool empty = true;
  1496. var min = int.MaxValue;
  1497. foreach (var element in source){
  1498. min = Math.Min (selector (element), min);
  1499. empty = false;
  1500. }
  1501. if (empty)
  1502. throw new InvalidOperationException ();
  1503. return min;
  1504. }
  1505. public static long Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
  1506. {
  1507. Check.SourceAndSelector (source, selector);
  1508. bool empty = true;
  1509. var min = long.MaxValue;
  1510. foreach (var element in source){
  1511. min = Math.Min (selector (element), min);
  1512. empty = false;
  1513. }
  1514. if (empty)
  1515. throw new InvalidOperationException ();
  1516. return min;
  1517. }
  1518. public static double Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
  1519. {
  1520. Check.SourceAndSelector (source, selector);
  1521. bool empty = true;
  1522. var min = double.MaxValue;
  1523. foreach (var element in source){
  1524. min = Math.Min (selector (element), min);
  1525. empty = false;
  1526. }
  1527. if (empty)
  1528. throw new InvalidOperationException ();
  1529. return min;
  1530. }
  1531. public static float Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
  1532. {
  1533. Check.SourceAndSelector (source, selector);
  1534. bool empty = true;
  1535. var min = float.MaxValue;
  1536. foreach (var element in source){
  1537. min = Math.Min (selector (element), min);
  1538. empty = false;
  1539. }
  1540. if (empty)
  1541. throw new InvalidOperationException ();
  1542. return min;
  1543. }
  1544. public static decimal Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
  1545. {
  1546. Check.SourceAndSelector (source, selector);
  1547. bool empty = true;
  1548. var min = decimal.MaxValue;
  1549. foreach (var element in source){
  1550. min = Math.Min (selector (element), min);
  1551. empty = false;
  1552. }
  1553. if (empty)
  1554. throw new InvalidOperationException ();
  1555. return min;
  1556. }
  1557. public static int? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
  1558. {
  1559. Check.SourceAndSelector (source, selector);
  1560. bool empty = true;
  1561. int? min = null;
  1562. foreach (var element in source) {
  1563. int? item = selector (element);
  1564. if (!min.HasValue)
  1565. min = item;
  1566. else if (item < min)
  1567. min = item;
  1568. empty = false;
  1569. }
  1570. if (empty)
  1571. return null;
  1572. return min;
  1573. }
  1574. public static long? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
  1575. {
  1576. Check.SourceAndSelector (source, selector);
  1577. bool empty = true;
  1578. long? min = null;
  1579. foreach (var element in source) {
  1580. long? item = selector (element);
  1581. if (!min.HasValue)
  1582. min = item;
  1583. else if (item < min)
  1584. min = item;
  1585. empty = false;
  1586. }
  1587. if (empty)
  1588. return null;
  1589. return min;
  1590. }
  1591. public static float? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
  1592. {
  1593. Check.SourceAndSelector (source, selector);
  1594. bool empty = true;
  1595. float? min = null;
  1596. foreach (var element in source) {
  1597. float? item = selector (element);
  1598. if (!min.HasValue)
  1599. min = item;
  1600. else if (item < min)
  1601. min = item;
  1602. empty = false;
  1603. }
  1604. if (empty)
  1605. return null;
  1606. return min;
  1607. }
  1608. public static double? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
  1609. {
  1610. Check.SourceAndSelector (source, selector);
  1611. bool empty = true;
  1612. double? min = null;
  1613. foreach (var element in source) {
  1614. double? item = selector (element);
  1615. if (!min.HasValue)
  1616. min = item;
  1617. else if (item < min)
  1618. min = item;
  1619. empty = false;
  1620. }
  1621. if (empty)
  1622. return null;
  1623. return min;
  1624. }
  1625. public static decimal? Min<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
  1626. {
  1627. Check.SourceAndSelector (source, selector);
  1628. bool empty = true;
  1629. decimal? min = null;
  1630. foreach (var element in source) {
  1631. decimal? item = selector (element);
  1632. if (!min.HasValue)
  1633. min = item;
  1634. else if (item < min)
  1635. min = item;
  1636. empty = false;
  1637. }
  1638. if (empty)
  1639. return null;
  1640. return min;
  1641. }
  1642. public static TResult Min<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1643. {
  1644. Check.SourceAndSelector (source, selector);
  1645. // TODO: inline
  1646. return source.Select (selector).Min ();
  1647. }
  1648. #endregion
  1649. #region OfType
  1650. public static IEnumerable<TResult> OfType<TResult> (this IEnumerable source)
  1651. {
  1652. Check.Source (source);
  1653. return CreateOfTypeIterator<TResult> (source);
  1654. }
  1655. static IEnumerable<TResult> CreateOfTypeIterator<TResult> (IEnumerable source)
  1656. {
  1657. foreach (object element in source)
  1658. if (element is TResult)
  1659. yield return (TResult) element;
  1660. }
  1661. #endregion
  1662. #region OrderBy
  1663. public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey> (this IEnumerable<TSource> source,
  1664. Func<TSource, TKey> keySelector)
  1665. {
  1666. return OrderBy<TSource, TKey> (source, keySelector, null);
  1667. }
  1668. public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey> (this IEnumerable<TSource> source,
  1669. Func<TSource, TKey> keySelector,
  1670. IComparer<TKey> comparer)
  1671. {
  1672. Check.SourceAndKeySelector (source, keySelector);
  1673. return new OrderedSequence<TSource, TKey> (source, keySelector, comparer, SortDirection.Ascending);
  1674. }
  1675. #endregion
  1676. #region OrderByDescending
  1677. public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey> (this IEnumerable<TSource> source,
  1678. Func<TSource, TKey> keySelector)
  1679. {
  1680. return OrderByDescending<TSource, TKey> (source, keySelector, null);
  1681. }
  1682. public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey> (this IEnumerable<TSource> source,
  1683. Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  1684. {
  1685. Check.SourceAndKeySelector (source, keySelector);
  1686. return new OrderedSequence<TSource, TKey> (source, keySelector, comparer, SortDirection.Descending);
  1687. }
  1688. #endregion
  1689. #region Range
  1690. public static IEnumerable<int> Range (int start, int count)
  1691. {
  1692. if (count < 0)
  1693. throw new ArgumentOutOfRangeException ("count");
  1694. long upto = ((long) start + count) - 1;
  1695. if (upto > int.MaxValue)
  1696. throw new ArgumentOutOfRangeException ();
  1697. return CreateRangeIterator (start, (int) upto);
  1698. }
  1699. static IEnumerable<int> CreateRangeIterator (int start, int upto)
  1700. {
  1701. for (int i = start; i <= upto; i++)
  1702. yield return i;
  1703. }
  1704. #endregion
  1705. #region Repeat
  1706. public static IEnumerable<TResult> Repeat<TResult> (TResult element, int count)
  1707. {
  1708. if (count < 0)
  1709. throw new ArgumentOutOfRangeException ();
  1710. return CreateRepeatIterator (element, count);
  1711. }
  1712. static IEnumerable<TResult> CreateRepeatIterator<TResult> (TResult element, int count)
  1713. {
  1714. for (int i = 0; i < count; i++)
  1715. yield return element;
  1716. }
  1717. #endregion
  1718. #region Reverse
  1719. public static IEnumerable<TSource> Reverse<TSource> (this IEnumerable<TSource> source)
  1720. {
  1721. Check.Source (source);
  1722. return CreateReverseIterator (source);
  1723. }
  1724. static IEnumerable<TSource> CreateReverseIterator<TSource> (IEnumerable<TSource> source)
  1725. {
  1726. var list = source as IList<TSource>;
  1727. if (list == null)
  1728. list = new List<TSource> (source);
  1729. for (int i = list.Count - 1; i >= 0; i--)
  1730. yield return list [i];
  1731. }
  1732. #endregion
  1733. #region Select
  1734. public static IEnumerable<TResult> Select<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1735. {
  1736. Check.SourceAndSelector (source, selector);
  1737. return CreateSelectIterator (source, selector);
  1738. }
  1739. static IEnumerable<TResult> CreateSelectIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, TResult> selector)
  1740. {
  1741. foreach (var element in source)
  1742. yield return selector (element);
  1743. }
  1744. public static IEnumerable<TResult> Select<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
  1745. {
  1746. Check.SourceAndSelector (source, selector);
  1747. return CreateSelectIterator (source, selector);
  1748. }
  1749. static IEnumerable<TResult> CreateSelectIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
  1750. {
  1751. int counter = 0;
  1752. foreach (TSource element in source) {
  1753. yield return selector (element, counter);
  1754. counter++;
  1755. }
  1756. }
  1757. #endregion
  1758. #region SelectMany
  1759. public static IEnumerable<TResult> SelectMany<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
  1760. {
  1761. Check.SourceAndSelector (source, selector);
  1762. return CreateSelectManyIterator (source, selector);
  1763. }
  1764. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)
  1765. {
  1766. foreach (TSource element in source)
  1767. foreach (TResult item in selector (element))
  1768. yield return item;
  1769. }
  1770. public static IEnumerable<TResult> SelectMany<TSource, TResult> (this IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector)
  1771. {
  1772. Check.SourceAndSelector (source, selector);
  1773. return CreateSelectManyIterator (source, selector);
  1774. }
  1775. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TResult> (IEnumerable<TSource> source, Func<TSource, int, IEnumerable<TResult>> selector)
  1776. {
  1777. int counter = 0;
  1778. foreach (TSource element in source) {
  1779. foreach (TResult item in selector (element, counter))
  1780. yield return item;
  1781. counter++;
  1782. }
  1783. }
  1784. public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
  1785. Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
  1786. {
  1787. Check.SourceAndCollectionSelectors (source, collectionSelector, selector);
  1788. return CreateSelectManyIterator (source, collectionSelector, selector);
  1789. }
  1790. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
  1791. Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
  1792. {
  1793. foreach (TSource element in source)
  1794. foreach (TCollection collection in collectionSelector (element))
  1795. yield return selector (element, collection);
  1796. }
  1797. public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult> (this IEnumerable<TSource> source,
  1798. Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
  1799. {
  1800. Check.SourceAndCollectionSelectors (source, collectionSelector, selector);
  1801. return CreateSelectManyIterator (source, collectionSelector, selector);
  1802. }
  1803. static IEnumerable<TResult> CreateSelectManyIterator<TSource, TCollection, TResult> (IEnumerable<TSource> source,
  1804. Func<TSource, int, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> selector)
  1805. {
  1806. int counter = 0;
  1807. foreach (TSource element in source)
  1808. foreach (TCollection collection in collectionSelector (element, counter++))
  1809. yield return selector (element, collection);
  1810. }
  1811. #endregion
  1812. #region Single
  1813. static TSource Single<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate, Fallback fallback)
  1814. {
  1815. var found = false;
  1816. var item = default (TSource);
  1817. foreach (var element in source) {
  1818. if (!predicate (element))
  1819. continue;
  1820. if (found)
  1821. throw new InvalidOperationException ();
  1822. found = true;
  1823. item = element;
  1824. }
  1825. if (!found && fallback == Fallback.Throw)
  1826. throw new InvalidOperationException ();
  1827. return item;
  1828. }
  1829. public static TSource Single<TSource> (this IEnumerable<TSource> source)
  1830. {
  1831. Check.Source (source);
  1832. return source.Single (PredicateOf<TSource>.Always, Fallback.Throw);
  1833. }
  1834. public static TSource Single<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1835. {
  1836. Check.SourceAndPredicate (source, predicate);
  1837. return source.Single (predicate, Fallback.Throw);
  1838. }
  1839. #endregion
  1840. #region SingleOrDefault
  1841. public static TSource SingleOrDefault<TSource> (this IEnumerable<TSource> source)
  1842. {
  1843. Check.Source (source);
  1844. return source.Single (PredicateOf<TSource>.Always, Fallback.Default);
  1845. }
  1846. public static TSource SingleOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1847. {
  1848. Check.SourceAndPredicate (source, predicate);
  1849. return source.Single (predicate, Fallback.Default);
  1850. }
  1851. #endregion
  1852. #region Skip
  1853. public static IEnumerable<TSource> Skip<TSource> (this IEnumerable<TSource> source, int count)
  1854. {
  1855. Check.Source (source);
  1856. return CreateSkipIterator (source, count);
  1857. }
  1858. static IEnumerable<TSource> CreateSkipIterator<TSource> (IEnumerable<TSource> source, int count)
  1859. {
  1860. var enumerator = source.GetEnumerator ();
  1861. try {
  1862. while (count-- > 0)
  1863. if (!enumerator.MoveNext ())
  1864. yield break;
  1865. while (enumerator.MoveNext ())
  1866. yield return enumerator.Current;
  1867. } finally {
  1868. enumerator.Dispose ();
  1869. }
  1870. }
  1871. #endregion
  1872. #region SkipWhile
  1873. public static IEnumerable<TSource> SkipWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1874. {
  1875. Check.SourceAndPredicate (source, predicate);
  1876. return CreateSkipWhileIterator (source, predicate);
  1877. }
  1878. static IEnumerable<TSource> CreateSkipWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, bool> predicate)
  1879. {
  1880. bool yield = false;
  1881. foreach (TSource element in source) {
  1882. if (yield)
  1883. yield return element;
  1884. else
  1885. if (!predicate (element)) {
  1886. yield return element;
  1887. yield = true;
  1888. }
  1889. }
  1890. }
  1891. public static IEnumerable<TSource> SkipWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  1892. {
  1893. Check.SourceAndPredicate (source, predicate);
  1894. return CreateSkipWhileIterator (source, predicate);
  1895. }
  1896. static IEnumerable<TSource> CreateSkipWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  1897. {
  1898. int counter = 0;
  1899. bool yield = false;
  1900. foreach (TSource element in source) {
  1901. if (yield)
  1902. yield return element;
  1903. else
  1904. if (!predicate (element, counter)) {
  1905. yield return element;
  1906. yield = true;
  1907. }
  1908. counter++;
  1909. }
  1910. }
  1911. #endregion
  1912. #region Sum
  1913. public static int Sum (this IEnumerable<int> source)
  1914. {
  1915. Check.Source (source);
  1916. int total = 0;
  1917. foreach (var element in source)
  1918. total = checked (total + element);
  1919. return total;
  1920. }
  1921. public static int? Sum (this IEnumerable<int?> source)
  1922. {
  1923. Check.Source (source);
  1924. int total = 0;
  1925. foreach (var element in source) {
  1926. if (element.HasValue)
  1927. total = checked (total + element.Value);
  1928. }
  1929. return total;
  1930. }
  1931. public static int Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int> selector)
  1932. {
  1933. Check.SourceAndSelector (source, selector);
  1934. int total = 0;
  1935. foreach (var element in source)
  1936. total = checked (total + selector (element));
  1937. return total;
  1938. }
  1939. public static int? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, int?> selector)
  1940. {
  1941. Check.SourceAndSelector (source, selector);
  1942. int total = 0;
  1943. foreach (var element in source) {
  1944. var value = selector (element);
  1945. if (value.HasValue)
  1946. total = checked (total + value.Value);
  1947. }
  1948. return total;
  1949. }
  1950. public static long Sum (this IEnumerable<long> source)
  1951. {
  1952. Check.Source (source);
  1953. long total = 0;
  1954. foreach (var element in source)
  1955. total = checked (total + element);
  1956. return total;
  1957. }
  1958. public static long? Sum (this IEnumerable<long?> source)
  1959. {
  1960. Check.Source (source);
  1961. long total = 0;
  1962. foreach (var element in source) {
  1963. if (element.HasValue)
  1964. total = checked (total + element.Value);
  1965. }
  1966. return total;
  1967. }
  1968. public static long Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long> selector)
  1969. {
  1970. Check.SourceAndSelector (source, selector);
  1971. long total = 0;
  1972. foreach (var element in source)
  1973. total = checked (total + selector (element));
  1974. return total;
  1975. }
  1976. public static long? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, long?> selector)
  1977. {
  1978. Check.SourceAndSelector (source, selector);
  1979. long total = 0;
  1980. foreach (var element in source) {
  1981. var value = selector (element);
  1982. if (value.HasValue)
  1983. total = checked (total + value.Value);
  1984. }
  1985. return total;
  1986. }
  1987. public static double Sum (this IEnumerable<double> source)
  1988. {
  1989. Check.Source (source);
  1990. double total = 0;
  1991. foreach (var element in source)
  1992. total = checked (total + element);
  1993. return total;
  1994. }
  1995. public static double? Sum (this IEnumerable<double?> source)
  1996. {
  1997. Check.Source (source);
  1998. double total = 0;
  1999. foreach (var element in source) {
  2000. if (element.HasValue)
  2001. total = checked (total + element.Value);
  2002. }
  2003. return total;
  2004. }
  2005. public static double Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double> selector)
  2006. {
  2007. Check.SourceAndSelector (source, selector);
  2008. double total = 0;
  2009. foreach (var element in source)
  2010. total = checked (total + selector (element));
  2011. return total;
  2012. }
  2013. public static double? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, double?> selector)
  2014. {
  2015. Check.SourceAndSelector (source, selector);
  2016. double total = 0;
  2017. foreach (var element in source) {
  2018. var value = selector (element);
  2019. if (value.HasValue)
  2020. total = checked (total + value.Value);
  2021. }
  2022. return total;
  2023. }
  2024. public static float Sum (this IEnumerable<float> source)
  2025. {
  2026. Check.Source (source);
  2027. float total = 0;
  2028. foreach (var element in source)
  2029. total = checked (total + element);
  2030. return total;
  2031. }
  2032. public static float? Sum (this IEnumerable<float?> source)
  2033. {
  2034. Check.Source (source);
  2035. float total = 0;
  2036. foreach (var element in source) {
  2037. if (element.HasValue)
  2038. total = checked (total + element.Value);
  2039. }
  2040. return total;
  2041. }
  2042. public static float Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float> selector)
  2043. {
  2044. Check.SourceAndSelector (source, selector);
  2045. float total = 0;
  2046. foreach (var element in source)
  2047. total = checked (total + selector (element));
  2048. return total;
  2049. }
  2050. public static float? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, float?> selector)
  2051. {
  2052. Check.SourceAndSelector (source, selector);
  2053. float total = 0;
  2054. foreach (var element in source) {
  2055. var value = selector (element);
  2056. if (value.HasValue)
  2057. total = checked (total + value.Value);
  2058. }
  2059. return total;
  2060. }
  2061. public static decimal Sum (this IEnumerable<decimal> source)
  2062. {
  2063. Check.Source (source);
  2064. decimal total = 0;
  2065. foreach (var element in source)
  2066. total = checked (total + element);
  2067. return total;
  2068. }
  2069. public static decimal? Sum (this IEnumerable<decimal?> source)
  2070. {
  2071. Check.Source (source);
  2072. decimal total = 0;
  2073. foreach (var element in source) {
  2074. if (element.HasValue)
  2075. total = checked (total + element.Value);
  2076. }
  2077. return total;
  2078. }
  2079. public static decimal Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal> selector)
  2080. {
  2081. Check.SourceAndSelector (source, selector);
  2082. decimal total = 0;
  2083. foreach (var element in source)
  2084. total = checked (total + selector (element));
  2085. return total;
  2086. }
  2087. public static decimal? Sum<TSource> (this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
  2088. {
  2089. Check.SourceAndSelector (source, selector);
  2090. decimal total = 0;
  2091. foreach (var element in source) {
  2092. var value = selector (element);
  2093. if (value.HasValue)
  2094. total = checked (total + value.Value);
  2095. }
  2096. return total;
  2097. }
  2098. #endregion
  2099. #region Take
  2100. public static IEnumerable<TSource> Take<TSource> (this IEnumerable<TSource> source, int count)
  2101. {
  2102. Check.Source (source);
  2103. return CreateTakeIterator (source, count);
  2104. }
  2105. static IEnumerable<TSource> CreateTakeIterator<TSource> (IEnumerable<TSource> source, int count)
  2106. {
  2107. if (count <= 0)
  2108. yield break;
  2109. int counter = 0;
  2110. foreach (TSource element in source) {
  2111. yield return element;
  2112. if (++counter == count)
  2113. yield break;
  2114. }
  2115. }
  2116. #endregion
  2117. #region TakeWhile
  2118. public static IEnumerable<TSource> TakeWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2119. {
  2120. Check.SourceAndPredicate (source, predicate);
  2121. return CreateTakeWhileIterator (source, predicate);
  2122. }
  2123. static IEnumerable<TSource> CreateTakeWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2124. {
  2125. foreach (var element in source) {
  2126. if (!predicate (element))
  2127. yield break;
  2128. yield return element;
  2129. }
  2130. }
  2131. public static IEnumerable<TSource> TakeWhile<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2132. {
  2133. Check.SourceAndPredicate (source, predicate);
  2134. return CreateTakeWhileIterator (source, predicate);
  2135. }
  2136. static IEnumerable<TSource> CreateTakeWhileIterator<TSource> (IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2137. {
  2138. int counter = 0;
  2139. foreach (var element in source) {
  2140. if (!predicate (element, counter))
  2141. yield break;
  2142. yield return element;
  2143. counter++;
  2144. }
  2145. }
  2146. #endregion
  2147. #region ThenBy
  2148. public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey> (this IOrderedEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  2149. {
  2150. return ThenBy<TSource, TKey> (source, keySelector, null);
  2151. }
  2152. public static IOrderedEnumerable<TSource> ThenBy<TSource, TKey> (this IOrderedEnumerable<TSource> source,
  2153. Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  2154. {
  2155. Check.SourceAndKeySelector (source, keySelector);
  2156. return source.CreateOrderedEnumerable (keySelector, comparer, false);
  2157. }
  2158. #endregion
  2159. #region ThenByDescending
  2160. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey> (this IOrderedEnumerable<TSource> source,
  2161. Func<TSource, TKey> keySelector)
  2162. {
  2163. return ThenByDescending<TSource, TKey> (source, keySelector, null);
  2164. }
  2165. public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey> (this IOrderedEnumerable<TSource> source,
  2166. Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
  2167. {
  2168. Check.SourceAndKeySelector (source, keySelector);
  2169. return source.CreateOrderedEnumerable (keySelector, comparer, true);
  2170. }
  2171. #endregion
  2172. #region ToArray
  2173. public static TSource [] ToArray<TSource> (this IEnumerable<TSource> source)
  2174. {
  2175. Check.Source (source);
  2176. var collection = source as ICollection<TSource>;
  2177. if (collection != null) {
  2178. var array = new TSource [collection.Count];
  2179. collection.CopyTo (array, 0);
  2180. return array;
  2181. }
  2182. return new List<TSource> (source).ToArray ();
  2183. }
  2184. #endregion
  2185. #region ToDictionary
  2186. public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2187. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  2188. {
  2189. return ToDictionary<TSource, TKey, TElement> (source, keySelector, elementSelector, null);
  2190. }
  2191. public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2192. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  2193. {
  2194. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  2195. if (comparer == null)
  2196. comparer = EqualityComparer<TKey>.Default;
  2197. var dict = new Dictionary<TKey, TElement> (comparer);
  2198. foreach (var e in source)
  2199. dict.Add (keySelector (e), elementSelector (e));
  2200. return dict;
  2201. }
  2202. public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this IEnumerable<TSource> source,
  2203. Func<TSource, TKey> keySelector)
  2204. {
  2205. return ToDictionary (source, keySelector, null);
  2206. }
  2207. public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this IEnumerable<TSource> source,
  2208. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  2209. {
  2210. return ToDictionary<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, comparer);
  2211. }
  2212. #endregion
  2213. #region ToList
  2214. public static List<TSource> ToList<TSource> (this IEnumerable<TSource> source)
  2215. {
  2216. Check.Source (source);
  2217. return new List<TSource> (source);
  2218. }
  2219. #endregion
  2220. #region ToLookup
  2221. public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
  2222. {
  2223. return ToLookup<TSource, TKey, TSource> (source, keySelector, Function<TSource>.Identity, null);
  2224. }
  2225. public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this IEnumerable<TSource> source,
  2226. Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
  2227. {
  2228. return ToLookup<TSource, TKey, TSource> (source, keySelector, element => element, comparer);
  2229. }
  2230. public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2231. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
  2232. {
  2233. return ToLookup<TSource, TKey, TElement> (source, keySelector, elementSelector, null);
  2234. }
  2235. public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this IEnumerable<TSource> source,
  2236. Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  2237. {
  2238. Check.SourceAndKeyElementSelectors (source, keySelector, elementSelector);
  2239. List<TElement> nullKeyElements = null;
  2240. var dictionary = new Dictionary<TKey, List<TElement>> (comparer ?? EqualityComparer<TKey>.Default);
  2241. foreach (var element in source) {
  2242. var key = keySelector (element);
  2243. List<TElement> list;
  2244. if (key == null) {
  2245. if (nullKeyElements == null)
  2246. nullKeyElements = new List<TElement> ();
  2247. list = nullKeyElements;
  2248. } else if (!dictionary.TryGetValue (key, out list)) {
  2249. list = new List<TElement> ();
  2250. dictionary.Add (key, list);
  2251. }
  2252. list.Add (elementSelector (element));
  2253. }
  2254. return new Lookup<TKey, TElement> (dictionary, nullKeyElements);
  2255. }
  2256. #endregion
  2257. #region SequenceEqual
  2258. public static bool SequenceEqual<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  2259. {
  2260. return first.SequenceEqual (second, null);
  2261. }
  2262. public static bool SequenceEqual<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  2263. {
  2264. Check.FirstAndSecond (first, second);
  2265. if (comparer == null)
  2266. comparer = EqualityComparer<TSource>.Default;
  2267. using (IEnumerator<TSource> first_enumerator = first.GetEnumerator (),
  2268. second_enumerator = second.GetEnumerator ()) {
  2269. while (first_enumerator.MoveNext ()) {
  2270. if (!second_enumerator.MoveNext ())
  2271. return false;
  2272. if (!comparer.Equals (first_enumerator.Current, second_enumerator.Current))
  2273. return false;
  2274. }
  2275. return !second_enumerator.MoveNext ();
  2276. }
  2277. }
  2278. #endregion
  2279. #region Union
  2280. public static IEnumerable<TSource> Union<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  2281. {
  2282. Check.FirstAndSecond (first, second);
  2283. return first.Union (second, null);
  2284. }
  2285. public static IEnumerable<TSource> Union<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  2286. {
  2287. Check.FirstAndSecond (first, second);
  2288. if (comparer == null)
  2289. comparer = EqualityComparer<TSource>.Default;
  2290. return CreateUnionIterator (first, second, comparer);
  2291. }
  2292. static IEnumerable<TSource> CreateUnionIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  2293. {
  2294. var items = new HashSet<TSource> (comparer);
  2295. foreach (var element in first) {
  2296. if (! items.Contains (element)) {
  2297. items.Add (element);
  2298. yield return element;
  2299. }
  2300. }
  2301. foreach (var element in second) {
  2302. if (! items.Contains (element)) {
  2303. items.Add (element);
  2304. yield return element;
  2305. }
  2306. }
  2307. }
  2308. #endregion
  2309. #if NET_4_0 || MOONLIGHT
  2310. #region Zip
  2311. public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult> (this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
  2312. {
  2313. Check.FirstAndSecond (first, second);
  2314. if (resultSelector == null)
  2315. throw new ArgumentNullException ("resultSelector");
  2316. return CreateZipIterator (first, second, resultSelector);
  2317. }
  2318. static IEnumerable<TResult> CreateZipIterator<TFirst, TSecond, TResult> (IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> selector)
  2319. {
  2320. using (IEnumerator<TFirst> first_enumerator = first.GetEnumerator ()) {
  2321. using (IEnumerator<TSecond> second_enumerator = second.GetEnumerator ()) {
  2322. while (first_enumerator.MoveNext () && second_enumerator.MoveNext ()) {
  2323. yield return selector (first_enumerator.Current, second_enumerator.Current);
  2324. }
  2325. }
  2326. }
  2327. }
  2328. #endregion
  2329. #endif
  2330. #region Where
  2331. public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2332. {
  2333. Check.SourceAndPredicate (source, predicate);
  2334. return CreateWhereIterator (source, predicate);
  2335. }
  2336. static IEnumerable<TSource> CreateWhereIterator<TSource> (IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2337. {
  2338. foreach (TSource element in source)
  2339. if (predicate (element))
  2340. yield return element;
  2341. }
  2342. public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2343. {
  2344. Check.SourceAndPredicate (source, predicate);
  2345. return CreateWhereIterator (source, predicate);
  2346. }
  2347. static IEnumerable<TSource> CreateWhereIterator<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2348. {
  2349. int counter = 0;
  2350. foreach (TSource element in source) {
  2351. if (predicate (element, counter))
  2352. yield return element;
  2353. counter++;
  2354. }
  2355. }
  2356. #endregion
  2357. internal static ReadOnlyCollection<TSource> ToReadOnlyCollection<TSource> (this IEnumerable<TSource> source)
  2358. {
  2359. if (source == null)
  2360. return ReadOnlyCollectionOf<TSource>.Empty;
  2361. var ro = source as ReadOnlyCollection<TSource>;
  2362. if (ro != null)
  2363. return ro;
  2364. return new ReadOnlyCollection<TSource> (source.ToArray<TSource> ());
  2365. }
  2366. }
  2367. }