【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;

        //构造器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();
        }
        }

    }
时间: 2025-01-12 23:04:16

【C#】19. Set、Dictionary创建AssocArray的相关文章

(一)Python入门-3序列:19集合-特点-创建和删除-交集并集差集运算

集合: 集合是无序可变,元素不能重复.实际上,集合底层是字典实现,集合的所有元素都是字典 中的“键对象”,因此是不能重复的且唯一的. 一:集合的创建和删除 1. 使用{}创建集合对象,并使用 add()方法添加元素 1 >>> a = {1,3,5} 2 >>> a 3 {1, 3, 5} 4 >>> a.add(7) 5 >>> a 6 {1, 3, 5, 7} 7 >>> a.add(5) 8 >>&

.net源码分析 – Dictionary&lt;TKey, TValue&gt;

接上篇:.net源码分析 – List<T> Dictionary<TKey, TValue>源码地址:https://github.com/dotnet/corefx/blob/master/src/System.Collections/src/System/Collections/Generic/Dictionary.cs 接口 Dictionary<TKey, TValue>和List<T>的接口形式差不多,不重复说了,可以参考List<T>

iOS开发项目篇—19获取授权过的访问标记

iOS开发项目篇—19获取授权过的访问标记 一.简单说明 1.获取授权 2.简单说明 说明: (1)只能使用post请求 (2)post请求的参数有5个,且五个参数都是必须的. (3)新浪会返回一个JSON,转成OC对象为字典,可以通过Key取出ACCESS_TOKEN. 二.实现 1.导入第三方框架 2.使用字典封装请求参数,五个参数都是必须的,就算少一个都是非法请求. 封装代码 1 //2.封装请求参数 2 /* 3 url:https://api.weibo.com/oauth2/acce

第 19 章 相机 I:取景器

请参考教材,全面理解和完成本章节内容... ... 记录办公室陋习时,如果能以现场照片佐证,问题解决起来就会容易很多.接下来的两章,使用系统自带的Camera API,为CriminalIntent应用添加拍摄作案现场照片的功能. Camera API功能虽然强大,但要用好它并不容易.不仅要编写大量的实现代码,还要苦苦挣扎着学习和理解一大堆全新概念.因此,很容易产生的一个疑问就是:"只是拍张快照,难道就没有便捷的标准接口可以使用吗?" 答案是肯定的.我们可以通过隐式intent与照相机

OC学习笔记 Foundation 集合类 字典Dictionary

字典 由键-值对组成的数据集合 4种创建字典方式 1>针对单个键值对 直接赋值 1 NSDictionary *dic = [NSDictionary dictionaryWithObject:@"dajie" forKey:@"name"];//直接创建字典 2 id object = [dic objectForKey:@"name"];// 取出name键对应值 3 NSLog(@"%@",object); 2&g

创建表格

用数据来创建表格 var data = [ { name: 'jim', age: 19, gender: 'male'}, { name: 'jim', age: 19, gender: 'male'}, { name: 'jim', age: 19, gender: 'male'}, { name: 'jim', age: 19, gender: 'male'}, { name: 'jim', age: 19, gender: 'male'}, { name: 'jim', age: 19,

第19章 使用PXE+Kickstart部署无人值守安装

章节概述: 本章节将教会您通过PXE+DHCP+TFTP+VSftpd+Kickstart服务程序搭建出无人值守安装系统,从而批量部署客户机系统. 这种系统能够实现自动化运维.避免了重复性劳动,帮助提升工作效率,对于运维人员真的是太有帮助了. 本章目录结构 19.1 无人值守系统 19.2 部署相关服务程序 19.2.1 配置DHCP服务程序 19.2.2 配置TFTP服务程序 19.2.3 配置SYSLinux服务程序 19.2.4 配置VSFtpd服务程序 19.2.4 创建KickStar

【symfoware NATIVE】创建实例

symfoware NATIVE系[PKG安装]傻瓜式一键安装(默认),安装时可以看到各个目录的位置,也可以在安装中自己指定位置和参数.默认情况下,安装目录:Symfoware Server 安装目录:   C:\SFWSVSymfoware Server 控制文件仓库目录:   C:\SFWETC数据库编码:   Shift_JISSymfoware Server Client 安装目录:   C:\SFWCLNTSymfoware Server Connection Manager 安装目录

批处理实现批量创建快捷方式

功能:自动读取./dir.txt文件中配置的每行目录,并在当前目录下创建该目录的快捷方式(目录可以是可访问的网络磁盘目录,此功能可以取代网络磁盘映射,特别适用于网络磁盘映射太多,而虚拟网络磁盘驱动器数量不足的情况) 注意:批处理中的for循环中的语句会被解析为一条语句执行,所以必须适用变量延迟才能保证数据的正确性,同时,for循环中的右括号要启用转义 测试: ./dir.txt内容如下: 运行前目录: 运行后目录: 1 @echo off 2 3 color 0a 4 title 自动创建文件目