Giter Site home page Giter Site logo

frankfanslc / go-vector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from erizocosmico/go-vector

0.0 1.0 0.0 9 KB

Persistent bit-partitioned vector trie implementation for Go, a slice-like persistent data structure.

License: Other

Go 100.00%

go-vector's Introduction

go-vector GoDoc Build Status codecov Go Report Card License: MIT

Persistent bit-partitioned vector trie implementation for Go, a slice-like persistent data structure. Pretty much like the one used by the Clojure language.

Install

go get github.com/erizocosmico/go-vector

Usage

vemtpy := vector.New() // empty vector
v := vector.New(1, 2, 3, 4, 5) // vector with items

v = v.Append(6) // new vector with 6 appended at the end

elem := v.Get(2) // elem is 3

v.Last() // last element
v.First() // first element
v.Tail() // new vector without the first element

v = v.Set(0, -1) // Set element 0 to -1

// Iterate over all elements.
err := v.Range(func(x interface{}) error {
    // do something with x
    return nil
})

v.Slice() // return the elements as a slice

squared := v.Map(func(x interface{}) interface{} {
    x := x.(int)
    return x * x
})

even := v.Filter(func(x interface{}) bool {
    return x.(int) % 2 == 0
})

firstThree := v.Take(3)
allButFirst := v.Drop(1)

vector.Equal(vector.New(-1, 2, 3, 4, 5), v) // will output true

// Check if they're equal using a custom function. This will return false
// because the vectors are not equal.
vector.EqualFunc(vector.New(1, 2, 3, 4, 5), v, func(a, b interface{}) bool {
    return a.(int) == b.(int)
})

For more info, check out the package documentation.

Thread safety

This implementation is not intended to be thread safe.

Type safety

Due to Go's lack of generic types, this data structure uses interface{}, that means you can store any kind of element and not just a fixed type. Also, you will need to convert the results obtained from the vector from interface{} to the actual type.

Benchmarks

BenchmarkAppend/10-4         	 3000000	       547 ns/op	     423 B/op	       4 allocs/op
BenchmarkAppend/100-4        	 3000000	       517 ns/op	     423 B/op	       4 allocs/op
BenchmarkAppend/1000-4       	 3000000	       502 ns/op	     423 B/op	       4 allocs/op
BenchmarkGet/10-4            	100000000	        20.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkGet/100-4           	100000000	        20.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkGet/1000-4          	100000000	        20.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkSet/10-4            	10000000	       236 ns/op	     248 B/op	       4 allocs/op
BenchmarkSet/100-4           	 3000000	       567 ns/op	    1104 B/op	       5 allocs/op
BenchmarkSet/1000-4          	 2000000	       726 ns/op	    1136 B/op	       5 allocs/op

License

MIT, see LICENSE

go-vector's People

Contributors

erizocosmico 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.