当前位置: 首页>>代码示例>>C#>>正文


C# ConcurrentDictionary.Aggregate方法代码示例

本文整理汇总了C#中ConcurrentDictionary.Aggregate方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentDictionary.Aggregate方法的具体用法?C# ConcurrentDictionary.Aggregate怎么用?C# ConcurrentDictionary.Aggregate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ConcurrentDictionary的用法示例。


在下文中一共展示了ConcurrentDictionary.Aggregate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Count

        public async Task<HttpResponseMessage> Count()
        {
            // For each partition client, keep track of partition information and the number of words
            ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>();
            IList<Task> tasks = new List<Task>();

            foreach (Int64RangePartitionInformation partition in await this.GetServicePartitionKeysAsync())
            {
                try
                {
                    ServicePartitionClient<HttpCommunicationClient> partitionClient
                        = new ServicePartitionClient<HttpCommunicationClient>(communicationFactory, serviceUri, new ServicePartitionKey(partition.LowKey));

                    await partitionClient.InvokeWithRetryAsync(
                        async (client) =>
                        {
                            HttpResponseMessage response = await client.HttpClient.GetAsync(new Uri(client.Url, "Count"));
                            string content = await response.Content.ReadAsStringAsync();
                            totals[partition] = Int64.Parse(content.Trim());
                        });
                }
                catch (Exception ex)
                {
                    // Sample code: print exception
                    ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request");
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<h1> Total:");
            sb.Append(totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total));
            sb.Append("</h1>");
            sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>");
            foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey))
            {
                sb.Append("<tr><td>");
                sb.Append(partitionData.Key.Id);
                sb.Append("</td><td>");
                sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey);
                sb.Append("</td><td>");
                sb.Append(partitionData.Value);
                sb.Append("</td></tr>");
            }

            sb.Append("</table>");

            return new HttpResponseMessage()
            {
                Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html")
            };
        }
开发者ID:smartpcr,项目名称:service-fabric-dotnet-getting-started,代码行数:51,代码来源:DefaultController.cs

示例2: Reproduction

 public KeyValuePair<IEnumerable<int>, double> Reproduction()
 {
     // Normalize, accumulate and order fitness by descending
     var fitnessSum = population.Sum(pair => pair.Value);
     var accumulator = 0.0;
     var roulette = (from pair in population orderby pair.Value descending select new { Chromosome = pair.Key, Fitness = pair.Value, AccumulatudFitness = (accumulator += pair.Value / fitnessSum) }).ToArray();
     // Count of children pairs to be produced
     var childrenPairs = (int)(populationSize * (1 - elitismRate) / 2);
     // Take count of elite chromosomes
     population = new ConcurrentDictionary<IEnumerable<int>, double>(roulette.Take(populationSize - childrenPairs * 2).ToDictionary(pair => pair.Chromosome, pair => pair.Fitness));
     // Produce children and fill population with them
     Parallel.For(0, childrenPairs, _ =>
     {
         foreach (var child in from chromosome in Crossover(roulette.First(pair => pair.AccumulatudFitness >= random.NextDouble()).Chromosome, roulette.First(pair => pair.AccumulatudFitness >= random.NextDouble()).Chromosome) select Mutation(chromosome))
             population[child] = Fitness(child);
     });
     // Return fittest cluster
     return population.Aggregate((max, next) => next.Value > max.Value ? next : max);
 }
开发者ID:sergey-podolsky,项目名称:university,代码行数:19,代码来源:GA.cs

示例3: Count

        public async Task<HttpResponseMessage> Count()
        {
            // Get the list of representative service partition clients.
            IList<ServicePartitionClient<CommunicationClient>> partitionClients = await this.GetServicePartitionClientsAsync();

            // For each partition client, keep track of partition information and the number of words
            ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>();
            IList<Task> tasks = new List<Task>(partitionClients.Count);
            foreach (ServicePartitionClient<CommunicationClient> partitionClient in partitionClients)
            {
                // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy.
                tasks.Add(
                    partitionClient.InvokeWithRetryAsync(
                        client =>
                        {
                            Uri serviceAddress = new Uri(client.BaseAddress, "Count");

                            HttpWebRequest request = WebRequest.CreateHttp(serviceAddress);
                            request.Method = "GET";
                            request.Timeout = (int) client.OperationTimeout.TotalMilliseconds;
                            request.ReadWriteTimeout = (int) client.ReadWriteTimeout.TotalMilliseconds;

                            using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
                            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                            {
                                totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim());
                            }

                            return Task.FromResult(true);
                        }));
            }

            try
            {
                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                // Sample code: print exception
                ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request");
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<h1> Total:");
            sb.Append(totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total));
            sb.Append("</h1>");
            sb.Append("<table><tr><td>Partition ID</td><td>Key Range</td><td>Total</td></tr>");
            foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey))
            {
                sb.Append("<tr><td>");
                sb.Append(partitionData.Key.Id);
                sb.Append("</td><td>");
                sb.AppendFormat("{0} - {1}", partitionData.Key.LowKey, partitionData.Key.HighKey);
                sb.Append("</td><td>");
                sb.Append(partitionData.Value);
                sb.Append("</td></tr>");
            }

            sb.Append("</table>");

            HttpResponseMessage message = new HttpResponseMessage();
            message.Content = new StringContent(sb.ToString(), Encoding.UTF8, "text/html");
            return message;
        }
