构建可反转排序的泛型字典类(9完)完善

 

9. 完善

大楼已经盖好,剩下的工作就是装修,装修好就可以入住了。从本文的题目得知,这是一个可反转排序的集合类,但我们只实现了降序插入功能,如果希望把升序转换为降序该怎么办呢?此例的解决方法是声明一个代表排序方向的属性Comparer,并加入一个sort方法,调用sort方法时根据Comparer属性进行排序:

private ListSortDirection _currentSortDirection = ListSortDirection.Descending;    
public SortDirectionComparer<TKey> Comparer
    {
        
get
        {
            
return this._sortDirectionComparer;
        }
    }
    
public void Sort()
    {
        
// 检查是否跟现有排序方向相同.
        if (this._currentSortDirection != this._sortDirectionComparer.SortDirection)
        {
            
// 如果不同,则进行反转.
            Array.Reverse(this.keys, 0this._size);
            Array.Reverse(
this.values, 0this._size);
            
// 设置当前排序.
            this._currentSortDirection = this._sortDirectionComparer.SortDirection;
        }
    }

 

其中SortDirectionComparer类是第二节所声明的类,请参考:

http://cgbluesky.blog.163.com/blog/static/241235582008113103320661/

接下来再增加一个剪除多余空间的功能:


//剪除多余空间
    public void TrimExcess()
    {
        
int num1 = (int)(this.keys.Length * 0.9);
        
if (this._size < num1)
        {
            
this.Capacity = this._size;
        }
}
 

当然,需要给Capacity属性添加set方法

public int Capacity //容量属性
    {
        
get
        {
            
return this.keys.Length;
        }
        
set
        {
            
this.InternalSetCapacity(value, true);
        }
    }

 

注意:这样的调整空间会导致另外开辟内存以重新存放元素。

好,最后的工作就是增加一些构造方法,比如指定排序方向,指定容量,以使得这个集合类的使用更为灵活:


 //用于指定排序方向的构造方法
    public ReversibleSortedList(SortDirectionComparer<TKey> comparer)
        : 
this()
    {
        
if (comparer != null)
        {
            
this._sortDirectionComparer = comparer;
            
this._currentSortDirection = _sortDirectionComparer.SortDirection;
        }
    }
    
//用于指定字典的构造方法
    public ReversibleSortedList(IDictionary<TKey, TValue> dictionary)
        : 
this(dictionary, (SortDirectionComparer<TKey>)null)
    {
    }
    
//用于指定初始容量的构造方法
    public ReversibleSortedList(int capacity)
    {
        
if (capacity < 0)
        {
            
throw new ArgumentOutOfRangeException(
                
"capacity""Non-negative number required");
        }
        
this.keys = new TKey[capacity];
        
this.values = new TValue[capacity];
        
this._sortDirectionComparer = new SortDirectionComparer<TKey>();
        
this._currentSortDirection = _sortDirectionComparer.SortDirection;
    }
    
//用于指定字典和排序方向的构造方法
    
//这个构造方法用于在指定集合中创建新的字典类
    public ReversibleSortedList(IDictionary<TKey, TValue> dictionary,
                                SortDirectionComparer
<TKey> comparer)
        : 
this((dictionary != null? dictionary.Count : 0, comparer)
    {
        
if (dictionary == null)
        {
            
throw new ArgumentNullException("dictionary");
        }
        dictionary.Keys.CopyTo(
this.keys, 0);
        dictionary.Values.CopyTo(
this.values, 0);
        Array.Sort
<TKey, TValue>(this.keys, this.values,
                                        
this._sortDirectionComparer);
        
this._size = dictionary.Count;
    }
    
//用于指定容量和排序方向的构造方法
    public ReversibleSortedList(int capacity, SortDirectionComparer<TKey> comparer)
        : 
this(comparer)
    {
        
this.Capacity = capacity;
    }

好!添加测试代码:
static void Main()
    {
        ReversibleSortedList
<stringstring> rs = new ReversibleSortedList<stringstring>();
        
//添加元素
        rs.Add("3""a");
        rs.Add(
"1""b");
        rs.Add(
"2""c");
        rs.Add(
"6""d");
        rs.Add(
"5""e");
        rs.Add(
"4""f");
        
//使用DictionaryEntry打印键/值
        foreach (KeyValuePair<stringstring> d in rs)
        {
            Console.WriteLine(d.Key 
+ "    " + d.Value);
        }
        Console.WriteLine(
"重新排序");
        rs.Comparer.SortDirection 
= ListSortDirection.Ascending;
        rs.Sort();
        
foreach (KeyValuePair<stringstring> d in rs)
        {
            Console.WriteLine(d.Key 
+ "    " + d.Value);
        }
    }

 

下载完整代码:

运行结果:

ReversibleSortedList 1.0版本:完成

1   b

2   c

3   a

4   f

5   e

6   d

重新排序

6           a

5   e

4   f

3   a

2   c

1   b

10. 结束语

从翻译《C# Cookbook》中的泛型内容到翻译《Programming C#》中的泛型内容,再到写完这篇文章,一共写了129页的Word文档。当然,这里有很大一部份是代码。写到后面我自己都有些不耐烦了。呵呵,不管怎么说,能坚持做完一件事并不容易,现在坚持下来了总算是对自己有个交待,也值得庆贺。

读完这一系列文章,您应该对FCL中几个重要的集合接口已经非常熟悉了吧。现在去看FCL中几个集合类的源代码将不再困难,还犹豫什么,阅读源代码将会给您带来极大的提高!

原文地址:https://www.cnblogs.com/abatei/p/1074572.html