Code Monkey home page Code Monkey logo

zset's Introduction

Go Report Card

zset

Implementing sorted set in Redis with golang.

TODO

Key type int64 to string, or just waiting for generics.

Installation

go get -u github.com/liyiheng/zset

Usage

Removed RWLock in the SortedSet. Just implement it yourself if needed.

s := zset.New()
// add data
s.Set(66, 1001, "test1")
s.Set(77, 1002, "test2")
s.Set(88, 1003, "test3")
s.Set(100, 1004, "liyiheng")
s.Set(99, 1005, "test4")
s.Set(44, 1006, "test5")
// update data
s.Set(44, 1001, "test1")

// get rank by id
rank, score, extra := s.GetRank(1004, false)
// get data by rank
id, score, extra := s.GetDataByRank(0, true)
// get data by id
dat, ok := s.GetData(1001)

// delete data by id
s.Delete(1001)

// Increase score
s.IncrBy(5.0, 1001)

// ZRANGE, ASC
five := make([]int64, 0, 5)
s.Range(0, 5, func(score float64, k int64, _ interface{}) {
	five = append(five, k)
})

// ZREVRANGE, DESC
all := make([]int64, 0)
s.RevRange(0, -1, func(score float64, k int64, _ interface{}) {
	all = append(all, k)
})

Benchmark

 OS: Arch Linux 
 Kernel: x86_64 Linux 5.1.5-arch1-2-ARCH
 CPU: Intel Core i7-8750H @ 12x 4.1GHz [46.0°C]
 RAM: 3295MiB / 7821MiB
go test -test.bench=".*"
goos: linux
goarch: amd64
BenchmarkSortedSet_Add-12              	 1000000	      3050 ns/op
BenchmarkSortedSet_GetRank-12          	  500000	      2963 ns/op
BenchmarkSortedSet_GetDataByRank-12    	 2000000	       620 ns/op
PASS

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.