Thanks to visit codestin.com
Credit goes to github.com

Skip to content
/ rbtree Public

Purely functional red-black trees for Go

License

ichiban/rbtree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rbtree - purely functional red-black trees for Go

Go Reference

rbtree provides a map-like data structure rbtree.Map[K, V] that can rollback to any points in time.

It allocates internal nodes with bump allocation, and they point each other with indices, which minimizes the impact on GC.

This implementation is roughly based on red-black tree from Purely Functional Data Structures by Chris Okasaki.

Installation

go get github.com/ichiban/rbtree

Basic Usage

package main

import (
	"fmt"

	"github.com/ichiban/rbtree"
)

func main() {
	var m rbtree.Map[string, int]
	m.Set("a", 1) // {a: 1}
	m.Set("b", 2) // {a: 1, b: 2}
	snapshot := m
	m.Set("c", 3) 
	m.Set("a", 4) // Overwrites "a".
	m.Delete("b") // Deletes "b".

	for k, v := range rbtree.All(m) {
		fmt.Printf("%s: %v\n", k, v)
	}

	fmt.Println("rollback to snapshot")
	m = snapshot

	for k, v := range rbtree.All(m) {
		fmt.Printf("%s: %v\n", k, v)
	}
}
a: 4
c: 3
rollback to snapshot
a: 1
b: 2

License

Distributed under the MIT license. See LICENSE for more information.

Contributing

  1. Fork it (https://github.com/ichiban/rbtree/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request

About

Purely functional red-black trees for Go

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages