Skip to content

Commit

Permalink
Merge pull request #100 from gotomicro/dev
Browse files Browse the repository at this point in the history
Release v0.0.4
  • Loading branch information
flycash authored Sep 28, 2022
2 parents 5beeea4 + 926d442 commit a953461
Show file tree
Hide file tree
Showing 31 changed files with 1,816 additions and 242 deletions.
11 changes: 10 additions & 1 deletion .CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 开发中

# v0.0.4
- [slice: 重构 index 和 contains 的方法,直接调用对应Func 版本](https://github.com/gotomicro/ekit/pull/87)
- [list: 优化 ArrayList Delete 的缩容逻辑](https://github.com/gotomicro/ekit/pull/88)
- [sqlx: 加密列 EncryptColumn 支持](https://github.com/gotomicro/ekit/pull/92)

# v0.0.3
- [ekit: add ToPtr function](https://github.com/gotomicro/ekit/pull/6)
- [sqlx: 支持 JsonColumn](https://github.com/gotomicro/ekit/pull/7)
Expand All @@ -26,4 +31,8 @@
- [ekit: 修复OnDemandBlockTaskPool测试不稳定](https://github.com/gotomicro/ekit/pull/70)
- [syncx: 使用泛型封装 sync.Map](https://github.com/gotomicro/ekit/pull/79)
- [slice: 支持 Diff*, Intersection*, Union*, Index* 类方法](https://github.com/gotomicro/ekit/pull/83)
- [slice: 聚合函数 Max, Min 和 Sum](https://github.com/gotomicro/ekit/pull/82)
- [slice: 聚合函数 Max, Min 和 Sum](https://github.com/gotomicro/ekit/pull/82)
- [slice: FilterMap 和 Delete 方法](https://github.com/gotomicro/ekit/pull/91)
- [pool: 重构TaskPool](https://github.com/gotomicro/ekit/pull/93)
- [slice: Reverse 和 ReverseSelf方法](https://github.com/gotomicro/ekit/pull/96)
- [pool: 重构TaskPool —— 清理注释](https://github.com/gotomicro/ekit/pull/95)
39 changes: 39 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2021 gotomicro
#
# 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.

coverage:
status:
project:
default:
# basic
target: 95%
threshold: 0.5%
# advanced settings
branches:
- main
- dev
if_ci_failed: error #success, failure, error, ignore
informational: false
only_pulls: false
patch:
default:
# basic
target: 95%
threshold: 0.5%
branches:
- main
- dev
if_ci_failed: error #success, failure, error, ignore
informational: false
only_pulls: false
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@
# Dependency directories (remove the comment below to include it)
# vendor/

.idea
.idea

**/.DS_Store
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# ekit
泛型工具库
泛型工具库。

- [文档](https://ekit.gocn.vip/ekit/develop/guide/)
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/gotomicro/ekit
go 1.18

require (
github.com/mattn/go-sqlite3 v1.14.15
github.com/stretchr/testify v1.7.1
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
6 changes: 3 additions & 3 deletions list/errors.go → internal/errs/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package list
package errs

import "fmt"

// newErrIndexOutOfRange 创建一个代表
func newErrIndexOutOfRange(length int, index int) error {
// NewErrIndexOutOfRange 创建一个代表下标超出范围的错误
func NewErrIndexOutOfRange(length int, index int) error {
return fmt.Errorf("ekit: 下标超出范围,长度 %d, 下标 %d", length, index)
}
35 changes: 35 additions & 0 deletions internal/slice/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021 gotomicro
//
// 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/gotomicro/ekit/internal/errs"

func Delete[T any](src []T, index int) ([]T, T, error) {
length := len(src)
if index < 0 || index >= length {
var zero T
return nil, zero, errs.NewErrIndexOutOfRange(length, index)
}
j := 0
res := src[index]
for i, v := range src {
if i != index {
src[j] = v
j++
}
}
src = src[:j]
return src, res, nil
}
79 changes: 79 additions & 0 deletions internal/slice/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2021 gotomicro
//
// 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/gotomicro/ekit/internal/errs"
"github.com/stretchr/testify/assert"
)

func TestDelete(t *testing.T) {
testCases := []struct {
name string
slice []int
index int
wantSlice []int
wantVal int
wantErr error
}{
{
name: "index 0",
slice: []int{123, 100},
index: 0,
wantSlice: []int{100},
wantVal: 123,
},
{
name: "index middle",
slice: []int{123, 124, 125},
index: 1,
wantSlice: []int{123, 125},
wantVal: 124,
},
{
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},
index: 5,
wantSlice: []int{123, 100, 101, 102, 102},
wantVal: 102,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
res, val, err := Delete(tc.slice, tc.index)
assert.Equal(t, tc.wantErr, err)
if err != nil {
return
}
assert.Equal(t, tc.wantSlice, res)
assert.Equal(t, tc.wantVal, val)
})
}
}
16 changes: 16 additions & 0 deletions internal/slice/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021 gotomicro
//
// 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 后续逐步把切片会在不同部分使用的公共方法挪过来这个内部包
package slice
48 changes: 21 additions & 27 deletions list/array_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package list

import (
"github.com/gotomicro/ekit/internal/errs"
"github.com/gotomicro/ekit/internal/slice"
)

var (
_ List[any] = &ArrayList[any]{}
)
Expand All @@ -38,7 +43,7 @@ func NewArrayListOf[T any](ts []T) *ArrayList[T] {
func (a *ArrayList[T]) Get(index int) (t T, e error) {
l := a.Len()
if index < 0 || index >= l {
return t, newErrIndexOutOfRange(l, index)
return t, errs.NewErrIndexOutOfRange(l, index)
}
return a.vals[index], e
}
Expand All @@ -53,7 +58,7 @@ func (a *ArrayList[T]) Append(ts ...T) error {
// 当index等于ArrayList长度等同于append
func (a *ArrayList[T]) Add(index int, t T) error {
if index < 0 || index > len(a.vals) {
return newErrIndexOutOfRange(len(a.vals), index)
return errs.NewErrIndexOutOfRange(len(a.vals), index)
}
a.vals = append(a.vals, t)
copy(a.vals[index+1:], a.vals[index:])
Expand All @@ -65,32 +70,27 @@ func (a *ArrayList[T]) Add(index int, t T) error {
func (a *ArrayList[T]) Set(index int, t T) error {
length := len(a.vals)
if index >= length || index < 0 {
return newErrIndexOutOfRange(length, index)
return errs.NewErrIndexOutOfRange(length, index)
}
a.vals[index] = t
return nil
}

// Delete 方法会在必要的时候引起缩容,其缩容规则是:
// - 如果容量 > 2048,并且长度小于容量一半,那么就会缩容为原本的 5/8
// - 如果容量 (64, 2048],如果长度是容量的 1/4,那么就会缩容为原本的一半
// - 如果此时容量 <= 64,那么我们将不会执行缩容。在容量很小的情况下,浪费的内存很少,所以没必要消耗 CPU去执行缩容
func (a *ArrayList[T]) Delete(index int) (T, error) {
length := len(a.vals)
if index < 0 || index >= length {
var zero T
return zero, newErrIndexOutOfRange(length, index)
res, t, err := slice.Delete(a.vals, index)
if err != nil {
return t, err
}
j := 0
res := a.vals[index]
for i, v := range a.vals {
if i != index {
a.vals[j] = v
j++
}
}
a.vals = a.vals[:j]
a.vals = res
a.shrink()
return res, nil
return t, nil
}

// arrShrinkage 数组缩容
// shrink 数组缩容
func (a *ArrayList[T]) shrink() {
var newCap int
c, l := a.Cap(), a.Len()
Expand All @@ -101,9 +101,6 @@ func (a *ArrayList[T]) shrink() {
newCap = int(float32(c) * float32(0.625))
} else if c <= 2048 && (c/l >= 4) {
newCap = c / 2
if newCap < 64 {
newCap = 64
}
} else {
// 不满足缩容
return
Expand All @@ -114,9 +111,6 @@ func (a *ArrayList[T]) shrink() {
}

func (a *ArrayList[T]) Len() int {
if a == nil {
return 0
}
return len(a.vals)
}

Expand All @@ -135,7 +129,7 @@ func (a *ArrayList[T]) Range(fn func(index int, t T) error) error {
}

func (a *ArrayList[T]) AsSlice() []T {
slice := make([]T, len(a.vals))
copy(slice, a.vals)
return slice
res := make([]T, len(a.vals))
copy(res, a.vals)
return res
}
Loading

0 comments on commit a953461

Please sign in to comment.