From f4328e7dc99dc5f1ac56300fe5d42c648c218be4 Mon Sep 17 00:00:00 2001 From: lxzan Date: Thu, 28 Dec 2023 09:28:15 +0800 Subject: [PATCH] add methods for vector --- algorithm/{helper.go => algorithm.go} | 8 ++++---- algorithm/{helper_test.go => algorithm_test.go} | 4 ++-- vector/vector.go | 16 ++++++++++++++++ vector/vector_test.go | 6 ++++++ 4 files changed, 28 insertions(+), 6 deletions(-) rename algorithm/{helper.go => algorithm.go} (93%) rename algorithm/{helper_test.go => algorithm_test.go} (96%) diff --git a/algorithm/helper.go b/algorithm/algorithm.go similarity index 93% rename from algorithm/helper.go rename to algorithm/algorithm.go index 1d6db90..0615065 100644 --- a/algorithm/helper.go +++ b/algorithm/algorithm.go @@ -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 } diff --git a/algorithm/helper_test.go b/algorithm/algorithm_test.go similarity index 96% rename from algorithm/helper_test.go rename to algorithm/algorithm_test.go index 2af0eb0..117138e 100644 --- a/algorithm/helper_test.go +++ b/algorithm/algorithm_test.go @@ -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) @@ -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 }) diff --git a/vector/vector.go b/vector/vector.go index 91fe238..8958daa 100644 --- a/vector/vector.go +++ b/vector/vector.go @@ -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) { diff --git a/vector/vector_test.go b/vector/vector_test.go index 846032d..a903717 100644 --- a/vector/vector_test.go +++ b/vector/vector_test.go @@ -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)) +}