Skip to content

Commit

Permalink
Merge pull request #6 from lxzan/dev
Browse files Browse the repository at this point in the history
Adjusting the order of vector generic parameters
  • Loading branch information
lxzan committed Dec 25, 2023
2 parents c323dee + 89b6b80 commit 6e9a952
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
70 changes: 35 additions & 35 deletions vector/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,68 @@ import (
)

// New 创建动态数组
func New[D Document[K], K cmp.Ordered](capacity int) *Vector[D, K] {
c := Vector[D, K](make([]D, 0, capacity))
func New[K cmp.Ordered, V Document[K]](capacity int) *Vector[K, V] {
c := Vector[K, V](make([]V, 0, capacity))
return &c
}

// NewFromDocs 从可变参数创建动态数组
func NewFromDocs[D Document[K], K cmp.Ordered](values ...D) *Vector[D, K] {
c := Vector[D, K](values)
func NewFromDocs[K cmp.Ordered, V Document[K]](values ...V) *Vector[K, V] {
c := Vector[K, V](values)
return &c
}

// NewFromInts 创建动态数组
func NewFromInts(values ...int) *Vector[Int, int] {
func NewFromInts(values ...int) *Vector[int, Int] {
var b = *(*[]Int)(unsafe.Pointer(&values))
v := Vector[Int, int](b)
v := Vector[int, Int](b)
return &v
}

// NewFromInt64s 创建动态数组
func NewFromInt64s(values ...int64) *Vector[Int64, int64] {
func NewFromInt64s(values ...int64) *Vector[int64, Int64] {
var b = *(*[]Int64)(unsafe.Pointer(&values))
v := Vector[Int64, int64](b)
v := Vector[int64, Int64](b)
return &v
}

// NewFromStrings 创建动态数组
func NewFromStrings(values ...string) *Vector[String, string] {
func NewFromStrings(values ...string) *Vector[string, String] {
var b = *(*[]String)(unsafe.Pointer(&values))
v := Vector[String, string](b)
v := Vector[string, String](b)
return &v
}

// Vector 动态数组
type Vector[D Document[K], K cmp.Ordered] []D
type Vector[K cmp.Ordered, V Document[K]] []V

// Reset 重置
func (c *Vector[D, K]) Reset() {
func (c *Vector[K, V]) Reset() {
*c = (*c)[:0]
}

// Len 获取元素数量
func (c *Vector[D, K]) Len() int {
func (c *Vector[K, V]) Len() int {
return len(*c)
}

// Get 根据下标取值
func (c *Vector[D, K]) Get(index int) D {
func (c *Vector[K, V]) Get(index int) V {
return (*c)[index]
}

// Update 根据下标修改值
func (c *Vector[D, K]) Update(index int, value D) {
func (c *Vector[K, V]) Update(index int, value V) {
(*c)[index] = value
}

// Elem 取值
func (c *Vector[D, K]) Elem() []D {
func (c *Vector[K, V]) Elem() []V {
return *c
}

// Exists 根据id判断某条数据是否存在
func (c *Vector[D, K]) Exists(id K) (v D, exist bool) {
func (c *Vector[K, V]) Exists(id K) (v V, exist bool) {
for _, item := range *c {
if item.GetID() == id {
return item, true
Expand All @@ -80,31 +80,31 @@ func (c *Vector[D, K]) Exists(id K) (v D, exist bool) {
}

// Unique 排序并根据id去重
func (c *Vector[D, K]) Unique() *Vector[D, K] {
*c = algorithm.UniqueBy(*c, func(item D) K {
func (c *Vector[K, V]) Unique() *Vector[K, V] {
*c = algorithm.UniqueBy(*c, func(item V) K {
return item.GetID()
})
return c
}

// Filter 过滤
func (c *Vector[D, K]) Filter(f func(i int, v D) bool) *Vector[D, K] {
func (c *Vector[K, V]) Filter(f func(i int, v V) bool) *Vector[K, V] {
*c = algorithm.Filter(*c, f)
return c
}

// Sort 排序
func (c *Vector[D, K]) Sort() *Vector[D, K] {
algorithm.SortBy(*c, func(a, b D) int {
func (c *Vector[K, V]) Sort() *Vector[K, V] {
algorithm.SortBy(*c, func(a, b V) int {
return cmp.Compare(a.GetID(), b.GetID())
})
return c
}

// IdList 获取id数组
func (c *Vector[D, K]) IdList() []K {
var d D
switch any(d).(type) {
// GetIdList 获取id数组
func (c *Vector[K, V]) GetIdList() []K {
var v V
switch any(v).(type) {
case Int, Int64, String:
var keys = *(*[]K)(unsafe.Pointer(c))
return keys
Expand All @@ -118,21 +118,21 @@ func (c *Vector[D, K]) IdList() []K {
}

// ToMap 生成map[K]D
func (c *Vector[D, K]) ToMap() hashmap.HashMap[K, D] {
var m = hashmap.New[K, D](c.Len())
func (c *Vector[K, V]) ToMap() hashmap.HashMap[K, V] {
var m = hashmap.New[K, V](c.Len())
for _, item := range *c {
m.Set(item.GetID(), item)
}
return m
}

// PushBack 向尾部追加元素
func (c *Vector[D, K]) PushBack(v D) {
func (c *Vector[K, V]) PushBack(v V) {
*c = append(*c, v)
}

// PopFront 从头部弹出元素
func (c *Vector[D, K]) PopFront() (value D) {
func (c *Vector[K, V]) PopFront() (value V) {
switch c.Len() {
case 0:
return value
Expand All @@ -144,7 +144,7 @@ func (c *Vector[D, K]) PopFront() (value D) {
}

// PopBack 从尾部弹出元素
func (c *Vector[D, K]) PopBack() (value D) {
func (c *Vector[K, V]) PopBack() (value V) {
n := c.Len()
switch n {
case 0:
Expand All @@ -157,7 +157,7 @@ func (c *Vector[D, K]) PopBack() (value D) {
}

// Range 遍历
func (c *Vector[D, K]) Range(f func(i int, v D) bool) {
func (c *Vector[K, V]) Range(f func(i int, v V) bool) {
for index, value := range *c {
if !f(index, value) {
return
Expand All @@ -166,18 +166,18 @@ func (c *Vector[D, K]) Range(f func(i int, v D) bool) {
}

// Clone 拷贝
func (c *Vector[D, K]) Clone() *Vector[D, K] {
func (c *Vector[K, V]) Clone() *Vector[K, V] {
var d = utils.Clone(*c)
return &d
}

// Slice 截取子数组
func (c *Vector[D, K]) Slice(start, end int) *Vector[D, K] {
func (c *Vector[K, V]) Slice(start, end int) *Vector[K, V] {
var children = (*c)[start:end]
return &children
}

func (c *Vector[D, K]) Reverse() *Vector[D, K] {
func (c *Vector[K, V]) Reverse() *Vector[K, V] {
*c = algorithm.Reverse(*c)
return c
}
24 changes: 12 additions & 12 deletions vector/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestUser_GetID(t *testing.T) {
var docs Vector[user, string]
var docs Vector[string, user]
docs = append(docs, user{ID: "a"})
docs = append(docs, user{ID: "c"})
docs = append(docs, user{ID: "c"})
Expand All @@ -30,20 +30,20 @@ func (u user) GetID() string {

func TestNewFromInts(t *testing.T) {
var a = NewFromInts(1, 3, 5)
var b = a.IdList()
var b = a.GetIdList()
assert.ElementsMatch(t, b, []int{1, 3, 5})
}

func TestNewFromInt64s(t *testing.T) {
var a = NewFromInt64s(1, 3, 5)
var b = a.IdList()
var b = a.GetIdList()
assert.ElementsMatch(t, b, []int64{1, 3, 5})
}

func TestVector_Keys(t *testing.T) {
t.Run("", func(t *testing.T) {
var a = NewFromStrings("a", "b", "c")
var b = a.IdList()
var b = a.GetIdList()
assert.ElementsMatch(t, b, []string{"a", "b", "c"})
assert.Equal(t, a.Get(0).GetID(), "a")

Expand All @@ -56,17 +56,17 @@ func TestVector_Keys(t *testing.T) {
})

t.Run("", func(t *testing.T) {
var docs = NewFromDocs[user, string](
var docs = NewFromDocs[string, user](
user{ID: "a"},
user{ID: "b"},
user{ID: "c"},
)
assert.ElementsMatch(t, docs.IdList(), []string{"a", "b", "c"})
assert.ElementsMatch(t, docs.GetIdList(), []string{"a", "b", "c"})
})
}

func TestVector_Exists(t *testing.T) {
var v = New[Int, int](8)
var v = New[int, Int](8)
v.PushBack(1)
v.PushBack(3)
v.PushBack(5)
Expand All @@ -88,7 +88,7 @@ func TestVector_Exists(t *testing.T) {
}

func TestVector_PushBack(t *testing.T) {
var v = New[Int, int](8)
var v = New[int, Int](8)
v.PushBack(1)
v.PushBack(3)
v.PushBack(5)
Expand All @@ -102,7 +102,7 @@ func TestVector_PushBack(t *testing.T) {
}

func TestVector_PopFront(t *testing.T) {
var v = New[Int, int](8)
var v = New[int, Int](8)
v.PushBack(1)
v.PushBack(3)
v.PushBack(5)
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestVector_Range(t *testing.T) {
}

func TestVector_ToMap(t *testing.T) {
var a = NewFromDocs[user, string](
var a = NewFromDocs[string, user](
user{ID: "a"},
user{ID: "b"},
user{ID: "c"},
Expand All @@ -151,7 +151,7 @@ func TestVector_ToMap(t *testing.T) {
func TestVector_Slice(t *testing.T) {
var a = NewFromStrings("a", "b", "c", "d")
var b = a.Slice(1, 3)
var values = b.IdList()
var values = b.GetIdList()
assert.ElementsMatch(t, values, []string{"b", "c"})

assert.Equal(t, a.Len(), 4)
Expand All @@ -160,7 +160,7 @@ func TestVector_Slice(t *testing.T) {
}

func TestVector_Sort(t *testing.T) {
var a = NewFromInts(1, 3, 5, 2, 4, 6).Sort().IdList()
var a = NewFromInts(1, 3, 5, 2, 4, 6).Sort().GetIdList()
assert.True(t, utils.IsSameSlice(a, []int{1, 2, 3, 4, 5, 6}))
}

Expand Down

0 comments on commit 6e9a952

Please sign in to comment.