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