Enumerable.cs 76 KB

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