Skip to content

Commit

Permalink
Merge pull request #8 from lxzan/dev
Browse files Browse the repository at this point in the history
add methods for vector
  • Loading branch information
lxzan committed Dec 29, 2023
2 parents 2056d82 + f4328e7 commit 274c760
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
8 changes: 4 additions & 4 deletions algorithm/helper.go → algorithm/algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ func UniqueBy[T any, K cmp.Ordered, A ~[]T](arr A, getKey func(item T) K) A {
// Sum 求和
func Sum[T cmp.Number](arr []T) T {
var sum T
return Reduce(arr, sum, func(s T, item T) T { return s + item })
return Reduce(arr, sum, func(s T, i int, v T) T { return s + v })
}

// Reduce 对数组中的每个元素按序执行一个提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,
// 最后将其结果汇总为单个返回值。
func Reduce[T any, S any](arr []T, initialValue S, reducer func(s S, item T) S) S {
for _, item := range arr {
initialValue = reducer(initialValue, item)
func Reduce[T any, S any](arr []T, initialValue S, reducer func(s S, i int, v T) S) S {
for index, value := range arr {
initialValue = reducer(initialValue, index, value)
}
return initialValue
}
Expand Down
4 changes: 2 additions & 2 deletions algorithm/helper_test.go → algorithm/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func TestIsZero(t *testing.T) {
func TestReduce(t *testing.T) {
t.Run("", func(t *testing.T) {
var arr = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var sum = Reduce(arr, 0, func(summarize int, item int) int {
var sum = Reduce(arr, 0, func(summarize int, i int, item int) int {
return summarize + item
})
assert.Equal(t, sum, 55)
Expand All @@ -196,7 +196,7 @@ func TestReduce(t *testing.T) {
t.Run("", func(t *testing.T) {
var arr = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var m = hashmap.New[int, struct{}](10)
Reduce(arr, m, func(s hashmap.HashMap[int, struct{}], item int) hashmap.HashMap[int, struct{}] {
Reduce(arr, m, func(s hashmap.HashMap[int, struct{}], i int, item int) hashmap.HashMap[int, struct{}] {
s.Set(item, struct{}{})
return s
})
Expand Down
16 changes: 16 additions & 0 deletions vector/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,27 @@ func (c *Vector[K, V]) Len() int {
return len(*c)
}

func (c *Vector[K, V]) Cap() int {
return cap(*c)
}

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

// Front 获取头部元素
// 注意: 未作越界检查
func (c *Vector[K, V]) Front() V {
return c.Get(0)
}

// Back 获取尾部元素
// 注意: 未作越界检查
func (c *Vector[K, V]) Back() V {
return c.Get(c.Len() - 1)
}

// Delete 根据下标删除某个元素
// 性能不好, 少用
func (c *Vector[K, V]) Delete(index int) {
Expand Down
6 changes: 6 additions & 0 deletions vector/vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,9 @@ func TestVector_Delete(t *testing.T) {
assert.True(t, utils.IsSameSlice(v.Elem(), []Int{1, 5, 7}))
})
}

func TestVector_Get(t *testing.T) {
var v = NewFromInts(1, 3, 5)
assert.Equal(t, v.Front(), Int(1))
assert.Equal(t, v.Back(), Int(5))
}

0 comments on commit 274c760

Please sign in to comment.