Bit 数组

来源:互联网 发布:免费php主机 编辑:程序博客网 时间:2024/06/16 14:12
// An IntSet is a set of small non-negative integers.// Its zero value represents the empty set.type IntSet struct {words []uint64}// Has reports whether the set contains the non-negative value x.func (s *IntSet) Has(x int) bool {word, bit := x/64, uint(x%64)return word < len(s.words) && s.words[word]&(1<<bit) != 0}// Add adds the non-negative value x to the set.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}// UnionWith sets s to the union of s and t.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)}}}


Go语言圣经    gopl.io/ch6/intset

原创粉丝点击