开发者ID:oanapl,项目名称:servicefabric-samples-1,代码行数:64,代码来源:DefaultController.cs

示例4: Count

        public async Task<CountResponse> Count()
        {
            // Get the list of representative service partition clients.
            IList<ServicePartitionClient<CommunicationClient>> partitionClients = 
                await this.GetServicePartitionClientsAsync();

            // For each partition client, keep track of partition information and the number of words
            ConcurrentDictionary<Int64RangePartitionInformation, long> totals = new ConcurrentDictionary<Int64RangePartitionInformation, long>();
            IList<Task> tasks = new List<Task>(partitionClients.Count);
            foreach (ServicePartitionClient<CommunicationClient> partitionClient in partitionClients)
            {
                // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy.
                tasks.Add(
                    partitionClient.InvokeWithRetryAsync(
                        client =>
                        {
                            Uri serviceAddress = new Uri(client.BaseAddress, "Count");

                            HttpWebRequest request = WebRequest.CreateHttp(serviceAddress);
                            request.Method = "GET";
                            request.Timeout = (int)client.OperationTimeout.TotalMilliseconds;
                            request.ReadWriteTimeout = (int)client.ReadWriteTimeout.TotalMilliseconds;

                            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                            {
                                totals[client.ResolvedServicePartition.Info as Int64RangePartitionInformation] = Int64.Parse(reader.ReadToEnd().Trim());
                            }

                            return Task.FromResult(true);
                        }));
            }

            try
            {
                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                // Sample code: print exception
                ServiceEventSource.Current.OperationFailed(ex.Message, "Count - run web request");
            }

            var retVal = new CountResponse();

            retVal.Total = totals.Aggregate<KeyValuePair<Int64RangePartitionInformation, long>, long>(0, (total, next) => next.Value + total);
            foreach (KeyValuePair<Int64RangePartitionInformation, long> partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey))
            {
                var info = new Info();

                info.Id = partitionData.Key.Id;
                info.LowKey = partitionData.Key.LowKey;
                info.HighKey = partitionData.Key.HighKey;
                info.Hits = partitionData.Value;

                retVal.Infos.Add(info);
            }

            return retVal;
        }
开发者ID:alibaloch,项目名称:service-fabric-dotnet-getting-started,代码行数:60,代码来源:DefaultController.cs

示例5: GetHitsCount

        public async Task<HitsCountResponse> GetHitsCount()
        {
            // Get the list of representative service partition clients.
            var partitionClients = await this.GetServicePartitionClientsAsync();

            // For each partition client, keep track of partition information and the number of words
            var totals = new ConcurrentDictionary<Int64RangePartitionInformation, Task<long>>();
            IList<Task> tasks = new List<Task>(partitionClients.Count);
            foreach (var partitionClient in partitionClients)
            {
                // partitionClient internally resolves the address and retries on transient errors based on the configured retry policy.
                Task<long> tt = partitionClient.beanCache.GetHitsCount();
                tasks.Add(tt);
                totals[partitionClient.part as Int64RangePartitionInformation] = tt;
            }
            try
            {
                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                // Sample code: print exception
                ServiceEventSource.Current.Message(ex.Message, "Count - run web request");
                throw ex;
            }

            var response = new HitsCountResponse();
            response.Total = totals.Aggregate(0, (total, next) => (int)next.Value.Result + total);
 
            foreach (var partitionData in totals.OrderBy(partitionData => partitionData.Key.LowKey))
            {
                var cachInfo = new CacheInfo();

                cachInfo.Id = partitionData.Key.Id;
                cachInfo.LowKey = partitionData.Key.LowKey;
                cachInfo.HighKey = partitionData.Key.HighKey;
                cachInfo.Hits = partitionData.Value.Result;

                response.CacheInfo.Add(cachInfo);
            }
            return response;
        }
开发者ID:pveerath,项目名称:BeanCache,代码行数:42,代码来源:CacheManagementController.cs


注:本文中的ConcurrentDictionary.Aggregate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。