Skip to content

Commit

Permalink
fix: 优化删除逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
xinghe903 committed Sep 28, 2024
1 parent 350e895 commit c57eb77
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
35 changes: 28 additions & 7 deletions list/copy_on_write_array_list.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package list

import (
Expand Down Expand Up @@ -89,15 +102,23 @@ func (a *CopyOnWriteArrayList[T]) Set(index int, t T) error {
func (a *CopyOnWriteArrayList[T]) Delete(index int) (T, error) {
a.mutex.Lock()
defer a.mutex.Unlock()
newItems := make([]T, len(a.vals))
copy(newItems, a.vals)
res, t, err := slice.Delete(newItems, index)
if err != nil {
return t, err
var ret T
n := len(a.vals)
if index >= n || index < 0 {
return ret, errs.NewErrIndexOutOfRange(n, index)
}
newItems := make([]T, len(a.vals)-1)
item := 0
for i, v := range a.vals {
if i == index {
ret = v
continue
}
newItems[item] = v
item++
}
newItems = res
a.vals = newItems
return t, nil
return ret, nil
}

func (a *CopyOnWriteArrayList[T]) Len() int {
Expand Down
13 changes: 13 additions & 0 deletions list/copy_on_write_array_list_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package list

import (
Expand Down

0 comments on commit c57eb77

Please sign in to comment.