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

34 篇文章 1 订阅
32 篇文章 0 订阅

创建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;

        //构造器1
        public Set()
        {   
            dict = new Dictionary<T, int>();
        }
        //构造器2
        public 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 constructor
        public AssocArray()
        {
            str = new Dictionary<Key, Value>();
            keys = new Set<Key>();
        }
        // 构造器2:Copy constructor
        public 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;
            }
        }
        // 构造器4
        public 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; }

        }
        //Dictionary
        public 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中都有的主键对应的keyvaluepair
        IEnumerator<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();
        }
        }

      
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值