Enumerable.cs 78 KB

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