Code Snippets-Golang implementing collection operations

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("FMT" "Sort" "sync") type Set struct {sync. RWMUTEXM map[int]bool}//New Collection Object func new (items ... int) *set {s: = &set{m:make (Map[int]bool, Len (items)),}s.add (items ...) Return s}//add element func (S *set) Add (items ... int) {S.lock () defer S.unlock () for _, V: = range items {S.m[v] = true}}//Delete Element fu NC (S *set) Remove (items ... int) {S.lock () defer S.unlock () for _, V: = Range Items {Delete (s.m, v)}}//determine if the element exists func (s *set  Has (items ... int) bool {s.rlock () defer S.runlock () for _, V: = Range Items {If _, OK: = S.m[v];!ok {return False}}return true}//element number func (s *set) Count () int {return Len (S.M)}//emptying collection func (S *set) Clear () {s.lock () defer s.unlock () S.M = Map[in t]bool{}}//Empty Set judgment func (S *set) empty () bool {return Len (s.m) = = 0}//unordered list func (S *set) list () []int {s.rlock () defer S.RUNL Ock () List: = Make ([]int, 0, Len (S.M)) for item: = Range S.M {list = append (list, item)}return list}//Sort list func (S *set) Sor TList () []int {s.rlock () defer s.runlock () List: = Make ([]int, 0, Len (S.M)) for item: = Range S.M {list = append (list, item)}sort. Ints (list) return list}//and set func (S *set) Union (Sets ... *set) *set {r: = New (S.list () ...) For _, Set: = Range Sets {for e: = Range set.m {r.m[e] = True}}return r}//Difference set func (S *set) minus (sets ... *set) *set {r: = New (S.list () ...) For _, Set: = Range Sets {for e: = Range SET.M {If _, OK: = S.m[e]; OK {Delete (R.M, e)}}}return r}//intersection func (S *set) Inter Sect (Sets ... *set) *set {r: = New (S.list () ...) For _, Set: = Range Sets {for e: = Range S.M {If _, OK: = Set.m[e];!ok {Delete (R.M, E)}}}return r}//complement func (s *set) Comp Lement (full *set) *set {r: = New () for e: = Range FULL.M {If _, OK: = S.m[e];!ok {r.add (e)}}return R}func main () {s1: = Ne W (1, 2, 3, 4, 5, 6, 7, 8) S2: = new (3, 4, 5, 6) S3: = new (5, 6, 9, ten) R1: = S1. Union (S2, S3)//Gets and sets R2: = S1. Minus (S2, S3)//Get the difference set r3: = S1. Intersect (S2, S3)//Get intersection R4: = S3.complement (S1)//Get S3 complement to S1 FMT. Println (R1. Sortlist ()) fmt. PRINTLN (R2. Sortlist ()) fmt. PRINTLN (R3. Sortlist ()) fmt. Println (R4. Sortlist ())} 
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.