"C #" 19. Set, dictionary Create Assocarray

Source: Internet
Author: User

The purpose of creating assocarray is to correlate data and primary keys: for example "A1" ~ 1; "B1" ~ 2.08 ...

The set is a custom data structure, which contains dictionary data, which can be used for operations such as merging, intersection, etc.


Using system;using system.collections.generic;using system.collections;using system.linq;using System.Text;    Namespace finmktreverseengineering{//1. Set #region set type public class Set<t>: ienumerable<t> {//member protected DICTIONARY&L T        T, int> dict;        Constructor 1 public Set () {dict = new dictionary<t, int> ();            }//Constructor 2 public Set (set<t> set) {dict = new dictionary<t, int> ();            foreach (T key in Set.dict.Keys) {dict[key]=0; }}//getenumerator iterator ienumerator<t> ienumerable<t>. GetEnumerator () {foreach (Keyvaluepair<t, int> item in dict) {yield re Turn item.            Key;  }}//"Modify" IEnumerator Ienumerable.getenumerator () {foreach (Keyvaluepair<t,    Int> item in Dict) {            Yield return item.            Key;        }}//insert method public void Insert (T key) {dict[key]=0; }//remove method public void Remove (T key) {Dict.        Remove (key); }//contains method public bool Contains (T key) {if (Dict.            ContainsKey (key)) {return true;        } else return false; }//replace method public void Replace (T key1,t key2) {foreach (T-key in Dict. Keys) {if (key.                    Equals (Key1)) {Remove (key1);                    Insert (Key2);                Break }}}//intersection method public static set<t> intersection (set<t> S1, SET&LT;T&G 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 method public static set<t> Union (set<t> s1, set<t> S2) {set<t& Gt                        Setunion = new set<t> (S1);            foreach (T key2 in S2.dict.Keys) {Setunion.insert (Key2);        } return setunion;            }//difference method 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 method 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 () method public void print () {foreach (keyvaluepair<t,int> item in Dict) {Console.Write ("{0},", item.)            Key);        } Console.WriteLine (); }//size () method public int Size () {return dict.        Count; }//count Property public int Count {get {return dict. Count; }}//Is the empty set public bool IsEmpty {get {return dict. Count==0?true:false; }}//Two collections are the same 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 Type public class Assocarray<key, value>: Ienumerable<keyvaluepair<key, Val        ue>> {//member Dictionary<key, value> str;//This dictionary may have a larger number of primary keys than the set.        set<key> keys;            Constructor 1:default Constructor Public Assocarray () {str = new Dictionary<key, value> ();        Keys = new set<key> (); }//Constructor 2:copy constructor public Assocarray (Assocarray<key, value> arr2) {str = NE     W Dictionary<key, Value> (ARR2.STR);        Dictionary copy constructor keys = new set<key> (Arr2.keys);      }  Constructor 3: Initialize the keywords and values public assocarray (set<key> keys, value val) {this.keys = keys;            str = new Dictionary<key, value> ();            foreach (Key k in This.keys) {str[k] = val;            }}//Constructor 4 public assocarray (set<key> keys, Dictionary<key, value> str) {            This.keys = keys;        This.str = str;                }//this Index public Value this[key K]//k in Set keys {set { Keys.                Insert (k); STR[K] = value;        Str Dictionary} get {return str[k];}            }//keys Set Property 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;} }//In COnsole print public void print () {foreach (key key in keys) {if (str.                ContainsKey (key)) {Console.Write ("{0}, {1}", key, Str[key]);        }} Console.WriteLine (); }//returns the KeyValuePair Ienumerator<keyvaluepair<key corresponding to the primary key in dictionary and set, value>> ienumerable&lt ; 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 Create Assocarray

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.