ParallelEnumerable.cs 80 KB

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