Go implementation set and operation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Setimport ("FMT" "bytes") type HashSet struct{m map[interface{}]bool}func newhashset () *hashset{return & Amp   Hashset{m:make (Map[interface{}]bool)}}func (self *hashset) Add (e interface{}) bool{if self.m[e]{return false} Self.m[e] = True return True}func (self *hashset) Remove (e interface{}) bool{Delete (SELF.M, E) return True}func ( Self *hashset) Clear () bool {self.m = Do (Map[interface{}]bool) return True}func (self *hashset) Contains (e Interfa ce{}) bool {return self.m[e]}func (self *hashset) len () int {return Len (SELF.M)}func (self *hashset) Same (other *has Hset) bool{if other = = nil {return false} if self. Len ()! = other. Len () {return false} for k,_: = Range other.m{if!self. Contains (k) {return false}} return True}func (self *hashset) Elements () interface{} {//for k: = Ran GE self.m{//snapshot = Snapshot (snapshot, k)//} Initiallen: = self. Len () Actuallen: = 0 Snapshot: = Make ([]interface{}, Initiallen) for k: = Range self.m{If Actuallen < initiallen{Snapshot[actuallen]      = k} else {snapshot = append (snapshot, k)} Actuallen + +} if Actuallen < initiallen{ snapshot = Snapshot[:actuallen]} return Snapshot}func (self *hashset) string () string {var buf bytes. Buffer buf. WriteString ("set{") Flag: = True for k: = Range self.m{If flag {flag = false} else {buf. WriteString ("")} buf. WriteString (FMT. Sprintf ("%v", K))} buf. WriteString ("}") return BUF. String ()}func (self *hashset) Issuperset (other *hashset) bool {If other = nil{return false} Selflen: = Self . Len () Otherlen: = other. Len () if Otherlen = = 0 | | Selflen = = Otherlen {return false} if Selflen > 0 && otherlen = = 0{return true} for V: = Range other.m{if!self. Contains (v) {return false}} return true}//A or element belonging to B func (Self *hashset) Union (Other *hashset) *hashset {//if other = = Nil | | other. Len () = = 0{//return self//}////for V: = range other.m{//self. ADD (v)//}//return self//cannot change the range of set a union: = Newhashset () for V: = Range self.m{Union. Add (v)} for V: = Range other.m{Union. Add (v)} return union}//the element that belongs to A and B is func (self *hashset) Intersect (other *hashset) *hashset {if and = Nil | | other . Len () = = 0{return Newhashset ()} Intsset: = Newhashset () for v,_: = Range other.m{if self. Contains (v) {Intsset.add (v)}} return intsset}//is a and does not belong to the element of B func (self *hashset) difference (Other *hashse T) *hashset {diffset: = Newhashset () If other = Nil | | Len () = = 0{diffset.union (self)} else {for V: = Range self.m{if!other. Contains (v) {Diffset.add (v)}}} return diffset}//collection A and all elements in collection B that are not part of a∩b func (self *hashset) Symmetricdifference (Other *hashset) *hashset {//At this time all elements in A∩b=∅,a are not part of the empty set//If other = Nil | | Len () = = 0{//return self//}//INTs: = self. Intersect (Other)/////At this time a∩b=∅,a is empty or B is empty, B is empty before it has been judged, at this time B cannot be empty, that is, a is empty//if ints = = Nil | | INTs. Len () = = 0 {//return other//}////Unionset: = self. Union (Other)//Result: = Newhashset ()//for V: = Range unionset.m{//if!ints. Contains (v) {//result. ADD (v)//}//} ints: = self. Difference (other) union: = self. Union (other) return union. Difference (ints)}

Test code

package mainimport (   "fmt"   "qii/set")func main()  {   set1 := set.NewHashSet()   set1.Add(1)   set1.Add("e2")   set1.Add(3)   set1.Add("e4")   fmt.Println("set1:", set1)   fmt.Printf("set1 Elements:%v\n", set1.Elements())   set2 := set.NewHashSet()   set2.Add(3)   set2.Add("e2")   set2.Add(5)   set2.Add("e6")   fmt.Println("set2:", set1)   fmt.Printf("set1 union set2:%v\n", set1.Union(set2))   fmt.Printf("set1 intersect set2:%v\n", set1.Intersect(set2))   fmt.Println(set1,set2)   fmt.Printf("set1 difference set2:%v\n", set1.Difference(set2))   fmt.Printf("set1 SymmetricDifference set2:%v\n", set1.SymmetricDifference(set2))   set1.Clear()   fmt.Println(set1)}

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.