Sets in Go are usually implemented as a `map[T]bool`

, where `T`

is the
element type. A set represented by a map is very flexible
but, for certain problems, a specialized representation may outperform it. For
example, in domains such as dataflow analysis where set
elements are small non-negative integers, sets have many elements, and
set operations like union and intersection are common, a *bit vector*
is ideal.

A bit vector uses a slice of unsigned integer values or “words,” each
bit of which represents a possible element of the set. The set
contains *i* if the *i*-th bit is set. The following program
demonstrates a simple bit vector type with three methods:

gopl.io/ch6/intset

// An IntSet ...

Start Free Trial

No credit card required