mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-23 13:52:26 +08:00
Compare commits
5 Commits
4edefcca67
...
v2.1.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6314889c6a | ||
|
|
294bd5a5ed | ||
|
|
ca44815fd5 | ||
|
|
bcd1cabf80 | ||
|
|
fab24c8d12 |
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
|
|||||||
@@ -93,6 +93,46 @@ func (hm *HashMap) Contains(key any) bool {
|
|||||||
return node != nil
|
return node != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iterate executes iteratee funcation for every key and value pair of hashmap (random order)
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value any)) {
|
||||||
|
if hm.size > 0 {
|
||||||
|
for i := 0; i < len(hm.table); i++ {
|
||||||
|
item := hm.table[i]
|
||||||
|
if item != nil {
|
||||||
|
iteratee(item.key, item.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keys returns a slice of the hashmap's keys (random order)
|
||||||
|
func (hm *HashMap) Keys() []any {
|
||||||
|
keys := make([]any, int(hm.size))
|
||||||
|
index := 0
|
||||||
|
if hm.size > 0 {
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
keys[index] = key
|
||||||
|
index++
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values returns a slice of the hashmap's keys (random order)
|
||||||
|
func (hm *HashMap) Values() []any {
|
||||||
|
values := make([]any, int(hm.size))
|
||||||
|
index := 0
|
||||||
|
if hm.size > 0 {
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
values[index] = value
|
||||||
|
index++
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
func (hm *HashMap) resize() {
|
func (hm *HashMap) resize() {
|
||||||
hm.capacity <<= 1
|
hm.capacity <<= 1
|
||||||
|
|
||||||
|
|||||||
@@ -52,3 +52,20 @@ func TestHashMap_Contains(t *testing.T) {
|
|||||||
hm.Put("abc", 3)
|
hm.Put("abc", 3)
|
||||||
assert.Equal(true, hm.Contains("abc"))
|
assert.Equal(true, hm.Contains("abc"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHashMap_KeysValues(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHashMap_KeysValues")
|
||||||
|
|
||||||
|
hm := NewHashMap()
|
||||||
|
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
values := hm.Values()
|
||||||
|
t.Log(keys, values)
|
||||||
|
|
||||||
|
assert.Equal(3, len(values))
|
||||||
|
assert.Equal(3, len(keys))
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ import (
|
|||||||
- [Put](#Put)
|
- [Put](#Put)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contains](#Contains)
|
- [Contains](#Contains)
|
||||||
|
- [Iterate](#Iterate)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -207,3 +210,105 @@ func main() {
|
|||||||
fmt.Println(hm.Contains("b")) //false
|
fmt.Println(hm.Contains("b")) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Iterate">Iterate</span>
|
||||||
|
|
||||||
|
<p>Executes iteratee funcation for every key and value pair of hashmap.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value any))
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
fmt.Println(key)
|
||||||
|
fmt.Println(value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
|
||||||
|
<p>Return a slice of the hashmap's keys (random order).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Keys() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
fmt.Println(keys) //[]interface{"a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
|
||||||
|
<p>Return a slice of the hashmap's values (random order).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Values() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
values := hm.Values()
|
||||||
|
fmt.Println(values) //[]interface{2, 1, 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,13 @@ import (
|
|||||||
|
|
||||||
- [NewHashMap](#NewHashMap)
|
- [NewHashMap](#NewHashMap)
|
||||||
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
|
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
|
||||||
|
|
||||||
- [Get](#Get)
|
- [Get](#Get)
|
||||||
- [Put](#Put)
|
- [Put](#Put)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contains](#Contains)
|
- [Contains](#Contains)
|
||||||
|
- [Iterate](#Iterate)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -203,3 +205,104 @@ func main() {
|
|||||||
fmt.Println(hm.Contains("b")) //false
|
fmt.Println(hm.Contains("b")) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Iterate">Iterate</span>
|
||||||
|
|
||||||
|
<p>迭代hashmap,对每个key和value执行iteratee函数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value any))
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
fmt.Println(key)
|
||||||
|
fmt.Println(value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
|
||||||
|
<p>返回hashmap所有key的切片 (随机顺序)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Keys() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
fmt.Println(keys) //[]interface{"a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
|
||||||
|
<p>返回hashmap所有值的切片 (随机顺序).</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Values() []any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
values := hm.Values()
|
||||||
|
fmt.Println(values) //[]interface{2, 1, 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user