Enumerable.cs 78 KB

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