【C#】19. Set、Dictionary创建AssocArray

创建AssocArray的目的是关联数据和主键:例如"A1" ~ 1; "B1" ~ 2.08 …

其中,Set是自定义的数据结构,内部含有dictionary数据,,可以进行诸如合并,交集等的操作。

using System;using System.Collections.Generic;using System.Collections;using System.Linq;using System.Text;namespace FinMktReverseEngineering{//1. Set#region Set 类型public class Set<T> : IEnumerable<T>{// 成员protected Dictionary<T, int> dict;//构造器1public Set(){dict = new Dictionary<T, int>();}//构造器2public Set(Set<T> set){dict = new Dictionary<T, int>();foreach (T key in set.dict.Keys){dict[key]=0;}}//GetEnumerator迭代器IEnumerator<T> IEnumerable<T>.GetEnumerator(){foreach (KeyValuePair<T, int> item in dict){yield return item.Key;}}// 【修改】IEnumerator IEnumerable.GetEnumerator(){foreach (KeyValuePair<T, int> item in dict){yield return item.Key;}}//Insert方法public void Insert(T key){dict[key]=0;}//Remove方法public void Remove(T key){dict.Remove(key);}//Contains方法public bool Contains(T key){if (dict.ContainsKey(key)){return true;}else return false;}//Replace方法public void Replace(T key1,T key2){foreach (T key in dict.Keys){if (key.Equals(key1)){Remove(key1);Insert(key2);break;}}}//Intersection方法public static Set<T> Intersection(Set<T> s1, Set<T> s2){Set<T> setIntsec = new Set<T>();foreach (T key1 in s1.dict.Keys){foreach (T key2 in s2.dict.Keys){if (key1.Equals(key2)) setIntsec.Insert(key1);}}return setIntsec;}//Union方法public static Set<T> Union(Set<T> s1, Set<T> s2){Set<T> setUnion = new Set<T>(s1);foreach (T key2 in s2.dict.Keys){setUnion.Insert(key2);}return setUnion;}//Difference方法public static Set<T> Difference(Set<T> s1, Set<T> s2){Set<T> setDiff = new Set<T>(s1);foreach (T key1 in s1.dict.Keys){foreach (T key2 in s2.dict.Keys){if (key1.Equals(key2)) setDiff.Remove(key1);}}return setDiff;}//SymmetricDifference方法public static Set<T> SymmetricDifference(Set<T> s1, Set<T> s2){Set<T> setSymDiff = Set<T>.Union(s1,s2);foreach (T key1 in s1.dict.Keys){foreach (T key2 in s2.dict.Keys){if (key1.Equals(key2)) setSymDiff.Remove(key1);}}return setSymDiff;}//print()方法public void print(){foreach (KeyValuePair<T,int> item in dict){Console.Write("{0}, ", item.Key);}Console.WriteLine();}//Size()方法public int Size(){return dict.Count;}//count属性public int count{get { return dict.Count; }}//是否是空集public bool IsEmpty{get { return dict.Count==0?true:false; }}//两个集合是否相同public bool IsEqual(Set<T> newSet){if (this.dict.Count!=newSet.count){return false;}else{foreach (T key in newSet){if (!this.dict.ContainsKey(key)) { return false; }}return true;}}}#endregion//2. AssocArray#region AssocArray 类型public class AssocArray<Key, Value> : IEnumerable<KeyValuePair<Key, Value>>{//成员Dictionary<Key, Value> str; //这个字典的主键数量可能比Set中的要大。Set<Key> keys;// 构造器1:Default constructorpublic AssocArray(){str = new Dictionary<Key, Value>();keys = new Set<Key>();}// 构造器2:Copy constructorpublic AssocArray(AssocArray<Key, Value> arr2){str = new Dictionary<Key, Value>(arr2.str);// Dictionary的copy构造器keys = new Set<Key>(arr2.keys);}// 构造器3:初始化关键字和值public AssocArray(Set<Key> keys, Value val){this.keys = keys;str = new Dictionary<Key, Value>();foreach (Key k in this.keys){str[k] = val;}}// 构造器4public AssocArray(Set<Key> keys, Dictionary<Key, Value> str){this.keys = keys;this.str = str;}//this索引public Value this[Key k] // k in Set keys{set{keys.Insert(k);str[k] = value; //str Dictionary}get { return str[k]; }}//Keys Set 属性public Set<Key> Keys{set { this.keys = value; }get { return keys; }}//Dictionarypublic Dictionary<Key,Value> Dict{set { this.str = value; }get { return this.str; }}//在console中打印public void print(){foreach (Key key in keys){if (str.ContainsKey(key)){Console.Write("{0}, {1} ", key, str[key]);}}Console.WriteLine();}// 返回Dictionary和Set中都有的主键对应的keyvaluepairIEnumerator<KeyValuePair<Key, Value>> IEnumerable<KeyValuePair<Key, Value>>.GetEnumerator(){for (int i = 0; i < this.keys.count; i++){yield return this.str.ElementAt(i);}}IEnumerator IEnumerable.GetEnumerator(){for (int i = 0; i < this.keys.count; i++){yield return this.str.ElementAt(i);}}}#endregion}using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Diagnostics;using System.Globalization;namespace FinMktReverseEngineering{class test{static void Main(string[] args){Set<string> set1 = new Set<string>();Dictionary<string, double> dict = new Dictionary<string, double>();set1.Insert("A1");set1.Insert("A2");set1.Insert("A3");set1.Insert("A7");dict.Add("A1", 1);dict.Add("A2", 2);dict.Add("A3", 3);dict.Add("A4", 4);dict.Add("A5", 5);dict.Add("A6", 6);AssocArray<string, double> arr = new AssocArray<string, double>(set1, dict);arr["B1"] = 7;arr.print();Console.Read();}}}

以前我是个爱仰望天空的人,苍蓝的天空总是给我求生的勇气,

【C#】19. Set、Dictionary创建AssocArray

相关文章:

你感兴趣的文章:

标签云: