基于有序数组实现集合
class SortedArray1Set<Key> : ISet<Key> where Key : IComparable<Key>
{
private SortedArray1<Key> s;
public int Count {
get {
return s.Count; } }
public bool IsEmpty {
get {
return s.IsEmpty; } }
public SortedArray1Set(int capacity)
{
s = new SortedArray1<Key>();
}
public SortedArray1Set()
{
s = new SortedArray1<Key>();
}
public void Add(Key key)
{
s.Add(key);
}
public void Remove(Key key)
{
s.Remove(key);
}
public bool Contains(Key key)
{
return s.Contains(key);
}
}
测试:
class Program
{
public static long TestSet(ISet<string> set, List<string> words)
{
Stopwatch t = new Stopwatch();
t.Start();
foreach (var 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.WriteLine();
Console.WriteLine("链表集合");
LinkedList1Set<string> linkedList1Set = new LinkedList1Set<string>();
long t1 = TestSet(linkedList1Set, words);
Console.WriteLine("不同单词的总数: "+linkedList1Set.Count);
Console.WriteLine("运行时间: "+t1+"ms");
Console.WriteLine();
Console.WriteLine("有序数组集合");
SortedArray1Set<string> sortedArray1Set = new SortedArray1Set<string>();
long t2 = TestSet(sortedArray1Set, words);
Console