基于哈希表实现集合
class HashST1Set<Key> : ISet<Key>
{
private HashST1<Key> hashST1;
public int Count {
get {
return hashST1.Count; } }
public bool IsEmpty {
get {
return hashST1.IsEmpty; } }
public HashST1Set(int M)
{
hashST1 = new HashST1<Key>(M);
}
public HashST1Set()
{
hashST1 = new HashST1<Key>();
}
public void Add(Key key)
{
hashST1.Add(key);
}
public void Remove(Key key)
{
hashST1.Remove(key);
}
public bool Contains(Key key)
{
return hashST1.Contains(key);
}
}
基于红黑树实现集合与基于哈希表实现的集合对比测试:
class Program
{
private static long TestSet(ISet<string> set, List<string> words)
{
Stopwatch t = new Stopwatch();
t.Start();
foreach (string word in words)
set.Add(word);
t.Stop();
return t.ElapsedMilliseconds;
}
static void Main(string[] args)
{
Console.WriteLine("双城记");
List<string> words = TestHelper.ReadFile("测试文件1/双城记.txt");
Console.WriteLine("词汇量总数:" + words.Count);
Console.