func(s *IntSet)Has(x int)bool { word, bit := x/64, uint(x%64) returnlen(s.words) > word && s.words[word]&(1<<bit) != 0 }
func(s *IntSet)Add(x int) { word, bit := x/64, uint(x%64) for word >= len(s.words) { s.words = append(s.words, 0) } s.words[word] |= 1 << bit }
func(s *IntSet)UnionWith(t *IntSet) { for i, tword := range t.words { if i < len(s.words) { s.words[i] |= tword } else { s.words = append(s.words, tword) } } }
func(s *IntSet)String()string { var buf bytes.Buffer buf.WriteByte('{') for i, word := range s.words { if word == 0 { continue } for j := 0; j < 64; j++ { if word&(1<<uint(j)) != 0 { // 这里为什么要用 uint() ? if buf.Len() > len("{") { buf.WriteByte(' ') } fmt.Fprintf(&buf, "%d", 64*i+j) } } } buf.WriteByte('}') return buf.String() }
func(s *IntSet)Len()int { eleNum := 0 for _, word := range s.words { if word == 0 { continue } for j := 0; j < 64; j++ { if word&(1<<j) != 0 { eleNum += 1 } } } return eleNum } // return the number of elements
func(s *IntSet)AddAll(xList ...int) { for _, x := range xList { s.Add(x) } }