ParallelEnumerable.cs 83 KB

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