ParallelEnumerable.cs 86 KB

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