You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.0 KiB

  1. gtreap
  2. ------
  3. gtreap is an immutable treap implementation in the Go Language
  4. [![GoDoc](https://godoc.org/github.com/steveyen/gtreap?status.svg)](https://godoc.org/github.com/steveyen/gtreap) [![Build Status](https://drone.io/github.com/steveyen/gtreap/status.png)](https://drone.io/github.com/steveyen/gtreap/latest) [![Coverage Status](https://coveralls.io/repos/steveyen/gtreap/badge.png)](https://coveralls.io/r/steveyen/gtreap)
  5. Overview
  6. ========
  7. gtreap implements an immutable treap data structure in golang.
  8. By treap, this data structure is both a heap and a binary search tree.
  9. By immutable, any updates/deletes to a treap will return a new treap
  10. which can share internal nodes with the previous treap. All nodes in
  11. this implementation are read-only after their creation. This allows
  12. concurrent readers to operate safely with concurrent writers as
  13. modifications only create new data structures and never modify
  14. existing data structures. This is a simple approach to achieving MVCC
  15. or multi-version concurrency control.
  16. By heap, items in the treap follow the heap-priority property, where a
  17. parent node will have higher priority than its left and right children
  18. nodes.
  19. By binary search tree, items are store lexigraphically, ordered by a
  20. user-supplied Compare function.
  21. To get a probabilistic O(lg N) tree height, you should use a random
  22. priority number during the Upsert() operation.
  23. LICENSE
  24. =======
  25. MIT
  26. Example
  27. =======
  28. import (
  29. "math/rand"
  30. "github.com/steveyen/gtreap"
  31. )
  32. func stringCompare(a, b interface{}) int {
  33. return bytes.Compare([]byte(a.(string)), []byte(b.(string)))
  34. }
  35. t := gtreap.NewTreap(stringCompare)
  36. t = t.Upsert("hi", rand.Int())
  37. t = t.Upsert("hola", rand.Int())
  38. t = t.Upsert("bye", rand.Int())
  39. t = t.Upsert("adios", rand.Int())
  40. hi = t.Get("hi")
  41. bye = t.Get("bye")
  42. // Some example Delete()'s...
  43. t = t.Delete("bye")
  44. nilValueHere = t.Get("bye")
  45. t2 = t.Delete("hi")
  46. nilValueHere2 = t2.Get("hi")
  47. // Since we still hold onto treap t, we can still access "hi".
  48. hiStillExistsInTreapT = t.Get("hi")
  49. t.VisitAscend("cya", func(i Item) bool {
  50. // This visitor callback will be invoked with every item
  51. // from "cya" onwards. So: "hi", "hola".
  52. // If we want to stop visiting, return false;
  53. // otherwise a true return result means keep visiting items.
  54. return true
  55. })
  56. Tips
  57. ====
  58. The Upsert() method takes both an Item (an interface{}) and a heap
  59. priority. Usually, that priority should be a random int
  60. (math/rand.Int()) or perhaps even a hash of the item. However, if you
  61. want to shuffle more commonly accessed items nearer to the top of the
  62. treap for faster access, at the potential cost of not approaching a
  63. probabilistic O(lg N) tree height, then you might tweak the priority.
  64. See also
  65. ========
  66. For a simple, ordered, key-value storage or persistence library built
  67. on immutable treaps, see: https://github.com/steveyen/gkvlite