使用动态数组实现了vector,利用并发控制锁保证了在高并发过程中的线程安全

This commit is contained in:
hlccd 2021-10-20 22:04:13 +08:00 committed by GitHub
parent 3c92bb06dd
commit 91995f6ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -246,7 +246,7 @@ func (v *vector) PopBack() {
}
v.mutex.Lock()
v.len--
if v.cap-v.len >= 2^16 {
if v.cap-v.len >= 65536 {
//容量和实际使用差值超过2^16时,容量直接减去2^16
v.cap -= 2 ^ 16
tmp := make([]interface{}, v.cap, v.cap)
@ -283,7 +283,7 @@ func (v *vector) Insert(idx uint64, e interface{}) {
var tmp []interface{}
if v.len >= v.cap {
//冗余不足,进行扩容
if v.cap <= 2^16 {
if v.cap <= 65536 {
//容量翻倍
if v.cap == 0 {
v.cap = 1
@ -332,7 +332,7 @@ func (v *vector) Erase(idx uint64) {
v.data[p] = v.data[p+1]
}
v.len--
if v.cap-v.len >= 2^16 {
if v.cap-v.len >= 65536 {
//容量和实际使用差值超过2^16时,容量直接减去2^16
v.cap -= 2 ^ 16
tmp := make([]interface{}, v.cap, v.cap)