Daily Go language Bible-pointers to object methods-bit array Exercise 2

Source: Internet
Author: User
Tags addall

Practice 6.3: (*intset). Unionwith uses the | operator to calculate the intersection of the two sets, and we then implement a few additional functions Intersectwith for Intset (intersection: The element appears in a set B collection), Differencewith (The difference: the element appears in a set, does not appear in the B collection) , Symmetricdifference (and the difference set: The element appears in a but does not appear in B, or appears in B does not appear in a).

* Exercise 6.4: Implement a Elems method that returns all the elements in the collection for traversal operations such as range.

Exercise 6.2: Define a variable parameter method (*intset). AddAll (... int), this method can add a set of Intset, such as S. AddAll (a).

/* Practice 6.3: (*intset). Unionwith uses the | operator to calculate the intersection of the two sets, and we then implement a few additional functions Intersectwith for Intset (intersection: The element appears in a set B collection), Differencewith (The difference: the element appears in a set, does not appear in the B collection) , Symmetricdifference (and the difference set: The element appears in a but does not appear in B, or appears in B does not appear in a).  *///intersection: The element appears in a set B collection func (S *intset) Intersectwith (t *intset) intset {var result intset for I, Word: = Range s.words {if I >= len (t.words) {break} result . Words = Append (Result.words, word&t.words[i])} return result}//difference set: The element appears in a set, does not appear in the B set Func (S *intset) Differencewith (t *intset) intset {var result intset for I, Word: = range S.words {if i >                = Len (t.words) {result.words = append (result.words, Word) continue } result.words = Append (Result.words, word& (Word^t.words[i]))} return Resul t}//: The element appears in a but does not appear in B, or appears in B does not appear in Afunc (S *intset) symmetricdifference (t *intset) IntSet {var result intset for I, Word: = range S.words {if I >= len (t.words) { Result.words = append (result.words, Word) continue} re Sult.words = Append (Result.words, word^t.words[i])} return result}//Exercise 6.4: Implement a Elems method that returns all the elements in the collection for doing some R Traversal operations such as Ange. Func (S *intset) Elems () []int {var result []int for i, Word: = Range S.words {for j: = 0; J < 64;  J + + {if word& (1<<uint (j))! = 0 {result = Append (result, 64*I+J)}}} return result}//Exercise 6.2: Define a variable parameter method (*intset). AddAll (... int), this method can add a set of Intset, such as S. AddAll (a). Func (S *intset) AddAll (elements ... int) {for _, R: = range Elements {S.add (r)}}

  

Daily Go language Bible-pointers to object methods-bit array Exercise 2

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.