{"id":22796065,"url":"https://github.com/krasun/rbytree","last_synced_at":"2025-04-19T12:47:41.050Z","repository":{"id":57581023,"uuid":"362707960","full_name":"krasun/rbytree","owner":"krasun","description":"Red-black tree implementation for Go with byte-slice keys and values","archived":false,"fork":false,"pushed_at":"2022-01-09T12:33:04.000Z","size":51,"stargazers_count":2,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-09T17:00:36.915Z","etag":null,"topics":["bst","bst-tree","bstree","map","redblack-tree","redblacktree","redblacktrees","searchtrees","tree"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krasun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-29T06:05:26.000Z","updated_at":"2023-02-15T03:14:54.000Z","dependencies_parsed_at":"2022-09-26T19:31:00.491Z","dependency_job_id":null,"html_url":"https://github.com/krasun/rbytree","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krasun%2Frbytree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krasun%2Frbytree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krasun%2Frbytree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krasun%2Frbytree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krasun","download_url":"https://codeload.github.com/krasun/rbytree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229333196,"owners_count":18056671,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bst","bst-tree","bstree","map","redblack-tree","redblacktree","redblacktrees","searchtrees","tree"],"created_at":"2024-12-12T05:09:54.812Z","updated_at":"2024-12-12T05:09:55.534Z","avatar_url":"https://github.com/krasun.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **rb**y**tree**\n\n[![Build](https://github.com/krasun/rbytree/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/krasun/rbytree/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/krasun/rbytree/branch/main/graph/badge.svg?token=8NU6LR4FQD)](https://codecov.io/gh/krasun/rbytree)\n[![Go Report Card](https://goreportcard.com/badge/github.com/krasun/rbytree)](https://goreportcard.com/report/github.com/krasun/rbytree)\n[![GoDoc](https://godoc.org/https://godoc.org/github.com/krasun/rbytree?status.svg)](https://godoc.org/github.com/krasun/rbytree)\n\nA [red-black tree](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree) implementation for Go with byte-slice keys and values. \n\n## Installation \n\nTo install, run:\n\n```\ngo get github.com/krasun/rbytree\n```\n\n## Quickstart\n\nFeel free to play: \n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/krasun/rbytree\"\n)\n\nfunc main() {\n\ttree := rbytree.New()\n\n\ttree.Put([]byte(\"apple\"), []byte(\"sweet\"))\n\ttree.Put([]byte(\"banana\"), []byte(\"honey\"))\n\ttree.Put([]byte(\"cinnamon\"), []byte(\"savoury\"))\n\n\tbanana, ok := tree.Get([]byte(\"banana\"))\n\tif ok {\n\t\tfmt.Printf(\"banana = %s\\n\", string(banana))\n\t} else {\n\t\tfmt.Println(\"value for banana not found\")\n\t}\n\n\ttree.ForEach(func(key, value []byte) {\n\t\tfmt.Printf(\"key = %s, value = %s\\n\", string(key), string(value))\n\t})\n\n\t// Output: \n\t// banana = honey\n\t// key = apple, value = sweet\n\t// key = banana, value = honey\n\t// key = cinnamon, value = savoury\n}\n```\n\nYou can use an iterator: \n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/krasun/rbytree\"\n)\n\nfunc main() {\n\ttree := rbytree.New()\n\n\ttree.Put([]byte(\"apple\"), []byte(\"sweet\"))\n\ttree.Put([]byte(\"banana\"), []byte(\"honey\"))\n\ttree.Put([]byte(\"cinnamon\"), []byte(\"savoury\"))\n\n\tbanana, ok := tree.Get([]byte(\"banana\"))\n\tif ok {\n\t\tfmt.Printf(\"banana = %s\\n\", string(banana))\n\t} else {\n\t\tfmt.Println(\"value for banana not found\")\n\t}\n\n\tfor it := tree.Iterator(); it.HasNext(); {\n\t\tkey, value := it.Next()\n\t\tfmt.Printf(\"key = %s, value = %s\\n\", string(key), string(value))\n\t}\n\n\t// Output: \n\t// banana = honey\n\t// key = apple, value = sweet\n\t// key = banana, value = honey\n\t// key = cinnamon, value = savoury\n}\n```\n\nAn iterator is stateful. You can have multiple iterators without any impact on each other, but make sure to synchronize access to them and the tree in a concurrent environment.\n\nCaution! `Next` panics if there is no next element. Make sure to test for the next element with `HasNext` before.\n\n## Use cases \n\n1. When you want to use []byte as a key in the map. \n2. When you want to iterate over keys in map in sorted order.\n\n## Limitations \n\n**Caution!** To guarantee that the red-black tree properties are not violated, keys are copied. \n\nYou should clearly understand what []byte slice is and why it is dangerous to use it as a key. Go language authors do prohibit using byte slice ([]byte) as a map key for a reason. The point is that you can change the values of the key and thus violate the invariants of map: \n\n```go\n// if it worked \nb := []byte{1}\nm := make(map[[]byte]int)\nm[b] = 1\n\nb[0] = 2 // it would violate the invariants \nm[[]byte{1}] // what do you expect to receive?\n```\n\nSo to make sure that this situation does not occur in the tree, the key is copied byte by byte.\n\n## Benchmark\n\nRegular Go map is as twice faster for put and get than red-black tree. But if you \nneed to iterate over keys in sorted order, the picture is sligthly different: \n\n```\n$ go test -benchmem -bench .\ngoos: darwin\ngoarch: amd64\npkg: github.com/krasun/rbytree\nBenchmarkTreePut-8                     \t     330\t   3573752 ns/op\t 1040040 B/op\t   49902 allocs/op\nBenchmarkMapPut-8                      \t     496\t   2477226 ns/op\t 1732586 B/op\t   20151 allocs/op\nBenchmarkTreePutRandomized-8           \t     260\t   4394145 ns/op\t 1040029 B/op\t   49901 allocs/op\nBenchmarkMapPutRandomized-8            \t     630\t   1890784 ns/op\t  981565 B/op\t   20111 allocs/op\nBenchmarkMapGet-8                      \t    1496\t    768210 ns/op\t   38880 B/op\t    9900 allocs/op\nBenchmarkTreeGet-8                     \t     723\t   1604544 ns/op\t   38880 B/op\t    9900 allocs/op\nBenchmarkTreePutAndForEach-8           \t     300\t   4056864 ns/op\t 1040043 B/op\t   49903 allocs/op\nBenchmarkMapPutAndIterateAfterSort-8   \t     202\t   5559646 ns/op\t 2558408 B/op\t   20173 allocs/op\nPASS\nok  \tgithub.com/krasun/rbytree\t12.096s\n```\n\n## Tests\n\nRun tests with: \n\n```\n$ go test -cover .\nok  \tgithub.com/krasun/rbytree\t0.245s\tcoverage: 100.0% of statements\n```\n\n## Known Usages \n\n1. [krasun/lsmtree](https://github.com/krasun/lsmtree) - my experimental implementation of [log-structured merge-tree](https://en.wikipedia.org/wiki/Log-structured_merge-tree).\n\n## License \n\n**rb**y**tree** is released under [the MIT license](LICENSE).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrasun%2Frbytree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrasun%2Frbytree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrasun%2Frbytree/lists"}