• Stars
    star
    375
  • Rank 114,096 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fast and easy-to-use skip list for Go.

Skip List in Golang

Go Go Doc Go Report Coverage Status

Skip list is an ordered map. See wikipedia page skip list to learn algorithm details about this data structure.

Highlights in this implementation:

  • Built-in types can be used as key with predefined key types. See Int and related constants as a sample.
  • Support custom comparable function so that any type can be used as key.
  • Key sort order can be changed quite easily. See Reverse and LessThanFunc.
  • Rand source and max level can be changed per list. It can be useful in performance critical scenarios.

Install

Install this package through go get.

    go get github.com/huandu/skiplist

Basic Usage

Here is a quick sample.

package main

import (
    "fmt"

    "github.com/huandu/skiplist"
)

func main() {
    // Create a skip list with int key.
    list := skiplist.New(skiplist.Int)

    // Add some values. Value can be anything.
    list.Set(12, "hello world")
    list.Set(34, 56)
    list.Set(78, 90.12)

    // Get element by index.
    elem := list.Get(34)                // Value is stored in elem.Value.
    fmt.Println(elem.Value)             // Output: 56
    next := elem.Next()                 // Get next element.
    prev := next.Prev()                 // Get previous element.
    fmt.Println(next.Value, prev.Value) // Output: 90.12    56

    // Or, directly get value just like a map
    val, ok := list.GetValue(34)
    fmt.Println(val, ok) // Output: 56  true

    // Find first elements with score greater or equal to key
    foundElem := list.Find(30)
    fmt.Println(foundElem.Key(), foundElem.Value) // Output: 34 56

    // Remove an element for key.
    list.Remove(34)
}

Using GreaterThanFunc and LessThanFunc

Define your own GreaterThanFunc or LessThanFunc to use any custom type as the key in a skip list.

The signature of GreaterThanFunc are LessThanFunc are the same. The only difference between them is that LessThanFunc reverses result returned by custom func to make the list ordered by key in a reversed order.

type T struct {
    Rad float64
}
list := New(GreaterThanFunc(func(k1, k2 interface{}) int {
    s1 := math.Sin(k1.(T).Rad)
    s2 := math.Sin(k2.(T).Rad)

    if s1 > s2 {
        return 1
    } else if s1 < s2 {
        return -1
    }

    return 0
}))
list.Set(T{math.Pi / 8}, "sin(Ο€/8)")
list.Set(T{math.Pi / 2}, "sin(Ο€/2)")
list.Set(T{math.Pi}, "sin(Ο€)")

fmt.Println(list.Front().Value) // Output: sin(Ο€)
fmt.Println(list.Back().Value)  // Output: sin(Ο€/2)

License

This library is licensed under MIT license. See LICENSE for details.

More Repositories

1

go-sqlbuilder

A flexible and powerful SQL string builder library plus a zero-config ORM.
Go
1,445
star
2

xstrings

Implements string functions widely used in other languages but absent in Go.
Go
1,398
star
3

facebook

A Facebook Graph API SDK For Go.
Go
1,330
star
4

go-clone

Clone any Go data structure deeply and thoroughly.
Go
302
star
5

go-tls

A bit safer approach to implement Thread Local Storage (TLS) for Go 1.7+.
Go
162
star
6

goroutine

[DEPRECATED] Expose goroutine id to wild world. Alternative approach is https://github.com/huandu/go-tls
Go
111
star
7

go-assert

Magic assert macros for Go.
Go
27
star
8

gin

gin - a simple & efficient HTML5 game engine
JavaScript
25
star
9

node-ascii85

Ascii85 (Base85) encoding/decoding module for node.js.
JavaScript
18
star
10

ObjCMixin

A ruby-like mixin in Object-C.
Objective-C
10
star
11

gin-samples

Samples using the gin
JavaScript
9
star
12

spritemapper

A Java program which creates sprite maps (or sprite sheets) from a set of input images.
Java
9
star
13

nocycle.js

Detect cycle `require` in node.js.
JavaScript
9
star
14

go-magicstring

Attach arbitrary data to a Go string
Go
7
star
15

handyhttpd

A handy http server to enable public http access on any folder within a few seconds.
Go
6
star
16

yuki

A flexible and efficient mysql client written in C
C
5
star
17

bashtools

Several bash scripts to make life easier
Shell
3
star
18

go-singleton

Generic singleton for Go.
Go
2
star
19

heybox-url

Calculate Heybox url hash.
JavaScript
2
star
20

node-facter

A wrapper for puppet `facter` tool
JavaScript
2
star
21

express-handler

Provide a per-request `this` object for express router handlers.
JavaScript
1
star
22

design-doc-chs

1
star