本文整理汇总了C#中ConcurrentDictionary.ContainsKey方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentDictionary.ContainsKey方法的具体用法?C# ConcurrentDictionary.ContainsKey怎么用?C# ConcurrentDictionary.ContainsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentDictionary
的用法示例。
在下文中一共展示了ConcurrentDictionary.ContainsKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldBeAbleToPullExistingInfoFromCache
public void ShouldBeAbleToPullExistingInfoFromCache()
{
var monitorConfig = new MonitorConfig { Name = "Test" };
var reduceLevels = new List<ReduceLevel>();
var connection = new Mock<IDbConnection>();
var connectionInstance = connection.Object;
var storageCommands = new Mock<IStorageCommands>();
storageCommands.Setup(x => x.CreateConfigAndReduceLevels(monitorConfig, reduceLevels, connectionInstance));
var setupSystemTables = new Mock<ISetupSystemTables>();
setupSystemTables.Setup(x => x.ValidateAndCreateDataTables(connectionInstance)).Verifiable();
var monitorConfigsDictionary = new ConcurrentDictionary<string, MonitorConfig>();
var cache = new Mock<IDataCache>();
cache.SetupGet(x => x.MonitorConfigs).Returns(monitorConfigsDictionary).Verifiable();
var storageFactory = new Mock<IStorageFactory>();
storageFactory.Setup(x => x.CreateConnection()).Returns(connectionInstance).Verifiable();
var settings = BuildSettings();
var defaults = new SetupMonitorConfig(storageCommands.Object, setupSystemTables.Object, cache.Object, storageFactory.Object, settings.Object);
defaults.CreateDefaultReduceLevels(monitorConfig, reduceLevels);
Assert.Equal(1, monitorConfigsDictionary.Count);
Assert.True(monitorConfigsDictionary.ContainsKey("Test"));
storageCommands.VerifyAll();
setupSystemTables.VerifyAll();
storageFactory.VerifyAll();
cache.VerifyAll();
}
示例2: TestLockFreeDictionary
public void TestLockFreeDictionary()
{
IDictionary<int, Guid> dict = new ConcurrentDictionary<int, Guid>();
KeyValuePair<int, Guid> test = new KeyValuePair<int, Guid>(-64, Guid.NewGuid());
dict.Add(42, Guid.NewGuid());
dict.Add(22, Guid.NewGuid());
dict.Add(test);
dict.Add(55, Guid.NewGuid());
Assert.IsTrue(dict.ContainsKey(-64));
Assert.IsTrue(dict.Contains(test));
Assert.IsFalse(dict.Contains(new KeyValuePair<int, Guid>(-64, new Guid())));
dict[-64] = Guid.NewGuid();
Assert.IsFalse(dict.Contains(test));
Guid newID = Guid.NewGuid();
dict[12] = newID;
Guid id = dict[12];
Assert.IsTrue(newID == id);
Assert.IsTrue(dict.Count == 5);
dict.Remove(-64);
Assert.IsTrue(dict.Count == 4);
}
示例3: GetOrAdd
public static Task<string> GetOrAdd(string title, string language, string uri)
{
Lazy<Task<string>> lazyTask = new Lazy<Task<string>>(
() =>
{
var uriSubCache = new ConcurrentDictionary<string, Task<string>>();
var languageSubCache = new ConcurrentDictionary<string, ConcurrentDictionary<string, Task<string>>>();
if (!uriSubCache.ContainsKey(uri))
{
uriSubCache.TryAdd(uri, GetInfoAsync(uri));
}
if (!languageSubCache.ContainsKey(language))
{
languageSubCache.TryAdd(language, uriSubCache);
}
var returnTask = Cache.GetOrAdd(title, languageSubCache)
.GetOrAdd(language, uriSubCache)
.GetOrAdd(uri, GetInfoAsync(uri));
return returnTask;
}
, LazyThreadSafetyMode.PublicationOnly
);
return lazyTask.Value;
}
开发者ID:luismdcp,项目名称:PROMPT11-07-ConcurrentProgramming.luismdcp.TrabalhoFinal,代码行数:29,代码来源:ConcurrentCache.cs
示例4: AddStatements
public void AddStatements(ConcurrentQueue<CoveredStatement> coveredStatements, ConcurrentDictionary<int, string> objectNameCache)
{
while (!coveredStatements.IsEmpty)
{
CoveredStatement statement;
if (coveredStatements.TryDequeue(out statement))
{
if (!objectNameCache.ContainsKey(statement.ObjectId))
continue;
var name = objectNameCache[statement.ObjectId].ToLowerInvariant();
if (!_statements.ContainsKey(name))
{
_statements[name] = new List<CoveredStatement>();
}
var statments = _statements[name];
if (statments.All(p => p.Offset != statement.Offset))
{
statments.Add(statement);
}
else
{
statments.Remove(statments.First(p => p.Offset == statement.Offset));
statments.Add(statement);
}
}
}
}
示例5: MostFunnyUsers
public JsonResult MostFunnyUsers()
{
using (GagsDbContext model = new GagsDbContext())
{
var allGags = model.Gags.Include("Owner");
ConcurrentDictionary<Guid, int> counters = new ConcurrentDictionary<Guid, int>();
foreach (var gag in allGags)
{
if (!counters.ContainsKey(gag.Owner.Id))
counters[gag.Owner.Id] = 0;
counters[gag.Owner.Id]++;
}
var top10 = counters.OrderByDescending(x => x.Value).Take(10);
var context = new List<StatsModel>(10);
foreach (var user in top10)
{
context.Add(new StatsModel()
{
Name = model.Users.Where(x => x.Id == user.Key).First().Nickname,
Value = user.Value
});
}
JsonResult result = new JsonResult() { Data = context, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
return result;
}
}
示例6: Queue_WithHighThreadCount_WorksCorrectly
public void Queue_WithHighThreadCount_WorksCorrectly(int threads, int enqueues, int dequeues)
{
//The elements we insert are unique and sentinel value is -1
int initialValue = -1;
int currentValue = initialValue;
ConcurrentDictionary<int, int> table = new ConcurrentDictionary<int, int>();
Core.Queue.Queue<int> queue = new Core.Queue.Queue<int>(initialValue);
ThreadBuilder
.Empty()
.AddThreads(() =>
{
for (int i = 0; i < enqueues; i++)
{
int value = Interlocked.Increment(ref currentValue);
queue.Enqueue(value);
}
}, threads)
.AddThreads(() =>
{
for (int i = 0; i < dequeues; i++)
{
int value = queue.Dequeue();
table.AddOrUpdate(value, x => 1, (k, v) => v + 1);
}
}, threads)
.Start();
//The sentinel value can be returned more than once if queue is empty at the time of a pop
int expectedPops = table.Keys.Count + (table.ContainsKey(initialValue) ? -1 : 0);
int actualPops = table.Count(x => x.Key != initialValue && x.Value == 1);
Assert.AreEqual(expectedPops, actualPops);
}
示例7: ContainsKey_ReturnsFalseWhenKeyIsNotPresent
public void ContainsKey_ReturnsFalseWhenKeyIsNotPresent()
{
// Arrange
ConcurrentDictionary<int, int> dictionary = new ConcurrentDictionary<int, int>();
// Act & Assert
Assert.False(dictionary.ContainsKey(3));
}
示例8: DeserializeEnum_WithCache_EnumsAddedToCache
public void DeserializeEnum_WithCache_EnumsAddedToCache()
{
const int YourvalueInt = 123;
const int MyValueInt = 99;
var cache = new ConcurrentDictionary<string, FakeTestingEnum>();
FakeTestingEnum yourvalueEnum =
PrettyEnumHelpers<FakeTestingEnum>.DeserializeEnum(YourvalueInt.ToString(), cache);
FakeTestingEnum myvalueEnum = PrettyEnumHelpers<FakeTestingEnum>.DeserializeEnum(
MyValueInt.ToString(), cache);
Assert.Equal(FakeTestingEnum.YourValue, yourvalueEnum);
Assert.Equal(FakeTestingEnum.MyValue, myvalueEnum);
Assert.Equal(2, cache.Count);
Assert.True(cache.ContainsKey(YourvalueInt.ToString()));
Assert.True(cache.ContainsKey(MyValueInt.ToString()));
}
示例9: DeserializeEnum_WithCacheAndEmptyString_DefaultEnumAndAddedToCache
public void DeserializeEnum_WithCacheAndEmptyString_DefaultEnumAndAddedToCache()
{
var cache = new ConcurrentDictionary<string, FakeTestingEnum>();
FakeTestingEnum deserializedEnum = PrettyEnumHelpers<FakeTestingEnum>.DeserializeEnum(
string.Empty, cache);
Assert.Equal(default(FakeTestingEnum), deserializedEnum);
Assert.Equal(1, cache.Count);
Assert.True(cache.ContainsKey(string.Empty));
}
示例10: ContainsKey_KeyDoesntExist_Int64
public void ContainsKey_KeyDoesntExist_Int64()
{
var dictionary = new ConcurrentDictionary<long, long>(Enumerable.Range(0, Iterations).Select(x => (long)x).ToDictionary(x => x));
var stopwatch = Stopwatch.StartNew();
for (long i = -1; i >= -Iterations; i--)
{
dictionary.ContainsKey(-1);
}
stopwatch.StopAndLog(Iterations);
}
示例11: ContainsKey_KeyDoesntExist_Int32
public void ContainsKey_KeyDoesntExist_Int32()
{
var dictionary = new ConcurrentDictionary<int, int>(Enumerable.Range(0, Iterations).ToDictionary(x => x));
var stopwatch = Stopwatch.StartNew();
for (var i = -1; i >= -Iterations; i--)
{
dictionary.ContainsKey(-1);
}
stopwatch.StopAndLog(Iterations);
}
示例12: DeserializeEnum_WithCacheAndWhiteSpaceString_DefaultEnumAndAddedToCache
public void DeserializeEnum_WithCacheAndWhiteSpaceString_DefaultEnumAndAddedToCache()
{
const string Whitespace = " \r \t ";
var cache = new ConcurrentDictionary<string, FakeTestingEnum>();
FakeTestingEnum deserializedEnum = PrettyEnumHelpers<FakeTestingEnum>.DeserializeEnum(Whitespace, cache);
Assert.Equal(default(FakeTestingEnum), deserializedEnum);
Assert.Equal(1, cache.Count);
Assert.True(cache.ContainsKey(Whitespace));
}
示例13: ContainsKey_ReturnsTrueWhenKeyIsPresent
public void ContainsKey_ReturnsTrueWhenKeyIsPresent()
{
// Arrange
ConcurrentDictionary<int, int> dictionary = new ConcurrentDictionary<int, int>();
// Act
dictionary.TryAdd(1, 2);
// Assert
Assert.True(dictionary.ContainsKey(1));
}
示例14: Cache
protected static async Task Cache(string url, ConcurrentDictionary<string, Page> cache)
{
if (!cache.ContainsKey(url))
{
Page page = await Downloader.DownloadAsync(url);
cache.TryAdd(url, page);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Downloaded: {0}", url);
}
}
示例15: Execute
public override ConfusionMatrix Execute()
{
var richTesting = testSet;
ConcurrentDictionary<double, ActivationNetwork> networks = new ConcurrentDictionary<double, ActivationNetwork>();
Parallel.For(0, 2000, (int i) =>
{
//Create an activation network
ThreadLocal<ActivationNetwork> network = new ThreadLocal<ActivationNetwork>(() =>
{
return new ActivationNetwork(new SigmoidFunction(2), 2, 2, 1);
});
ThreadLocal<ResilientBackpropagationLearning> teachear = new ThreadLocal<ResilientBackpropagationLearning>(() =>
{
return new ResilientBackpropagationLearning(network.Value);
});
ThreadLocal<int> iter = new ThreadLocal<int>(() => { return 0; });
ThreadLocal<double> error = new ThreadLocal<double>(() => { return 0; });
while (networks.IsEmpty)
{
error.Value = teachear.Value.RunEpoch(trainingSet, trainingOutput);
iter.Value++;
if (iter.Value == 1000) break;
}
if (!networks.ContainsKey(error.Value)) networks.TryAdd(error.Value, network.Value);
}
);
Dictionary<ConfusionMatrix, ActivationNetwork> cms = new Dictionary<ConfusionMatrix, ActivationNetwork>();
foreach (var keyv in networks)
{
var p = richTesting
.Select(x => keyv.Value.Compute(x))
.Select(x => Convert.ToInt32(x[0]))
.ToArray();
ConfusionMatrix cm = new ConfusionMatrix(p, expected, POSITIVE, NEGATIVE);
cms.Add(cm, keyv.Value);
}
var kv = (from x in cms
orderby x.Key.Accuracy descending
select x).First();
var neurons = from neuron in kv.Value.Layers[0].Neurons
select neuron as ActivationNeuron;
OnAlgorithmEnded(neurons, kv.Key);
return kv.Key;
}