Go language implementation bitmap sorting (bitmap)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The go language provides a byte type with a byte corresponding to 8 bits, so the bitmap can be implemented by converting it.

Code:

Package Main//author:xcl//date:2014-1-25import ("FMT") func main () {arrInt32: = [...] Uint32{5, 4, 2, 1, 3, 13}var arrmax uint32 = 20bit: = Newbitmap (Arrmax) for _, V: = range ArrInt32 {bit. Set (v)}fmt. Println ("After sorting:") for I: = UInt32 (0); i < Arrmax; i++ {if k: = bit. Test (i); K = = 1 {fmt. Printf ("%d", i)}}}const (bitsize = 8//One byte 8-bit) type Bitmap struct {BitArray []bytearraysize uint32}func newbitmap (Max UIn T32) *bitmap {var r uint32switch {case Max <= bitsize:r = 1default:r = Max/bitsizeif Max%bitsize! = 0 {r + = 1}}fmt. PRINTLN ("Array size:", R) return &bitmap{bitarray:make ([]byte, R), Arraysize:r}}func (Bitmap *bitmap) Set (i uint32) {idx, P OS: = Bitmap.calc (i) bitmap. BITARRAY[IDX] |= 1 << posfmt. Println ("Set () value=", I, "idx=", idx, "pos=", POS, bytetobinarystring (bitmap. BITARRAY[IDX])}func (bitmap *bitmap) Test (i UInt32) byte {idx, pos: = Bitmap.calc (i) return bitmap. BITARRAY[IDX] >> Pos & 1}func (bitmap *bitmap) Clear (i uint32) {idx, pos: = Bitmap.calc (i) bitMap. BITARRAY[IDX] &^= 1 << pos}func (bitmap *bitmap) calc (i UInt32) (idx, pos uint32) {idx = i >> 3//equals I/8 , that is, the byte position if idx >= bitmap. ArraySize {Panic ("array out of bounds.") Return}pos = i% bitsize//bit position return}//bytetobinarystring function Source://Go language version of the binary string representation of the byte variable//http://www.sharejs.com/codes/ Go/4357func bytetobinarystring (data byte) (str string) {var a bytefor I: = 0; i < 8; i++ {a = Datadata <<= 1data >>= 1switch A {case data:str + = "0" Default:str + = "1"}data <<= 1}return str}
Operation Result:

Array size: 3set ()  value= 5  idx= 0  pos= 5 00100000set ()  value= 4  idx= 0  pos= 4 00110000set ()  Value= 2  idx= 0  pos= 2 00110100set ()  value= 1  idx= 0  pos= 1 00110110set ()  value= 3  idx= 0  pos= 3 00111110set ()  value=  idx= 2  pos= 1 00000010set ()  value= idx=  1  pos= 5 00100000 after sequencing: 1 2 3 4 5 13 17

Mail:xcl_168@aliyun.com

blog:http://blog.csdn.net./xcl168



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.