ParallelEnumerable.cs 80 KB

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