BitMap 算法
引導(dǎo)
如果我們現(xiàn)在有一堆數(shù)據(jù),[0 ,3 ,4 ,7 ,9 ,1 ,2 ,5 ,6 ,8 ,2 ,3 ,5 ,7 ,9 ,0 ,1 ,4 ,6 ,8],需要對(duì)數(shù)據(jù)進(jìn)行排重,只留下最原始的數(shù)據(jù)贾虽。
那么我們可以用如下方式實(shí)現(xiàn):
package main
import (
"fmt"
)
func main() {
//獲取到一個(gè)數(shù)組
nums := []int{0, 3, 4, 7, 9, 1, 2, 5, 6, 8, 2, 3, 5, 7, 9, 0, 1, 4, 6, 8}
//獲取一個(gè)排重?cái)?shù)組
numMap := map[int]bool{}
//排重后的結(jié)果
newNums := []int{}
for _, num := range nums {
_, ok := numMap[num]
if ok { //已經(jīng)存在了
continue
}
//記錄重復(fù)狀態(tài)
numMap[num] = true
newNums = append(newNums, num)
}
fmt.Println("排除重復(fù)前:", nums)
fmt.Println("排除重復(fù)后:", newNums)
//排除重復(fù)前: [0 3 4 7 9 1 2 5 6 8 2 3 5 7 9 0 1 4 6 8]
//排除重復(fù)后: [0 3 4 7 9 1 2 5 6 8]
}
流程如下
1 . 實(shí)例化一個(gè)map零远,存儲(chǔ)標(biāo)記。
2 . 如果數(shù)據(jù)存在宝踪,則改變狀態(tài)箭昵。
3 . 不存在的數(shù)據(jù)税朴,就加入到新的數(shù)組中。
4 . 排序完成家制。
不知小伙伴絕不覺得和計(jì)數(shù)排序的邏輯很像正林。當(dāng)然優(yōu)化方案也可以按照計(jì)數(shù)排序的方式。
缺點(diǎn)分析
針對(duì)數(shù)據(jù)量大的數(shù)據(jù)颤殴,我們需求的map就會(huì)很大觅廓。這樣占用的空間特別高。所以針對(duì)這種情況我們需要一種替代方案來完成涵但,海量數(shù)據(jù)排重操作杈绸。
BitMap
所謂的Bit-map就是用一個(gè)bit位來標(biāo)記某個(gè)元素對(duì)應(yīng)的Value, 而Key即是該元素矮瘟。由于采用了Bit為單位來存儲(chǔ)數(shù)據(jù)瞳脓,因此在存儲(chǔ)空間方面,可以大大節(jié)省
其實(shí)如果你知道計(jì)數(shù)排序的話澈侠,你就會(huì)發(fā)現(xiàn)這個(gè)和計(jì)數(shù)排序很像劫侧。
bitmap應(yīng)用
- 可進(jìn)行數(shù)據(jù)的快速查找,判重,刪除烧栋。
- 去重?cái)?shù)據(jù)而達(dá)到壓縮數(shù)據(jù).
目前bitmap在數(shù)據(jù)采集【大數(shù)據(jù)-爬蟲】環(huán)節(jié)非常有用写妥,比如對(duì)url去重。
bitmap 實(shí)現(xiàn)
package bitmap
import (
"bytes"
"sync"
)
type Bit uint64 //統(tǒng)一bit類型
const BitSize = 64 //每一個(gè)bit位存儲(chǔ)數(shù)據(jù)量
const IndexBit = 6 //位置位移數(shù)量
type Bitmap []Bit //bitmap
var mux = sync.Mutex{}
var bitsMap [BitSize]Bit
//func init() {
// for i := 0; i < BitSize; i++ {
// bitsMap[i] = Bit(0x01) << Bit(i)
// }
//}
//設(shè)置bit
func (b *Bitmap) SetBit(value Bit, stat bool) *Bitmap {
mux.Lock()
defer mux.Unlock()
index, pos := b.ValueToIndexAndPos(value)
if stat {
b.UpCapToIndex(index)
//設(shè)置
(*b)[index] |= Bit(0x01) << pos
} else {
//取消
if index < len(*b) {
(*b)[index] &^= Bit(0x01) << pos
}
}
return b
}
func (b *Bitmap) Set(value Bit) *Bitmap {
b.SetBit(value, true)
return b
}
func (b *Bitmap) Unset(value Bit) *Bitmap {
b.SetBit(value, false)
return b
}
func (b *Bitmap) Get(value Bit) bool {
index, pos := b.ValueToIndexAndPos(value)
//(*b)[index] &^= Bit(0x01) << pos
return index < len(*b) && ((*b)[index]&(Bit(0x01)<<pos) != 0)
}
func (b *Bitmap) ValueToIndexAndPos(value Bit) (int, Bit) {
return int(value >> IndexBit), value % Bit(BitSize)
}
func (b *Bitmap) UpCapToIndex(index int) {
toIndex := index + 1
l := len(*b)
if l < toIndex {
l2 := 2 * l
if toIndex < l2 {
toIndex = l2
}
n := make([]Bit, toIndex, toIndex)
copy(n, *b)
*b = n
}
}
func (b *Bitmap) ToString(split ...string) string {
l := len(*b)
sl := len(split)
ss := ""
if sl > 0 {
ss = split[0]
}
str := ""
for i := 0; i < l; i++ {
if i > 0 && sl > 0 {
str += ss
}
str += (*b)[i].ToString()
}
return str
}
func (bit *Bit) ToString() string {
b := &bytes.Buffer{}
t := *bit
for i := 0; i < BitSize; i++ {
if (t & bitsMap[i]) != 0 {
b.WriteString("1")
} else {
b.WriteString("0")
}
}
return b.String()
}
關(guān)鍵技能解析
位運(yùn)算
為了更好描述效果审姓,這里我們使用較小的數(shù)據(jù)類型: byte 珍特,一個(gè)byte包含8bit位。
package main
import (
"fmt"
"github.com/imroc/biu"
)
func main() {
var b byte
b = 1
fmt.Println("常規(guī)展示:", b) // 原始數(shù)據(jù): 1
fmt.Println("二進(jìn)制位:", biu.ByteToBinaryString(b)) //二進(jìn)制方式:00000001
//向左移動(dòng)一位
b = b << 1
fmt.Println("左邊移動(dòng)一位:", b) // 原始數(shù)據(jù): 2
fmt.Println("二進(jìn)制位:", biu.ByteToBinaryString(b)) //二進(jìn)制方式:00000010
var b1 byte = 3
fmt.Println("b1=:", b1) // 原始數(shù)據(jù): 3
fmt.Println("二進(jìn)制位:", biu.ByteToBinaryString(b1)) //二進(jìn)制方式:00000011
// 或(|) 運(yùn)算: 二進(jìn)制位中對(duì)應(yīng)位置有1則為1魔吐,否則為0扎筒。
// 2 的二進(jìn)制為: 10 , 3 的二進(jìn)制為: 11
// 那么 2|3 = 11
fmt.Println("(2|3)二進(jìn)制位:", biu.ByteToBinaryString(2|3)) //二進(jìn)制方式:00000011
// 與(&) 運(yùn)算: 二進(jìn)制位中對(duì)應(yīng)位置都為1則為1画畅,否則為0砸琅。
// 2 的二進(jìn)制為: 10 , 3 的二進(jìn)制為: 11
// 那么 2&3 = 10
fmt.Println("(2&3)二進(jìn)制位:", biu.ByteToBinaryString(2&3)) //二進(jìn)制方式:00000010
//異或(^)運(yùn)算: 二進(jìn)制中對(duì)應(yīng)位置不同的為1轴踱,否則為0.
// 2 的二進(jìn)制為: 10 症脂, 3 的二進(jìn)制為: 11
// 那么 2^3 = 01
fmt.Println("(2^3)二進(jìn)制位:", biu.ByteToBinaryString(2^3)) //二進(jìn)制方式:00000001
var c = byte(2)
//取反(^):二進(jìn)制中的所有位置 0 為1,1為 0.
fmt.Println("(^2)常規(guī)輸出:", ^c) //二進(jìn)制方式:253
fmt.Println("(^2)二進(jìn)制位:", biu.ByteToBinaryString(^c)) //二進(jìn)制方式:11111101
}
Bitmap使用
bm := &bitmap.Bitmap{} //實(shí)例化bitmap
bm.SetBit(100, true) //設(shè)置位置100
bm.SetBit(12, true)
bm.SetBit(1, true)
bm.SetBit(12, true) //設(shè)置位置12
//獲取狀態(tài)
fmt.Println("bit:100:",bm.Get(100)) //true
fmt.Println("bit:12:",bm.Get(12)) //true
//取消設(shè)置
bm.SetBit(12, false)
fmt.Println("bit:12:",bm.Get(12)) //false