Commit bb681342 authored by suyanlong's avatar suyanlong

Add ordered map and ordered set structure

parent 2d9df6c2
Pipeline #8068 failed with stages
......@@ -158,6 +158,7 @@ build-dep:
@go install mvdan.cc/sh/v3/cmd/gosh@latest
@go install mvdan.cc/unparam@latest
@go install github.com/incu6us/goimports-reviser@master
@go install github.com/psampaz/go-mod-outdated@v0.8.0
# @apt install clang-format or brew isntall clang-format
# @apt install shellcheck or brew install shellcheck
......@@ -184,3 +185,5 @@ checkgofmt: ## get all go files and run go fmt on them
check: aligner-check checkgofmt
modmated:
@go list -u -m -json all | go-mod-outdated
......@@ -55,11 +55,14 @@ require (
require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/cornelk/hashmap v1.0.2-0.20210201213917-c93d96ce6b8a // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f // indirect
github.com/dchest/siphash v1.1.0 // indirect
github.com/devfeel/mapper v0.7.5 // indirect
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 // indirect
github.com/elliotchance/orderedmap v1.4.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
......@@ -163,6 +166,7 @@ require (
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.0.2 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
go.opencensus.io v0.22.3 // indirect
......
......@@ -93,6 +93,10 @@ github.com/coreos/go-systemd v0.0.0-20180511133405-39ca1b05acc7/go.mod h1:F5haX7
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cornelk/hashmap v1.0.1 h1:RXGcy29hEdLLV8T6aK4s+BAd4tq4+3Hq50N2GoG0uIg=
github.com/cornelk/hashmap v1.0.1/go.mod h1:8wbysTUDnwJGrPZ1Iwsou3m+An6sldFrJItjRhfegCw=
github.com/cornelk/hashmap v1.0.2-0.20210201213917-c93d96ce6b8a h1:qv8c3h9mYAfJP3xPuMlhm12taC8VI0Vq4a9mivqCZBc=
github.com/cornelk/hashmap v1.0.2-0.20210201213917-c93d96ce6b8a/go.mod h1:SI48x/mQnWtjsJuM7GgmODo4o5O8FhGJgClCRmtOtIQ=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
......@@ -108,6 +112,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davidlazar/go-crypto v0.0.0-20170701192655-dcfb0a7ac018/go.mod h1:rQYf4tfk5sSwFsnDg3qYaBxSjsD9S8+59vW0dKUgme4=
github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f h1:BOaYiTvg8p9vBUXpklC22XSK/mifLF7lG9jtmYYi3Tc=
github.com/davidlazar/go-crypto v0.0.0-20190912175916-7055855a373f/go.mod h1:rQYf4tfk5sSwFsnDg3qYaBxSjsD9S8+59vW0dKUgme4=
github.com/dchest/siphash v1.1.0 h1:1Rs9eTUlZLPBEvV+2sTaM8O0NWn0ppbgqS7p11aWawI=
github.com/dchest/siphash v1.1.0/go.mod h1:q+IRvb2gOSrUnYoPqHiyHXS0FOBBOdl6tONBlVnOnt4=
github.com/deckarep/golang-set v1.7.2-0.20201129021324-03b572015f8e h1:cZH/C3SR4/BGzKwFwtq5PfYqbKYeJpSFw/4coUAV0/A=
github.com/deckarep/golang-set v1.7.2-0.20201129021324-03b572015f8e/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
......@@ -131,6 +137,8 @@ github.com/eapache/go-resiliency v1.2.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5m
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/elliotchance/orderedmap v1.4.0 h1:wZtfeEONCbx6in1CZyE6bELEt/vFayMvsxqI5SgsR+A=
github.com/elliotchance/orderedmap v1.4.0/go.mod h1:wsDwEaX5jEoyhbs7x93zk2H/qv0zwuhg4inXhDkYqys=
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
......@@ -953,6 +961,8 @@ github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk
github.com/valyala/quicktemplate v1.2.0/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2 h1:ViCftzy+iQHmxIJPZqJH3i0znSxFufq075LnMwt2/VQ=
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=
github.com/wasmerio/go-ext-wasm v0.3.1/go.mod h1:VGyarTzasuS7k5KhSIGpM3tciSZlkP31Mp9VJTHMMeI=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 h1:EKhdznlJHPMoKr0XTrX+IlJs1LH3lyx2nfr1dOlZ79k=
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1/go.mod h1:8UvriyWtv5Q5EOgjHaSseUEdkQfvwFv1I/In/O2M9gc=
......
package tool
// go get github.com/cornelk/hashmap@master
// go get -u github.com/elliotchance/orderedmap@master
......@@ -79,9 +79,9 @@ func TestMapper(t *testing.T) {
valMap["Score"] = 100
valMap["Time"] = time.Now()
assert.NotNil(t, mapper.Mapper(student, user))
assert.NotNil(t, mapper.AutoMapper(student, teacher))
assert.NotNil(t, mapper.MapperMap(valMap, userMap))
assert.Nil(t, mapper.Mapper(student, user))
assert.Nil(t, mapper.AutoMapper(student, teacher))
assert.Nil(t, mapper.MapperMap(valMap, userMap))
fmt.Println("student:", student)
fmt.Println("user:", user)
......
package tool
// https://github.com/elliotchance/orderedmap
package tool
import (
"testing"
"github.com/elliotchance/orderedmap"
"github.com/stretchr/testify/assert"
)
func TestNewOrderedMap(t *testing.T) {
m := orderedmap.NewOrderedMap()
assert.IsType(t, &orderedmap.OrderedMap{}, m)
}
func TestOrderedMapGet(t *testing.T) {
t.Run("ReturnsNotOKIfStringKeyDoesntExist", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
_, ok := m.Get("foo")
assert.False(t, ok)
})
t.Run("ReturnsNotOKIfNonStringKeyDoesntExist", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
_, ok := m.Get(123)
assert.False(t, ok)
})
t.Run("ReturnsOKIfKeyExists", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "bar")
_, ok := m.Get("foo")
assert.True(t, ok)
})
t.Run("ReturnsValueForKey", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "bar")
value, _ := m.Get("foo")
assert.Equal(t, "bar", value)
})
t.Run("ReturnsDynamicValueForKey", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "baz")
value, _ := m.Get("foo")
assert.Equal(t, "baz", value)
})
t.Run("KeyDoesntExistOnNonEmptyMap", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "baz")
_, ok := m.Get("bar")
assert.False(t, ok)
})
t.Run("ValueForKeyDoesntExistOnNonEmptyMap", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "baz")
value, _ := m.Get("bar")
assert.Nil(t, value)
})
}
func TestOrderedMapSet(t *testing.T) {
t.Run("ReturnsTrueIfStringKeyIsNew", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
ok := m.Set("foo", "bar")
assert.True(t, ok)
})
t.Run("ReturnsTrueIfNonStringKeyIsNew", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
ok := m.Set(123, "bar")
assert.True(t, ok)
})
t.Run("ValueCanBeNonString", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
ok := m.Set(123, true)
assert.True(t, ok)
})
t.Run("ReturnsFalseIfKeyIsNotNew", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "bar")
ok := m.Set("foo", "bar")
assert.False(t, ok)
})
t.Run("SetThreeDifferentKeys", func(t *testing.T) {
m := orderedmap.NewOrderedMap()
m.Set("foo", "bar")
m.Set("baz", "qux")
ok := m.Set("quux", "corge")
assert.True(t, ok)
})
}
package tool
// https://github.com/wangjia184/sortedset
package tool
import (
"testing"
"github.com/wangjia184/sortedset"
)
// https://github.com/wangjia184/sortedset
func TestOrderedSet(t *testing.T) {
// create a new set
set := sortedset.New()
// fill in new node
set.AddOrUpdate("a", 89, "Kelly")
set.AddOrUpdate("b", 100, "Staley")
set.AddOrUpdate("c", 100, "Jordon")
set.AddOrUpdate("d", -321, "Park")
set.AddOrUpdate("e", 101, "Albert")
set.AddOrUpdate("f", 99, "Lyman")
set.AddOrUpdate("g", 99, "Singleton")
set.AddOrUpdate("h", 70, "Audrey")
// update an existing node by key
set.AddOrUpdate("e", 99, "ntrnrt")
// get the node by key
set.GetByKey("b")
// remove node by key
set.Remove("b")
// get the number of nodes in this set
set.GetCount()
// find the rank(postion) in the set.
set.FindRank("d") // return 1 here
// get and remove the node with minimum score
set.PopMin()
// get the node with maximum score
set.PeekMax()
// get the node at rank 1 (the node with minimum score)
set.GetByRank(1, false)
// get & remove the node at rank -1 (the node with maximum score)
set.GetByRank(-1, true)
// get the node with the 2nd highest maximum score
set.GetByRank(-2, false)
// get nodes with in rank range [1, -1], that is all nodes actually
set.GetByRankRange(1, -1, false)
// get & remove the 2nd/3rd nodes in reserve order
set.GetByRankRange(-2, -3, true)
// get the nodes whose score are within the interval [60,100]
set.GetByScoreRange(60, 100, nil)
// get the nodes whose score are within the interval (60,100]
set.GetByScoreRange(60, 100, &sortedset.GetByScoreRangeOptions{
ExcludeStart: true,
})
// get the nodes whose score are within the interval [60,100)
set.GetByScoreRange(60, 100, &sortedset.GetByScoreRangeOptions{
ExcludeEnd: true,
})
// get the nodes whose score are within the interval [60,100] in reverse order
set.GetByScoreRange(100, 60, nil)
// get the top 2 nodes with lowest scores within the interval [60,100]
set.GetByScoreRange(60, 100, &sortedset.GetByScoreRangeOptions{
Limit: 2,
})
// get the top 2 nodes with highest scores within the interval [60,100]
set.GetByScoreRange(100, 60, &sortedset.GetByScoreRangeOptions{
Limit: 2,
})
// get the top 2 nodes with highest scores within the interval (60,100)
set.GetByScoreRange(100, 60, &sortedset.GetByScoreRangeOptions{
Limit: 2,
ExcludeStart: true,
ExcludeEnd: true,
})
}
package tool
// go get ithub.com/liyue201/gostl
// https://github.com/cornelk/hashmap
......@@ -47,3 +47,11 @@ func Exist(path string) bool {
// go get -u github.com/zekroTJA/timedmap
// https://github.com/m-zajac/json2go
// https://github.com/mitchellh/mapstructure
// https://github.com/ulule/deepcopier --replace copier
// https://github.com/mohae/deepcopy --replace copier
// https://github.com/antlabs/deepcopy --replace copier
// go get github.com/fatih/structs
// https://github.com/airplayx/gormat
// https://github.com/golangci/awesome-go-linters
// https://github.com/jedib0t/go-pretty
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment