diff --git a/algorithm/algorithm.go b/algo/algorithm.go similarity index 99% rename from algorithm/algorithm.go rename to algo/algorithm.go index 0615065..8c2113c 100644 --- a/algorithm/algorithm.go +++ b/algo/algorithm.go @@ -1,4 +1,4 @@ -package algorithm +package algo import ( "github.com/lxzan/dao/types/cmp" diff --git a/algorithm/algorithm_test.go b/algo/algorithm_test.go similarity index 99% rename from algorithm/algorithm_test.go rename to algo/algorithm_test.go index 117138e..5b6b5e6 100644 --- a/algorithm/algorithm_test.go +++ b/algo/algorithm_test.go @@ -1,4 +1,4 @@ -package algorithm +package algo import ( "github.com/lxzan/dao/hashmap" diff --git a/algorithm/sort.go b/algo/sort.go similarity index 99% rename from algorithm/sort.go rename to algo/sort.go index 3867fa4..5048625 100644 --- a/algorithm/sort.go +++ b/algo/sort.go @@ -1,4 +1,4 @@ -package algorithm +package algo import ( "github.com/lxzan/dao/types/cmp" diff --git a/algorithm/sort_test.go b/algo/sort_test.go similarity index 99% rename from algorithm/sort_test.go rename to algo/sort_test.go index 0fc3048..4d7c45e 100644 --- a/algorithm/sort_test.go +++ b/algo/sort_test.go @@ -1,4 +1,4 @@ -package algorithm +package algo import ( "github.com/lxzan/dao/internal/utils" diff --git a/benchmark/rbtree_test.go b/benchmark/rbtree_test.go index 807d922..9942788 100644 --- a/benchmark/rbtree_test.go +++ b/benchmark/rbtree_test.go @@ -1,7 +1,7 @@ package benchmark import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/rbtree" "math/rand" "testing" @@ -41,7 +41,7 @@ func BenchmarkRBTree_FindAll(b *testing.B) { for j := 0; j < bench_count; j++ { x, y := rand.Intn(bench_count), rand.Intn(bench_count) if x > y { - algorithm.Swap(&x, &y) + algo.Swap(&x, &y) } tree. NewQuery(). @@ -65,7 +65,7 @@ func BenchmarkRBTree_FindAOne(b *testing.B) { for j := 0; j < bench_count; j++ { x, y := rand.Intn(bench_count), rand.Intn(bench_count) if x > y { - algorithm.Swap(&x, &y) + algo.Swap(&x, &y) } tree. NewQuery(). diff --git a/benchmark/sort_test.go b/benchmark/sort_test.go index c0c4e7a..d404449 100644 --- a/benchmark/sort_test.go +++ b/benchmark/sort_test.go @@ -1,7 +1,7 @@ package benchmark import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "sort" "testing" ) @@ -10,7 +10,7 @@ func BenchmarkSort_Quick(b *testing.B) { for i := 0; i < b.N; i++ { var arr = make([]int, bench_count) copy(arr, testvals[:bench_count]) - algorithm.Sort(arr) + algo.Sort(arr) } } diff --git a/dict/dict_test.go b/dict/dict_test.go index 606408d..66ce465 100644 --- a/dict/dict_test.go +++ b/dict/dict_test.go @@ -2,7 +2,7 @@ package dict import ( "fmt" - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/internal/utils" "github.com/lxzan/dao/internal/validator" "github.com/stretchr/testify/assert" @@ -40,8 +40,8 @@ func TestNew(t *testing.T) { return true }) - arr1 = algorithm.Unique(arr1) - arr2 = algorithm.Unique(arr2) + arr1 = algo.Unique(arr1) + arr2 = algo.Unique(arr2) if !utils.IsSameSlice(arr1, arr2) { t.Fatal("error!") } diff --git a/dict/encode.go b/dict/encode.go index 74ec4ce..ffd1db2 100644 --- a/dict/encode.go +++ b/dict/encode.go @@ -1,6 +1,6 @@ package dict -import "github.com/lxzan/dao/algorithm" +import "github.com/lxzan/dao/algo" var defaultIndexes = []uint8{32, 32, 16, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4} @@ -24,7 +24,7 @@ func (c *Dict[T]) getIndex(iter *iterator) int { } func (c *Dict[T]) begin(key string, initialize bool) *iterator { - var iter = &iterator{Node: c.root, Key: key, N: algorithm.Min(len(key), len(c.indexes)-1), Initialize: initialize} + var iter = &iterator{Node: c.root, Key: key, N: algo.Min(len(key), len(c.indexes)-1), Initialize: initialize} var idx = c.getIndex(iter) if iter.Node.Children[idx] == nil { if !iter.Initialize { diff --git a/heap/heap.go b/heap/heap.go index e93d747..50c14a0 100644 --- a/heap/heap.go +++ b/heap/heap.go @@ -1,7 +1,7 @@ package heap import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/internal/utils" "github.com/lxzan/dao/types/cmp" ) @@ -42,7 +42,7 @@ func (c *Heap[T]) SetCap(n int) *Heap[T] { // setWays 设置分叉数 func (c *Heap[T]) setWays(n uint32) *Heap[T] { - n = algorithm.SelectValue(n == 0, Quadratic, n) + n = algo.SelectValue(n == 0, Quadratic, n) if !utils.IsBinaryNumber(n) { panic("incorrect number of ways") } @@ -85,7 +85,7 @@ func (c *Heap[T]) down(i int) { return } - var end = algorithm.Min(base+c.ways, n-1) + var end = algo.Min(base+c.ways, n-1) for j := base + 2; j <= end; j++ { if c.less(j, index) { index = j diff --git a/heap/heap_test.go b/heap/heap_test.go index de58925..bb9f087 100644 --- a/heap/heap_test.go +++ b/heap/heap_test.go @@ -2,7 +2,7 @@ package heap import ( "fmt" - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/internal/utils" "github.com/lxzan/dao/types/cmp" "github.com/stretchr/testify/assert" @@ -25,7 +25,7 @@ func validateHeap[T cmp.Ordered](t *testing.T, h *Heap[T], compare cmp.CompareFu i++ var base = index << h.bits - var end = algorithm.Min(base+h.ways, n-1) + var end = algo.Min(base+h.ways, n-1) for j := base + 1; j <= end; j++ { child := h.data[j] assert.True(t, compare(value, child) <= 0) @@ -37,7 +37,7 @@ func validateHeap[T cmp.Ordered](t *testing.T, h *Heap[T], compare cmp.CompareFu for h.Len() > 0 { keys = append(keys, h.Pop()) } - assert.True(t, algorithm.IsSorted(keys, compare)) + assert.True(t, algo.IsSorted(keys, compare)) } func TestHeap_Random(t *testing.T) { diff --git a/heap/index.go b/heap/index.go index a7c8100..dd9e567 100644 --- a/heap/index.go +++ b/heap/index.go @@ -1,7 +1,7 @@ package heap import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/internal/utils" "github.com/lxzan/dao/types/cmp" ) @@ -51,7 +51,7 @@ func NewIndexedHeap[K cmp.Ordered, V any](ways uint32, lessFunc cmp.LessFunc[K]) // SetForkNumber 设置分叉数 func (c *IndexedHeap[K, V]) setWays(n uint32) *IndexedHeap[K, V] { - n = algorithm.SelectValue(n == 0, Quadratic, n) + n = algo.SelectValue(n == 0, Quadratic, n) if !utils.IsBinaryNumber(n) { panic("incorrect number of ways") } @@ -106,7 +106,7 @@ func (c *IndexedHeap[K, V]) down(i int) { return } - var end = algorithm.Min(base+c.ways, n-1) + var end = algo.Min(base+c.ways, n-1) for j := base + 2; j <= end; j++ { if c.less(j, index) { index = j diff --git a/heap/index_test.go b/heap/index_test.go index 6936a21..02c4179 100644 --- a/heap/index_test.go +++ b/heap/index_test.go @@ -2,7 +2,7 @@ package heap import ( "fmt" - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/internal/utils" "github.com/lxzan/dao/types/cmp" "github.com/stretchr/testify/assert" @@ -23,7 +23,7 @@ func validateIndexedHeap[K cmp.Ordered, V any](t *testing.T, h *IndexedHeap[K, V i++ var base = ele.Index() << h.bits - var end = algorithm.Min(base+h.ways, n-1) + var end = algo.Min(base+h.ways, n-1) for j := base + 1; j <= end; j++ { child := h.GetByIndex(j) assert.True(t, compare(ele.Key(), child.Key()) <= 0) @@ -35,7 +35,7 @@ func validateIndexedHeap[K cmp.Ordered, V any](t *testing.T, h *IndexedHeap[K, V for h.Len() > 0 { keys = append(keys, h.Pop().Key()) } - assert.True(t, algorithm.IsSorted(keys, compare)) + assert.True(t, algo.IsSorted(keys, compare)) } func TestIndexedHeap_Random(t *testing.T) { @@ -89,7 +89,7 @@ func TestIndexedHeap_Sort(t *testing.T) { for h.Len() > 0 { arr = append(arr, h.Pop().Key()) } - assert.True(t, algorithm.IsSorted(arr, func(a, b int) int { + assert.True(t, algo.IsSorted(arr, func(a, b int) int { if a > b { return 1 } else if a < b { diff --git a/rbtree/query.go b/rbtree/query.go index 4504b38..a88e0a9 100644 --- a/rbtree/query.go +++ b/rbtree/query.go @@ -1,7 +1,7 @@ package rbtree import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/stack" "github.com/lxzan/dao/types/cmp" ) @@ -201,7 +201,7 @@ func (c *QueryBuilder[K, V]) getMaxPair(filter func(key K) bool) (result *Pair[K func (c *QueryBuilder[K, V]) getMinPair(filter func(key K) bool) (result *Pair[K, V], exist bool) { var s = stack.Stack[*rbtree_node[K, V]]{} - filter = algorithm.SelectValue(filter == nil, TrueFunc[K], filter) + filter = algo.SelectValue(filter == nil, TrueFunc[K], filter) s.Push(c.tree.root) for s.Len() > 0 { var node = s.Pop() diff --git a/rbtree/rbtree_test.go b/rbtree/rbtree_test.go index d441990..1561f90 100644 --- a/rbtree/rbtree_test.go +++ b/rbtree/rbtree_test.go @@ -2,7 +2,7 @@ package rbtree import ( "fmt" - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/hashmap" "github.com/lxzan/dao/internal/utils" "github.com/lxzan/dao/internal/validator" @@ -166,7 +166,7 @@ func TestRBTree_Between(t *testing.T) { Order(DESC). Limit(limit). FindAll() - var keys1 = algorithm.Map[Pair[string, int], string](values, func(i int, v Pair[string, int]) string { + var keys1 = algo.Map[Pair[string, int], string](values, func(i int, v Pair[string, int]) string { return v.Key }) @@ -177,7 +177,7 @@ func TestRBTree_Between(t *testing.T) { } } sort.Strings(keys2) - algorithm.Reverse(keys2) + algo.Reverse(keys2) if len(keys2) > limit { keys2 = keys2[:limit] } @@ -213,7 +213,7 @@ func TestRBTree_Between(t *testing.T) { Limit(limit). Offset(10). FindAll() - var keys1 = algorithm.Map[Pair[string, int], string](values, func(i int, v Pair[string, int]) string { + var keys1 = algo.Map[Pair[string, int], string](values, func(i int, v Pair[string, int]) string { return v.Key }) @@ -262,7 +262,7 @@ func TestRBTree_Between(t *testing.T) { Order(ASC). Limit(10). FindAll() - var keys1 = algorithm.Map(values1, func(i int, v Pair[int, uint8]) int { return v.Key }) + var keys1 = algo.Map(values1, func(i int, v Pair[int, uint8]) int { return v.Key }) assert.True(t, utils.IsSameSlice(keys1, []int{1, 2, 3})) }) } @@ -285,7 +285,7 @@ func TestRBTree_GreaterEqual(t *testing.T) { Left(func(key string) bool { return key >= left }). Limit(limit). FindAll() - var keys1 = algorithm.Map[Pair[string, int], string](values, func(i int, v Pair[string, int]) string { + var keys1 = algo.Map[Pair[string, int], string](values, func(i int, v Pair[string, int]) string { return v.Key }) var keys2 = make([]string, 0) @@ -322,7 +322,7 @@ func TestRBTree_LessEqual(t *testing.T) { Order(DESC). Limit(limit). FindAll() - var keys1 = algorithm.Map[Pair[string, int], string](results, func(i int, v Pair[string, int]) string { + var keys1 = algo.Map[Pair[string, int], string](results, func(i int, v Pair[string, int]) string { return v.Key }) var keys2 = make([]string, 0) diff --git a/segment_tree/impl.go b/segment_tree/impl.go index db86be8..f560b40 100644 --- a/segment_tree/impl.go +++ b/segment_tree/impl.go @@ -1,7 +1,7 @@ package segment_tree import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" ) type Int64 int64 @@ -33,8 +33,8 @@ type Int64Schema struct { // Merge 合并摘要信息 func (c Int64Schema) Merge(d Int64Schema) Int64Schema { return Int64Schema{ - MaxValue: algorithm.Max(c.MaxValue, d.MaxValue), - MinValue: algorithm.Min(c.MinValue, d.MinValue), + MaxValue: algo.Max(c.MaxValue, d.MaxValue), + MinValue: algo.Min(c.MinValue, d.MinValue), Sum: c.Sum + d.Sum, } } diff --git a/segment_tree/segement_tree_test.go b/segment_tree/segement_tree_test.go index 681dc5e..87df52b 100644 --- a/segment_tree/segement_tree_test.go +++ b/segment_tree/segement_tree_test.go @@ -1,7 +1,7 @@ package segment_tree import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/internal/utils" "testing" ) @@ -30,8 +30,8 @@ func TestSegmentTree_Query(t *testing.T) { } for j := left; j <= right; j++ { result2.Sum += arr[j].Value() - result2.MaxValue = algorithm.Max(result2.MaxValue, arr[j].Value()) - result2.MinValue = algorithm.Min(result2.MinValue, arr[j].Value()) + result2.MaxValue = algo.Max(result2.MaxValue, arr[j].Value()) + result2.MinValue = algo.Min(result2.MinValue, arr[j].Value()) } if result1.Sum != result2.Sum || result1.MinValue != result2.MinValue || result1.MaxValue != result2.MaxValue { @@ -60,8 +60,8 @@ func TestSegmentTree_Query(t *testing.T) { } for j := left; j <= right; j++ { result2.Sum += arr[j].Value() - result2.MaxValue = algorithm.Max(result2.MaxValue, arr[j].Value()) - result2.MinValue = algorithm.Min(result2.MinValue, arr[j].Value()) + result2.MaxValue = algo.Max(result2.MaxValue, arr[j].Value()) + result2.MinValue = algo.Min(result2.MinValue, arr[j].Value()) } if result1.Sum != result2.Sum || result1.MinValue != result2.MinValue || result1.MaxValue != result2.MaxValue { diff --git a/vector/vector.go b/vector/vector.go index 8958daa..68d4e4d 100644 --- a/vector/vector.go +++ b/vector/vector.go @@ -1,7 +1,7 @@ package vector import ( - "github.com/lxzan/dao/algorithm" + "github.com/lxzan/dao/algo" "github.com/lxzan/dao/hashmap" "github.com/lxzan/dao/internal/utils" "github.com/lxzan/dao/types/cmp" @@ -80,49 +80,49 @@ func (c *Vector[K, V]) Exists(id K) (v V, exist bool) { // MapString 转换为字符串数组 func (c *Vector[K, V]) MapString(transfer func(i int, v V) string) *Vector[string, String] { - return NewFromStrings(algorithm.Map(*c, transfer)...) + return NewFromStrings(algo.Map(*c, transfer)...) } // MapInt 转换为int数组 func (c *Vector[K, V]) MapInt(transfer func(i int, v V) int) *Vector[int, Int] { - return NewFromInts(algorithm.Map(*c, transfer)...) + return NewFromInts(algo.Map(*c, transfer)...) } // MapInt64 转换为int64数组 func (c *Vector[K, V]) MapInt64(transfer func(i int, v V) int64) *Vector[int64, Int64] { - return NewFromInt64s(algorithm.Map(*c, transfer)...) + return NewFromInt64s(algo.Map(*c, transfer)...) } // Unique 排序并根据id去重 func (c *Vector[K, V]) Unique() *Vector[K, V] { - *c = algorithm.UniqueBy(*c, func(item V) K { return item.GetID() }) + *c = algo.UniqueBy(*c, func(item V) K { return item.GetID() }) return c } func (c *Vector[K, V]) UniqueByString(transfer func(v V) string) *Vector[K, V] { - *c = algorithm.UniqueBy(*c, transfer) + *c = algo.UniqueBy(*c, transfer) return c } func (c *Vector[K, V]) UniqueByInt(transfer func(v V) int) *Vector[K, V] { - *c = algorithm.UniqueBy(*c, transfer) + *c = algo.UniqueBy(*c, transfer) return c } func (c *Vector[K, V]) UniqueByInt64(transfer func(v V) int64) *Vector[K, V] { - *c = algorithm.UniqueBy(*c, transfer) + *c = algo.UniqueBy(*c, transfer) return c } // Filter 过滤 func (c *Vector[K, V]) Filter(f func(i int, v V) bool) *Vector[K, V] { - *c = algorithm.Filter(*c, f) + *c = algo.Filter(*c, f) return c } // Sort 排序 func (c *Vector[K, V]) Sort() *Vector[K, V] { - algorithm.SortBy(*c, func(a, b V) int { + algo.SortBy(*c, func(a, b V) int { return cmp.Compare(a.GetID(), b.GetID()) }) return c @@ -214,6 +214,6 @@ func (c *Vector[K, V]) Slice(start, end int) *Vector[K, V] { } func (c *Vector[K, V]) Reverse() *Vector[K, V] { - *c = algorithm.Reverse(*c) + *c = algo.Reverse(*c) return c }