Skip to content

Commit

Permalink
更新slicemap结构
Browse files Browse the repository at this point in the history
  • Loading branch information
pangdogs committed Dec 3, 2024
1 parent 360296b commit 3dd5440
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions utils/generic/slicemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ func (m *SliceMap[K, V]) Delete(k K) bool {
return ok
}

func (m SliceMap[K, V]) Index(k K) int {
idx, _ := slices.BinarySearchFunc(m, KV[K, V]{K: k}, func(a, b KV[K, V]) int {
return cmp.Compare(a.K, b.K)
})
return idx
}

func (m SliceMap[K, V]) Get(k K) (V, bool) {
idx, ok := slices.BinarySearchFunc(m, KV[K, V]{K: k}, func(a, b KV[K, V]) int {
return cmp.Compare(a.K, b.K)
Expand Down
6 changes: 6 additions & 0 deletions utils/generic/slicemap_unordered.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func (m *UnorderedSliceMap[K, V]) Delete(k K) bool {
return idx >= 0
}

func (m UnorderedSliceMap[K, V]) Index(k K) int {
return slices.IndexFunc(m, func(kv UnorderedKV[K, V]) bool {
return kv.K == k
})
}

func (m UnorderedSliceMap[K, V]) Get(k K) (V, bool) {
idx := slices.IndexFunc(m, func(kv UnorderedKV[K, V]) bool {
return kv.K == k
Expand Down

0 comments on commit 3dd5440

Please sign in to comment.