multimap.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //@File multimap.go
  2. //@Time 2022/05/12
  3. //@Author #Suyghur,
  4. package treemap
  5. import (
  6. "ylink/comm/ds/rbtree"
  7. "ylink/comm/utils/comparator"
  8. "ylink/comm/utils/sync"
  9. "ylink/comm/utils/visitor"
  10. )
  11. // MultiMap uses RbTress for internal data structure, and keys can bee repeated.
  12. type MultiMap struct {
  13. tree *rbtree.RbTree
  14. keyCmp comparator.Comparator
  15. locker sync.Locker
  16. }
  17. //NewMultiMap creates a new MultiMap
  18. func NewMultiMap(opts ...Option) *MultiMap {
  19. option := Options{
  20. keyCmp: defaultKeyComparator,
  21. locker: defaultLocker,
  22. }
  23. for _, opt := range opts {
  24. opt(&option)
  25. }
  26. return &MultiMap{tree: rbtree.New(rbtree.WithKeyComparator(option.keyCmp)),
  27. keyCmp: option.keyCmp,
  28. locker: option.locker,
  29. }
  30. }
  31. //Insert inserts a key-value to the MultiMap
  32. func (mm *MultiMap) Insert(key, value interface{}) {
  33. mm.locker.Lock()
  34. defer mm.locker.Unlock()
  35. mm.tree.Insert(key, value)
  36. }
  37. //Get returns the first node's value by the passed key if the key is in the MultiMap, otherwise returns nil
  38. func (mm *MultiMap) Get(key interface{}) interface{} {
  39. mm.locker.RLock()
  40. defer mm.locker.RUnlock()
  41. node := mm.tree.FindNode(key)
  42. if node != nil {
  43. return node.Value()
  44. }
  45. return nil
  46. }
  47. //Erase erases the key in the MultiMap
  48. func (mm *MultiMap) Erase(key interface{}) {
  49. mm.locker.Lock()
  50. defer mm.locker.Unlock()
  51. for {
  52. node := mm.tree.FindNode(key)
  53. if node == nil {
  54. break
  55. }
  56. mm.tree.Delete(node)
  57. }
  58. }
  59. //Find finds the node by the passed key in the MultiMap and returns its iterator
  60. func (mm *MultiMap) Find(key interface{}) *MapIterator {
  61. mm.locker.RLock()
  62. defer mm.locker.RUnlock()
  63. node := mm.tree.FindNode(key)
  64. return &MapIterator{node: node}
  65. }
  66. //LowerBound find the first node that its key is equal or greater than the passed key in the MultiMap, and returns its iterator
  67. func (mm *MultiMap) LowerBound(key interface{}) *MapIterator {
  68. mm.locker.RLock()
  69. defer mm.locker.RUnlock()
  70. node := mm.tree.FindLowerBoundNode(key)
  71. return &MapIterator{node: node}
  72. }
  73. //UpperBound find the first node that its key is greater than the passed key in the MultiMap, and returns its iterator
  74. func (mm *MultiMap) UpperBound(key interface{}) *MapIterator {
  75. mm.locker.RLock()
  76. defer mm.locker.RUnlock()
  77. node := mm.tree.FindUpperBoundNode(key)
  78. return &MapIterator{node: node}
  79. }
  80. //Begin returns the first node's iterator
  81. func (mm *MultiMap) Begin() *MapIterator {
  82. mm.locker.RLock()
  83. defer mm.locker.RUnlock()
  84. return &MapIterator{node: mm.tree.First()}
  85. }
  86. //First returns the first node's iterator
  87. func (mm *MultiMap) First() *MapIterator {
  88. mm.locker.RLock()
  89. defer mm.locker.RUnlock()
  90. return &MapIterator{node: mm.tree.First()}
  91. }
  92. //Last returns the last node's iterator
  93. func (mm *MultiMap) Last() *MapIterator {
  94. mm.locker.RLock()
  95. defer mm.locker.RUnlock()
  96. return &MapIterator{node: mm.tree.Last()}
  97. }
  98. //Clear clears the MultiMap
  99. func (mm *MultiMap) Clear() {
  100. mm.locker.Lock()
  101. defer mm.locker.Unlock()
  102. mm.tree.Clear()
  103. }
  104. // Contains returns true if the passed value is in the MultiMap. otherwise returns false.
  105. func (mm *MultiMap) Contains(value interface{}) bool {
  106. mm.locker.RLock()
  107. defer mm.locker.RUnlock()
  108. if mm.tree.Find(value) != nil {
  109. return true
  110. }
  111. return false
  112. }
  113. // Size returns the amount of elements in the MultiMap
  114. func (mm *MultiMap) Size() int {
  115. mm.locker.RLock()
  116. defer mm.locker.RUnlock()
  117. return mm.tree.Size()
  118. }
  119. // Traversal traversals elements in the MultiMap, it will not stop until to the end of the MultiMap or the visitor returns false
  120. func (mm *MultiMap) Traversal(visitor visitor.KvVisitor) {
  121. mm.locker.RLock()
  122. defer mm.locker.RUnlock()
  123. mm.tree.Traversal(visitor)
  124. }