From 43f8c07bd14fd1386e9fc662472faff4b70c0b4f Mon Sep 17 00:00:00 2001 From: hlccd <56643462+hlccd@users.noreply.github.com> Date: Wed, 20 Oct 2021 16:56:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=8A=A8=E6=80=81=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E5=AE=9E=E7=8E=B0=E4=BA=86vector=EF=BC=8C=E5=88=A9?= =?UTF-8?q?=E7=94=A8=E5=B9=B6=E5=8F=91=E6=8E=A7=E5=88=B6=E9=94=81=E4=BF=9D?= =?UTF-8?q?=E8=AF=81=E4=BA=86=E5=9C=A8=E9=AB=98=E5=B9=B6=E5=8F=91=E8=BF=87?= =?UTF-8?q?=E7=A8=8B=E4=B8=AD=E7=9A=84=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对reverse中出现的问题进行了修复 --- goSTL/data_structure/vector/vector.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/goSTL/data_structure/vector/vector.go b/goSTL/data_structure/vector/vector.go index d77bc17..dba3098 100644 --- a/goSTL/data_structure/vector/vector.go +++ b/goSTL/data_structure/vector/vector.go @@ -206,7 +206,7 @@ func (v *vector) PushBack(e interface{}) { v.data[v.len] = e } else { //冗余不足,需要扩容 - if v.cap < 2^16 { + if v.cap <= 2^16 { //容量翻倍 if v.cap == 0 { v.cap = 1 @@ -280,9 +280,10 @@ func (v *vector) Insert(idx uint64, e interface{}) { v = New() } v.mutex.Lock() + var tmp []interface{} if v.len >= v.cap { //冗余不足,进行扩容 - if v.cap < 2^16 { + if v.cap <= 2^16 { //容量翻倍 if v.cap == 0 { v.cap = 1 @@ -293,7 +294,7 @@ func (v *vector) Insert(idx uint64, e interface{}) { v.cap += 2 ^ 16 } //复制扩容前的元素 - tmp := make([]interface{}, v.cap, v.cap) + tmp = make([]interface{}, v.cap, v.cap) copy(tmp, v.data) v.data = tmp } @@ -370,6 +371,7 @@ func (v *vector) Reverse() { tmp := make([]interface{}, v.len, v.len) copy(tmp, v.data) v.data = tmp + v.cap=v.len } 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]