Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module sortedset/m

go 1.15
6 changes: 6 additions & 0 deletions sortedset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type SortedSet struct {
dict map[string]*SortedSetNode
}

func (this *SortedSet) Print() {
if this.header != nil {
this.header.Print()
}
}
func createNode(level int, score SCORE, key string, value interface{}) *SortedSetNode {
node := SortedSetNode{
score: score,
Expand Down Expand Up @@ -96,6 +101,7 @@ func (this *SortedSet) insertNode(score SCORE, key string, value interface{}) *S
* if the element is already inside or not. */
level := randomLevel()

//level := this.level + 1
if level > this.level { // add a new level
for i := this.level; i < level; i++ {
rank[i] = 0
Expand Down
47 changes: 45 additions & 2 deletions sortedset_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package sortedset

import (
"fmt"
"math/rand"
"testing"
"time"
)

func checkOrder(t *testing.T, nodes []*SortedSetNode, expectedOrder []string) {
Expand All @@ -20,16 +23,23 @@ func TestCase1(t *testing.T) {
sortedset := New()

sortedset.AddOrUpdate("a", 89, "Kelly")
sortedset.Print()
sortedset.AddOrUpdate("b", 100, "Staley")
sortedset.Print()
sortedset.AddOrUpdate("c", 100, "Jordon")
sortedset.Print()
sortedset.AddOrUpdate("d", -321, "Park")
sortedset.Print()
sortedset.AddOrUpdate("e", 101, "Albert")
sortedset.Print()
sortedset.AddOrUpdate("f", 99, "Lyman")
sortedset.Print()
sortedset.AddOrUpdate("g", 99, "Singleton")
sortedset.Print()
sortedset.AddOrUpdate("h", 70, "Audrey")

sortedset.Print()
sortedset.AddOrUpdate("e", 99, "ntrnrt")

sortedset.Print()
sortedset.Remove("b")

node := sortedset.GetByRank(3, false)
Expand Down Expand Up @@ -162,3 +172,36 @@ func TestCase2(t *testing.T) {
nodes = sortedset.GetByScoreRange(500, -500, nil)
checkOrder(t, nodes, []string{"g", "f", "e", "a", "h"})
}
func TestCase3(t *testing.T) {
sortedset := New()
testSet := make(map[string]int64)
rand.Seed(0)
count := 100000
for len(testSet) < count {
k := rand.Int63()
ks := fmt.Sprintf("%d", k)
if _, ok := testSet[ks]; !ok {
testSet[ks] = k
}
}

startts := time.Now()

for k, v := range testSet {
sortedset.AddOrUpdate(k, SCORE(v), v)
}

dur := time.Since(startts)
t.Logf("insert cost %v ", dur/time.Duration(count))

startts = time.Now()
for {
min := sortedset.PopMin()
if min == nil {
break
}
}
dur = time.Since(startts)
t.Logf("pop cost %v ", dur/time.Duration(count))

}
16 changes: 16 additions & 0 deletions sortedsetnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package sortedset

import "fmt"

type SortedSetLevel struct {
forward *SortedSetNode
span int64
Expand All @@ -47,3 +49,17 @@ func (this *SortedSetNode) Key() string {
func (this *SortedSetNode) Score() SCORE {
return this.score
}
func (this *SortedSetNode) Print() {
for i, _ := range this.level {
x := this.level[i].forward
if x == nil {
continue
}
fmt.Printf("No %d", i)
for x != nil {
fmt.Printf(" %d:%p:[%d]->", x.score, x, x.level[i].span)
x = x.level[i].forward
}
fmt.Printf("nil\n")
}
}