sortedmap.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package sortedmap
  2. import (
  3. "container/list"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
  8. )
  9. var (
  10. ErrInvalidKVExpression = errors.New(`invalid key-value expression`)
  11. ErrInvalidKVS = errors.New("the length of kv must be a even number")
  12. )
  13. type KV []interface{}
  14. type SortedMap struct {
  15. kv *list.List
  16. keys map[interface{}]*list.Element
  17. }
  18. func New() *SortedMap {
  19. return &SortedMap{
  20. kv: list.New(),
  21. keys: make(map[interface{}]*list.Element),
  22. }
  23. }
  24. func (m *SortedMap) SetExpression(expression string) (key, value interface{}, err error) {
  25. idx := strings.Index(expression, "=")
  26. if idx == -1 {
  27. return "", "", ErrInvalidKVExpression
  28. }
  29. key = expression[:idx]
  30. if len(expression) == idx {
  31. value = ""
  32. } else {
  33. value = expression[idx+1:]
  34. }
  35. if keys, ok := key.(string); ok && stringx.ContainsWhiteSpace(keys) {
  36. return "", "", ErrInvalidKVExpression
  37. }
  38. if values, ok := value.(string); ok && stringx.ContainsWhiteSpace(values) {
  39. return "", "", ErrInvalidKVExpression
  40. }
  41. if len(key.(string)) == 0 {
  42. return "", "", ErrInvalidKVExpression
  43. }
  44. m.SetKV(key, value)
  45. return
  46. }
  47. func (m *SortedMap) SetKV(key, value interface{}) {
  48. e, ok := m.keys[key]
  49. if !ok {
  50. e = m.kv.PushBack(KV{
  51. key, value,
  52. })
  53. } else {
  54. e.Value.(KV)[1] = value
  55. }
  56. m.keys[key] = e
  57. }
  58. func (m *SortedMap) Set(kv KV) error {
  59. if len(kv) == 0 {
  60. return nil
  61. }
  62. if len(kv)%2 != 0 {
  63. return ErrInvalidKVS
  64. }
  65. for idx := 0; idx < len(kv); idx += 2 {
  66. m.SetKV(kv[idx], kv[idx+1])
  67. }
  68. return nil
  69. }
  70. func (m *SortedMap) Get(key interface{}) (interface{}, bool) {
  71. e, ok := m.keys[key]
  72. if !ok {
  73. return nil, false
  74. }
  75. return e.Value.(KV)[1], true
  76. }
  77. func (m *SortedMap) GetOr(key, dft interface{}) interface{} {
  78. e, ok := m.keys[key]
  79. if !ok {
  80. return dft
  81. }
  82. return e.Value.(KV)[1]
  83. }
  84. func (m *SortedMap) GetString(key interface{}) (string, bool) {
  85. value, ok := m.Get(key)
  86. if !ok {
  87. return "", false
  88. }
  89. vs, ok := value.(string)
  90. return vs, ok
  91. }
  92. func (m *SortedMap) GetStringOr(key interface{}, dft string) string {
  93. value, ok := m.GetString(key)
  94. if !ok {
  95. return dft
  96. }
  97. return value
  98. }
  99. func (m *SortedMap) HasKey(key interface{}) bool {
  100. _, ok := m.keys[key]
  101. return ok
  102. }
  103. func (m *SortedMap) HasValue(value interface{}) bool {
  104. var contains bool
  105. m.RangeIf(func(key, v interface{}) bool {
  106. if value == v {
  107. contains = true
  108. return false
  109. }
  110. return true
  111. })
  112. return contains
  113. }
  114. func (m *SortedMap) Keys() []interface{} {
  115. keys := make([]interface{}, 0)
  116. next := m.kv.Front()
  117. for next != nil {
  118. keys = append(keys, next.Value.(KV)[0])
  119. next = next.Next()
  120. }
  121. return keys
  122. }
  123. func (m *SortedMap) Values() []interface{} {
  124. keys := m.Keys()
  125. values := make([]interface{}, len(keys))
  126. for idx, key := range keys {
  127. values[idx] = m.keys[key].Value.(KV)[1]
  128. }
  129. return values
  130. }
  131. func (m *SortedMap) Range(iterator func(key, value interface{})) {
  132. next := m.kv.Front()
  133. for next != nil {
  134. value := next.Value.(KV)
  135. iterator(value[0], value[1])
  136. next = next.Next()
  137. }
  138. }
  139. func (m *SortedMap) RangeIf(iterator func(key, value interface{}) bool) {
  140. next := m.kv.Front()
  141. for next != nil {
  142. value := next.Value.(KV)
  143. loop := iterator(value[0], value[1])
  144. if !loop {
  145. return
  146. }
  147. next = next.Next()
  148. }
  149. }
  150. func (m *SortedMap) Remove(key interface{}) (value interface{}, ok bool) {
  151. v, ok := m.keys[key]
  152. if !ok {
  153. return nil, false
  154. }
  155. value = v.Value.(KV)[1]
  156. ok = true
  157. m.kv.Remove(v)
  158. delete(m.keys, key)
  159. return
  160. }
  161. func (m *SortedMap) Insert(sm *SortedMap) {
  162. sm.Range(func(key, value interface{}) {
  163. m.SetKV(key, value)
  164. })
  165. }
  166. func (m *SortedMap) Copy() *SortedMap {
  167. sm := New()
  168. m.Range(func(key, value interface{}) {
  169. sm.SetKV(key, value)
  170. })
  171. return sm
  172. }
  173. func (m *SortedMap) Format() []string {
  174. format := make([]string, 0)
  175. m.Range(func(key, value interface{}) {
  176. format = append(format, fmt.Sprintf("%s=%s", key, value))
  177. })
  178. return format
  179. }
  180. func (m *SortedMap) Reset() {
  181. m.kv.Init()
  182. for key := range m.keys {
  183. delete(m.keys, key)
  184. }
  185. }