Skip to content

Commit

Permalink
添加泛型Add函数,在指定位置插入元素 (#201)
Browse files Browse the repository at this point in the history
* 添加泛型Add函数,在指定位置插入元素

* 修改CHANGELOG文件
  • Loading branch information
johnwongx authored Aug 2, 2023
1 parent 4a29bd6 commit caa168d
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [mapx: LinkedMap 特性](https://github.com/ecodeclub/ekit/pull/191)
- [copier: ReflectCopier 支持忽略字段](https://github.com/ecodeclub/ekit/pull/196)
- [syncx: 重构LoadOrStoreFunc方法及相关测试](https://github.com/ecodeclub/ekit/pull/198)
- [slice: 添加Add函数,在指定位置插入元素](https://github.com/ecodeclub/ekit/pull/201)

# v0.0.7
- [slice: FilterDelete](https://github.com/ecodeclub/ekit/pull/152)
Expand Down
35 changes: 35 additions & 0 deletions internal/slice/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 slice

import "github.com/ecodeclub/ekit/internal/errs"

func Add[T any](src []T, element T, index int) ([]T, error) {
length := len(src)
if index < 0 || index >= length {
return nil, errs.NewErrIndexOutOfRange(length, index)
}

//先将src扩展一个元素
var zeroValue T
src = append(src, zeroValue)
for i := len(src) - 1; i > index; i-- {
if i-1 >= 0 {
src[i] = src[i-1]
}
}
src[index] = element
return src, nil
}
78 changes: 78 additions & 0 deletions internal/slice/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// 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 slice

import (
"testing"

"github.com/ecodeclub/ekit/internal/errs"
"github.com/stretchr/testify/assert"
)

func TestAdd(t *testing.T) {
testCases := []struct {
name string
slice []int
addVal int
index int
wantSlice []int
wantErr error
}{
{
name: "index 0",
slice: []int{123, 100},
addVal: 233,
index: 0,
wantSlice: []int{233, 123, 100},
},
{
name: "index middle",
slice: []int{123, 124, 125},
addVal: 233,
index: 1,
wantSlice: []int{123, 233, 124, 125},
},
{
name: "index out of range",
slice: []int{123, 100},
index: 12,
wantErr: errs.NewErrIndexOutOfRange(2, 12),
},
{
name: "index less than 0",
slice: []int{123, 100},
index: -1,
wantErr: errs.NewErrIndexOutOfRange(2, -1),
},
{
name: "index last",
slice: []int{123, 100, 101, 102, 102, 102},
addVal: 233,
index: 5,
wantSlice: []int{123, 100, 101, 102, 102, 233, 102},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := Add(tc.slice, tc.addVal, tc.index)
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.wantSlice, res)
})
}
}
24 changes: 24 additions & 0 deletions slice/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 slice

import "github.com/ecodeclub/ekit/internal/slice"

// Add 在index处添加元素
// index 范围应为[0, len(src))
func Add[Src any](src []Src, element Src, index int) ([]Src, error) {
res, err := slice.Add[Src](src, element, index)
return res, err
}
71 changes: 71 additions & 0 deletions slice/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 slice

import (
"fmt"
"testing"

"github.com/ecodeclub/ekit/internal/errs"

"github.com/stretchr/testify/assert"
)

func TestAdd(t *testing.T) {
// Delete 主要依赖于 internal/slice.Delete 来保证正确性
testCases := []struct {
name string
slice []int
addVal int
index int
wantSlice []int
wantErr error
}{
{
name: "index 0",
slice: []int{123, 100},
addVal: 233,
index: 0,
wantSlice: []int{233, 123, 100},
},
{
name: "index -1",
slice: []int{123, 100},
index: -1,
wantErr: errs.NewErrIndexOutOfRange(2, -1),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, err := Add(tc.slice, tc.addVal, tc.index)
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.wantSlice, res)
})
}
}

func ExampleAdd() {
res, _ := Add[int]([]int{1, 2, 3, 4}, 233, 2)
fmt.Println(res)
_, err := Add[int]([]int{1, 2, 3, 4}, 233, -1)
fmt.Println(err)
// Output:
// [1 2 233 3 4]
// ekit: 下标超出范围,长度 4, 下标 -1
}

0 comments on commit caa168d

Please sign in to comment.