当前位置:
首页 > 编程开发 > Objective-C编程 >
-
构建反转排序的泛型字典类8—实现Dictionary接口
制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
完整代码下载:http://file.ddvip.com/2008_10/1224750546_ddvip_1916.rar
8. 实现IDictionary<TKey, TValue>接口
由于前面实现了IDictionary接口,现在实现IDictionary<TKey, TValue>也就没什么困难的了,照葫芦画瓢。
首先改变类声明:
public class ReversibleSortedList<TKey, TValue> :IDictionary<TKey, TValue>
IDictionary, IEnumerable<KeyValuePair<TKey, TValue>>, ICollection,
IEnumerable, ICollection<KeyValuePair<TKey, TValue>>
然后实现ICollection<KeyValuePair<TKey, TValue>>接口成员:
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get
{
return false;
}
}
void ICollection<KeyValuePair<TKey, TValue>>.Add(
KeyValuePair<TKey, TValue> keyValuePair)
{
this.Add(keyValuePair.Key, keyValuePair.Value);
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(
KeyValuePair<TKey, TValue> keyValuePair)
{
int num1 = this.IndexOfKey(keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(this.values[num1],
keyValuePair.Value))
{
return true;
}
return false;
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if ((arrayIndex < 0) || (arrayIndex > array.Length))
{
throw new ArgumentOutOfRangeException(
"arrayIndex", "Need a non-negative number");
}
if ((array.Length - arrayIndex) < this.Count)
{
throw new ArgumentException("ArrayPlusOffTooSmall");
}
for (int num1 = 0; num1 < this.Count; num1++)
{
KeyValuePair<TKey, TValue> pair1;
pair1 = new KeyValuePair<TKey, TValue>(
this.keys[num1], this.values[num1]);
array[arrayIndex + num1] = pair1;
}
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
KeyValuePair<TKey, TValue> keyValuePair)
{
int num1 = this.IndexOfKey(keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(
this.values[num1], keyValuePair.Value))
{
this.RemoveAt(num1);
return true;
}
return false;
}
需要注意,Count属性和Clear方法都是共用方法,不需要再次实现。
接下来实现IDictionary<TKey, TValue>接口,它是最主要的接口,所有成员均为公有的,前面我们已经实现了Add、ContainsKey方法,这里不再列出:
ICollection<TKey> IDictionary<TKey, TValue>.Keys
{
get
{
return this.GetKeyListHelper();
}
}
ICollection<TValue> IDictionary<TKey, TValue>.Values
{
get
{
return this.GetValueListHelper();
}
}
public TValue this[TKey key] //Item属性,就是索引器
{
get
{
TValue local1;
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
return this.values[num1];
}
else
{
local1 = default(TValue);
return local1;
}
}
set
{
if (key == null)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 >= 0)
{
this.values[num1] = value;
this.version++;
}
else
{
this.Insert(~num1, key, value);
}
}
}
public bool Remove(TKey key)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
this.RemoveAt(num1);
}
return (num1 >= 0);
}
public bool TryGetValue(TKey key, out TValue value)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
value = this.values[num1];
return true;
}
value = default(TValue);
return false;
}
另外需要添加一个RemoveAt方法:
//移除指定索引元素
public void RemoveAt(int index)
{
if ((index < 0) || (index >= this._size))
{
throw new ArgumentOutOfRangeException("index", "Index out of range");
}
this._size--;
if (index < this._size)
{
Array.Copy(this.keys, (int)(index + 1), this.keys, index,
(int)(this._size - index));
Array.Copy(this.values, (int)(index + 1), this.values, index,
(int)(this._size - index));
}
this.keys[this._size] = default(TKey);
this.values[this._size] = default(TValue);
this.version++;
}
最后,进行测试,这一次不再使用int做为键,而改用string:
static void Main()
{
ReversibleSortedList<string, string> rs = new ReversibleSortedList<string, string>();
//添加元素
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<string, string> d in rs)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("删除索引为“2”的“5”的元素");
rs.Remove("2");
rs.Remove("5");
foreach (KeyValuePair<string, string> d in rs)
{
Console.WriteLine(d.Key + " " + d.Value);
}
}
ReversibleSortedList 0.7版本:实现IDictionary<TKey, TValue>接口
运行结果:
1 b
2 c
3 a
4 f
5 e
6 d
删除索引为“2”的“5”的元素
1 b
3 a
4 f
6 d
需要注意,Count属性和Clear方法都是共用方法,不需要再次实现。
接下来实现IDictionary<TKey, TValue>接口,它是最主要的接口,所有成员均为公有的,前面我们已经实现了Add、ContainsKey方法,这里不再列出:
ICollection<TKey> IDictionary<TKey, TValue>.Keys
{
get
{
return this.GetKeyListHelper();
}
}
ICollection<TValue> IDictionary<TKey, TValue>.Values
{
get
{
return this.GetValueListHelper();
}
}
public TValue this[TKey key] //Item属性,就是索引器
{
get
{
TValue local1;
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
return this.values[num1];
}
else
{
local1 = default(TValue);
return local1;
}
}
set
{
if (key == null)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 >= 0)
{
this.values[num1] = value;
this.version++;
}
else
{
this.Insert(~num1, key, value);
}
}
}
public bool Remove(TKey key)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
this.RemoveAt(num1);
}
return (num1 >= 0);
}
public bool TryGetValue(TKey key, out TValue value)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
value = this.values[num1];
return true;
}
value = default(TValue);
return false;
}
完整代码下载:http://file.ddvip.com/2008_10/1224750546_ddvip_1916.rar
8. 实现IDictionary<TKey, TValue>接口
由于前面实现了IDictionary接口,现在实现IDictionary<TKey, TValue>也就没什么困难的了,照葫芦画瓢。
首先改变类声明:
public class ReversibleSortedList<TKey, TValue> :IDictionary<TKey, TValue>
IDictionary, IEnumerable<KeyValuePair<TKey, TValue>>, ICollection,
IEnumerable, ICollection<KeyValuePair<TKey, TValue>>
然后实现ICollection<KeyValuePair<TKey, TValue>>接口成员:
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get
{
return false;
}
}
void ICollection<KeyValuePair<TKey, TValue>>.Add(
KeyValuePair<TKey, TValue> keyValuePair)
{
this.Add(keyValuePair.Key, keyValuePair.Value);
}
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(
KeyValuePair<TKey, TValue> keyValuePair)
{
int num1 = this.IndexOfKey(keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(this.values[num1],
keyValuePair.Value))
{
return true;
}
return false;
}
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(
KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if ((arrayIndex < 0) || (arrayIndex > array.Length))
{
throw new ArgumentOutOfRangeException(
"arrayIndex", "Need a non-negative number");
}
if ((array.Length - arrayIndex) < this.Count)
{
throw new ArgumentException("ArrayPlusOffTooSmall");
}
for (int num1 = 0; num1 < this.Count; num1++)
{
KeyValuePair<TKey, TValue> pair1;
pair1 = new KeyValuePair<TKey, TValue>(
this.keys[num1], this.values[num1]);
array[arrayIndex + num1] = pair1;
}
}
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(
KeyValuePair<TKey, TValue> keyValuePair)
{
int num1 = this.IndexOfKey(keyValuePair.Key);
if ((num1 >= 0) && EqualityComparer<TValue>.Default.Equals(
this.values[num1], keyValuePair.Value))
{
this.RemoveAt(num1);
return true;
}
return false;
}
需要注意,Count属性和Clear方法都是共用方法,不需要再次实现。
接下来实现IDictionary<TKey, TValue>接口,它是最主要的接口,所有成员均为公有的,前面我们已经实现了Add、ContainsKey方法,这里不再列出:
ICollection<TKey> IDictionary<TKey, TValue>.Keys
{
get
{
return this.GetKeyListHelper();
}
}
ICollection<TValue> IDictionary<TKey, TValue>.Values
{
get
{
return this.GetValueListHelper();
}
}
public TValue this[TKey key] //Item属性,就是索引器
{
get
{
TValue local1;
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
return this.values[num1];
}
else
{
local1 = default(TValue);
return local1;
}
}
set
{
if (key == null)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 >= 0)
{
this.values[num1] = value;
this.version++;
}
else
{
this.Insert(~num1, key, value);
}
}
}
public bool Remove(TKey key)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
this.RemoveAt(num1);
}
return (num1 >= 0);
}
public bool TryGetValue(TKey key, out TValue value)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
value = this.values[num1];
return true;
}
value = default(TValue);
return false;
}
另外需要添加一个RemoveAt方法:
//移除指定索引元素
public void RemoveAt(int index)
{
if ((index < 0) || (index >= this._size))
{
throw new ArgumentOutOfRangeException("index", "Index out of range");
}
this._size--;
if (index < this._size)
{
Array.Copy(this.keys, (int)(index + 1), this.keys, index,
(int)(this._size - index));
Array.Copy(this.values, (int)(index + 1), this.values, index,
(int)(this._size - index));
}
this.keys[this._size] = default(TKey);
this.values[this._size] = default(TValue);
this.version++;
}
最后,进行测试,这一次不再使用int做为键,而改用string:
static void Main()
{
ReversibleSortedList<string, string> rs = new ReversibleSortedList<string, string>();
//添加元素
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<string, string> d in rs)
{
Console.WriteLine(d.Key + " " + d.Value);
}
Console.WriteLine("删除索引为“2”的“5”的元素");
rs.Remove("2");
rs.Remove("5");
foreach (KeyValuePair<string, string> d in rs)
{
Console.WriteLine(d.Key + " " + d.Value);
}
}
ReversibleSortedList 0.7版本:实现IDictionary<TKey, TValue>接口
运行结果:
1 b
2 c
3 a
4 f
5 e
6 d
删除索引为“2”的“5”的元素
1 b
3 a
4 f
6 d
需要注意,Count属性和Clear方法都是共用方法,不需要再次实现。
接下来实现IDictionary<TKey, TValue>接口,它是最主要的接口,所有成员均为公有的,前面我们已经实现了Add、ContainsKey方法,这里不再列出:
ICollection<TKey> IDictionary<TKey, TValue>.Keys
{
get
{
return this.GetKeyListHelper();
}
}
ICollection<TValue> IDictionary<TKey, TValue>.Values
{
get
{
return this.GetValueListHelper();
}
}
public TValue this[TKey key] //Item属性,就是索引器
{
get
{
TValue local1;
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
return this.values[num1];
}
else
{
local1 = default(TValue);
return local1;
}
}
set
{
if (key == null)
{
throw new ArgumentNullException("key");
}
int num1 = Array.BinarySearch<TKey>(this.keys, 0, this._size, key,
this._sortDirectionComparer);
if (num1 >= 0)
{
this.values[num1] = value;
this.version++;
}
else
{
this.Insert(~num1, key, value);
}
}
}
public bool Remove(TKey key)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
this.RemoveAt(num1);
}
return (num1 >= 0);
}
public bool TryGetValue(TKey key, out TValue value)
{
int num1 = this.IndexOfKey(key);
if (num1 >= 0)
{
value = this.values[num1];
return true;
}
value = default(TValue);
return false;
}
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数