ParallelEnumerable.cs 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. //
  2. // ParallelEnumerable.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2010 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Threading;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Collections.Concurrent;
  31. using System.Linq.Parallel;
  32. using System.Linq.Parallel.QueryNodes;
  33. namespace System.Linq
  34. {
  35. public static class ParallelEnumerable
  36. {
  37. #region Range & Repeat
  38. public static ParallelQuery<int> Range (int start, int count)
  39. {
  40. if (int.MaxValue - start < count - 1)
  41. throw new ArgumentOutOfRangeException ("count", "start + count - 1 is larger than Int32.MaxValue");
  42. if (count < 0)
  43. throw new ArgumentOutOfRangeException ("count", "count is less than 0");
  44. return (new RangeList (start, count)).AsParallel ();
  45. }
  46. public static ParallelQuery<TResult> Repeat<TResult> (TResult element, int count)
  47. {
  48. if (count < 0)
  49. throw new ArgumentOutOfRangeException ("count", "count is less than 0");
  50. return (new RepeatList<TResult> (element, count)).AsParallel ();
  51. }
  52. #endregion
  53. #region Empty
  54. public static ParallelQuery<TResult> Empty<TResult> ()
  55. {
  56. return Repeat<TResult> (default (TResult), 0);
  57. }
  58. #endregion
  59. #region AsParallel
  60. public static ParallelQuery<TSource> AsParallel<TSource> (this IEnumerable<TSource> source)
  61. {
  62. if (source == null)
  63. throw new ArgumentNullException ("source");
  64. /* Someone might be trying to use AsParallel a bit too much, if the query was in fact
  65. * already a ParallelQuery, just cast it
  66. */
  67. ParallelQuery<TSource> query = source as ParallelQuery<TSource>;
  68. return query ?? new ParallelQuery<TSource> (new QueryStartNode<TSource> (source));
  69. }
  70. public static ParallelQuery<TSource> AsParallel<TSource> (this Partitioner<TSource> source)
  71. {
  72. if (source == null)
  73. throw new ArgumentNullException ("source");
  74. /* Someone might be trying to use AsParallel a bit too much, if the query was in fact
  75. * already a ParallelQuery, just cast it
  76. */
  77. ParallelQuery<TSource> query = source as ParallelQuery<TSource>;
  78. return query ?? new ParallelQuery<TSource> (new QueryStartNode<TSource> (source));
  79. }
  80. public static ParallelQuery AsParallel (this IEnumerable source)
  81. {
  82. if (source == null)
  83. throw new ArgumentNullException ("source");
  84. /* Someone might be trying to use AsParallel a bit too much, if the query was in fact
  85. * already a ParallelQuery, just cast it
  86. */
  87. ParallelQuery query = source as ParallelQuery;
  88. return query ?? new ParallelQuery<object> (new QueryStartNode<object> (source.Cast<object> ()));
  89. }
  90. public static IEnumerable<TSource> AsEnumerable<TSource> (this ParallelQuery<TSource> source)
  91. {
  92. if (source == null)
  93. throw new ArgumentNullException ("source");
  94. return source.AsSequential ();
  95. }
  96. public static IEnumerable<TSource> AsSequential<TSource> (this ParallelQuery<TSource> source)
  97. {
  98. if (source == null)
  99. throw new ArgumentNullException ("source");
  100. return source.Node.GetSequential ();
  101. }
  102. #endregion
  103. #region AsOrdered / AsUnordered
  104. public static ParallelQuery<TSource> AsOrdered<TSource> (this ParallelQuery<TSource> source)
  105. {
  106. if (source == null)
  107. throw new ArgumentNullException ("source");
  108. return new ParallelQuery<TSource> (new QueryAsOrderedNode<TSource> (source.Node));
  109. }
  110. public static ParallelQuery<TSource> AsUnordered<TSource> (this ParallelQuery<TSource> source)
  111. {
  112. if (source == null)
  113. throw new ArgumentNullException ("source");
  114. return new ParallelQuery<TSource> (new QueryAsUnorderedNode<TSource> (source.Node));
  115. }
  116. public static ParallelQuery AsOrdered (this ParallelQuery source)
  117. {
  118. if (source == null)
  119. throw new ArgumentNullException ("source");
  120. return source.TypedQuery.AsOrdered ();
  121. }
  122. #endregion
  123. #region With*
  124. public static ParallelQuery<TSource> WithExecutionMode<TSource> (this ParallelQuery<TSource> source,
  125. ParallelExecutionMode executionMode)
  126. {
  127. if (source == null)
  128. throw new ArgumentNullException ("source");
  129. return new ParallelQuery<TSource> (new ParallelExecutionModeNode<TSource> (executionMode, source.Node));
  130. }
  131. public static ParallelQuery<TSource> WithCancellation<TSource> (this ParallelQuery<TSource> source,
  132. CancellationToken cancellationToken)
  133. {
  134. if (source == null)
  135. throw new ArgumentNullException ("source");
  136. return new ParallelQuery<TSource> (new CancellationTokenNode<TSource> (cancellationToken, source.Node));
  137. }
  138. public static ParallelQuery<TSource> WithMergeOptions<TSource> (this ParallelQuery<TSource> source,
  139. ParallelMergeOptions mergeOptions)
  140. {
  141. if (source == null)
  142. throw new ArgumentNullException ("source");
  143. return new ParallelQuery<TSource> (new ParallelMergeOptionsNode<TSource> (mergeOptions, source.Node));
  144. }
  145. public static ParallelQuery<TSource> WithDegreeOfParallelism<TSource> (this ParallelQuery<TSource> source,
  146. int degreeOfParallelism)
  147. {
  148. if (degreeOfParallelism < 1 || degreeOfParallelism > 63)
  149. throw new ArgumentException ("degreeOfParallelism is less than 1 or greater than 63", "degreeOfParallelism");
  150. if (source == null)
  151. throw new ArgumentNullException ("source");
  152. return new ParallelQuery<TSource> (new DegreeOfParallelismNode<TSource> (degreeOfParallelism, source.Node));
  153. }
  154. internal static ParallelQuery<TSource> WithImplementerToken<TSource> (this ParallelQuery<TSource> source,
  155. CancellationTokenSource token)
  156. {
  157. return new ParallelQuery<TSource> (new ImplementerTokenNode<TSource> (token, source.Node));
  158. }
  159. #endregion
  160. #region Select
  161. public static ParallelQuery<TResult> Select<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, TResult> selector)
  162. {
  163. if (source == null)
  164. throw new ArgumentNullException ("source");
  165. if (selector == null)
  166. throw new ArgumentNullException ("selector");
  167. return new ParallelQuery<TResult> (new QuerySelectNode<TResult, TSource> (source.Node, selector));
  168. }
  169. public static ParallelQuery<TResult> Select<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, int, TResult> selector)
  170. {
  171. if (source == null)
  172. throw new ArgumentNullException ("source");
  173. if (selector == null)
  174. throw new ArgumentNullException ("selector");
  175. return new ParallelQuery<TResult> (new QuerySelectNode<TResult, TSource> (source.Node, selector));
  176. }
  177. #endregion
  178. #region SelectMany
  179. public static ParallelQuery<TResult> SelectMany<TSource, TResult> (this ParallelQuery<TSource> source,
  180. Func<TSource, IEnumerable<TResult>> selector)
  181. {
  182. return source.SelectMany (selector, (s, e) => e);
  183. }
  184. public static ParallelQuery<TResult> SelectMany<TSource, TResult> (this ParallelQuery<TSource> source,
  185. Func<TSource, int, IEnumerable<TResult>> selector)
  186. {
  187. return source.SelectMany (selector, (s, e) => e);
  188. }
  189. public static ParallelQuery<TResult> SelectMany<TSource, TCollection, TResult> (this ParallelQuery<TSource> source,
  190. Func<TSource, IEnumerable<TCollection>> collectionSelector,
  191. Func<TSource, TCollection, TResult> resultSelector)
  192. {
  193. return new ParallelQuery<TResult> (new QuerySelectManyNode<TSource, TCollection, TResult> (source.Node,
  194. collectionSelector,
  195. resultSelector));
  196. }
  197. public static ParallelQuery<TResult> SelectMany<TSource, TCollection, TResult> (this ParallelQuery<TSource> source,
  198. Func<TSource, int, IEnumerable<TCollection>> collectionSelector,
  199. Func<TSource, TCollection, TResult> resultSelector)
  200. {
  201. return new ParallelQuery<TResult> (new QuerySelectManyNode<TSource, TCollection, TResult> (source.Node,
  202. collectionSelector,
  203. resultSelector));
  204. }
  205. #endregion
  206. #region Where
  207. public static ParallelQuery<TSource> Where<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  208. {
  209. if (source == null)
  210. throw new ArgumentNullException ("source");
  211. if (predicate == null)
  212. throw new ArgumentNullException ("predicate");
  213. return new ParallelQuery<TSource> (new QueryWhereNode<TSource> (source.Node, predicate));
  214. }
  215. public static ParallelQuery<TSource> Where<TSource> (this ParallelQuery<TSource> source, Func<TSource, int, bool> predicate)
  216. {
  217. if (source == null)
  218. throw new ArgumentNullException ("source");
  219. if (predicate == null)
  220. throw new ArgumentNullException ("predicate");
  221. return new ParallelQuery<TSource> (new QueryWhereNode<TSource> (source.Node, predicate));
  222. }
  223. #endregion
  224. #region Aggregate
  225. public static TSource Aggregate<TSource> (this ParallelQuery<TSource> source, Func<TSource, TSource, TSource> func)
  226. {
  227. if (source == null)
  228. throw new ArgumentNullException ("source");
  229. if (func == null)
  230. throw new ArgumentNullException ("func");
  231. return source.Aggregate<TSource, TSource, TSource> ((Func<TSource>)null,
  232. func,
  233. func,
  234. (e) => e);
  235. }
  236. public static TAccumulate Aggregate<TSource, TAccumulate> (this ParallelQuery<TSource> source,
  237. TAccumulate seed,
  238. Func<TAccumulate, TSource, TAccumulate> func)
  239. {
  240. if (source == null)
  241. throw new ArgumentNullException ("source");
  242. if (func == null)
  243. throw new ArgumentNullException ("func");
  244. return source.Aggregate (seed, func, (e) => e);
  245. }
  246. public static TResult Aggregate<TSource, TAccumulate, TResult> (this ParallelQuery<TSource> source,
  247. TAccumulate seed,
  248. Func<TAccumulate, TSource, TAccumulate> func,
  249. Func<TAccumulate, TResult> resultSelector)
  250. {
  251. if (source == null)
  252. throw new ArgumentNullException ("source");
  253. if (func == null)
  254. throw new ArgumentNullException ("func");
  255. if (resultSelector == null)
  256. throw new ArgumentNullException ("resultSelector");
  257. TAccumulate accumulator = seed;
  258. foreach (TSource value in source)
  259. accumulator = func (accumulator, value);
  260. return resultSelector (accumulator);
  261. }
  262. public static TResult Aggregate<TSource, TAccumulate, TResult> (this ParallelQuery<TSource> source,
  263. TAccumulate seed,
  264. Func<TAccumulate, TSource, TAccumulate> updateAccumulatorFunc,
  265. Func<TAccumulate, TAccumulate, TAccumulate> combineAccumulatorsFunc,
  266. Func<TAccumulate, TResult> resultSelector)
  267. {
  268. if (source == null)
  269. throw new ArgumentNullException ("source");
  270. if (updateAccumulatorFunc == null)
  271. throw new ArgumentNullException ("updateAccumulatorFunc");
  272. if (combineAccumulatorsFunc == null)
  273. throw new ArgumentNullException ("combineAccumulatorsFunc");
  274. if (resultSelector == null)
  275. throw new ArgumentNullException ("resultSelector");
  276. return source.Aggregate (() => seed, updateAccumulatorFunc, combineAccumulatorsFunc, resultSelector);
  277. }
  278. public static TResult Aggregate<TSource, TAccumulate, TResult> (this ParallelQuery<TSource> source,
  279. Func<TAccumulate> seedFactory,
  280. Func<TAccumulate, TSource, TAccumulate> updateAccumulatorFunc,
  281. Func<TAccumulate, TAccumulate, TAccumulate> combineAccumulatorsFunc,
  282. Func<TAccumulate, TResult> resultSelector)
  283. {
  284. if (source == null)
  285. throw new ArgumentNullException ("source");
  286. if (seedFactory == null)
  287. throw new ArgumentNullException ("seedFactory");
  288. if (updateAccumulatorFunc == null)
  289. throw new ArgumentNullException ("updateAccumulatorFunc");
  290. if (combineAccumulatorsFunc == null)
  291. throw new ArgumentNullException ("combineAccumulatorsFunc");
  292. if (resultSelector == null)
  293. throw new ArgumentNullException ("resultSelector");
  294. TAccumulate accumulator = default (TAccumulate);
  295. ParallelExecuter.ProcessAndAggregate<TSource, TAccumulate> (source.Node, seedFactory, updateAccumulatorFunc, (list) => {
  296. accumulator = list [0];
  297. for (int i = 1; i < list.Count; i++)
  298. accumulator = combineAccumulatorsFunc (accumulator, list[i]);
  299. });
  300. return resultSelector (accumulator);;
  301. }
  302. #endregion
  303. #region ForAll
  304. public static void ForAll<TSource> (this ParallelQuery<TSource> source, Action<TSource> action)
  305. {
  306. if (source == null)
  307. throw new ArgumentNullException ("source");
  308. if (action == null)
  309. throw new ArgumentNullException ("action");
  310. ParallelExecuter.ProcessAndBlock (source.Node, (e, c) => action (e));
  311. }
  312. #endregion
  313. #region OrderBy
  314. public static OrderedParallelQuery<TSource> OrderByDescending<TSource, TKey> (this ParallelQuery<TSource> source,
  315. Func<TSource, TKey> keySelector,
  316. IComparer<TKey> comparer)
  317. {
  318. if (source == null)
  319. throw new ArgumentNullException ("source");
  320. if (keySelector == null)
  321. throw new ArgumentNullException ("keySelector");
  322. if (comparer == null)
  323. comparer = Comparer<TKey>.Default;
  324. Comparison<TSource> comparison = (e1, e2) => -comparer.Compare (keySelector (e1), keySelector (e2));
  325. return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
  326. }
  327. public static OrderedParallelQuery<TSource> OrderByDescending<TSource, TKey> (this ParallelQuery<TSource> source,
  328. Func<TSource, TKey> keySelector)
  329. {
  330. return OrderByDescending (source, keySelector, Comparer<TKey>.Default);
  331. }
  332. public static OrderedParallelQuery<TSource> OrderBy<TSource, TKey> (this ParallelQuery<TSource> source,
  333. Func<TSource, TKey> keySelector)
  334. {
  335. return OrderBy (source, keySelector, Comparer<TKey>.Default);
  336. }
  337. public static OrderedParallelQuery<TSource> OrderBy<TSource, TKey> (this ParallelQuery<TSource> source,
  338. Func<TSource, TKey> keySelector,
  339. IComparer<TKey> comparer)
  340. {
  341. if (source == null)
  342. throw new ArgumentNullException ("source");
  343. if (keySelector == null)
  344. throw new ArgumentNullException ("keySelector");
  345. if (comparer == null)
  346. comparer = Comparer<TKey>.Default;
  347. Comparison<TSource> comparison = (e1, e2) => comparer.Compare (keySelector (e1), keySelector (e2));
  348. return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
  349. }
  350. #endregion
  351. #region ThenBy
  352. public static OrderedParallelQuery<TSource> ThenBy<TSource, TKey> (this OrderedParallelQuery<TSource> source,
  353. Func<TSource, TKey> keySelector)
  354. {
  355. return ThenBy (source, keySelector, Comparer<TKey>.Default);
  356. }
  357. public static OrderedParallelQuery<TSource> ThenBy<TSource, TKey> (this OrderedParallelQuery<TSource> source,
  358. Func<TSource, TKey> keySelector,
  359. IComparer<TKey> comparer)
  360. {
  361. if (source == null)
  362. throw new ArgumentNullException ("source");
  363. if (keySelector == null)
  364. throw new ArgumentNullException ("keySelector");
  365. if (comparer == null)
  366. comparer = Comparer<TKey>.Default;
  367. Comparison<TSource> comparison = (e1, e2) => comparer.Compare (keySelector (e1), keySelector (e2));
  368. return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
  369. }
  370. public static OrderedParallelQuery<TSource> ThenByDescending<TSource, TKey> (this OrderedParallelQuery<TSource> source,
  371. Func<TSource, TKey> keySelector)
  372. {
  373. return ThenByDescending (source, keySelector, Comparer<TKey>.Default);
  374. }
  375. public static OrderedParallelQuery<TSource> ThenByDescending<TSource, TKey> (this OrderedParallelQuery<TSource> source,
  376. Func<TSource, TKey> keySelector,
  377. IComparer<TKey> comparer)
  378. {
  379. if (source == null)
  380. throw new ArgumentNullException ("source");
  381. if (keySelector == null)
  382. throw new ArgumentNullException ("keySelector");
  383. if (comparer == null)
  384. comparer = Comparer<TKey>.Default;
  385. Comparison<TSource> comparison = (e1, e2) => -comparer.Compare (keySelector (e1), keySelector (e2));
  386. return new OrderedParallelQuery<TSource> (new QueryOrderByNode<TSource> (source.Node, comparison));
  387. }
  388. #endregion
  389. #region All
  390. public static bool All<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  391. {
  392. if (source == null)
  393. throw new ArgumentNullException ("source");
  394. if (predicate == null)
  395. throw new ArgumentNullException ("predicate");
  396. CancellationTokenSource src = new CancellationTokenSource ();
  397. ParallelQuery<TSource> innerQuery = source.WithImplementerToken (src);
  398. bool result = true;
  399. try {
  400. innerQuery.ForAll ((e) => {
  401. if (!predicate (e)) {
  402. result = false;
  403. src.Cancel ();
  404. }
  405. });
  406. } catch (OperationCanceledException e) {
  407. if (e.CancellationToken != src.Token)
  408. throw e;
  409. }
  410. return result;
  411. }
  412. #endregion
  413. #region Any
  414. public static bool Any<TSource> (this ParallelQuery<TSource> source)
  415. {
  416. return Any<TSource> (source, (_) => true);
  417. }
  418. public static bool Any<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  419. {
  420. if (source == null)
  421. throw new ArgumentNullException ("source");
  422. if (predicate == null)
  423. throw new ArgumentNullException ("predicate");
  424. return !source.All ((e) => !predicate (e));
  425. }
  426. #endregion
  427. #region Contains
  428. public static bool Contains<TSource> (this ParallelQuery<TSource> source, TSource value)
  429. {
  430. return Contains<TSource> (source, value, EqualityComparer<TSource>.Default);
  431. }
  432. public static bool Contains<TSource> (this ParallelQuery<TSource> source, TSource value, IEqualityComparer<TSource> comparer)
  433. {
  434. if (source == null)
  435. throw new ArgumentNullException ("source");
  436. if (comparer == null)
  437. comparer = EqualityComparer<TSource>.Default;
  438. return Any<TSource> (source, (e) => comparer.Equals (value, e));
  439. }
  440. #endregion
  441. #region SequenceEqual
  442. public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first,
  443. ParallelQuery<TSource> second)
  444. {
  445. if (first == null)
  446. throw new ArgumentNullException ("first");
  447. if (second == null)
  448. throw new ArgumentNullException ("second");
  449. return first.SequenceEqual (second, EqualityComparer<TSource>.Default);
  450. }
  451. public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first,
  452. ParallelQuery<TSource> second,
  453. IEqualityComparer<TSource> comparer)
  454. {
  455. if (first == null)
  456. throw new ArgumentNullException ("first");
  457. if (second == null)
  458. throw new ArgumentNullException ("second");
  459. if (comparer == null)
  460. comparer = EqualityComparer<TSource>.Default;
  461. var source = new CancellationTokenSource ();
  462. var zip = new QueryZipNode<TSource, TSource, bool> (comparer.Equals, first.Node, second.Node) { Strict = true };
  463. var innerQuery = new ParallelQuery<bool> (zip).WithImplementerToken (source);
  464. bool result = true;
  465. try {
  466. innerQuery.ForAll (value => {
  467. if (!value) {
  468. result = false;
  469. source.Cancel ();
  470. }
  471. });
  472. } catch (AggregateException ex) {
  473. if (ex.InnerException is QueryZipException)
  474. return false;
  475. else
  476. throw ex;
  477. } catch (OperationCanceledException e) {
  478. if (e.CancellationToken != source.Token)
  479. throw e;
  480. }
  481. return result;
  482. }
  483. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  484. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  485. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  486. public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first, IEnumerable<TSource> second)
  487. {
  488. throw new NotSupportedException ();
  489. }
  490. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  491. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  492. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  493. public static bool SequenceEqual<TSource> (this ParallelQuery<TSource> first,
  494. IEnumerable<TSource> second,
  495. IEqualityComparer<TSource> comparer)
  496. {
  497. throw new NotSupportedException ();
  498. }
  499. #endregion
  500. #region GroupBy
  501. public static ParallelQuery<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this ParallelQuery<TSource> source,
  502. Func<TSource, TKey> keySelector)
  503. {
  504. return source.GroupBy (keySelector, EqualityComparer<TKey>.Default);
  505. }
  506. public static ParallelQuery<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this ParallelQuery<TSource> source,
  507. Func<TSource, TKey> keySelector,
  508. IEqualityComparer<TKey> comparer)
  509. {
  510. return source.GroupBy (keySelector, new Identity<TSource> ().Apply, comparer);
  511. }
  512. public static ParallelQuery<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
  513. Func<TSource, TKey> keySelector,
  514. Func<TSource, TElement> elementSelector)
  515. {
  516. return source.GroupBy (keySelector, elementSelector, EqualityComparer<TKey>.Default);
  517. }
  518. public static ParallelQuery<TResult> GroupBy<TSource, TKey, TResult> (this ParallelQuery<TSource> source,
  519. Func<TSource, TKey> keySelector,
  520. Func<TKey, IEnumerable<TSource>, TResult> resultSelector)
  521. {
  522. return source.GroupBy (keySelector)
  523. .Select ((g) => resultSelector (g.Key, (IEnumerable<TSource>)g));
  524. }
  525. public static ParallelQuery<IGrouping<TKey, TElement>> GroupBy<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
  526. Func<TSource, TKey> keySelector,
  527. Func<TSource, TElement> elementSelector,
  528. IEqualityComparer<TKey> comparer)
  529. {
  530. if (source == null)
  531. throw new ArgumentNullException ("source");
  532. if (keySelector == null)
  533. throw new ArgumentNullException ("keySelector");
  534. if (elementSelector == null)
  535. throw new ArgumentNullException ("elementSelector");
  536. if (comparer == null)
  537. comparer = EqualityComparer<TKey>.Default;
  538. return new ParallelQuery<IGrouping<TKey, TElement>> (new QueryGroupByNode<TSource, TKey, TElement> (source.Node, keySelector, elementSelector, comparer));
  539. }
  540. public static ParallelQuery<TResult> GroupBy<TSource, TKey, TElement, TResult> (this ParallelQuery<TSource> source,
  541. Func<TSource, TKey> keySelector,
  542. Func<TSource, TElement> elementSelector,
  543. Func<TKey, IEnumerable<TElement>, TResult> resultSelector)
  544. {
  545. return source.GroupBy (keySelector, elementSelector)
  546. .Select ((g) => resultSelector (g.Key, (IEnumerable<TElement>)g));
  547. }
  548. public static ParallelQuery<TResult> GroupBy<TSource, TKey, TResult> (this ParallelQuery<TSource> source,
  549. Func<TSource, TKey> keySelector,
  550. Func<TKey, IEnumerable<TSource>, TResult> resultSelector,
  551. IEqualityComparer<TKey> comparer)
  552. {
  553. return source.GroupBy (keySelector, comparer)
  554. .Select ((g) => resultSelector (g.Key, (IEnumerable<TSource>)g));
  555. }
  556. public static ParallelQuery<TResult> GroupBy<TSource, TKey, TElement, TResult> (this ParallelQuery<TSource> source,
  557. Func<TSource, TKey> keySelector,
  558. Func<TSource, TElement> elementSelector,
  559. Func<TKey, IEnumerable<TElement>, TResult> resultSelector,
  560. IEqualityComparer<TKey> comparer)
  561. {
  562. return source.GroupBy (keySelector, elementSelector, comparer)
  563. .Select ((g) => resultSelector (g.Key, (IEnumerable<TElement>)g));
  564. }
  565. #endregion
  566. #region GroupJoin
  567. public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  568. ParallelQuery<TInner> inner,
  569. Func<TOuter, TKey> outerKeySelector,
  570. Func<TInner, TKey> innerKeySelector,
  571. Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
  572. {
  573. return outer.GroupJoin (inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
  574. }
  575. public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  576. ParallelQuery<TInner> inner,
  577. Func<TOuter, TKey> outerKeySelector,
  578. Func<TInner, TKey> innerKeySelector,
  579. Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
  580. IEqualityComparer<TKey> comparer)
  581. {
  582. return outer.Join (inner.GroupBy (innerKeySelector, (e) => e), outerKeySelector, (e) => e.Key, (e1, e2) => resultSelector (e1, e2), comparer);
  583. }
  584. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  585. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  586. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  587. public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  588. IEnumerable<TInner> inner,
  589. Func<TOuter, TKey> outerKeySelector,
  590. Func<TInner, TKey> innerKeySelector,
  591. Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
  592. {
  593. throw new NotSupportedException ();
  594. }
  595. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  596. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  597. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  598. public static ParallelQuery<TResult> GroupJoin<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  599. IEnumerable<TInner> inner,
  600. Func<TOuter, TKey> outerKeySelector,
  601. Func<TInner, TKey> innerKeySelector,
  602. Func<TOuter, IEnumerable<TInner>, TResult> resultSelector,
  603. IEqualityComparer<TKey> comparer)
  604. {
  605. throw new NotSupportedException ();
  606. }
  607. #endregion
  608. #region ElementAt
  609. public static TSource ElementAt<TSource> (this ParallelQuery<TSource> source, int index)
  610. {
  611. if (source == null)
  612. throw new ArgumentNullException ("source");
  613. if (index < 0)
  614. throw new ArgumentOutOfRangeException ("index");
  615. if (index == 0) {
  616. try {
  617. return source.First ();
  618. } catch (InvalidOperationException) {
  619. throw new ArgumentOutOfRangeException ("index");
  620. }
  621. }
  622. TSource result = default (TSource);
  623. ParallelQuery<TSource> innerQuery = source.Where ((e, i) => i == index);
  624. try {
  625. result = innerQuery.First ();
  626. } catch (InvalidOperationException) {
  627. throw new ArgumentOutOfRangeException ("index");
  628. }
  629. return result;
  630. }
  631. public static TSource ElementAtOrDefault<TSource> (this ParallelQuery<TSource> source, int index)
  632. {
  633. if (source == null)
  634. throw new ArgumentNullException ("source");
  635. try {
  636. return source.ElementAt (index);
  637. } catch (ArgumentOutOfRangeException) {
  638. return default (TSource);
  639. }
  640. }
  641. #endregion
  642. #region Intersect
  643. public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first,
  644. ParallelQuery<TSource> second)
  645. {
  646. return Intersect<TSource> (first, second, EqualityComparer<TSource>.Default);
  647. }
  648. public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first,
  649. ParallelQuery<TSource> second,
  650. IEqualityComparer<TSource> comparer)
  651. {
  652. if (first == null)
  653. throw new ArgumentNullException ("first");
  654. if (second == null)
  655. throw new ArgumentNullException ("second");
  656. if (comparer == null)
  657. comparer = EqualityComparer<TSource>.Default;
  658. return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Intersect, comparer, first.Node, second.Node));
  659. }
  660. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  661. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  662. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  663. public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first, IEnumerable<TSource> second)
  664. {
  665. throw new NotSupportedException ();
  666. }
  667. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  668. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  669. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  670. public static ParallelQuery<TSource> Intersect<TSource> (this ParallelQuery<TSource> first,
  671. IEnumerable<TSource> second,
  672. IEqualityComparer<TSource> comparer)
  673. {
  674. throw new NotSupportedException ();
  675. }
  676. #endregion
  677. #region Join
  678. public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  679. ParallelQuery<TInner> inner,
  680. Func<TOuter, TKey> outerKeySelector,
  681. Func<TInner, TKey> innerKeySelector,
  682. Func<TOuter, TInner, TResult> resultSelector)
  683. {
  684. return outer.Join (inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
  685. }
  686. public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  687. ParallelQuery<TInner> inner,
  688. Func<TOuter, TKey> outerKeySelector,
  689. Func<TInner, TKey> innerKeySelector,
  690. Func<TOuter, TInner, TResult> resultSelector,
  691. IEqualityComparer<TKey> comparer)
  692. {
  693. return new ParallelQuery<TResult> (new QueryJoinNode<TOuter, TInner, TKey, TResult> (outer.Node, inner.Node, outerKeySelector, innerKeySelector, resultSelector, comparer));
  694. }
  695. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  696. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  697. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  698. public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  699. IEnumerable<TInner> inner,
  700. Func<TOuter, TKey> outerKeySelector,
  701. Func<TInner, TKey> innerKeySelector,
  702. Func<TOuter, TInner, TResult> resultSelector)
  703. {
  704. throw new NotSupportedException ();
  705. }
  706. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  707. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  708. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  709. public static ParallelQuery<TResult> Join<TOuter, TInner, TKey, TResult> (this ParallelQuery<TOuter> outer,
  710. IEnumerable<TInner> inner,
  711. Func<TOuter, TKey> outerKeySelector,
  712. Func<TInner, TKey> innerKeySelector,
  713. Func<TOuter, TInner, TResult> resultSelector,
  714. IEqualityComparer<TKey> comparer)
  715. {
  716. throw new NotSupportedException ();
  717. }
  718. #endregion
  719. #region Except
  720. public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
  721. ParallelQuery<TSource> second)
  722. {
  723. return Except<TSource> (first, second, EqualityComparer<TSource>.Default);
  724. }
  725. public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
  726. ParallelQuery<TSource> second,
  727. IEqualityComparer<TSource> comparer)
  728. {
  729. if (first == null)
  730. throw new ArgumentNullException ("first");
  731. if (second == null)
  732. throw new ArgumentNullException ("second");
  733. if (comparer == null)
  734. comparer = EqualityComparer<TSource>.Default;
  735. return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Except,
  736. comparer, first.Node, second.Node));
  737. }
  738. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  739. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  740. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  741. public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
  742. IEnumerable<TSource> second)
  743. {
  744. throw new NotSupportedException ();
  745. }
  746. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  747. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  748. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  749. public static ParallelQuery<TSource> Except<TSource> (this ParallelQuery<TSource> first,
  750. IEnumerable<TSource> second,
  751. IEqualityComparer<TSource> comparer)
  752. {
  753. throw new NotSupportedException ();
  754. }
  755. #endregion
  756. #region Distinct
  757. public static ParallelQuery<TSource> Distinct<TSource> (this ParallelQuery<TSource> source)
  758. {
  759. return Distinct<TSource> (source, EqualityComparer<TSource>.Default);
  760. }
  761. public static ParallelQuery<TSource> Distinct<TSource> (this ParallelQuery<TSource> source, IEqualityComparer<TSource> comparer)
  762. {
  763. if (source == null)
  764. throw new ArgumentNullException ("source");
  765. if (comparer == null)
  766. comparer = EqualityComparer<TSource>.Default;
  767. return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Distinct, comparer,
  768. source.Node, null));
  769. }
  770. #endregion
  771. #region Union
  772. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  773. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  774. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  775. public static ParallelQuery<TSource> Union<TSource> (this ParallelQuery<TSource> first,
  776. IEnumerable<TSource> second)
  777. {
  778. throw new NotSupportedException ();
  779. }
  780. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  781. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  782. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  783. public static ParallelQuery<TSource> Union<TSource>(this ParallelQuery<TSource> first,
  784. IEnumerable<TSource> second,
  785. IEqualityComparer<TSource> comparer)
  786. {
  787. throw new NotSupportedException ();
  788. }
  789. public static ParallelQuery<TSource> Union<TSource> (this ParallelQuery<TSource> first,
  790. ParallelQuery<TSource> second)
  791. {
  792. return first.Union (second, EqualityComparer<TSource>.Default);
  793. }
  794. public static ParallelQuery<TSource> Union<TSource> (this ParallelQuery<TSource> first,
  795. ParallelQuery<TSource> second,
  796. IEqualityComparer<TSource> comparer)
  797. {
  798. if (first == null)
  799. throw new ArgumentNullException ("first");
  800. if (second == null)
  801. throw new ArgumentNullException ("second");
  802. if (comparer == null)
  803. comparer = EqualityComparer<TSource>.Default;
  804. return new ParallelQuery<TSource> (new QuerySetNode<TSource> (SetInclusionDefaults.Union, comparer, first.Node, second.Node));
  805. }
  806. #endregion
  807. #region Take
  808. public static ParallelQuery<TSource> Take<TSource> (this ParallelQuery<TSource> source, int count)
  809. {
  810. if (source == null)
  811. throw new ArgumentNullException ("source");
  812. return new ParallelQuery<TSource> (new QueryHeadWorkerNode<TSource> (source.Node, count));
  813. }
  814. public static ParallelQuery<TSource> TakeWhile<TSource> (this ParallelQuery<TSource> source,
  815. Func<TSource, bool> predicate)
  816. {
  817. if (source == null)
  818. throw new ArgumentNullException ("source");
  819. if (predicate == null)
  820. throw new ArgumentNullException ("predicate");
  821. return new ParallelQuery<TSource> (new QueryHeadWorkerNode<TSource> (source.Node, (e, _) => predicate (e), false));
  822. }
  823. public static ParallelQuery<TSource> TakeWhile<TSource> (this ParallelQuery<TSource> source,
  824. Func<TSource, int, bool> predicate)
  825. {
  826. if (source == null)
  827. throw new ArgumentNullException ("source");
  828. if (predicate == null)
  829. throw new ArgumentNullException ("predicate");
  830. return new ParallelQuery<TSource> (new QueryHeadWorkerNode<TSource> (source.Node, predicate, true));
  831. }
  832. #endregion
  833. #region Skip
  834. public static ParallelQuery<TSource> Skip<TSource> (this ParallelQuery<TSource> source, int count)
  835. {
  836. if (source == null)
  837. throw new ArgumentNullException ("source");
  838. return source.Node.IsOrdered () ?
  839. source.Where ((e, i) => i >= count) :
  840. source.Where ((e) => count < 0 || Interlocked.Decrement (ref count) < 0);
  841. }
  842. public static ParallelQuery<TSource> SkipWhile<TSource> (this ParallelQuery<TSource> source,
  843. Func<TSource, bool> predicate)
  844. {
  845. if (source == null)
  846. throw new ArgumentNullException ("source");
  847. if (predicate == null)
  848. throw new ArgumentNullException ("predicate");
  849. return source.Node.IsOrdered () ?
  850. source.SkipWhile ((e, i) => predicate (e)) :
  851. source.Where ((e) => !predicate (e));
  852. }
  853. public static ParallelQuery<TSource> SkipWhile<TSource> (this ParallelQuery<TSource> source,
  854. Func<TSource, int, bool> predicate)
  855. {
  856. if (source == null)
  857. throw new ArgumentNullException ("source");
  858. if (predicate == null)
  859. throw new ArgumentNullException ("predicate");
  860. int indexCache = int.MaxValue;
  861. return source.Where ((e, i) => i >= indexCache || (!predicate (e, i) && (indexCache = i) == i));
  862. }
  863. #endregion
  864. #region Single
  865. static TSource SingleInternal<TSource> (this ParallelQuery<TSource> source, params TSource[] init)
  866. {
  867. TSource result = default(TSource);
  868. bool hasValue = false;
  869. foreach (TSource element in source) {
  870. if (hasValue)
  871. throw new InvalidOperationException ("The input sequence contains more than one element.");
  872. result = element;
  873. hasValue = true;
  874. }
  875. if (!hasValue && init.Length != 0) {
  876. result = init[0];
  877. hasValue = true;
  878. }
  879. if (!hasValue)
  880. throw new InvalidOperationException ("The input sequence is empty.");
  881. return result;
  882. }
  883. public static TSource Single<TSource> (this ParallelQuery<TSource> source)
  884. {
  885. if (source == null)
  886. throw new ArgumentNullException ("source");
  887. return SingleInternal<TSource> (source);
  888. }
  889. public static TSource Single<TSource> (this ParallelQuery<TSource> source,
  890. Func<TSource, bool> predicate)
  891. {
  892. if (source == null)
  893. throw new ArgumentNullException ("source");
  894. if (predicate == null)
  895. throw new ArgumentNullException ("predicate");
  896. return source.Where (predicate).Single ();
  897. }
  898. public static TSource SingleOrDefault<TSource> (this ParallelQuery<TSource> source)
  899. {
  900. if (source == null)
  901. throw new ArgumentNullException ("source");
  902. return SingleInternal<TSource> (source, default (TSource));
  903. }
  904. public static TSource SingleOrDefault<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  905. {
  906. if (source == null)
  907. throw new ArgumentNullException ("source");
  908. if (predicate == null)
  909. throw new ArgumentNullException ("predicate");
  910. return source.Where (predicate).SingleOrDefault ();
  911. }
  912. #endregion
  913. #region Count
  914. public static int Count<TSource> (this ParallelQuery<TSource> source)
  915. {
  916. if (source == null)
  917. throw new ArgumentNullException ("source");
  918. var helper = new CountAggregateHelper<TSource> ();
  919. return source.Aggregate<TSource, int, int> (helper.Seed,
  920. helper.Intermediate,
  921. helper.Reducer,
  922. helper.Final);
  923. }
  924. public static int Count<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  925. {
  926. if (source == null)
  927. throw new ArgumentNullException ("source");
  928. if (predicate == null)
  929. throw new ArgumentNullException ("predicate");
  930. return source.Where (predicate).Count ();
  931. }
  932. class CountAggregateHelper<TSource>
  933. {
  934. public int Seed ()
  935. {
  936. return 0;
  937. }
  938. public int Intermediate (int acc, TSource e)
  939. {
  940. return acc + 1;
  941. }
  942. public int Reducer (int acc1, int acc2)
  943. {
  944. return acc1 + acc2;
  945. }
  946. public int Final (int acc)
  947. {
  948. return acc;
  949. }
  950. }
  951. public static long LongCount<TSource> (this ParallelQuery<TSource> source)
  952. {
  953. if (source == null)
  954. throw new ArgumentNullException ("source");
  955. var helper = new LongCountAggregateHelper<TSource> ();
  956. return source.Aggregate<TSource, long, long> (helper.Seed,
  957. helper.Intermediate,
  958. helper.Reducer,
  959. helper.Final);
  960. }
  961. public static long LongCount<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  962. {
  963. if (source == null)
  964. throw new ArgumentNullException ("source");
  965. if (predicate == null)
  966. throw new ArgumentNullException ("predicate");
  967. return source.Where (predicate).LongCount ();
  968. }
  969. class LongCountAggregateHelper<TSource>
  970. {
  971. public long Seed ()
  972. {
  973. return 0;
  974. }
  975. public long Intermediate (long acc, TSource e)
  976. {
  977. return acc + 1;
  978. }
  979. public long Reducer (long acc1, long acc2)
  980. {
  981. return acc1 + acc2;
  982. }
  983. public long Final (long acc)
  984. {
  985. return acc;
  986. }
  987. }
  988. #endregion
  989. #region Average
  990. public static double Average (this ParallelQuery<int> source)
  991. {
  992. if (source == null)
  993. throw new ArgumentNullException ("source");
  994. return source.Aggregate (() => new int[2],
  995. (acc, e) => { acc[0] += e; acc[1]++; return acc; },
  996. (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
  997. (acc) => acc[0] / ((double)acc[1]));
  998. }
  999. public static double Average (this ParallelQuery<long> source)
  1000. {
  1001. if (source == null)
  1002. throw new ArgumentNullException ("source");
  1003. return source.Aggregate (() => new long[2],
  1004. (acc, e) => { acc[0] += e; acc[1]++; return acc; },
  1005. (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
  1006. (acc) => acc[0] / ((double)acc[1]));
  1007. }
  1008. public static decimal Average (this ParallelQuery<decimal> source)
  1009. {
  1010. if (source == null)
  1011. throw new ArgumentNullException ("source");
  1012. return source.Aggregate (() => new decimal[2],
  1013. (acc, e) => { acc[0] += e; acc[1]++; return acc; },
  1014. (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
  1015. (acc) => acc[0] / acc[1]);
  1016. }
  1017. public static double Average (this ParallelQuery<double> source)
  1018. {
  1019. if (source == null)
  1020. throw new ArgumentNullException ("source");
  1021. return source.Aggregate (() => new double[2],
  1022. (acc, e) => { acc[0] += e; acc[1]++; return acc; },
  1023. (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
  1024. (acc) => acc[0] / ((double)acc[1]));
  1025. }
  1026. public static float Average (this ParallelQuery<float> source)
  1027. {
  1028. if (source == null)
  1029. throw new ArgumentNullException ("source");
  1030. return source.Aggregate (() => new float[2],
  1031. (acc, e) => { acc[0] += e; acc[1]++; return acc; },
  1032. (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
  1033. (acc) => acc[0] / acc[1]);
  1034. }
  1035. #endregion
  1036. #region More Average
  1037. public static double? Average (this ParallelQuery<int?> source)
  1038. {
  1039. if (source == null)
  1040. throw new ArgumentNullException ("source");
  1041. return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();;
  1042. }
  1043. public static double? Average (this ParallelQuery<long?> source)
  1044. {
  1045. if (source == null)
  1046. throw new ArgumentNullException ("source");
  1047. return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
  1048. }
  1049. public static decimal? Average (this ParallelQuery<decimal?> source)
  1050. {
  1051. if (source == null)
  1052. throw new ArgumentNullException ("source");
  1053. return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
  1054. }
  1055. public static double? Average (this ParallelQuery<double?> source)
  1056. {
  1057. if (source == null)
  1058. throw new ArgumentNullException ("source");
  1059. return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
  1060. }
  1061. public static float? Average (this ParallelQuery<float?> source)
  1062. {
  1063. if (source == null)
  1064. throw new ArgumentNullException ("source");
  1065. return source.Select ((e) => e.HasValue ? e.Value : 0).Average ();
  1066. }
  1067. public static double Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> selector)
  1068. {
  1069. if (source == null)
  1070. throw new ArgumentNullException ("source");
  1071. if (selector == null)
  1072. throw new ArgumentNullException ("selector");
  1073. return source.Select (selector).Average ();
  1074. }
  1075. public static double Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> selector)
  1076. {
  1077. if (source == null)
  1078. throw new ArgumentNullException ("source");
  1079. if (selector == null)
  1080. throw new ArgumentNullException ("selector");
  1081. return source.Select (selector).Average ();
  1082. }
  1083. public static float Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> selector)
  1084. {
  1085. if (source == null)
  1086. throw new ArgumentNullException ("source");
  1087. if (selector == null)
  1088. throw new ArgumentNullException ("selector");
  1089. return source.Select (selector).Average ();
  1090. }
  1091. public static double Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> selector)
  1092. {
  1093. if (source == null)
  1094. throw new ArgumentNullException ("source");
  1095. if (selector == null)
  1096. throw new ArgumentNullException ("selector");
  1097. return source.Select (selector).Average ();
  1098. }
  1099. public static decimal Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> selector)
  1100. {
  1101. if (source == null)
  1102. throw new ArgumentNullException ("source");
  1103. if (selector == null)
  1104. throw new ArgumentNullException ("selector");
  1105. return source.Select (selector).Average ();
  1106. }
  1107. public static double? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> selector)
  1108. {
  1109. if (source == null)
  1110. throw new ArgumentNullException ("source");
  1111. if (selector == null)
  1112. throw new ArgumentNullException ("selector");
  1113. return source.Select (selector).Average ();
  1114. }
  1115. public static double? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> selector)
  1116. {
  1117. if (source == null)
  1118. throw new ArgumentNullException ("source");
  1119. if (selector == null)
  1120. throw new ArgumentNullException ("selector");
  1121. return source.Select (selector).Average ();
  1122. }
  1123. public static float? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> selector)
  1124. {
  1125. if (source == null)
  1126. throw new ArgumentNullException ("source");
  1127. if (selector == null)
  1128. throw new ArgumentNullException ("selector");
  1129. return source.Select (selector).Average ();
  1130. }
  1131. public static double? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> selector)
  1132. {
  1133. if (source == null)
  1134. throw new ArgumentNullException ("source");
  1135. if (selector == null)
  1136. throw new ArgumentNullException ("selector");
  1137. return source.Select (selector).Average ();
  1138. }
  1139. public static decimal? Average<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> selector)
  1140. {
  1141. if (source == null)
  1142. throw new ArgumentNullException ("source");
  1143. if (selector == null)
  1144. throw new ArgumentNullException ("selector");
  1145. return source.Select (selector).Average ();
  1146. }
  1147. #endregion
  1148. #region Sum
  1149. public static int Sum (this ParallelQuery<int> source)
  1150. {
  1151. if (source == null)
  1152. throw new ArgumentNullException ("source");
  1153. return source.Aggregate (0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
  1154. }
  1155. public static long Sum (this ParallelQuery<long> source)
  1156. {
  1157. if (source == null)
  1158. throw new ArgumentNullException ("source");
  1159. return source.Aggregate ((long)0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
  1160. }
  1161. public static float Sum (this ParallelQuery<float> source)
  1162. {
  1163. if (source == null)
  1164. throw new ArgumentNullException ("source");
  1165. return source.Aggregate (0.0f, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
  1166. }
  1167. public static double Sum (this ParallelQuery<double> source)
  1168. {
  1169. if (source == null)
  1170. throw new ArgumentNullException ("source");
  1171. return source.Aggregate (0.0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
  1172. }
  1173. public static decimal Sum (this ParallelQuery<decimal> source)
  1174. {
  1175. if (source == null)
  1176. throw new ArgumentNullException ("source");
  1177. return source.Aggregate ((decimal)0, (e1, e2) => e1 + e2, (sum1, sum2) => sum1 + sum2, (sum) => sum);
  1178. }
  1179. public static int? Sum (this ParallelQuery<int?> source)
  1180. {
  1181. return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
  1182. }
  1183. public static long? Sum (this ParallelQuery<long?> source)
  1184. {
  1185. if (source == null)
  1186. throw new ArgumentNullException ("source");
  1187. return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
  1188. }
  1189. public static float? Sum (this ParallelQuery<float?> source)
  1190. {
  1191. if (source == null)
  1192. throw new ArgumentNullException ("source");
  1193. return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
  1194. }
  1195. public static double? Sum (this ParallelQuery<double?> source)
  1196. {
  1197. if (source == null)
  1198. throw new ArgumentNullException ("source");
  1199. return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
  1200. }
  1201. public static decimal? Sum (this ParallelQuery<decimal?> source)
  1202. {
  1203. if (source == null)
  1204. throw new ArgumentNullException ("source");
  1205. return source.Select ((e) => e.HasValue ? e.Value : 0).Sum ();
  1206. }
  1207. public static int Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> selector)
  1208. {
  1209. if (source == null)
  1210. throw new ArgumentNullException ("source");
  1211. if (selector == null)
  1212. throw new ArgumentNullException ("selector");
  1213. return source.Select (selector).Sum ();
  1214. }
  1215. public static long Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> selector)
  1216. {
  1217. if (source == null)
  1218. throw new ArgumentNullException ("source");
  1219. if (selector == null)
  1220. throw new ArgumentNullException ("selector");
  1221. return source.Select (selector).Sum ();
  1222. }
  1223. public static decimal Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> selector)
  1224. {
  1225. if (source == null)
  1226. throw new ArgumentNullException ("source");
  1227. if (selector == null)
  1228. throw new ArgumentNullException ("selector");
  1229. return source.Select (selector).Sum ();
  1230. }
  1231. public static float Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> selector)
  1232. {
  1233. if (source == null)
  1234. throw new ArgumentNullException ("source");
  1235. if (selector == null)
  1236. throw new ArgumentNullException ("selector");
  1237. return source.Select (selector).Sum ();
  1238. }
  1239. public static double Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> selector)
  1240. {
  1241. if (source == null)
  1242. throw new ArgumentNullException ("source");
  1243. if (selector == null)
  1244. throw new ArgumentNullException ("selector");
  1245. return source.Select (selector).Sum ();
  1246. }
  1247. public static int? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> selector)
  1248. {
  1249. if (source == null)
  1250. throw new ArgumentNullException ("source");
  1251. if (selector == null)
  1252. throw new ArgumentNullException ("selector");
  1253. return source.Select (selector).Sum ();
  1254. }
  1255. public static long? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> selector)
  1256. {
  1257. if (source == null)
  1258. throw new ArgumentNullException ("source");
  1259. if (selector == null)
  1260. throw new ArgumentNullException ("selector");
  1261. return source.Select (selector).Sum ();
  1262. }
  1263. public static decimal? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> selector)
  1264. {
  1265. if (source == null)
  1266. throw new ArgumentNullException ("source");
  1267. if (selector == null)
  1268. throw new ArgumentNullException ("selector");
  1269. return source.Select (selector).Sum ();
  1270. }
  1271. public static float? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> selector)
  1272. {
  1273. if (source == null)
  1274. throw new ArgumentNullException ("source");
  1275. if (selector == null)
  1276. throw new ArgumentNullException ("selector");
  1277. return source.Select (selector).Sum ();
  1278. }
  1279. public static double? Sum<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> selector)
  1280. {
  1281. if (source == null)
  1282. throw new ArgumentNullException ("source");
  1283. if (selector == null)
  1284. throw new ArgumentNullException ("selector");
  1285. return source.Select (selector).Sum ();
  1286. }
  1287. #endregion
  1288. #region Min-Max
  1289. static T BestOrder<T> (ParallelQuery<T> source, BestOrderComparer<T> bestOrderComparer)
  1290. {
  1291. if (source == null)
  1292. throw new ArgumentNullException ("source");
  1293. T best = source.Aggregate (bestOrderComparer.Seed,
  1294. bestOrderComparer.Intermediate,
  1295. bestOrderComparer.Intermediate,
  1296. new Identity<T> ().Apply);
  1297. return best;
  1298. }
  1299. class BestOrderComparer<T>
  1300. {
  1301. IComparer<T> comparer;
  1302. int inverter;
  1303. T seed;
  1304. public BestOrderComparer (IComparer<T> comparer, int inverter, T seed)
  1305. {
  1306. this.comparer = comparer;
  1307. this.inverter = inverter;
  1308. this.seed = seed;
  1309. }
  1310. public T Seed ()
  1311. {
  1312. return seed;
  1313. }
  1314. public T Intermediate (T first, T second)
  1315. {
  1316. return Better (first, second) ? first : second;
  1317. }
  1318. bool Better (T first, T second)
  1319. {
  1320. return (inverter * comparer.Compare (first, second)) > 0;
  1321. }
  1322. }
  1323. public static int Min (this ParallelQuery<int> source)
  1324. {
  1325. return BestOrder (source, new BestOrderComparer<int> (Comparer<int>.Default, -1, int.MaxValue));
  1326. }
  1327. public static long Min (this ParallelQuery<long> source)
  1328. {
  1329. return BestOrder (source, new BestOrderComparer<long> (Comparer<long>.Default, -1, long.MaxValue));
  1330. }
  1331. public static float Min (this ParallelQuery<float> source)
  1332. {
  1333. return BestOrder (source, new BestOrderComparer<float> (Comparer<float>.Default, -1, float.MaxValue));
  1334. }
  1335. public static double Min (this ParallelQuery<double> source)
  1336. {
  1337. return BestOrder (source, new BestOrderComparer<double> (Comparer<double>.Default, -1, double.MaxValue));
  1338. }
  1339. public static decimal Min (this ParallelQuery<decimal> source)
  1340. {
  1341. return BestOrder (source, new BestOrderComparer<decimal> (Comparer<decimal>.Default, -1, decimal.MaxValue));
  1342. }
  1343. public static TSource Min<TSource> (this ParallelQuery<TSource> source)
  1344. {
  1345. IComparer<TSource> comparer = Comparer<TSource>.Default;
  1346. return BestOrder (source, new BestOrderComparer<TSource> (comparer, -1, default (TSource)));
  1347. }
  1348. public static TResult Min<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, TResult> selector)
  1349. {
  1350. if (source == null)
  1351. throw new ArgumentNullException ("source");
  1352. if (selector == null)
  1353. throw new ArgumentNullException ("selector");
  1354. return source.Select (selector).Min ();
  1355. }
  1356. public static int? Min (this ParallelQuery<int?> source)
  1357. {
  1358. if (source == null)
  1359. throw new ArgumentNullException ("source");
  1360. return source.Select ((e) => e.HasValue ? e.Value : int.MaxValue).Min ();
  1361. }
  1362. public static long? Min (this ParallelQuery<long?> source)
  1363. {
  1364. if (source == null)
  1365. throw new ArgumentNullException ("source");
  1366. return source.Select ((e) => e.HasValue ? e.Value : long.MaxValue).Min ();
  1367. }
  1368. public static float? Min (this ParallelQuery<float?> source)
  1369. {
  1370. if (source == null)
  1371. throw new ArgumentNullException ("source");
  1372. return source.Select ((e) => e.HasValue ? e.Value : float.MaxValue).Min ();
  1373. }
  1374. public static double? Min (this ParallelQuery<double?> source)
  1375. {
  1376. if (source == null)
  1377. throw new ArgumentNullException ("source");
  1378. return source.Select ((e) => e.HasValue ? e.Value : double.MaxValue).Min ();
  1379. }
  1380. public static decimal? Min (this ParallelQuery<decimal?> source)
  1381. {
  1382. if (source == null)
  1383. throw new ArgumentNullException ("source");
  1384. return source.Select ((e) => e.HasValue ? e.Value : decimal.MaxValue).Min ();
  1385. }
  1386. public static int Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> selector)
  1387. {
  1388. if (source == null)
  1389. throw new ArgumentNullException ("source");
  1390. if (selector == null)
  1391. throw new ArgumentNullException ("selector");
  1392. return source.Select (selector).Min ();
  1393. }
  1394. public static long Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> selector)
  1395. {
  1396. if (source == null)
  1397. throw new ArgumentNullException ("source");
  1398. if (selector == null)
  1399. throw new ArgumentNullException ("selector");
  1400. return source.Select (selector).Min ();
  1401. }
  1402. public static float Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> selector)
  1403. {
  1404. if (source == null)
  1405. throw new ArgumentNullException ("source");
  1406. if (selector == null)
  1407. throw new ArgumentNullException ("selector");
  1408. return source.Select (selector).Min ();
  1409. }
  1410. public static double Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> selector)
  1411. {
  1412. if (source == null)
  1413. throw new ArgumentNullException ("source");
  1414. if (selector == null)
  1415. throw new ArgumentNullException ("selector");
  1416. return source.Select (selector).Min ();
  1417. }
  1418. public static decimal Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> selector)
  1419. {
  1420. if (source == null)
  1421. throw new ArgumentNullException ("source");
  1422. if (selector == null)
  1423. throw new ArgumentNullException ("selector");
  1424. return source.Select (selector).Min ();
  1425. }
  1426. public static int? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> selector)
  1427. {
  1428. if (source == null)
  1429. throw new ArgumentNullException ("source");
  1430. if (selector == null)
  1431. throw new ArgumentNullException ("selector");
  1432. return source.Select (selector).Min ();
  1433. }
  1434. public static long? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> selector)
  1435. {
  1436. if (source == null)
  1437. throw new ArgumentNullException ("source");
  1438. if (selector == null)
  1439. throw new ArgumentNullException ("selector");
  1440. return source.Select (selector).Min ();
  1441. }
  1442. public static float? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> selector)
  1443. {
  1444. if (source == null)
  1445. throw new ArgumentNullException ("source");
  1446. if (selector == null)
  1447. throw new ArgumentNullException ("selector");
  1448. return source.Select (selector).Min ();
  1449. }
  1450. public static double? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> selector)
  1451. {
  1452. if (source == null)
  1453. throw new ArgumentNullException ("source");
  1454. if (selector == null)
  1455. throw new ArgumentNullException ("selector");
  1456. return source.Select (selector).Min ();
  1457. }
  1458. public static decimal? Min<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> selector)
  1459. {
  1460. if (source == null)
  1461. throw new ArgumentNullException ("source");
  1462. if (selector == null)
  1463. throw new ArgumentNullException ("selector");
  1464. return source.Select (selector).Min ();
  1465. }
  1466. public static int Max (this ParallelQuery<int> source)
  1467. {
  1468. return BestOrder (source, new BestOrderComparer<int> (Comparer<int>.Default, 1, int.MinValue));
  1469. }
  1470. public static long Max (this ParallelQuery<long> source)
  1471. {
  1472. return BestOrder (source, new BestOrderComparer<long> (Comparer<long>.Default, 1, long.MinValue));
  1473. }
  1474. public static float Max (this ParallelQuery<float> source)
  1475. {
  1476. return BestOrder (source, new BestOrderComparer<float> (Comparer<float>.Default, 1, float.MinValue));
  1477. }
  1478. public static double Max (this ParallelQuery<double> source)
  1479. {
  1480. return BestOrder (source, new BestOrderComparer<double> (Comparer<double>.Default, 1, double.MinValue));
  1481. }
  1482. public static decimal Max (this ParallelQuery<decimal> source)
  1483. {
  1484. return BestOrder (source, new BestOrderComparer<decimal> (Comparer<decimal>.Default, 1, decimal.MinValue));
  1485. }
  1486. public static TSource Max<TSource> (this ParallelQuery<TSource> source)
  1487. {
  1488. IComparer<TSource> comparer = Comparer<TSource>.Default;
  1489. return BestOrder (source, new BestOrderComparer<TSource> (comparer, 1, default (TSource)));
  1490. }
  1491. public static TResult Max<TSource, TResult> (this ParallelQuery<TSource> source, Func<TSource, TResult> selector)
  1492. {
  1493. if (source == null)
  1494. throw new ArgumentNullException ("source");
  1495. if (selector == null)
  1496. throw new ArgumentNullException ("selector");
  1497. return source.Select (selector).Max ();
  1498. }
  1499. public static int? Max (this ParallelQuery<int?> source)
  1500. {
  1501. if (source == null)
  1502. throw new ArgumentNullException ("source");
  1503. return source.Select ((e) => e.HasValue ? e.Value : int.MinValue).Max ();
  1504. }
  1505. public static long? Max (this ParallelQuery<long?> source)
  1506. {
  1507. if (source == null)
  1508. throw new ArgumentNullException ("source");
  1509. return source.Select ((e) => e.HasValue ? e.Value : long.MinValue).Max ();
  1510. }
  1511. public static float? Max (this ParallelQuery<float?> source)
  1512. {
  1513. if (source == null)
  1514. throw new ArgumentNullException ("source");
  1515. return source.Select ((e) => e.HasValue ? e.Value : float.MinValue).Max ();
  1516. }
  1517. public static double? Max (this ParallelQuery<double?> source)
  1518. {
  1519. if (source == null)
  1520. throw new ArgumentNullException ("source");
  1521. return source.Select ((e) => e.HasValue ? e.Value : double.MinValue).Max ();
  1522. }
  1523. public static decimal? Max (this ParallelQuery<decimal?> source)
  1524. {
  1525. if (source == null)
  1526. throw new ArgumentNullException ("source");
  1527. return source.Select ((e) => e.HasValue ? e.Value : decimal.MinValue).Max ();
  1528. }
  1529. public static int Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, int> selector)
  1530. {
  1531. if (source == null)
  1532. throw new ArgumentNullException ("source");
  1533. if (selector == null)
  1534. throw new ArgumentNullException ("selector");
  1535. return source.Select (selector).Max ();
  1536. }
  1537. public static long Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, long> selector)
  1538. {
  1539. if (source == null)
  1540. throw new ArgumentNullException ("source");
  1541. if (selector == null)
  1542. throw new ArgumentNullException ("selector");
  1543. return source.Select (selector).Max ();
  1544. }
  1545. public static float Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, float> selector)
  1546. {
  1547. if (source == null)
  1548. throw new ArgumentNullException ("source");
  1549. if (selector == null)
  1550. throw new ArgumentNullException ("selector");
  1551. return source.Select (selector).Max ();
  1552. }
  1553. public static double Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, double> selector)
  1554. {
  1555. if (source == null)
  1556. throw new ArgumentNullException ("source");
  1557. if (selector == null)
  1558. throw new ArgumentNullException ("selector");
  1559. return source.Select (selector).Max ();
  1560. }
  1561. public static decimal Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal> selector)
  1562. {
  1563. if (source == null)
  1564. throw new ArgumentNullException ("source");
  1565. if (selector == null)
  1566. throw new ArgumentNullException ("selector");
  1567. return source.Select (selector).Max ();
  1568. }
  1569. public static int? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, int?> selector)
  1570. {
  1571. if (source == null)
  1572. throw new ArgumentNullException ("source");
  1573. if (selector == null)
  1574. throw new ArgumentNullException ("selector");
  1575. return source.Select (selector).Max ();
  1576. }
  1577. public static long? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, long?> selector)
  1578. {
  1579. if (source == null)
  1580. throw new ArgumentNullException ("source");
  1581. if (selector == null)
  1582. throw new ArgumentNullException ("selector");
  1583. return source.Select (selector).Max ();
  1584. }
  1585. public static float? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, float?> selector)
  1586. {
  1587. if (source == null)
  1588. throw new ArgumentNullException ("source");
  1589. if (selector == null)
  1590. throw new ArgumentNullException ("selector");
  1591. return source.Select (selector).Max ();
  1592. }
  1593. public static double? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, double?> selector)
  1594. {
  1595. if (source == null)
  1596. throw new ArgumentNullException ("source");
  1597. if (selector == null)
  1598. throw new ArgumentNullException ("selector");
  1599. return source.Select (selector).Max ();
  1600. }
  1601. public static decimal? Max<TSource> (this ParallelQuery<TSource> source, Func<TSource, decimal?> selector)
  1602. {
  1603. if (source == null)
  1604. throw new ArgumentNullException ("source");
  1605. if (selector == null)
  1606. throw new ArgumentNullException ("selector");
  1607. return source.Select (selector).Max ();
  1608. }
  1609. #endregion
  1610. #region Cast / OfType
  1611. public static ParallelQuery<TResult> Cast<TResult> (this ParallelQuery source)
  1612. {
  1613. if (source == null)
  1614. throw new ArgumentNullException ("source");
  1615. return source.TypedQuery.Select ((e) => (TResult)e);
  1616. }
  1617. public static ParallelQuery<TResult> OfType<TResult> (this ParallelQuery source)
  1618. {
  1619. if (source == null)
  1620. throw new ArgumentNullException ("source");
  1621. return source.TypedQuery.Where ((e) => e is TResult).Cast<TResult> ();
  1622. }
  1623. #endregion
  1624. #region Reverse
  1625. public static ParallelQuery<TSource> Reverse<TSource> (this ParallelQuery<TSource> source)
  1626. {
  1627. if (source == null)
  1628. throw new ArgumentNullException ("source");
  1629. return new ParallelQuery<TSource> (new QueryReverseNode<TSource> (source));
  1630. }
  1631. #endregion
  1632. #region ToArray - ToList - ToDictionary - ToLookup
  1633. public static List<TSource> ToList<TSource> (this ParallelQuery<TSource> source)
  1634. {
  1635. if (source == null)
  1636. throw new ArgumentNullException ("source");
  1637. if (source.Node.IsOrdered ())
  1638. return ToListOrdered (source);
  1639. var helper = new ListAggregateHelper<TSource> ();
  1640. List<TSource> temp = source.Aggregate (helper.Seed,
  1641. helper.Intermediate,
  1642. helper.Reducer,
  1643. helper.Final);
  1644. return temp;
  1645. }
  1646. class ListAggregateHelper<TSource>
  1647. {
  1648. public List<TSource> Seed ()
  1649. {
  1650. return new List<TSource> (50);
  1651. }
  1652. public List<TSource> Intermediate (List<TSource> list, TSource e)
  1653. {
  1654. list.Add (e);
  1655. return list;
  1656. }
  1657. public List<TSource> Reducer (List<TSource> list, List<TSource> list2)
  1658. {
  1659. list.AddRange (list2);
  1660. return list;
  1661. }
  1662. public List<TSource> Final (List<TSource> list)
  1663. {
  1664. return list;
  1665. }
  1666. }
  1667. internal static List<TSource> ToListOrdered<TSource> (this ParallelQuery<TSource> source)
  1668. {
  1669. List<TSource> result = new List<TSource> ();
  1670. foreach (TSource element in source)
  1671. result.Add (element);
  1672. return result;
  1673. }
  1674. public static TSource[] ToArray<TSource> (this ParallelQuery<TSource> source)
  1675. {
  1676. if (source == null)
  1677. throw new ArgumentNullException ("source");
  1678. if (source.Node.IsOrdered ())
  1679. return ToListOrdered (source).ToArray ();
  1680. var helper = new ArrayAggregateHelper<TSource> ();
  1681. ParallelExecuter.ProcessAndAggregate<TSource, List<TSource>> (source.Node,
  1682. helper.Seed,
  1683. helper.Intermediate,
  1684. helper.Final);
  1685. return helper.Result;
  1686. }
  1687. class ArrayAggregateHelper<TSource>
  1688. {
  1689. TSource[] result;
  1690. public TSource[] Result {
  1691. get {
  1692. return result;
  1693. }
  1694. }
  1695. internal List<TSource> Seed ()
  1696. {
  1697. return new List<TSource> ();
  1698. }
  1699. internal List<TSource> Intermediate (List<TSource> list, TSource e)
  1700. {
  1701. list.Add (e);
  1702. return list;
  1703. }
  1704. internal void Final (IList<List<TSource>> list)
  1705. {
  1706. int count = 0;
  1707. for (int i = 0; i < list.Count; i++)
  1708. count += list[i].Count;
  1709. result = new TSource[count];
  1710. int insertIndex = -1;
  1711. for (int i = 0; i < list.Count; i++)
  1712. for (int j = 0; j < list[i].Count; j++)
  1713. result [++insertIndex] = list[i][j];
  1714. }
  1715. }
  1716. public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this ParallelQuery<TSource> source,
  1717. Func<TSource, TKey> keySelector,
  1718. IEqualityComparer<TKey> comparer)
  1719. {
  1720. return ToDictionary<TSource, TKey, TSource> (source, keySelector, (e) => e, comparer);
  1721. }
  1722. public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey> (this ParallelQuery<TSource> source,
  1723. Func<TSource, TKey> keySelector)
  1724. {
  1725. return ToDictionary<TSource, TKey, TSource> (source, keySelector, (e) => e, EqualityComparer<TKey>.Default);
  1726. }
  1727. public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
  1728. Func<TSource, TKey> keySelector,
  1729. Func<TSource, TElement> elementSelector)
  1730. {
  1731. return ToDictionary<TSource, TKey, TElement> (source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
  1732. }
  1733. public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
  1734. Func<TSource, TKey> keySelector,
  1735. Func<TSource, TElement> elementSelector,
  1736. IEqualityComparer<TKey> comparer)
  1737. {
  1738. if (source == null)
  1739. throw new ArgumentNullException ("source");
  1740. if (keySelector == null)
  1741. throw new ArgumentNullException ("keySelector");
  1742. if (comparer == null)
  1743. comparer = EqualityComparer<TKey>.Default;
  1744. if (elementSelector == null)
  1745. throw new ArgumentNullException ("elementSelector");
  1746. var helper = new DictionaryAggregateHelper<TSource, TKey, TElement> (comparer, keySelector, elementSelector);
  1747. return source.Aggregate (helper.Seed,
  1748. helper.Intermediate,
  1749. helper.Reducer,
  1750. helper.Final);
  1751. }
  1752. class DictionaryAggregateHelper<TSource, TKey, TElement>
  1753. {
  1754. IEqualityComparer<TKey> comparer;
  1755. Func<TSource, TKey> keySelector;
  1756. Func<TSource, TElement> elementSelector;
  1757. public DictionaryAggregateHelper (IEqualityComparer<TKey> comparer,
  1758. Func<TSource, TKey> keySelector,
  1759. Func<TSource, TElement> elementSelector)
  1760. {
  1761. this.comparer = comparer;
  1762. this.keySelector = keySelector;
  1763. this.elementSelector = elementSelector;
  1764. }
  1765. public Dictionary<TKey, TElement> Seed ()
  1766. {
  1767. return new Dictionary<TKey, TElement> (comparer);
  1768. }
  1769. public Dictionary<TKey, TElement> Intermediate (Dictionary<TKey, TElement> d, TSource e)
  1770. {
  1771. d.Add (keySelector (e), elementSelector (e));
  1772. return d;
  1773. }
  1774. public Dictionary<TKey, TElement> Reducer (Dictionary<TKey, TElement> d1, Dictionary<TKey, TElement> d2)
  1775. {
  1776. foreach (var couple in d2) d1.Add (couple.Key, couple.Value);
  1777. return d1;
  1778. }
  1779. public Dictionary<TKey, TElement> Final (Dictionary<TKey, TElement> d)
  1780. {
  1781. return d;
  1782. }
  1783. }
  1784. public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this ParallelQuery<TSource> source,
  1785. Func<TSource, TKey> keySelector)
  1786. {
  1787. return ToLookup<TSource, TKey, TSource> (source, keySelector, (e) => e, EqualityComparer<TKey>.Default);
  1788. }
  1789. public static ILookup<TKey, TSource> ToLookup<TSource, TKey> (this ParallelQuery<TSource> source,
  1790. Func<TSource, TKey> keySelector,
  1791. IEqualityComparer<TKey> comparer)
  1792. {
  1793. return ToLookup<TSource, TKey, TSource> (source, keySelector, (e) => e, comparer);
  1794. }
  1795. public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
  1796. Func<TSource, TKey> keySelector,
  1797. Func<TSource, TElement> elementSelector)
  1798. {
  1799. return ToLookup<TSource, TKey, TElement> (source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
  1800. }
  1801. public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement> (this ParallelQuery<TSource> source,
  1802. Func<TSource, TKey> keySelector,
  1803. Func<TSource, TElement> elementSelector,
  1804. IEqualityComparer<TKey> comparer)
  1805. {
  1806. if (source == null)
  1807. throw new ArgumentNullException ("source");
  1808. if (keySelector == null)
  1809. throw new ArgumentNullException ("keySelector");
  1810. if (comparer == null)
  1811. comparer = EqualityComparer<TKey>.Default;
  1812. if (elementSelector == null)
  1813. throw new ArgumentNullException ("elementSelector");
  1814. ConcurrentLookup<TKey, TElement> lookup = new ConcurrentLookup<TKey, TElement> (comparer);
  1815. source.ForAll ((e) => lookup.Add (keySelector (e), elementSelector (e)));
  1816. return lookup;
  1817. }
  1818. #endregion
  1819. #region Concat
  1820. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather than "
  1821. + "System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() extension method "
  1822. + "to convert the right data source to System.Linq.ParallelQuery<T>.")]
  1823. public static ParallelQuery<TSource> Concat<TSource>(this ParallelQuery<TSource> first,
  1824. IEnumerable<TSource> second)
  1825. {
  1826. throw new NotSupportedException ();
  1827. }
  1828. public static ParallelQuery<TSource> Concat<TSource> (this ParallelQuery<TSource> first, ParallelQuery<TSource> second)
  1829. {
  1830. return new ParallelQuery<TSource> (new QueryConcatNode<TSource> (first.Node, second.Node));
  1831. }
  1832. #endregion
  1833. #region DefaultIfEmpty
  1834. public static ParallelQuery<TSource> DefaultIfEmpty<TSource> (this ParallelQuery<TSource> source)
  1835. {
  1836. return source.DefaultIfEmpty (default (TSource));
  1837. }
  1838. public static ParallelQuery<TSource> DefaultIfEmpty<TSource> (this ParallelQuery<TSource> source, TSource defaultValue)
  1839. {
  1840. return new ParallelQuery<TSource> (new QueryDefaultEmptyNode<TSource> (source.Node, defaultValue));
  1841. }
  1842. #endregion
  1843. #region First
  1844. public static TSource First<TSource> (this ParallelQuery<TSource> source)
  1845. {
  1846. CancellationTokenSource src = new CancellationTokenSource ();
  1847. IEnumerator<TSource> enumerator = source.WithImplementerToken (src).GetEnumerator ();
  1848. if (enumerator == null || !enumerator.MoveNext ())
  1849. throw new InvalidOperationException ("source contains no element");
  1850. TSource result = enumerator.Current;
  1851. src.Cancel ();
  1852. enumerator.Dispose ();
  1853. return result;
  1854. }
  1855. public static TSource First<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  1856. {
  1857. return source.Where (predicate).First ();
  1858. }
  1859. public static TSource FirstOrDefault<TSource> (this ParallelQuery<TSource> source)
  1860. {
  1861. return source.DefaultIfEmpty ().First ();
  1862. }
  1863. public static TSource FirstOrDefault<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  1864. {
  1865. return source.Where (predicate).FirstOrDefault ();
  1866. }
  1867. #endregion
  1868. #region Last
  1869. public static TSource Last<TSource> (this ParallelQuery<TSource> source)
  1870. {
  1871. return source.Reverse ().First ();
  1872. }
  1873. public static TSource Last<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  1874. {
  1875. return source.Reverse ().First (predicate);
  1876. }
  1877. public static TSource LastOrDefault<TSource> (this ParallelQuery<TSource> source)
  1878. {
  1879. return source.Reverse ().FirstOrDefault ();
  1880. }
  1881. public static TSource LastOrDefault<TSource> (this ParallelQuery<TSource> source, Func<TSource, bool> predicate)
  1882. {
  1883. return source.Reverse ().FirstOrDefault (predicate);
  1884. }
  1885. #endregion
  1886. #region Zip
  1887. public static ParallelQuery<TResult> Zip<TFirst, TSecond, TResult> (this ParallelQuery<TFirst> first,
  1888. ParallelQuery<TSecond> second,
  1889. Func<TFirst, TSecond, TResult> resultSelector)
  1890. {
  1891. if (first == null)
  1892. throw new ArgumentNullException ("first");
  1893. if (second == null)
  1894. throw new ArgumentNullException ("second");
  1895. if (resultSelector == null)
  1896. throw new ArgumentNullException ("resultSelector");
  1897. return new ParallelQuery<TResult> (new QueryZipNode<TFirst, TSecond, TResult> (resultSelector, first.Node, second.Node));
  1898. }
  1899. [ObsoleteAttribute("The second data source of a binary operator must be of type System.Linq.ParallelQuery<T> rather "
  1900. + "than System.Collections.Generic.IEnumerable<T>. To fix this problem, use the AsParallel() "
  1901. + "extension method to convert the right data source to System.Linq.ParallelQuery<T>.")]
  1902. public static ParallelQuery<TResult> Zip<TFirst, TSecond, TResult> (this ParallelQuery<TFirst> first,
  1903. IEnumerable<TSecond> second,
  1904. Func<TFirst, TSecond, TResult> resultSelector)
  1905. {
  1906. throw new NotSupportedException ();
  1907. }
  1908. #endregion
  1909. #region Helpers
  1910. class Identity<T>
  1911. {
  1912. public T Apply (T input)
  1913. {
  1914. return input;
  1915. }
  1916. }
  1917. #endregion
  1918. }
  1919. }