| 1 | # go-patricia # |
| 2 | |
| 3 | **Documentation**: [GoDoc](http://godoc.org/github.com/tchap/go-patricia/patricia)<br /> |
| 4 | **Test Coverage**: [](https://coveralls.io/r/tchap/go-patricia) |
| 6 | |
| 7 | ## About ## |
| 8 | |
| 9 | A generic patricia trie (also called radix tree) implemented in Go (Golang). |
| 10 | |
| 11 | The patricia trie as implemented in this library enables fast visiting of items |
| 12 | in some particular ways: |
| 13 | |
| 14 | 1. visit all items saved in the tree, |
| 15 | 2. visit all items matching particular prefix (visit subtree), or |
| 16 | 3. given a string, visit all items matching some prefix of that string. |
| 17 | |
| 18 | `[]byte` type is used for keys, `interface{}` for values. |
| 19 | |
| 20 | `Trie` is not thread safe. Synchronize the access yourself. |
| 21 | |
| 22 | ### State of the Project ### |
| 23 | |
| 24 | Apparently some people are using this, so the API should not change often. |
| 25 | Any ideas on how to make the library better are still welcome. |
| 26 | |
| 27 | More (unit) testing would be cool as well... |
| 28 | |
| 29 | ## Usage ## |
| 30 | |
| 31 | Import the package from GitHub first. |
| 32 | |
| 33 | ```go |
| 34 | import "github.com/tchap/go-patricia/patricia" |
| 35 | ``` |
| 36 | |
| 37 | You can as well use gopkg.in thingie: |
| 38 | |
| 39 | ```go |
| 40 | import "gopkg.in/tchap/go-patricia.v2/patricia" |
| 41 | ``` |
| 42 | |
| 43 | Then you can start having fun. |
| 44 | |
| 45 | ```go |
| 46 | printItem := func(prefix patricia.Prefix, item patricia.Item) error { |
| 47 | fmt.Printf("%q: %v\n", prefix, item) |
| 48 | return nil |
| 49 | } |
| 50 | |
| 51 | // Create a new default trie (using the default parameter values). |
| 52 | trie := NewTrie() |
| 53 | |
| 54 | // Create a new custom trie. |
| 55 | trie := NewTrie(MaxPrefixPerNode(16), MaxChildrenPerSparseNode(10)) |
| 56 | |
| 57 | // Insert some items. |
| 58 | trie.Insert(Prefix("Pepa Novak"), 1) |
| 59 | trie.Insert(Prefix("Pepa Sindelar"), 2) |
| 60 | trie.Insert(Prefix("Karel Macha"), 3) |
| 61 | trie.Insert(Prefix("Karel Hynek Macha"), 4) |
| 62 | |
| 63 | // Just check if some things are present in the tree. |
| 64 | key := Prefix("Pepa Novak") |
| 65 | fmt.Printf("%q present? %v\n", key, trie.Match(key)) |
| 66 | // "Pepa Novak" present? true |
| 67 | key = Prefix("Karel") |
| 68 | fmt.Printf("Anybody called %q here? %v\n", key, trie.MatchSubtree(key)) |
| 69 | // Anybody called "Karel" here? true |
| 70 | |
| 71 | // Walk the tree in alphabetical order. |
| 72 | trie.Visit(printItem) |
| 73 | // "Karel Hynek Macha": 4 |
| 74 | // "Karel Macha": 3 |
| 75 | // "Pepa Novak": 1 |
| 76 | // "Pepa Sindelar": 2 |
| 77 | |
| 78 | // Walk a subtree. |
| 79 | trie.VisitSubtree(Prefix("Pepa"), printItem) |
| 80 | // "Pepa Novak": 1 |
| 81 | // "Pepa Sindelar": 2 |
| 82 | |
| 83 | // Modify an item, then fetch it from the tree. |
| 84 | trie.Set(Prefix("Karel Hynek Macha"), 10) |
| 85 | key = Prefix("Karel Hynek Macha") |
| 86 | fmt.Printf("%q: %v\n", key, trie.Get(key)) |
| 87 | // "Karel Hynek Macha": 10 |
| 88 | |
| 89 | // Walk prefixes. |
| 90 | prefix := Prefix("Karel Hynek Macha je kouzelnik") |
| 91 | trie.VisitPrefixes(prefix, printItem) |
| 92 | // "Karel Hynek Macha": 10 |
| 93 | |
| 94 | // Delete some items. |
| 95 | trie.Delete(Prefix("Pepa Novak")) |
| 96 | trie.Delete(Prefix("Karel Macha")) |
| 97 | |
| 98 | // Walk again. |
| 99 | trie.Visit(printItem) |
| 100 | // "Karel Hynek Macha": 10 |
| 101 | // "Pepa Sindelar": 2 |
| 102 | |
| 103 | // Delete a subtree. |
| 104 | trie.DeleteSubtree(Prefix("Pepa")) |
| 105 | |
| 106 | // Print what is left. |
| 107 | trie.Visit(printItem) |
| 108 | // "Karel Hynek Macha": 10 |
| 109 | ``` |
| 110 | |
| 111 | ## License ## |
| 112 | |
| 113 | MIT, check the `LICENSE` file. |
| 114 | |
| 115 | [](https://www.gittip.com/tchap/ |
| 117 | "Gittip Badge") |