github.com/go-ego/cedar@v0.10.2/README.md (about)

     1  # cedar
     2  [![Build Status](https://travis-ci.org/go-ego/cedar.svg)](https://travis-ci.org/go-ego/cedar)
     3  [![CircleCI Status](https://circleci.com/gh/go-ego/cedar.svg?style=shield)](https://circleci.com/gh/go-ego/cedar)
     4  [![codecov](https://codecov.io/gh/go-ego/cedar/branch/master/graph/badge.svg)](https://codecov.io/gh/go-ego/cedar)
     5  [![Go Report Card](https://goreportcard.com/badge/github.com/go-ego/cedar)](https://goreportcard.com/report/github.com/go-ego/cedar)
     6  [![GoDoc](https://godoc.org/github.com/go-ego/cedar?status.svg)](https://godoc.org/github.com/go-ego/cedar)
     7  [![Release](https://github-release-version.herokuapp.com/github/go-ego/cedar/release.svg?style=flat)](https://github.com/go-ego/cedar/releases/latest)
     8  [![Join the chat at https://gitter.im/go-ego/ego](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-ego/ego?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
     9  
    10  Package `cedar` implementes double-array trie, base on [cedar-go](https://github.com/adamzy/cedar-go).
    11  
    12  It is a [Golang](https://golang.org/) port of [cedar](http://www.tkl.iis.u-tokyo.ac.jp/~ynaga/cedar) which is written in C++ by Naoki Yoshinaga. `cedar-go` currently implements the `reduced` verion of cedar.
    13  This package is not thread safe if there is one goroutine doing insertions or deletions.
    14  
    15  ## Install
    16  ```
    17  go get github.com/go-ego/cedar
    18  ```
    19  
    20  ## Usage
    21  ```go
    22  package main
    23  
    24  import (
    25  	"fmt"
    26  
    27  	"github.com/go-ego/cedar"
    28  )
    29  
    30  func main() {
    31  	// create a new cedar trie.
    32  	trie := cedar.New()
    33  
    34  	// a helper function to print the id-key-value triple given trie node id
    35  	printIdKeyValue := func(id int) {
    36  		// the key of node `id`.
    37  		key, _ := trie.Key(id)
    38  		// the value of node `id`.
    39  		value, _ := trie.Value(id)
    40  		fmt.Printf("%d\t%s:%v\n", id, key, value)
    41  	}
    42  
    43  	// Insert key-value pairs.
    44  	// The order of insertion is not important.
    45  	trie.Insert([]byte("How many"), 0)
    46  	trie.Insert([]byte("How many loved"), 1)
    47  	trie.Insert([]byte("How many loved your moments"), 2)
    48  	trie.Insert([]byte("How many loved your moments of glad grace"), 3)
    49  	//
    50  	trie.Insert([]byte("姑苏"), 4)
    51  	trie.Insert([]byte("姑苏城外"), 5)
    52  	trie.Insert([]byte("姑苏城外寒山寺"), 6)
    53  
    54  	// Get the associated value of a key directly.
    55  	value, _ := trie.Get([]byte("How many loved your moments of glad grace"))
    56  	fmt.Println(value)
    57  
    58  	// Or, jump to the node first,
    59  	id, _ := trie.Jump([]byte("How many loved your moments"), 0)
    60  	// then get the key and the value
    61  	printIdKeyValue(id)
    62  
    63  	fmt.Println("\nPrefixMatch\nid\tkey:value")
    64  	for _, id := range trie.PrefixMatch([]byte("How many loved your moments of glad grace"), 0) {
    65  		printIdKeyValue(id)
    66  	}
    67  
    68  	fmt.Println("\nPrefixPredict\nid\tkey:value")
    69  	for _, id := range trie.PrefixPredict([]byte("姑苏"), 0) {
    70  		printIdKeyValue(id)
    71  	}
    72  }
    73  ```
    74  will produce
    75  ```
    76  3
    77  281	How many loved your moments:2
    78  
    79  PrefixMatch
    80  id	key:value
    81  262	How many:0
    82  268	How many loved:1
    83  281	How many loved your moments:2
    84  296	How many loved your moments of glad grace:3
    85  
    86  PrefixPredict
    87  id	key:value
    88  303	姑苏:4
    89  309	姑苏城外:5
    90  318	姑苏城外寒山寺:6
    91  ```
    92  ## License
    93  
    94  Under the GPL-3.0 License.