ParallelEnumerable.cs 81 KB

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