-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitArray.go
59 lines (51 loc) · 1.02 KB
/
bitArray.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package Torrent
import (
//"fmt"
"sync"
)
type BitArray struct {
bits int
data []byte
lock sync.Mutex
}
func (this *BitArray) Set(data []byte) {
this.data = data
this.bits = len(data) * 8
}
func (this *BitArray) IsLocked(index int) bool {
if index < this.bits && index >= 0 {
if (this.data[index/8] & (1 << (uint(7 - (index % 8))))) > 0 {
return true
}
}
return false
}
func (this *BitArray) LockPiece(index int) bool {
status := false
if index < this.bits && index >= 0 {
this.lock.Lock()
if this.IsLocked(index) == false {
this.data[index/8] |= (1 << uint(7-(index%8)))
status = true
}
this.lock.Unlock()
}
return status
}
func (this *BitArray) UnlockPiece(index int) {
if index < this.bits {
this.lock.Lock()
negation := byte(0xff ^ (1 << uint(7-index%8)))
this.data[index/8] &= negation
this.lock.Unlock()
}
}
func (this *BitArray) GetByte(index int) byte {
if index < this.bits {
return this.data[index/8]
}
return 0
}
func (this *BitArray) GetArray() []byte {
return this.data
}