mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 14:12:28 +08:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fafb59dad6 | ||
|
|
e36c01cac9 | ||
|
|
aa0afa8d94 | ||
|
|
18e0031e0e | ||
|
|
a5018c110c | ||
|
|
142deb83b2 | ||
|
|
ccc0188352 |
@@ -4,13 +4,12 @@
|
|||||||
<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)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -36,9 +35,9 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.6. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.2.7. </b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.6 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.2.7 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|||||||
@@ -4,13 +4,12 @@
|
|||||||
<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)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -36,9 +35,9 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.6。</b>
|
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.2.7。</b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.2.6 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.2.7 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) []
|
|||||||
preIndex--
|
preIndex--
|
||||||
}
|
}
|
||||||
|
|
||||||
slice[preIndex+1] = preItem
|
slice[preIndex+1] = currentItem
|
||||||
}
|
}
|
||||||
|
|
||||||
return slice
|
return slice
|
||||||
|
|||||||
@@ -89,10 +89,10 @@ func TestInsertionSort(t *testing.T) {
|
|||||||
asssert := internal.NewAssert(t, "TestInsertionSort")
|
asssert := internal.NewAssert(t, "TestInsertionSort")
|
||||||
|
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
sortedPeopleByAge := InsertionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{e 28} {a 20} {c 17} {b 10} {d 8}]"
|
||||||
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
actual := fmt.Sprintf("%v", sortedPeopleByAge)
|
||||||
|
|
||||||
asssert.Equal(expected, actual)
|
asssert.Equal(expected, actual)
|
||||||
|
|||||||
@@ -6,9 +6,10 @@ package convertor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -21,14 +22,44 @@ func ToBool(s string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToBytes convert interface to bytes
|
// ToBytes convert interface to bytes
|
||||||
func ToBytes(data any) ([]byte, error) {
|
func ToBytes(value any) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
v := reflect.ValueOf(value)
|
||||||
enc := gob.NewEncoder(&buf)
|
|
||||||
err := enc.Encode(data)
|
switch value.(type) {
|
||||||
if err != nil {
|
case int, int8, int16, int32, int64:
|
||||||
return nil, err
|
number := v.Int()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
|
number := v.Uint()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case float32:
|
||||||
|
number := float32(v.Float())
|
||||||
|
bits := math.Float32bits(number)
|
||||||
|
bytes := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case float64:
|
||||||
|
number := v.Float()
|
||||||
|
bits := math.Float64bits(number)
|
||||||
|
bytes := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case bool:
|
||||||
|
return strconv.AppendBool([]byte{}, v.Bool()), nil
|
||||||
|
case string:
|
||||||
|
return []byte(v.String()), nil
|
||||||
|
case []byte:
|
||||||
|
return v.Bytes(), nil
|
||||||
|
default:
|
||||||
|
newValue, err := json.Marshal(value)
|
||||||
|
return newValue, err
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToChar convert string to char slice
|
// ToChar convert string to char slice
|
||||||
|
|||||||
@@ -42,14 +42,21 @@ func TestToBytes(t *testing.T) {
|
|||||||
"1",
|
"1",
|
||||||
}
|
}
|
||||||
expected := [][]byte{
|
expected := [][]byte{
|
||||||
{3, 4, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{3, 2, 0, 0},
|
{102, 97, 108, 115, 101},
|
||||||
{4, 12, 0, 1, 49},
|
{49},
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual, _ := ToBytes(cases[i])
|
actual, _ := ToBytes(cases[i])
|
||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bytesData, err := ToBytes("abc")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
assert.Equal("abc", ToString(bytesData))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToInt(t *testing.T) {
|
func TestToInt(t *testing.T) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package datastructure
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/v2/datastructure"
|
"github.com/duke-git/lancet/v2/datastructure"
|
||||||
)
|
)
|
||||||
@@ -144,6 +145,27 @@ func (link *SinglyLink[T]) DeleteAt(index int) error {
|
|||||||
return errors.New("delete error")
|
return errors.New("delete error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteValue delete value in singly linklist
|
||||||
|
func (link *SinglyLink[T]) DeleteValue(value T) {
|
||||||
|
if link.Head == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dummyHead := datastructure.NewLinkNode(value)
|
||||||
|
dummyHead.Next = link.Head
|
||||||
|
current := dummyHead
|
||||||
|
|
||||||
|
for current.Next != nil {
|
||||||
|
if reflect.DeepEqual(current.Next.Value, value) {
|
||||||
|
current.Next = current.Next.Next
|
||||||
|
link.length--
|
||||||
|
} else {
|
||||||
|
current = current.Next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
link.Head = dummyHead.Next
|
||||||
|
}
|
||||||
|
|
||||||
// Reverse the linked list
|
// Reverse the linked list
|
||||||
func (link *SinglyLink[T]) Reverse() {
|
func (link *SinglyLink[T]) Reverse() {
|
||||||
var pre, next *datastructure.LinkNode[T]
|
var pre, next *datastructure.LinkNode[T]
|
||||||
|
|||||||
@@ -111,6 +111,24 @@ func TestSinglyLink_DeleteAtTail(t *testing.T) {
|
|||||||
assert.Equal(expected, values)
|
assert.Equal(expected, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSinglyLink_DeleteValue(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteValue")
|
||||||
|
|
||||||
|
link := NewSinglyLink[int]()
|
||||||
|
|
||||||
|
link.InsertAtTail(1)
|
||||||
|
link.InsertAtTail(2)
|
||||||
|
link.InsertAtTail(2)
|
||||||
|
link.InsertAtTail(3)
|
||||||
|
link.InsertAtTail(4)
|
||||||
|
|
||||||
|
link.DeleteValue(2)
|
||||||
|
assert.Equal([]int{1, 3, 4}, link.Values())
|
||||||
|
|
||||||
|
link.DeleteValue(1)
|
||||||
|
assert.Equal([]int{3, 4}, link.Values())
|
||||||
|
}
|
||||||
|
|
||||||
func TestSinglyLink_DeleteAt(t *testing.T) {
|
func TestSinglyLink_DeleteAt(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
assert := internal.NewAssert(t, "TestSinglyLink_DeleteAt")
|
||||||
|
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ func TestToUnix(t *testing.T) {
|
|||||||
func TestToFormat(t *testing.T) {
|
func TestToFormat(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestToFormat")
|
assert := internal.NewAssert(t, "TestToFormat")
|
||||||
|
|
||||||
tm, err := NewFormat("2022/03/18 17:04:05")
|
_, err := NewFormat("2022/03/18 17:04:05")
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
tm, err = NewFormat("2022-03-18 17:04:05")
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
t.Log("ToFormat -> ", tm.ToFormat())
|
t.Log("ToFormat -> ", tm.ToFormat())
|
||||||
@@ -31,23 +31,22 @@ func TestToFormat(t *testing.T) {
|
|||||||
func TestToFormatForTpl(t *testing.T) {
|
func TestToFormatForTpl(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestToFormatForTpl")
|
assert := internal.NewAssert(t, "TestToFormatForTpl")
|
||||||
|
|
||||||
tm, err := NewFormat("2022/03/18 17:04:05")
|
_, err := NewFormat("2022/03/18 17:04:05")
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
tm, err = NewFormat("2022-03-18 17:04:05")
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
t.Log("ToFormatForTpl -> ", tm.ToFormatForTpl("2006/01/02 15:04:05"))
|
t.Log("ToFormatForTpl -> ", tm.ToFormatForTpl("2006/01/02 15:04:05"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToIso8601(t *testing.T) {
|
func TestToIso8601(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestToIso8601")
|
assert := internal.NewAssert(t, "TestToIso8601")
|
||||||
|
|
||||||
tm, err := NewISO8601("2022-03-18 17:04:05")
|
_, err := NewISO8601("2022-03-18 17:04:05")
|
||||||
assert.IsNotNil(err)
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
tm, err = NewISO8601("2006-01-02T15:04:05.999Z")
|
tm, err := NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
t.Log("ToIso8601 -> ", tm.ToIso8601())
|
t.Log("ToIso8601 -> ", tm.ToIso8601())
|
||||||
|
|||||||
Reference in New Issue
Block a user