map.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //@File map.go
  2. //@Time 2022/05/12
  3. //@Author #Suyghur,
  4. package treemap
  5. import (
  6. gosync "sync"
  7. "ylink/comm/ds/rbtree"
  8. "ylink/comm/utils/comparator"
  9. "ylink/comm/utils/iterator"
  10. "ylink/comm/utils/sync"
  11. "ylink/comm/utils/visitor"
  12. )
  13. var (
  14. defaultKeyComparator = comparator.BuiltinTypeComparator
  15. defaultLocker sync.FakeLocker
  16. )
  17. type Options struct {
  18. keyCmp comparator.Comparator
  19. locker sync.Locker
  20. }
  21. // Option is a function type used to set Options
  22. type Option func(option *Options)
  23. // WithKeyComparator is used to set the key comparator of map
  24. func WithKeyComparator(cmp comparator.Comparator) Option {
  25. return func(option *Options) {
  26. option.keyCmp = cmp
  27. }
  28. }
  29. func WithGoroutineSafe() Option {
  30. return func(option *Options) {
  31. option.locker = &gosync.RWMutex{}
  32. }
  33. }
  34. // Map uses RbTress for internal data structure, and every key can must bee unique.
  35. type Map struct {
  36. tree *rbtree.RbTree
  37. locker sync.Locker
  38. }
  39. // New creates a new map
  40. func New(opts ...Option) *Map {
  41. option := Options{
  42. keyCmp: defaultKeyComparator,
  43. locker: defaultLocker,
  44. }
  45. for _, opt := range opts {
  46. opt(&option)
  47. }
  48. return &Map{tree: rbtree.New(rbtree.WithKeyComparator(option.keyCmp)),
  49. locker: option.locker,
  50. }
  51. }
  52. //Insert inserts a key-value to the map
  53. func (m *Map) Insert(key, value interface{}) {
  54. m.locker.Lock()
  55. defer m.locker.Unlock()
  56. node := m.tree.FindNode(key)
  57. if node != nil {
  58. node.SetValue(value)
  59. return
  60. }
  61. m.tree.Insert(key, value)
  62. }
  63. //Get returns the value of the passed key if the key is in the map, otherwise returns nil
  64. func (m *Map) Get(key interface{}) interface{} {
  65. m.locker.RLock()
  66. defer m.locker.RUnlock()
  67. node := m.tree.FindNode(key)
  68. if node != nil {
  69. return node.Value()
  70. }
  71. return nil
  72. }
  73. //Erase erases the node by the passed key from the map if the key in the Map
  74. func (m *Map) Erase(key interface{}) {
  75. m.locker.Lock()
  76. defer m.locker.Unlock()
  77. node := m.tree.FindNode(key)
  78. if node != nil {
  79. m.tree.Delete(node)
  80. }
  81. }
  82. //EraseIter erases the node that iterator iter point to from the map
  83. func (m *Map) EraseIter(iter iterator.ConstKvIterator) {
  84. m.locker.Lock()
  85. defer m.locker.Unlock()
  86. mpIter, ok := iter.(*MapIterator)
  87. if ok {
  88. m.tree.Delete(mpIter.node)
  89. }
  90. }
  91. //Find finds a node by the passed key and returns its iterator
  92. func (m *Map) Find(key interface{}) *MapIterator {
  93. m.locker.RLock()
  94. defer m.locker.RUnlock()
  95. node := m.tree.FindNode(key)
  96. return &MapIterator{node: node}
  97. }
  98. //LowerBound finds a node that its key is equal or greater than the passed key and returns its iterator
  99. func (m *Map) LowerBound(key interface{}) *MapIterator {
  100. m.locker.RLock()
  101. defer m.locker.RUnlock()
  102. node := m.tree.FindLowerBoundNode(key)
  103. return &MapIterator{node: node}
  104. }
  105. //UpperBound finds a node that its key is greater than the passed key and returns its iterator
  106. func (m *Map) UpperBound(key interface{}) *MapIterator {
  107. m.locker.RLock()
  108. defer m.locker.RUnlock()
  109. node := m.tree.FindUpperBoundNode(key)
  110. return &MapIterator{node: node}
  111. }
  112. //Begin returns the first node's iterator
  113. func (m *Map) Begin() *MapIterator {
  114. m.locker.RLock()
  115. defer m.locker.RUnlock()
  116. return &MapIterator{node: m.tree.First()}
  117. }
  118. //First returns the first node's iterator
  119. func (m *Map) First() *MapIterator {
  120. m.locker.RLock()
  121. defer m.locker.RUnlock()
  122. return &MapIterator{node: m.tree.First()}
  123. }
  124. //Last returns the last node's iterator
  125. func (m *Map) Last() *MapIterator {
  126. m.locker.RLock()
  127. defer m.locker.RUnlock()
  128. return &MapIterator{node: m.tree.Last()}
  129. }
  130. //Clear clears the map
  131. func (m *Map) Clear() {
  132. m.locker.Lock()
  133. defer m.locker.Unlock()
  134. m.tree.Clear()
  135. }
  136. // Contains returns true if the key is in the map. otherwise returns false.
  137. func (m *Map) Contains(key interface{}) bool {
  138. m.locker.RLock()
  139. defer m.locker.RUnlock()
  140. if m.tree.Find(key) != nil {
  141. return true
  142. }
  143. return false
  144. }
  145. // Size returns the amount of elements in the map
  146. func (m *Map) Size() int {
  147. m.locker.RLock()
  148. defer m.locker.RUnlock()
  149. return m.tree.Size()
  150. }
  151. // Traversal traversals elements in the map, it will not stop until to the end or the visitor returns false
  152. func (m *Map) Traversal(visitor visitor.KvVisitor) {
  153. m.locker.RLock()
  154. defer m.locker.RUnlock()
  155. m.tree.Traversal(visitor)
  156. }