Enumerable.cs 78 KB

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