About bitmap algorithm introduces a lot of information online, not repeat them here, to achieve a variety of languages are many, but not many Go language version of the bitmap, paper to write bitmap implement a version of Go.
First, create a bitmap. Go file, define a bitmap structure, provide some method of operation. Detailed code is as follows:
Bitmap Package Import ( "FMT" "strings" ) const ( BitSize =. 8 ) var bitmask = [] {byte. 1,. 1. 1 <<, <<. 1 2,. 1. 3 <<, <<. 1. 4,. 1. 5 << ,. 6 <<. 1,. 1. 7} << type struct {Bitmap bits [] byte bitCount UInt64 // number of digits has been filled capacity uint64 // capacity } FUNC NewBitmap (MaxNum UInt64) * Bitmap { return {& Bitmap bits: the make ([] byte, (+ MaxNum. 7) / BitSize), bitCount: 0, Capacity: MaxNum} } // enter the number FUNC (the this Bitmap *) the Set (NUM UInt64) { byteIndex, bitpos: = this.offset (NUM ) // bitPos left 1 bit or bit (set to 1) the this.bits[byteIndex] |= bitmask[bitPos] the this.++ bitCount } // Clear Digital filled FUNC (the this Bitmap *) the Reset (NUM UInt64) { byteIndex, bitpos: = this.offset (NUM) // reset gap (reset to 0) this.bits [byteIndex] = ^ & bitmask [bitpos] the this. bitCount-- } // if the digital bitmap FUNC (the this Bitmap *) the Test (NUM UInt64) {BOOL byteIndex: NUM = / BitSize iF byteIndex> UInt64 = (len (this.bits)) { return to false } bitpos: % BitSize NUM = // bitpos bit right shift and bitwise with a return! (this.bits [byteIndex] & bitmask [bitpos] == 0) } FUNC (the this Bitmap *) offset (NUM UInt64) (UInt64 byteIndex, bitpos byte) { byteIndex NUM = / // byte index BitSize byteIndex IF> = UInt64 (len (this.bits)) { panic (fmt.Sprintf ( "Runtime error:% D OUT index value of Range", byteIndex)) return } bitpos = byte (NUM% BitSize) // 'bit positions byteIndex return, bitpos } // bitmap capacity FUNC (the this bitmap *) Size () {UInt64 return UInt64 (len (this.bits) * BitSize) } // if vacancy FIG func (this * bitmap) IsEmpty ( ) bool { return this.bitCount == 0 } // has been filled func (this * bitmap) IsFully () {BOOL return this.bitCount == this.capacity } // number of digits has been filled func (this * bitmap ) the Count () {UInt64 return this.bitCount } // Get fill the digital slides func (this *bitmap) GetData() []uint64 { var data []uint64 count := this.Size() for index := uint64(0); index < count; index++ { if this.Test(index) { data = append(data, index) } } return data } func (this *bitmap) String() string { var sb strings.Builder for index := len(this.bits) - 1; index >= 0; index-- { sb.WriteString(byteToBinaryString(this.bits[index])) sb.WriteString(" ") } return sb.String() } func byteToBinaryString(data byte) string { var sb strings.Builder for index := 0; index < bitSize; index++ { if (bitmask[7-index] & data) == 0 { sb.WriteString("0") } else { sb.WriteString("1") } } return sb.String() }
There are comments in the code, it is easy to understand. The following write test code, test the bitmap.
main Package Import ( "Bitmap" "FMT" ) FUNC main () { Array: = [...] UInt64 {0,. 6,. 3,. 7, 2,. 8,. 1,. 4} var. 9 maxNum UInt64 = BM: = bitmap.NewBitmap (maxNum) for _, V: Array = {Range bm.Set (V) } bm.Set (. 5) fmt.Println (bm.IsFully ()) fmt.Println (bm.IsEmpty ()) FMT. println ( "bitmap numbers exist:") fmt.Println (bm.GetData ()) fmt.Println ( "Bitmap binary string") fmt.Println (bm.String ()) fmt.Println ( "Bitmap in the number of digits: ", bm.Count ()) fmt.Println (" Bitmap size: ", bm.Size ()) fmt.Println (" the Test (0): ", Bm.Test (0)) bm.Reset (. 5) FMT.Println(bm.String()) fmt.Println("Test(5):", bm.Test(5)) fmt.Println(bm.GetData()) }
Output test code is as follows:
to true
to false
the bitmap figures present:
[0 2. 3. 1. 4. 5. 6. 7. 8]
the bitmap binary string
0,000,000,111,111,111
number of digits in the bitmap:. 9
bitmap size: 16
the Test (0): to true
00000001 11011111
the Test (. 5) : to false
[0. 1. 4. 3 2. 6. 7. 8]
Summary Finally, Bitmap implementation code may be added to other processes as necessary, implementations herein for reference. In addition text of the code is not thread safe, you need to lock in the use of multiple threads.