zset

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2025 License: AGPL-3.0, MIT Imports: 3 Imported by: 0

README

Go Report Card

zset

Implementing sorted set in Redis with golang.

Installation

go get -u github.com/liyiheng/zset

Usage

s := zset.New[int64]()
// add data
s.Set(66, 1001)
s.Set(77, 1002)
s.Set(88, 1003)
s.Set(100, 1004)
s.Set(99, 1005)
s.Set(44, 1006)
// update data
s.Set(44, 1001)

// get rank by key
rank, score := s.GetRank(1004, false)
// get data by rank
id, score := s.GetDataByRank(0, true)

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) {
	five = append(five, k)
})

// ZREVRANGE, DESC
all := make([]int64, 0)
s.RevRange(0, -1, func(score float64, k int64) {
	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

Documentation

Overview

Package zset is a port of t_zset.c in Redis

  • Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  • Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  • All rights reserved. *
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are met: *
  • * Redistributions of source code must retain the above copyright notice,
  • this list of conditions and the following disclaimer.
  • * Redistributions in binary form must reproduce the above copyright
  • notice, this list of conditions and the following disclaimer in the
  • documentation and/or other materials provided with the distribution.
  • * Neither the name of Redis nor the names of its contributors may be used
  • to endorse or promote products derived from this software without
  • specific prior written permission. *
  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  • AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  • IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  • ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  • LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  • CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  • SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  • INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  • CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  • ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  • POSSIBILITY OF SUCH DAMAGE.

Index

Constants

View Source
const (
	NoRank = -1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Key

type Key interface {
	cmp.Ordered
}

Key constraint

type SortedSet

type SortedSet[K Key] struct {
	// contains filtered or unexported fields
}

SortedSet is the final exported sorted set we can use

func New

func New[K Key]() *SortedSet[K]

New creates a new SortedSet and return its pointer

func (*SortedSet[K]) Delete

func (z *SortedSet[K]) Delete(key K) (ok bool)

Delete removes an element from the SortedSet by its key.

func (*SortedSet[K]) GetDataByRank

func (z *SortedSet[K]) GetDataByRank(rank int64, reverse bool) (key K, score float64)

GetDataByRank returns the id,score and extra data of an element which found by position in the rank. The parameter rank is the position, reverse says if in the descend rank.

func (*SortedSet[K]) GetRank

func (z *SortedSet[K]) GetRank(key K, reverse bool) (rank int64, score float64)

GetRank returns position,score and extra data of an element which found by the parameter key. The parameter reverse determines the rank is descent or ascend, true means descend and false means ascend.

func (*SortedSet[K]) GetScore

func (z *SortedSet[K]) GetScore(key K) (score float64, ok bool)

GetScore implements ZScore

func (*SortedSet[K]) IncrBy

func (z *SortedSet[K]) IncrBy(score float64, key K) float64

IncrBy ..

func (*SortedSet[K]) Length

func (z *SortedSet[K]) Length() int64

Length returns counts of elements

func (*SortedSet[K]) Range

func (z *SortedSet[K]) Range(start, end int64, f func(float64, K))

Range implements ZRANGE

func (*SortedSet[K]) RevRange

func (z *SortedSet[K]) RevRange(start, end int64, f func(float64, K))

RevRange implements ZREVRANGE

func (*SortedSet[K]) Set

func (z *SortedSet[K]) Set(score float64, key K)

Set is used to add or update an element

Directories

Path Synopsis
Package cmp provides types and functions related to comparing ordered values.
Package cmp provides types and functions related to comparing ordered values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL