Giter Site home page Giter Site logo

skiplist's Introduction

SkipList

Package skiplist implements in-memory skiplist.

https://godoc.org/github.com/hit9/skiplist

Example

package main

import (
	"fmt"
	"github.com/hit9/skiplist"
	"os"
)

// Item implements skiplist.Item
type Item struct {
	score int
	value string
}

// Less returns true if item is less than another.
func (item Item) Less(than skiplist.Item) bool {
	return item.score < than.(Item).score
}

func main() {
	// New skiplist with max level 16
	sl := skiplist.New(16)
	// Put some items.
	sl.Put(Item{3, "data1"})
	sl.Put(Item{1, "data2"})
	sl.Put(Item{5, "data3"})
	sl.Put(Item{2, "data4"})
	sl.Put(Item{6, "data5"})
	// Get one item.
	item := sl.Get(Item{score: 2})
	fmt.Println(item.(Item).value) // "data4"
	// Print the skiplist.
	sl.Print(os.Stdout)
}

Output:

Level[0]: {1 data2} -> {2 data4} -> {3 data1} -> {5 data3} -> {6 data5} -> nil
Level[1]: {1 data2} -> {2 data4} -> {5 data3} -> {6 data5} -> nil
Level[2]: {1 data2} -> {5 data3} -> {6 data5} -> nil
Level[3]: {1 data2} -> {6 data5} -> nil
Level[4]: {6 data5} -> nil

Complexity

Operation Put/Get/Delete time complexity are all O(logN). And the space complexity is O(NlogN).

License

BSD.

skiplist's People

Contributors

damnever avatar hit9 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.