mirror of
https://github.com/hlccd/goSTL.git
synced 2025-01-31 12:19:32 +08:00
使用动态数组实现了vector,利用并发控制锁保证了在高并发过程中的线程安全
对reverse中出现的问题进行了修复
This commit is contained in:
parent
fb520f4e85
commit
43f8c07bd1
@ -206,7 +206,7 @@ func (v *vector) PushBack(e interface{}) {
|
|||||||
v.data[v.len] = e
|
v.data[v.len] = e
|
||||||
} else {
|
} else {
|
||||||
//冗余不足,需要扩容
|
//冗余不足,需要扩容
|
||||||
if v.cap < 2^16 {
|
if v.cap <= 2^16 {
|
||||||
//容量翻倍
|
//容量翻倍
|
||||||
if v.cap == 0 {
|
if v.cap == 0 {
|
||||||
v.cap = 1
|
v.cap = 1
|
||||||
@ -280,9 +280,10 @@ func (v *vector) Insert(idx uint64, e interface{}) {
|
|||||||
v = New()
|
v = New()
|
||||||
}
|
}
|
||||||
v.mutex.Lock()
|
v.mutex.Lock()
|
||||||
|
var tmp []interface{}
|
||||||
if v.len >= v.cap {
|
if v.len >= v.cap {
|
||||||
//冗余不足,进行扩容
|
//冗余不足,进行扩容
|
||||||
if v.cap < 2^16 {
|
if v.cap <= 2^16 {
|
||||||
//容量翻倍
|
//容量翻倍
|
||||||
if v.cap == 0 {
|
if v.cap == 0 {
|
||||||
v.cap = 1
|
v.cap = 1
|
||||||
@ -293,7 +294,7 @@ func (v *vector) Insert(idx uint64, e interface{}) {
|
|||||||
v.cap += 2 ^ 16
|
v.cap += 2 ^ 16
|
||||||
}
|
}
|
||||||
//复制扩容前的元素
|
//复制扩容前的元素
|
||||||
tmp := make([]interface{}, v.cap, v.cap)
|
tmp = make([]interface{}, v.cap, v.cap)
|
||||||
copy(tmp, v.data)
|
copy(tmp, v.data)
|
||||||
v.data = tmp
|
v.data = tmp
|
||||||
}
|
}
|
||||||
@ -370,6 +371,7 @@ func (v *vector) Reverse() {
|
|||||||
tmp := make([]interface{}, v.len, v.len)
|
tmp := make([]interface{}, v.len, v.len)
|
||||||
copy(tmp, v.data)
|
copy(tmp, v.data)
|
||||||
v.data = tmp
|
v.data = tmp
|
||||||
|
v.cap=v.len
|
||||||
}
|
}
|
||||||
for i := uint64(0); i < v.len/2; i++ {
|
for i := uint64(0); i < v.len/2; i++ {
|
||||||
v.data[i], v.data[v.len-i-1] = v.data[v.len-i-1], v.data[i]
|
v.data[i], v.data[v.len-i-1] = v.data[v.len-i-1], v.data[i]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user