本文整理汇总了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")
};
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}