/// <summary> /// Method to find file references (more specific in the using context of this method: file references to copybooks) without a given parent folder. /// Because there is no distinct location given, the selection of the file reference is based on a searching pattern: /// If there is only one file with the given name in the cache, it's selected. If there are more than one, it's checked if only one of them ends /// with the extension *.cbl. If that is true, this file reference is selected, if not an exception is thrown. This method could also search directly /// for the filename + extension but that would cut out the possibility of loading files with functionally not correct extension like *.txt (in case their /// name is unique). /// </summary> /// <param name="programName">The name of the program the file reference is wanted for.</param> /// <returns>The file reference for the program.</returns> public FileReference GetFileReferenceWithoutKnownFolderName(string programName) { if (string.IsNullOrWhiteSpace(programName)) { return(null); } //Selecting all files with the programName in the file cache ignoring the file extension. ParallelQuery <FileReference> allFileReferencesWithGivenName = _files.AsParallel().Where(file => file.Key.Contains(programName + ".")).Select(file => file.Value); //If more than one file of that name is found, a more specific search is done, icluding the file extension. if (allFileReferencesWithGivenName.Count() > 1) { allFileReferencesWithGivenName = _files.AsParallel().Where(file => file.Key.Contains(programName + ".cbl")).Select(file => file.Value); } if (allFileReferencesWithGivenName.Count() > 1) { //If there is still more than one file, an exception is thrown, stating the fact, that a distinct file selection is impossible. throw new CopiedRessourceNotIdentifiedDistinctlyByNameException(programName); } else { //Else the found reference is returned. return(allFileReferencesWithGivenName.First()); } }
public static void Count_All(Labeled <ParallelQuery <int> > labeled, int count) { ParallelQuery <int> query = labeled.Item; Assert.Equal(count, query.Count()); Assert.Equal(count, query.Count(i => i < count)); }
public void UnorderedTakeTestCase() { ParallelTestHelper.Repeat(() => { ParallelQuery <int> async = baseEnumerable.AsReallyParallel().Take(2000); IEnumerable <int> sync = baseEnumerable.Take(2000); Assert.AreEqual(sync.Count(), async.Count(), "#1"); async = baseEnumerable.AsReallyParallel().Take(100); sync = baseEnumerable.Take(100); Assert.AreEqual(sync.Count(), async.Count(), "#2"); }, 20); }
static void Main(string[] args) { byte [] array = new byte [1000000000]; Random random = new Random(); for (int i = 0; i < array.Length; i++) { array[i] = (byte)random.Next(0, 100); } Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); ParallelQuery <byte> numbers = from number in array.AsParallel() where (number % 2) == 1 select number; int count = numbers.Count(); stopwatch.Stop(); //foreach (int number in numbers) // Console.Write(number + " "); Console.WriteLine($"PLINQ has been working for {stopwatch.ElapsedMilliseconds} ms and it found {count} odd numbers"); Console.ReadKey(); }
execute( KMeansOptions options, IEnumerable <Vector> vectors, ref Vector[] centers, out int[] clusterIds, int nParallelDegree = 1, bool bAvoidLazyEval = true ) { ParallelQuery <Vector> result = null; m_pStopwatch.Start(); m_nStartParallelPhase = m_pStopwatch.ElapsedMilliseconds; result = PLINQKMeans.Steps(vectors.AsParallel().WithDegreeOfParallelism(nParallelDegree), centers.AsParallel().WithDegreeOfParallelism(nParallelDegree), options.m_nMaxIterations, nParallelDegree, bAvoidLazyEval); result.Count(); m_pStopwatch.Stop(); m_nEndParallelPhase = m_pStopwatch.ElapsedMilliseconds; clusterIds = null; centers = result.ToArray(); return(options.m_nMaxIterations); }
public static void Process <T>(string opName, ParallelQuery <T> opQueue) { if (!_Updating) { OnAbort(); return; } CMOptions.ToConsole("Processing {0}...", opName); int c = opQueue.Count(); List <T> list = VitaNexCore.TryCatchGet(opQueue.Take(c).ToList); while (list.Count > 0) { if (World.Loading) { Thread.Sleep(10); continue; } if (World.Saving) { World.WaitForWriteCompletion(); Thread.Sleep(10); continue; } if (!_Updating) { OnAbort(); break; } int cur = ExportQueue.Values.Sum(l => l.Count); int count = Math.Max(0, Math.Min(CMOptions.QueueCapacity - cur, list.Count)); Parallel.ForEach(list.Take(count), Enqueue); list.RemoveRange(0, count); list.TrimExcess(); cur += count; if (cur >= CMOptions.QueueCapacity) { Flush(); } Thread.Sleep(0); } list.Clear(); list.TrimExcess(); }
/// <summary> /// Get all stream segments which intersect with the current position -> position + count /// </summary> /// <param name="count">Number of bytes from the current position</param> /// <returns>Parallel query of stream segments which intersect</returns> private ParallelQuery <StreamSegment> GetIntersectingStreams(int count) { ParallelQuery <StreamSegment> intersections = _carrierStreams .AsParallel() .Where(i => i.Intersects(Position, Position + count)); _logger.Debug(h => h("GetIntersectingStreams: Count = {0}, Intersection Count = {1}", count, intersections.Count())); return(intersections); }
public static void DefaultIfEmpty_Empty<T>(Labeled<ParallelQuery<T>> labeled, T def) { ParallelQuery<T> notEmpty = labeled.Item.DefaultIfEmpty(); Assert.NotEmpty(notEmpty); Assert.Equal(1, notEmpty.Count()); Assert.Single(notEmpty, default(T)); ParallelQuery<T> specified = labeled.Item.DefaultIfEmpty(def); Assert.NotEmpty(specified); Assert.Equal(1, specified.Count()); Assert.Single(specified, def); }
static double CalculateStandardDeviation(CancellationToken ct, double mean) { return(_inputIntegers.AsParallel().WithCancellation(ct).Aggregate(0d, (subTotal, thisNumber) => subTotal + Math.Pow((thisNumber - mean), 2), (total, thisTask) => total + thisTask, finalSum => Math.Sqrt((finalSum / (_inputIntegers.Count() - 1))))); }
/// <summary> /// 标准偏差 /// </summary> /// <param name="ct"></param> /// <param name="mean"></param> /// <returns></returns> private static double CalculateStandardDeviation(System.Threading.CancellationToken ct, double mean) { return(inputIntegers.AsParallel().WithCancellation(ct).Aggregate ( //Seed 0d, // Update accumulator function (subTotal, thisNumber) => subTotal + Math.Pow((thisNumber - mean), 2), //Combine accumlators function (total, thisTask) => total + thisTask, //Result selector ((finalSum) => Math.Sqrt((finalSum / inputIntegers.Count() - 1))) )); }
/// <summary> /// Does the current iterator have another batch of data to process? /// </summary> /// <returns>true if more data can be accessed via <code>CurrentBatch</code></returns> /// <remarks> /// Creates a new database context, issues a query, and places a materialized collection in <code>CurrentBatch</code>. /// Context is disposed once the query is issued. /// Materialized collection is specified by <code>BuildIQueryable</code>. Use of any associated navigation properties /// must be accounted for by using the appropriate <code>.Include</code> operator where the query is /// built in <code>BuildIQueryable</code>. /// </remarks> public bool HasNext() { using (YourObjectContextHere db = new YourObjectContextHere()) { this.currentBatch = this.BuildIQueryable(db) .OrderBy(this.orderBy) .Skip(this.currentIndex) .Take(this.batchSize) .ToList() .AsParallel(); this.currentIndex += this.batchSize; return(currentBatch.Count() > 0); } }
public virtual ParallelQuery <TEntity> ParallelSelectBottomN(int n) { ObjectSet <TEntity> objectSet = GetObjectSet(); ParallelQuery <TEntity> query1 = from o in objectSet.AsParallel() select o; int count = query1.Count(); int skipIndex = count - n; if (skipIndex < 0) { skipIndex = 0; } ParallelQuery <TEntity> query2 = (from o in query1 orderby 1 select o).Skip(skipIndex).Take(n); return(query2); }
private static void IndirectConsumptionFor(ParallelQuery <ProductResult> f, ModelNode node, Dictionary <Consumable, decimal> dic) { var count = f.Count(t => t.ModelNode == node.ResourceModel.ProcessName); decimal downtime = (from i in SourceMachineStates where i.Machine == node.ResourceModel.ProcessName && i.State == "Down" select i.Total) .FirstOrDefault(); decimal idletime = (from i in SourceMachineStates where i.Machine == node.ResourceModel.ProcessName && i.State == "Idle" select i.Total) .FirstOrDefault(); foreach (State state in node.ResourceModel.States.Where(t => t.Name == "Down" || t.Name == "Idle")) { foreach (Consumption consumption in state.Consumptions) { var res = consumption.Amount / 3600000; if (!consumption.Static) { if (state.Name == "Down") { res += res * downtime; } else if (state.Name == "Idle") { res += res * idletime; } } else { res = res * count; } if (dic.ContainsKey(consumption.Consumable)) { dic[consumption.Consumable] += res; } else { dic.Add(consumption.Consumable, res); } } } }
private decimal IndirectConsumptionFor(ParallelQuery <ProductResult> f, ModelNode node, string index) { var count = f.Count(t => t.ModelNode == node.ResourceModel.ProcessName); decimal downtime = (from i in SourceMachineStates where i.Machine == node.ResourceModel.ProcessName && i.State == "Down" select i.Total) .FirstOrDefault(); decimal idletime = (from i in SourceMachineStates where i.Machine == node.ResourceModel.ProcessName && i.State == "Idle" select i.Total) .FirstOrDefault(); decimal result = 0; foreach (State state in node.ResourceModel.States.Where(t => t.Name == "Down" || t.Name == "Idle")) { foreach (Consumption consumption in state.Consumptions.Where(t => t.Consumable.Name == index)) { var res = consumption.Amount / 3600000; if (!consumption.Static) { if (state.Name == "Down") { res += res * downtime; } else if (state.Name == "Idle") { res += res * idletime; } } else { res = res * count; } result += res; } } return(result); }
public static void Count_None(Labeled <ParallelQuery <int> > labeled, int count) { ParallelQuery <int> query = labeled.Item; Assert.Equal(0, query.Count(i => i == -1)); }
private void RefreshResults() { if (null != mToken) { mToken.Cancel(); } mToken = new CancellationTokenSource(); string sSearch = textBox.Text; Task.Run(() => { try { //Common.Instance.SolutionWatcher.SetNeedRefresh(); ParallelQuery <FileData> source = null; if (mHistoryOnly || string.IsNullOrWhiteSpace(sSearch)) { source = Common.Instance.SolutionWatcher.Files .AsParallel() .WithCancellation(mToken.Token) .Where(fileData => fileData.Status == FileStatus.Recent) ; } else { string[] exts = Common.Instance.Settings.ListedExtensions; source = Common.Instance.SolutionWatcher.Files .AsParallel() .WithCancellation(mToken.Token) .Where(fileData => exts.Any(ext => fileData.File.EndsWith(ext))) ; } int total = source.Count(); IEnumerable <SearchResultData <FileData> > results = source .Select(fileData => new SearchResultData <FileData>(fileData, sSearch, fileData.Path, CommonUtils.ToArray <string>("\\", "/"))) ; if (!string.IsNullOrWhiteSpace(sSearch)) { int searchStringLen = sSearch.Length; results = results.Where(resultData => resultData.SearchScore > searchStringLen); } results = results.OrderByDescending(fileData => fileData.SearchScore) // Sort by score .ThenByDescending(fileData => fileData.Data.RecentIndex) // Sort by last access ; int count = results.Count(); Action <IEnumerable> setMethod = (res) => { listView.ItemsSource = res; CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(listView.ItemsSource); view.GroupDescriptions.Clear(); if (string.IsNullOrWhiteSpace(sSearch)) { PropertyGroupDescription groupDescription = new PropertyGroupDescription("Data.StatusString"); view.GroupDescriptions.Add(groupDescription); } string title = mQuickFileToolWindow.Title; int pos = title.IndexOf(" ["); if (pos != -1) { title = title.Substring(0, pos); } mQuickFileToolWindow.Title = title + " [" + count + "/" + total + "]"; }; Dispatcher.Invoke(setMethod, results.ToList()); } catch (Exception) { } }); }
public static void Count_One(Labeled <ParallelQuery <int> > labeled, int count, int position) { ParallelQuery <int> query = labeled.Item; Assert.Equal(Math.Min(1, count), query.Count(i => i == position)); }
private static void RunAllTests( TestTracker result, ParallelQuery <int> q, bool orderPreserved, string leftOpName, bool leftOrderDefined) { LogTestRun(leftOpName, "All1", orderPreserved); result.MustEqual( q.All(i => i > 100), q.ToArray().Any(i => i > 100)); LogTestRun(leftOpName, "All2", orderPreserved); result.MustEqual( q.All(i => i == 75), q.ToArray().All(i => i == 75)); LogTestRun(leftOpName, "Any1", orderPreserved); result.MustEqual( q.Any(i => i > 100), q.ToArray().Any(i => i > 100)); LogTestRun(leftOpName, "Any2", orderPreserved); result.MustEqual( q.Any(i => i == 75), q.ToArray().Any(i => i == 75)); LogTestRun(leftOpName, "Concat", orderPreserved); result.MustSequenceEqual( q.Concat(q).Concat(new int[] { 1, 2, 3 }.AsParallel()), q.Reverse().Reverse().ToArray().Concat(q.Reverse().Reverse()).Concat(new int[] { 1, 2, 3 }), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "DefaultIfEmpty", orderPreserved); result.MustSequenceEqual( q.DefaultIfEmpty(), q.ToArray().DefaultIfEmpty(), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "ElementAt", orderPreserved); IEnumerable <int> q2 = q.ToArray(); int count1 = q.Count(), count2 = q2.Count(); List <int> list1 = new List <int>(); List <int> list2 = new List <int>(); for (int i = 0; i < count1; i++) { list1.Add(q.ElementAt(i)); } for (int i = 0; i < count2; i++) { list2.Add(q2.ElementAt(i)); } result.MustSequenceEqual(list1, list2, leftOrderDefined); LogTestRun(leftOpName, "Except", orderPreserved); result.MustSequenceEqual( q.Except(Enumerable.Range(90, 50).AsParallel()), q.ToArray().Except(Enumerable.Range(90, 50)), false); LogTestRun(leftOpName, "First", orderPreserved); CheckFirstOrLast( result, q.First(), q.ToArray().First(), leftOrderDefined); LogTestRun(leftOpName, "GroupBy", orderPreserved); result.MustGroupByEqual( q.GroupBy(i => i % 5, (i, e) => new Pair <int, IEnumerable <int> >(i, e)), q.ToArray().GroupBy(i => i % 5, (i, e) => new Pair <int, IEnumerable <int> >(i, e))); LogTestRun(leftOpName, "GroupJoin", orderPreserved); result.MustSequenceEqual( q.GroupJoin(q, i => i, i => i, (i, e) => e.FirstOrDefault()), q.ToArray().GroupJoin(q.ToArray(), i => i, i => i, (i, e) => e.FirstOrDefault()), false); LogTestRun(leftOpName, "Intersect", orderPreserved); result.MustSequenceEqual( q.Intersect(Enumerable.Range(90, 50).AsParallel()), q.ToArray().Intersect(Enumerable.Range(90, 50)), false); LogTestRun(leftOpName, "Join1", orderPreserved); result.MustSequenceEqual( q.Join((new int[] { 1, 1, 2, 3, 3 }).AsParallel(), i => i, i => i, (i, j) => i + j), q.ToArray().Join(new int[] { 1, 1, 2, 3, 3 }, i => i, i => i, (i, j) => i + j), false); LogTestRun(leftOpName, "Join2", orderPreserved); result.MustSequenceEqual( q.Join((new int[] { 1, 1, 100, 3, 3 }).AsParallel(), i => new String('a', i), i => new String('a', i), (i, j) => i + j), q.ToArray().Join(new int[] { 1, 1, 100, 3, 3 }, i => new String('a', i), i => new String('a', i), (i, j) => i + j), false); LogTestRun(leftOpName, "Last", orderPreserved); CheckFirstOrLast( result, q.Last(), q.ToArray().Last(), leftOrderDefined); LogTestRun(leftOpName, "Min", orderPreserved); CheckFirstOrLast( result, q.Min(), q.ToArray().Min(), leftOrderDefined); LogTestRun(leftOpName, "Max", orderPreserved); CheckFirstOrLast( result, q.Min(), q.ToArray().Min(), leftOrderDefined); LogTestRun(leftOpName, "OrderBy-ThenBy", orderPreserved); result.MustSequenceEqual( q.Concat(q).OrderBy(i => i % 5).ThenBy(i => - i), q.ToArray().Concat(q).OrderBy(i => i % 5).ThenBy(i => - i), true); LogTestRun(leftOpName, "OrderByDescending-ThenByDescending", orderPreserved); result.MustSequenceEqual( q.Concat(q).OrderByDescending(i => i % 5).ThenByDescending(i => - i), q.ToArray().Concat(q).OrderByDescending(i => i % 5).ThenByDescending(i => - i), true); LogTestRun(leftOpName, "Reverse", orderPreserved); result.MustSequenceEqual( q.Concat(q).Reverse(), q.ToArray().Concat(q).Reverse(), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "Select", orderPreserved); result.MustSequenceEqual( q.Select(i => 5 * i - 17), q.ToArray().Select(i => 5 * i - 17), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "SelectMany", orderPreserved); result.MustSequenceEqual( q.SelectMany(i => new int[] { 1, 2, 3 }, (i, j) => i + 100 * j), q.ToArray().SelectMany(i => new int[] { 1, 2, 3 }, (i, j) => i + 100 * j), false); LogTestRun(leftOpName, "SequenceEqual", orderPreserved); if (orderPreserved && leftOrderDefined) { result.MustEqual(q.SequenceEqual(q), true); } else { // We don't check the return value as it can be either true or false q.SequenceEqual(q); } LogTestRun(leftOpName, "Skip", orderPreserved); CheckTakeSkip( result, q.Skip(10), q.ToArray().Skip(10), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "SkipWhile", orderPreserved); CheckTakeSkip( result, q.SkipWhile(i => i < 30), q.ToArray().SkipWhile(i => i < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "SkipWhileIndexed", orderPreserved); CheckTakeSkip( result, q.SkipWhile((i, j) => j < 30), q.ToArray().SkipWhile((i, j) => j < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "Take", orderPreserved); CheckTakeSkip( result, q.Take(10), q.ToArray().Take(10), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "TakeWhile", orderPreserved); CheckTakeSkip( result, q.TakeWhile(i => i < 30), q.ToArray().TakeWhile(i => i < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "TakeWhileIndexed", orderPreserved); CheckTakeSkip( result, q.TakeWhile((i, j) => j < 30), q.ToArray().TakeWhile((i, j) => j < 30), leftOrderDefined && orderPreserved); LogTestRun(leftOpName, "Union", orderPreserved); result.MustSequenceEqual( q.Union(Enumerable.Range(90, 50).AsParallel()), q.ToArray().Union(Enumerable.Range(90, 50)), false); LogTestRun(leftOpName, "Where", orderPreserved); result.MustSequenceEqual( q.Where(i => i < 20 || i > 80), q.ToArray().Where(i => i < 20 || i > 80), orderPreserved && leftOrderDefined); LogTestRun(leftOpName, "Zip", orderPreserved); IEnumerable <KeyValuePair <int, int> > zipQ = q.Zip(q, (i, j) => new KeyValuePair <int, int>(i, j)); result.MustSequenceEqual( zipQ.Select(p => p.Key), q.Reverse().Reverse().ToArray(), orderPreserved && leftOrderDefined); result.MustSequenceEqual( zipQ.Select(p => p.Value), q.Reverse().Reverse().ToArray(), orderPreserved && leftOrderDefined); }
private IEnumerable <string> FormatFunctionalParallel(ParallelQuery <string> shoppingList) => shoppingList .Select(StringExtensions.Capitalize) .Zip(ParallelEnumerable.Range(1, shoppingList.Count()), (i, j) => $"{i}. {j}");
private void RefreshResults() { if (null != mToken) { mToken.Cancel(); } mToken = new CancellationTokenSource(); string sSearch = textBox.Text; System.Threading.Tasks.Task.Run(() => { lock (mSymbolLocker) { if (!mToken.IsCancellationRequested) { if (!mSearchInSolution && mSymbols == null) { mSymbols = CTagsGenerator.GeneratorFromDocument(Common.Instance.DTE2.ActiveDocument); } try { //System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); //sw.Start(); ParallelQuery <SymbolData> source = null; if (mSearchInSolution) { source = Common.Instance.SolutionWatcher.Files .AsParallel() .WithCancellation(mToken.Token) .Where(file => file != null && file.Symbols != null) .SelectMany(file => file.Symbols) ; } else { source = mSymbols .AsParallel() .WithCancellation(mToken.Token) ; } ParallelQuery <SearchResultData <SymbolData> > results = source .Where(symbol => (symbol.Type & mSupportedSymbolTypes) != 0) .Select(symbolData => new SearchResultData <SymbolData>(symbolData, sSearch, symbolData.Symbol, null, symbolData.Class, symbolData.Parameters)); int total = results.Count(); if (!string.IsNullOrWhiteSpace(sSearch)) { int searchStringLen = sSearch.Length; results = results.Where(resultData => resultData.SearchScore > searchStringLen); } results = results .OrderByDescending(resultData => resultData.SearchScore) ; int count = results.Count(); //EnvDTE.FontsAndColorsItems fontsAndColor = Common.Instance.DTE2.Properties.Item("FontsAndColorsItems") as EnvDTE.FontsAndColorsItems; //fontsAndColor.Item("Line Number").Foreground //fontsAndColor.Item("Keywords").Foreground /*Action<IEnumerable> refreshMethod = (res) => * { * results.ForAll(result => result.RefreshSearchFormatted()); * }; * Dispatcher.BeginInvoke(refreshMethod, results);*/ Action <IEnumerable> setMethod = (res) => { listView.ItemsSource = res; string title = mQuickMethodToolWindow.Title; int pos = title.IndexOf(" ["); if (pos != -1) { title = title.Substring(0, pos); } mQuickMethodToolWindow.Title = title + " [" + count + "/" + total + "]"; }; Dispatcher.Invoke(setMethod, results.ToList()); //sw.Stop(); //System.Diagnostics.Debug.WriteLine("PLINQ time " + sw.Elapsed.TotalMilliseconds); } catch (Exception) { } } } }); }