集合之SortedList+Hashtable
来源:互联网 发布:live800服务端源码 编辑:程序博客网 时间:2024/06/08 06:02
C#集合之SORTEDLIST
http://www.cnblogs.com/wisdomforce/archive/2010/05/20/1740115.html
SortedList 用法
http://www.cnblogs.com/carekee/articles/2219986.html
很详细,很有用的。
有关 Hashtable 和 SortedList
http://q.cnblogs.com/q/3134/
先进先出(FIFO, First in first out),明显该用队列
Queue myQ = new Queue(); myQ.Enqueue("Hello"); myQ.Enqueue("World"); myQ.Enqueue("!");
重写hashtable的Add,clear,remove方法,等,用ArrayList来添加它的key(ArrayList存在先进先出特性)。代码如何下:using System;using System.Collections;namespace NoSortHashtable { public class NoSortHashtable : Hashtable { private ArrayList keys = new ArrayList(); public NoSortHashtable() { } public override void Add(object key, object value) { base.Add (key, value); keys.Add (key); } public override ICollection Keys { get { return keys; } } public override void Clear() { base.Clear (); keys.Clear (); } public override void Remove(object key) { base.Remove (key); keys.Remove (key); } public override IDictionaryEnumerator GetEnumerator() { return base.GetEnumerator (); } }}
来源:http://www.cnblogs.com/ericguo/archive/2007/02/20/generic_sortedlist_support_multikey.html
非强类型的SortedList如何支持重复键可以参照Pharaoh在2005年就写的blog:《不排序和可以重复key的SortedList》,强类型的SortedList同非强类型的一样可以支持重复键,并不像MSDN上所说的那样,“In either case, a SortedList does not allow duplicate keys.”。
比较非强类型的SortedList,强类型的SortedList需要综合应用C#泛型,接口,继承以及Singleton设计模式来实现,短短几十行代码还是很有点味道的,下面示例是一个允许DateTime键重复,按照DateTime先后顺序排序的一个事件队列的实现:
1 internal class CEventListComparer : IComparer<DateTime>
2 {
3 static private CEventListComparer mono;
4 public static CEventListComparer EarlyFirst
5 {
6 get
7 {
8 if (mono == null)
9 mono = new CEventListComparer();
10 return mono;
11 }
12 }
13
14 #region IComparer
15 public int Compare(DateTime x, DateTime y)
16 {
17 if (x == y)
18 return -1;
19 else if (x < y)
20 return -1;
21 else
22 return 1;
23 }
24 #endregion
25 }
26
27 internal class CEventList : SortedList<DateTime, IScheduleable>
28 {
29 public CEventList() : base(CEventListComparer.EarlyFirst)
30 {
31 }
32
33 public IScheduleable PopEarlistSchedule(out DateTime newTime)
34 {
35 IScheduleable ish = null;
36 IEnumerator<KeyValuePair<DateTime, IScheduleable>> getFirst = GetEnumerator();
37 getFirst.MoveNext();
38 newTime = getFirst.Current.Key;
39 ish = getFirst.Current.Value;
40 RemoveAt(0);
41 return ish;
42 }
43
44 internal void Schedule(DateTime ScheduledTime, IScheduleable ScheduledCall)
45 {
46 Add(ScheduledTime, ScheduledCall);
47 }
48 }
2 {
3 static private CEventListComparer mono;
4 public static CEventListComparer EarlyFirst
5 {
6 get
7 {
8 if (mono == null)
9 mono = new CEventListComparer();
10 return mono;
11 }
12 }
13
14 #region IComparer
15 public int Compare(DateTime x, DateTime y)
16 {
17 if (x == y)
18 return -1;
19 else if (x < y)
20 return -1;
21 else
22 return 1;
23 }
24 #endregion
25 }
26
27 internal class CEventList : SortedList<DateTime, IScheduleable>
28 {
29 public CEventList() : base(CEventListComparer.EarlyFirst)
30 {
31 }
32
33 public IScheduleable PopEarlistSchedule(out DateTime newTime)
34 {
35 IScheduleable ish = null;
36 IEnumerator<KeyValuePair<DateTime, IScheduleable>> getFirst = GetEnumerator();
37 getFirst.MoveNext();
38 newTime = getFirst.Current.Key;
39 ish = getFirst.Current.Value;
40 RemoveAt(0);
41 return ish;
42 }
43
44 internal void Schedule(DateTime ScheduledTime, IScheduleable ScheduledCall)
45 {
46 Add(ScheduledTime, ScheduledCall);
47 }
48 }
上面的事件队列(CEventList)正是我在编写的离散事件仿真程序的核心数据结构,是生产代码哦
- 集合之SortedList+Hashtable
- C#集合之SortedList
- 集合类 arrallist,stack,queue,hashtable,sortedlist
- C#集合类ArrayList HashTable SortedList Dictionary
- C#学习笔记集合类型之Hashtable、Dictionary和SortedList(2)
- Net中的集合ArrayList,Hashtable,SortedList,Queue,Stack
- Java集合之Hashtable
- Java集合之Hashtable
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)
- C# 集合整理 StringCollection,Array,ArrayList,IList,Item,Queue,Stack,Hashtable,SortedList
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类) 收藏
- C#集合的區別(Array、Arraylist、 List、 Hashtable、SortedList、 Dictionary、 Stack、 Queue )
- C#集合类详解,Hashtable、Dictionary、SortedList、SortedLi、Array、ArrayList、List、Stack、SortedDictionar
- C#学习 第三章 数组与集合 Array,ArrayList,Hashtable,Dictionry,Stack,Queue,SortedList
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)
- C#集合类详解,Hashtable 和 SortedList 类,Dictionary 和 SortedLi,Array ,ArrayList ,List ,Stack,SortedDictionar
- C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、SortedList类)
- C#集合类详解,Hashtable 和 SortedList 类,Dictionary 和 SortedLi,Array ,ArrayList ,List ,Stack,SortedDictionary ,ListDictionary ,StringDic
- Oracle/plsql 进程锁死处理
- pitch
- 关于Xerces-C++的一篇介绍文章
- 循环-猜数字
- 润乾设计器的换行问题
- 集合之SortedList+Hashtable
- ESQL
- 利用SAPI对WAV文件进行语音识别
- 虚基类和多重继承总结
- c#笔记04-委托 事件
- vim快捷键大全
- 使用POI导出excel,完美兼容2003及2007以上版本,购物车原理
- 一个有用的ANSI字符串类String的实现及使用演示
- 跨平台视频会议