mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 06:32:28 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6314889c6a | ||
|
|
294bd5a5ed | ||
|
|
ca44815fd5 | ||
|
|
bcd1cabf80 | ||
|
|
4edefcca67 | ||
|
|
fab24c8d12 | ||
|
|
604acd9b07 |
@@ -1,10 +1,10 @@
|
|||||||
<div align=center>
|
<div align=center>
|
||||||
<img src="./logo.png" width="200" height="200"/>
|
<a href="https://uvdream.github.io/lancet-docs/en/"><img src="./logo.png" width="200" height="200"/></a>
|
||||||
|
|
||||||
<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)
|
||||||
@@ -68,7 +68,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
## [lancet API doc](https://uvdream.github.io/lancet-docs/) Thanks [@UvDream](https://github.com/UvDream) for contributing.
|
||||||
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
<div align=center>
|
<div align=center>
|
||||||
<img src="./logo.png" width="200" height="200"/>
|
<a href="https://uvdream.github.io/lancet-docs/"><img src="./logo.png" width="200" height="200"/><a/>
|
||||||
|
|
||||||
<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)
|
||||||
@@ -68,7 +68,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API文档
|
## API文档
|
||||||
|
## [lancet API doc](https://uvdream.github.io/lancet-docs/) 感谢[@UvDream](https://github.com/UvDream)整理
|
||||||
### 1. algorithm算法包实现一些基本算法。eg. sort, search.
|
### 1. algorithm算法包实现一些基本算法。eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -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))
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,24 +4,24 @@
|
|||||||
// Package datetime implements some functions to format date and time.
|
// Package datetime implements some functions to format date and time.
|
||||||
// Note:
|
// Note:
|
||||||
// 1. `format` param in FormatTimeToStr function should be as flow:
|
// 1. `format` param in FormatTimeToStr function should be as flow:
|
||||||
//"yyyy-mm-dd hh:mm:ss"
|
// "yyyy-mm-dd hh:mm:ss"
|
||||||
//"yyyy-mm-dd hh:mm"
|
// "yyyy-mm-dd hh:mm"
|
||||||
//"yyyy-mm-dd hh"
|
// "yyyy-mm-dd hh"
|
||||||
//"yyyy-mm-dd"
|
// "yyyy-mm-dd"
|
||||||
//"yyyy-mm"
|
// "yyyy-mm"
|
||||||
//"mm-dd"
|
// "mm-dd"
|
||||||
//"dd-mm-yy hh:mm:ss"
|
// "dd-mm-yy hh:mm:ss"
|
||||||
//"yyyy/mm/dd hh:mm:ss"
|
// "yyyy/mm/dd hh:mm:ss"
|
||||||
//"yyyy/mm/dd hh:mm"
|
// "yyyy/mm/dd hh:mm"
|
||||||
//"yyyy/mm/dd hh"
|
// "yyyy/mm/dd hh"
|
||||||
//"yyyy/mm/dd"
|
// "yyyy/mm/dd"
|
||||||
//"yyyy/mm"
|
// "yyyy/mm"
|
||||||
//"mm/dd"
|
// "mm/dd"
|
||||||
//"dd/mm/yy hh:mm:ss"
|
// "dd/mm/yy hh:mm:ss"
|
||||||
//"yyyy"
|
// "yyyy"
|
||||||
//"mm"
|
// "mm"
|
||||||
//"hh:mm:ss"
|
// "hh:mm:ss"
|
||||||
//"mm:ss"
|
// "mm:ss"
|
||||||
package datetime
|
package datetime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -147,16 +147,32 @@ func EndOfDay(t time.Time) time.Time {
|
|||||||
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfWeek return beginning week, week begin from Sunday
|
// BeginOfWeek return beginning week, default week begin from Sunday
|
||||||
func BeginOfWeek(t time.Time) time.Time {
|
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
|
||||||
y, m, d := t.AddDate(0, 0, 0-int(BeginOfDay(t).Weekday())).Date()
|
var beginFromWeekday = time.Sunday
|
||||||
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
if len(beginFrom) > 0 {
|
||||||
|
beginFromWeekday = beginFrom[0]
|
||||||
|
}
|
||||||
|
y, m, d := t.AddDate(0, 0, int(beginFromWeekday-t.Weekday())).Date()
|
||||||
|
beginOfWeek := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
|
if beginOfWeek.After(t) {
|
||||||
|
return beginOfWeek.AddDate(0, 0, -7)
|
||||||
|
}
|
||||||
|
return beginOfWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfWeek return end week time, week end with Saturday
|
// EndOfWeek return end week time, default week end with Saturday
|
||||||
func EndOfWeek(t time.Time) time.Time {
|
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
|
||||||
y, m, d := BeginOfWeek(t).AddDate(0, 0, 7).Add(-time.Nanosecond).Date()
|
var endWithWeekday = time.Saturday
|
||||||
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
if len(endWith) > 0 {
|
||||||
|
endWithWeekday = endWith[0]
|
||||||
|
}
|
||||||
|
y, m, d := t.AddDate(0, 0, int(endWithWeekday-t.Weekday())).Date()
|
||||||
|
var endWithWeek = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
|
if endWithWeek.Before(t) {
|
||||||
|
endWithWeek = endWithWeek.AddDate(0, 0, 7)
|
||||||
|
}
|
||||||
|
return endWithWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfMonth return beginning of month
|
// BeginOfMonth return beginning of month
|
||||||
|
|||||||
@@ -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