Skip to content

Commit

Permalink
添加 mapx 的例子,准备 v0.0.6 (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
flycash authored Jan 31, 2023
1 parent 8160bae commit 4c1af75
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 开发中

# v0.0.6
- [queue: 基于semaphore的并发阻塞队列实现](https://github.com/gotomicro/ekit/pull/129)
- [mapx: hashmap实现](https://github.com/gotomicro/ekit/pull/132)
- [mapx: 添加 Keys 方法](https://github.com/gotomicro/ekit/pull/134)
Expand Down
3 changes: 3 additions & 0 deletions mapx/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func (m *HashMap[T, ValType]) newNode(key Hashable, val ValType) *node[T, ValTyp
}

type Hashable interface {
// Code 返回该元素的哈希值
// 注意:哈希值应该尽可能的均匀以避免冲突
Code() uint64
// Equals 比较两个元素是否相等。如果返回 true,那么我们会认为两个键是一样的。
Equals(key any) bool
}

Expand Down
64 changes: 64 additions & 0 deletions mapx/map_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2021 gotomicro
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mapx_test

import (
"fmt"

"github.com/gotomicro/ekit/mapx"
)

func ExampleNewHashMap() {
m := mapx.NewHashMap[MockKey, int](10)
_ = m.Put(MockKey{}, 123)
val, _ := m.Get(MockKey{})
fmt.Println(val)
// Output:
// 123
}

type MockKey struct {
values []int
}

func (m MockKey) Code() uint64 {
res := 3
for _, v := range m.values {
res += v * 7
}
return uint64(res)
}

func (m MockKey) Equals(key any) bool {
k, ok := key.(MockKey)
if !ok {
return false
}
if len(k.values) != len(m.values) {
return false
}
if k.values == nil && m.values != nil {
return false
}
if k.values != nil && m.values == nil {
return false
}
for i, v := range m.values {
if v != k.values[i] {
return false
}
}
return true
}
31 changes: 31 additions & 0 deletions mapx/treemap_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2021 gotomicro
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mapx_test

import (
"fmt"

"github.com/gotomicro/ekit"
"github.com/gotomicro/ekit/mapx"
)

func ExampleNewTreeMap() {
m, _ := mapx.NewTreeMap[int, int](ekit.ComparatorRealNumber[int])
_ = m.Put(1, 11)
val, _ := m.Get(1)
fmt.Println(val)
// Output:
// 11
}

0 comments on commit 4c1af75

Please sign in to comment.