Enumerable.cs 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170
  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. return source.ElementAt (index, Fallback.Throw);
  570. }
  571. #endregion
  572. #region ElementAtOrDefault
  573. public static TSource ElementAtOrDefault<TSource> (this IEnumerable<TSource> source, int index)
  574. {
  575. Check.Source (source);
  576. if (index < 0)
  577. return default (TSource);
  578. var list = source as IList<TSource>;
  579. if (list != null)
  580. return index < list.Count ? list [index] : default (TSource);
  581. return source.ElementAt (index, Fallback.Default);
  582. }
  583. #endregion
  584. #region Empty
  585. public static IEnumerable<TResult> Empty<TResult> ()
  586. {
  587. return EmptyOf<TResult>.Instance;
  588. }
  589. #endregion
  590. #region Except
  591. public static IEnumerable<TSource> Except<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second)
  592. {
  593. return Except (first, second, null);
  594. }
  595. public static IEnumerable<TSource> Except<TSource> (this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  596. {
  597. Check.FirstAndSecond (first, second);
  598. if (comparer == null)
  599. comparer = EqualityComparer<TSource>.Default;
  600. return CreateExceptIterator (first, second, comparer);
  601. }
  602. static IEnumerable<TSource> CreateExceptIterator<TSource> (IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
  603. {
  604. var items = new HashSet<TSource> (second, comparer);
  605. foreach (var element in first) {
  606. if (items.Add (element))
  607. yield return element;
  608. }
  609. }
  610. #endregion
  611. #region First
  612. static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate, Fallback fallback)
  613. {
  614. foreach (var element in source)
  615. if (predicate (element))
  616. return element;
  617. if (fallback == Fallback.Throw)
  618. throw NoMatchingElement ();
  619. return default (TSource);
  620. }
  621. public static TSource First<TSource> (this IEnumerable<TSource> source)
  622. {
  623. Check.Source (source);
  624. var list = source as IList<TSource>;
  625. if (list != null) {
  626. if (list.Count != 0)
  627. return list [0];
  628. } else {
  629. using (var enumerator = source.GetEnumerator ()) {
  630. if (enumerator.MoveNext ())
  631. return enumerator.Current;
  632. }
  633. }
  634. throw EmptySequence ();
  635. }
  636. public static TSource First<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  637. {
  638. Check.SourceAndPredicate (source, predicate);
  639. return source.First (predicate, Fallback.Throw);
  640. }
  641. #endregion
  642. #region FirstOrDefault
  643. public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source)
  644. {
  645. Check.Source (source);
  646. #if !FULL_AOT_RUNTIME
  647. return source.First (PredicateOf<TSource>.Always, Fallback.Default);
  648. #else
  649. // inline the code to reduce dependency o generic causing AOT errors on device (e.g. bug #3285)
  650. foreach (var element in source)
  651. return element;
  652. return default (TSource);
  653. #endif
  654. }
  655. public static TSource FirstOrDefault<TSource> (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
  656. {
  657. Check.SourceAndPredicate (source, predicate);
  658. return source.First (predicate, Fallback.Default);
  659. }
  660. #endregion
  661. #region GroupBy
  662. private static List<T> ContainsGroup<K, T> (
  663. Dictionary<K, List<T>> items, K key, IEqualityComparer<K> comparer)
  664. {
  665. IEqualityComparer<K> comparerInUse = (comparer ?? EqualityComparer<K>.Default);
  666. foreach (KeyValuePair<K, List<T>> value in items) {
  667. if (comparerInUse.Equals (value.Key, key))
  668. return value.Value;
  669. }
  670. return null;
  671. }
  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>> ();
  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 = ContainsGroup<TKey, TSource> (groups, key, comparer);
  700. if (group == null) {
  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>> ();
  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 = ContainsGroup<TKey, TElement> (groups, key, comparer);
  751. if (group == null) {
  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 (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 (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 || MOBILE
  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. return CreateWhereIterator (source, predicate);
  2417. }
  2418. static IEnumerable<TSource> CreateWhereIterator<TSource> (IEnumerable<TSource> source, Func<TSource, bool> predicate)
  2419. {
  2420. foreach (TSource element in source)
  2421. if (predicate (element))
  2422. yield return element;
  2423. }
  2424. public static IEnumerable<TSource> Where<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2425. {
  2426. Check.SourceAndPredicate (source, predicate);
  2427. return CreateWhereIterator (source, predicate);
  2428. }
  2429. static IEnumerable<TSource> CreateWhereIterator<TSource> (this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  2430. {
  2431. int counter = 0;
  2432. foreach (TSource element in source) {
  2433. if (predicate (element, counter))
  2434. yield return element;
  2435. counter++;
  2436. }
  2437. }
  2438. #endregion
  2439. internal static ReadOnlyCollection<TSource> ToReadOnlyCollection<TSource> (this IEnumerable<TSource> source)
  2440. {
  2441. if (source == null)
  2442. return ReadOnlyCollectionOf<TSource>.Empty;
  2443. var ro = source as ReadOnlyCollection<TSource>;
  2444. if (ro != null)
  2445. return ro;
  2446. return new ReadOnlyCollection<TSource> (source.ToArray<TSource> ());
  2447. }
  2448. #region Exception helpers
  2449. static Exception EmptySequence ()
  2450. {
  2451. return new InvalidOperationException (Locale.GetText ("Sequence contains no elements"));
  2452. }
  2453. static Exception NoMatchingElement ()
  2454. {
  2455. return new InvalidOperationException (Locale.GetText ("Sequence contains no matching element"));
  2456. }
  2457. static Exception MoreThanOneElement ()
  2458. {
  2459. return new InvalidOperationException (Locale.GetText ("Sequence contains more than one element"));
  2460. }
  2461. static Exception MoreThanOneMatchingElement ()
  2462. {
  2463. return new InvalidOperationException (Locale.GetText ("Sequence contains more than one matching element"));
  2464. }
  2465. #endregion
  2466. }
  2467. }