GithubHelp home page GithubHelp logo

boycetien / sortedset Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wangjia184/sortedset

0.0 1.0 0.0 63 KB

An ordered collection implemented in Golang with O(log(N)) time complexity on adding / searching / removing

License: BSD 2-Clause "Simplified" License

Go 100.00%

sortedset's Introduction

Sorted Set in Golang

Sorted Set is a data-struct inspired by the one from Redis. It allows fast access by key or score.

Property Type Description
key string The identifier of the node. It must be unique within the set.
value interface {} value associated with this node
score int64 score is in order to take the sorted set ordered. It may be repeated.

Each node in the set is associated with a key. While keys are unique, scores may be repeated. Nodes are taken in order instead of ordered afterwards, from low score to high score. If scores are the same, the node is ordered by its key in lexicographic order. Each node in the set is associated with rank, which represents the position of the node in the sorted set. The rank is 1-based, that is to say, rank 1 is the node with minimum score.

Sorted Set is implemented basing on skip list and hash map internally. With sorted sets you can add, remove, or update nodes in a very fast way (in a time proportional to the logarithm of the number of nodes). You can also get ranges by score or by rank (position) in a very fast way. Accessing the middle of a sorted set is also very fast, so you can use Sorted Sets as a smart list of non repeating nodes where you can quickly access everything you need: nodes in order, fast existence test, fast access to nodes in the middle!

A typical use case of sorted set is a leader board in a massive online game, where every time a new score is submitted you update it using AddOrUpdate(). You can easily take the top users using GetByRankRange(), you can also, given an user id, return its rank in the listing using FindRank(). Using FindRank() and GetByRankRange() together you can show users with a score similar to a given user. All very quickly.

Documentation

https://godoc.org/github.com/wangjia184/sortedset

sortedset's People

Contributors

wangjia184 avatar

Watchers

 avatar

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.