本文整理汇总了C#中HashTable.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# HashTable.Contains方法的具体用法?C# HashTable.Contains怎么用?C# HashTable.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HashTable
的用法示例。
在下文中一共展示了HashTable.Contains方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main()
{
const int elementsCount = 30000;
var rand = new Random();
var beforeHash = GC.GetTotalMemory(true);
var hashTest = new HashTable<int, int>();
var stopWatch = new Stopwatch();
stopWatch.Start();
for (var i = 0; i < elementsCount; i++)
{
var currentNum = rand.Next();
hashTest.Add(currentNum, currentNum);
hashTest.Contains(currentNum);
}
stopWatch.Stop();
var afterHash = GC.GetTotalMemory(true);
Console.WriteLine(
"The time needed to add {0} items in the HashTalbe was {1}",
hashTest.Count,
stopWatch.Elapsed);
Console.WriteLine("Current capacity is {0} items", hashTest.Capacity);
Console.WriteLine("{0} kb used", (afterHash - beforeHash) / 1024);
}
示例2: HashTable
private static int HashTable(char[] data)
{
int[] diffs = new int[data.Length];
int diff = 0;
for(int i = 0; i < data.Length; i++)
{
if (LettersAndNumbers.IsNumber(data[i]))
diff++;
else
diff--;
diffs[i] = diff;
}
HashTable<int, int> hashTable = new HashTable<int, int>();
hashTable[0] = -1;
int max = 0;
for(int i = 0; i < diffs.Length; i++)
{
if (hashTable.Contains(diffs[i]))
max = Math.Max(max, i - hashTable[diffs[i]]);
else
hashTable[diffs[i]] = i;
}
return max;
}
示例3: MakeFrequencyTable
public static HashTable<char, int> MakeFrequencyTable(string input)
{
HashTable<char, int> result = new HashTable<char, int>(char.MaxValue, HashChar);
foreach (char c in input){
if (result.Contains(c)) {
result[c] += 1;
}
else {
result.Add(c, 1);
}
}
return result;
}
示例4: HashTableTest
public void HashTableTest()
{
for(int i = 0; i < 10; i++)
{
int[] data = ArrayUtilities.CreateRandomArray(20, 0, 15);
HashTable<int, int> hashTable = new HashTable<int, int>();
for(int j = 0; j < data.Length; j++)
{
hashTable[j] = data[j];
Assert.IsTrue(hashTable.Contains(j));
}
for(int j = 0; j < data.Length; j++)
{
bool contains = hashTable.Contains(j);
bool deleted = hashTable.Delete(j);
Assert.AreEqual(contains, deleted);
Assert.IsFalse(hashTable.Contains(j));
}
}
}
示例5: TestHashTableContains
public void TestHashTableContains()
{
HashTable<string, int> hashTable = new HashTable<string, int>(3, 0.9f);
hashTable.Add("m", 0);
hashTable.Add("b", 0);
hashTable.Add("t", 0);
hashTable.Add("o", 0);
hashTable.Add("z", 0);
hashTable.Add("k", 0);
hashTable.Add("g", 0);
hashTable.Add("a5", 0);
hashTable.Add("c", 0);
hashTable.Add("a2", 0);
hashTable.Add("a7", 0);
hashTable.Add("i", 0);
hashTable.Add("h", 0);
Assert.IsTrue(hashTable.ContainsKey("m"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("m", 0)));
Assert.IsTrue(hashTable.ContainsKey("b"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("b", 0)));
Assert.IsTrue(hashTable.ContainsKey("t"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("t", 0)));
Assert.IsTrue(hashTable.ContainsKey("o"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("o", 0)));
Assert.IsTrue(hashTable.ContainsKey("z"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("z", 0)));
Assert.IsTrue(hashTable.ContainsKey("k"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("k", 0)));
Assert.IsTrue(hashTable.ContainsKey("g"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("g", 0)));
Assert.IsTrue(hashTable.ContainsKey("a5"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("a5", 0)));
Assert.IsTrue(hashTable.ContainsKey("c"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("a2", 0)));
Assert.IsTrue(hashTable.ContainsKey("a7"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("a7", 0)));
Assert.IsTrue(hashTable.ContainsKey("i"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("i", 0)));
Assert.IsTrue(hashTable.ContainsKey("h"));
Assert.IsTrue(hashTable.Contains(new KeyValuePair<string, int>("h", 0)));
}
示例6: TestHashTableRemove
public void TestHashTableRemove()
{
HashTable<string, int> hashTable = new HashTable<string, int>(3, 0.9f);
hashTable.Add("m", 10);
hashTable.Add("b", 2000);
hashTable.Add("t", 0);
hashTable.Add("o", 33);
hashTable.Add("z", 0);
hashTable.Add("k", -812);
hashTable.Add("g", 0);
hashTable.Add("a5", 91);
hashTable.Add("c", 0);
hashTable.Add("a2", 22);
hashTable.Add("a7", 66);
hashTable.Add("i", 707);
hashTable.Add("h", -15);
int value = 0;
bool success = hashTable.Remove(new KeyValuePair<string, int>("a5", 91));
Assert.IsTrue(success);
Assert.AreEqual(12, hashTable.Count);
Assert.IsFalse(hashTable.ContainsKey("a5"));
Assert.IsFalse(hashTable.Contains(new KeyValuePair<string, int>("a5", 91)));
success = hashTable.TryGetValue("a5", out value);
Assert.IsFalse(success);
}
示例7: TestIfRemoveWorksCorrectly
public void TestIfRemoveWorksCorrectly()
{
var table = new HashTable<int, int>();
table.Add(1, 11);
table.Add(2, 12);
table.Add(3, 13);
table.Add(4, 14);
table.Add(5, 15);
table.Add(6, 16);
table.Add(7, 17);
table.Add(8, 18);
var count = table.Count;
Assert.AreEqual(8, count);
table.Remove(4);
Assert.AreEqual(false, table.Contains(4));
Assert.AreEqual(7, table.Count);
}
示例8: ContainsShouldReturnTrueWhenTheGivenKeyValuePairIsPresent
public void ContainsShouldReturnTrueWhenTheGivenKeyValuePairIsPresent()
{
var hashtable = new HashTable<double, string>();
hashtable.Add(1.1, "fsdfsdf");
Assert.IsTrue(hashtable.Contains(new KeyValuePair<double, string>(1.1, "fsdfsdf")));
}