set.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package btree
  2. type Set[K ordered] struct {
  3. base Map[K, struct{}]
  4. }
  5. // Copy
  6. func (tr *Set[K]) Copy() *Set[K] {
  7. tr2 := new(Set[K])
  8. tr2.base = *tr.base.Copy()
  9. return tr2
  10. }
  11. func (tr *Set[K]) IsoCopy() *Set[K] {
  12. tr2 := new(Set[K])
  13. tr2.base = *tr.base.IsoCopy()
  14. return tr2
  15. }
  16. // Insert an item
  17. func (tr *Set[K]) Insert(key K) {
  18. tr.base.Set(key, struct{}{})
  19. }
  20. func (tr *Set[K]) Scan(iter func(key K) bool) {
  21. tr.base.Scan(func(key K, value struct{}) bool {
  22. return iter(key)
  23. })
  24. }
  25. // Get a value for key
  26. func (tr *Set[K]) Contains(key K) bool {
  27. _, ok := tr.base.Get(key)
  28. return ok
  29. }
  30. // Len returns the number of items in the tree
  31. func (tr *Set[K]) Len() int {
  32. return tr.base.Len()
  33. }
  34. // Delete an item
  35. func (tr *Set[K]) Delete(key K) {
  36. tr.base.Delete(key)
  37. }
  38. // Ascend the tree within the range [pivot, last]
  39. // Pass nil for pivot to scan all item in ascending order
  40. // Return false to stop iterating
  41. func (tr *Set[K]) Ascend(pivot K, iter func(key K) bool) {
  42. tr.base.Ascend(pivot, func(key K, value struct{}) bool {
  43. return iter(key)
  44. })
  45. }
  46. func (tr *Set[K]) Reverse(iter func(key K) bool) {
  47. tr.base.Reverse(func(key K, value struct{}) bool {
  48. return iter(key)
  49. })
  50. }
  51. // Descend the tree within the range [pivot, first]
  52. // Pass nil for pivot to scan all item in descending order
  53. // Return false to stop iterating
  54. func (tr *Set[K]) Descend(pivot K, iter func(key K) bool) {
  55. tr.base.Descend(pivot, func(key K, value struct{}) bool {
  56. return iter(key)
  57. })
  58. }
  59. // Load is for bulk loading pre-sorted items
  60. func (tr *Set[K]) Load(key K) {
  61. tr.base.Load(key, struct{}{})
  62. }
  63. // Min returns the minimum item in tree.
  64. // Returns nil if the treex has no items.
  65. func (tr *Set[K]) Min() (K, bool) {
  66. key, _, ok := tr.base.Min()
  67. return key, ok
  68. }
  69. // Max returns the maximum item in tree.
  70. // Returns nil if the tree has no items.
  71. func (tr *Set[K]) Max() (K, bool) {
  72. key, _, ok := tr.base.Max()
  73. return key, ok
  74. }
  75. // PopMin removes the minimum item in tree and returns it.
  76. // Returns nil if the tree has no items.
  77. func (tr *Set[K]) PopMin() (K, bool) {
  78. key, _, ok := tr.base.PopMin()
  79. return key, ok
  80. }
  81. // PopMax removes the maximum item in tree and returns it.
  82. // Returns nil if the tree has no items.
  83. func (tr *Set[K]) PopMax() (K, bool) {
  84. key, _, ok := tr.base.PopMax()
  85. return key, ok
  86. }
  87. // GetAt returns the value at index.
  88. // Return nil if the tree is empty or the index is out of bounds.
  89. func (tr *Set[K]) GetAt(index int) (K, bool) {
  90. key, _, ok := tr.base.GetAt(index)
  91. return key, ok
  92. }
  93. // DeleteAt deletes the item at index.
  94. // Return nil if the tree is empty or the index is out of bounds.
  95. func (tr *Set[K]) DeleteAt(index int) (K, bool) {
  96. key, _, ok := tr.base.DeleteAt(index)
  97. return key, ok
  98. }
  99. // Height returns the height of the tree.
  100. // Returns zero if tree has no items.
  101. func (tr *Set[K]) Height() int {
  102. return tr.base.Height()
  103. }
  104. // SetIter represents an iterator for btree.Set
  105. type SetIter[K ordered] struct {
  106. base MapIter[K, struct{}]
  107. }
  108. // Iter returns a read-only iterator.
  109. func (tr *Set[K]) Iter() SetIter[K] {
  110. return SetIter[K]{tr.base.Iter()}
  111. }
  112. // Seek to item greater-or-equal-to key.
  113. // Returns false if there was no item found.
  114. func (iter *SetIter[K]) Seek(key K) bool {
  115. return iter.base.Seek(key)
  116. }
  117. // First moves iterator to first item in tree.
  118. // Returns false if the tree is empty.
  119. func (iter *SetIter[K]) First() bool {
  120. return iter.base.First()
  121. }
  122. // Last moves iterator to last item in tree.
  123. // Returns false if the tree is empty.
  124. func (iter *SetIter[K]) Last() bool {
  125. return iter.base.Last()
  126. }
  127. // Next moves iterator to the next item in iterator.
  128. // Returns false if the tree is empty or the iterator is at the end of
  129. // the tree.
  130. func (iter *SetIter[K]) Next() bool {
  131. return iter.base.Next()
  132. }
  133. // Prev moves iterator to the previous item in iterator.
  134. // Returns false if the tree is empty or the iterator is at the beginning of
  135. // the tree.
  136. func (iter *SetIter[K]) Prev() bool {
  137. return iter.base.Prev()
  138. }
  139. // Key returns the current iterator item key.
  140. func (iter *SetIter[K]) Key() K {
  141. return iter.base.Key()
  142. }
  143. // Keys returns all the keys in order.
  144. func (tr *Set[K]) Keys() []K {
  145. return tr.base.Keys()
  146. }
  147. // Clear will delete all items.
  148. func (tr *Set[K]) Clear() {
  149. tr.base.Clear()
  150. }