using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
namespace WWLTEST
{
/// <summary>
/// 字典
/// </summary>
/// <typeparam name="TKey">键的类型</typeparam>
/// <typeparam name="TValue">值的类型</typeparam>
public class JH<TKey, TValue> : IEnumerable
{
public KV<TKey, TValue> First { get; set; }
public KV<TKey, TValue> Last { get; set; }
/// <summary>
/// 添加键值对数据
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public void Add(TKey key, TValue value)
{
KV<TKey, TValue> newNode = new KV<TKey, TValue>(key, value);
if (First == null)
{
First = newNode;
Last = First;
}
else
{
Last.Next = newNode;
//需开启上一条数据显示,请取消注释以下行
//Last.Next.Prev = Last;
Last = Last.Next;
}
}
/// <summary>
/// 根据键移除数据
/// </summary>
/// <param name="key">键</param>
public void Remove(TKey key)
{
KV<TKey, TValue> current = First;
KV<TKey, TValue> current2 = First.Next;
if (current.key.Equals(key))
{
First = First.Next;
}
else
{
while (current2 != null)
{
if (current2.key.Equals(key))
{
current.Next = current2.Next;
break;
}
current = current2;
current2 = current2.Next;
}
}
}
/// <summary>
/// 清除字典所有数据
/// </summary>
public void Clear()
{
First = null;
}
/// <summary>
/// 此实体类foreach循环时,将以此方法的返回值做代替操作
/// </summary>
public IEnumerator GetEnumerator()
{
KV<TKey, TValue> current = First;
while (current != null)
{
yield return current;
current = current.Next;
}
}
/// <summary>
/// 检测字典是否包含某键值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Include(TKey key)
{
KV<TKey, TValue> current = First;
while (current != null)
{
if (current.key.Equals(key)) return true;
current = current.Next;
}
return false;
}
/// <summary>
/// 简便行赋值取值方式
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
public TValue this[TKey key]
{
get {
return this.Where(a => a.key.Equals(key));
}
set {
//开启键不重复模式,请换成注释的以下行
//if (this.ContainsKey(a => a.key.Equals(key))) this.Change(value, a => a.key.Equals(key));else Add(key, value);
Add(key, value);
}
}
}
public class KV<TKey, TValue>
{
public KV(TKey key, TValue value)
{
this.key = key;
this.value = value;
}
public TKey key { get; set; }
public TValue value { get; set; }
public KV<TKey, TValue> Next { get; set; }
public KV<TKey, TValue> Prev { get; set; }
}
public static class KZ
{
/// <summary>
/// 取字典某一条数据
/// </summary>
/// <typeparam name="TSource">键类型</typeparam>
/// <typeparam name="TResult">值类型</typeparam>
/// <param name="source">字典</param>
/// <param name="selector">委托函数</param>
/// <returns></returns>
public static TResult Where<TSource, TResult>(this JH<TSource, TResult> source, Func<KV<TSource, TResult>, bool> selector)
{
TResult resoult = default(TResult);
foreach (KV<TSource, TResult> item in source)
{
if (selector(item) == true)
{
resoult = item.value;
break;
}
}
return resoult;
}
/// <summary>
/// 取字典多条数据
/// </summary>
/// <typeparam name="TSource">键类型</typeparam>
/// <typeparam name="TResult">值类型</typeparam>
/// <param name="source">字典</param>
/// <param name="selector">委托函数</param>
/// <returns></returns>
public static List<TResult> WhereList<TSource, TResult>(this JH<TSource, TResult> source, Func<KV<TSource, TResult>, bool> selector)
{
List<TResult> ls = new List<TResult>();
foreach (KV<TSource, TResult> item in source)
{
if (selector(item) == true) ls.Add(item.value);
}
return ls;
}
/// <summary>
/// 改变字典某条数据
/// </summary>
/// <typeparam name="TSource">键类型</typeparam>
/// <typeparam name="TResult">值类型</typeparam>
/// <param name="source">字典</param>
/// <param name="result">需要改变的数据</param>
/// <param name="selector">委托函数</param>
public static void Change<TSource,TResult>(this JH<TSource, TResult> source,TResult result,Func<KV<TSource, TResult>, bool> selector)
{
foreach(KV<TSource, TResult> item in source)
{
if (selector(item) == true)
{
item.value = result;
break;
}
}
}
/// <summary>
/// 给字典赋值时检测字典里是否有某键值
/// </summary>
/// <typeparam name="TSource">键类型</typeparam>
/// <typeparam name="TResult">值类型</typeparam>
/// <param name="source">字典</param>
/// <param name="selector">委托函数</param>
/// <returns></returns>
public static bool ContainsKey<TSource, TResult>(this JH<TSource, TResult> source, Func<KV<TSource, TResult>, bool> selector)
{
foreach (KV<TSource, TResult> item in source)
{
if (selector(item) == true) return true;
}
return false;
}
}
}
之前一直在找字典是如何封装的,一直找不到,自己就试着封装了个字典(Dictionary)
最新推荐文章于 2023-06-24 12:12:27 发布