dict.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package dictpool
  2. import (
  3. "sort"
  4. "github.com/savsgio/gotils/strconv"
  5. )
  6. func New() *Dict {
  7. return new(Dict)
  8. }
  9. func (d *Dict) allocKV() *KV {
  10. n := d.len()
  11. if cap(d.D) > n {
  12. d.D = d.D[:n+1]
  13. } else {
  14. d.D = append(d.D, KV{}) // nolint:exhaustruct
  15. }
  16. return &d.D[n]
  17. }
  18. func (d *Dict) append(key string, value interface{}) {
  19. kv := d.allocKV()
  20. kv.Key = key
  21. kv.Value = value
  22. }
  23. func (d *Dict) indexOf(key string) int {
  24. n := d.len()
  25. if d.BinarySearch {
  26. idx := sort.Search(n, func(i int) bool {
  27. return key <= d.D[i].Key
  28. })
  29. if idx < n && d.D[idx].Key == key {
  30. return idx
  31. }
  32. } else {
  33. for i := 0; i < n; i++ {
  34. if d.D[i].Key == key {
  35. return i
  36. }
  37. }
  38. }
  39. return -1
  40. }
  41. func (d *Dict) len() int {
  42. return len(d.D)
  43. }
  44. func (d *Dict) swap(i, j int) {
  45. d.D[i], d.D[j] = d.D[j], d.D[i]
  46. }
  47. func (d *Dict) less(i, j int) bool {
  48. return d.D[i].Key < d.D[j].Key
  49. }
  50. func (d *Dict) get(key string) interface{} {
  51. if idx := d.indexOf(key); idx > -1 {
  52. return d.D[idx].Value
  53. }
  54. return nil
  55. }
  56. func (d *Dict) set(key string, value interface{}) {
  57. if idx := d.indexOf(key); idx > -1 {
  58. d.D[idx].Value = value
  59. } else {
  60. d.append(key, value)
  61. if d.BinarySearch {
  62. sort.Sort(d)
  63. }
  64. }
  65. }
  66. func (d *Dict) del(key string) {
  67. if idx := d.indexOf(key); idx > -1 {
  68. d.D = append(d.D[:idx], d.D[idx+1:]...)
  69. }
  70. }
  71. func (d *Dict) has(key string) bool {
  72. return d.indexOf(key) > -1
  73. }
  74. func (d *Dict) reset() {
  75. d.D = d.D[:0]
  76. }
  77. // Len is the number of elements in the Dict.
  78. func (d *Dict) Len() int {
  79. return d.len()
  80. }
  81. // Swap swaps the elements with indexes i and j.
  82. func (d *Dict) Swap(i, j int) {
  83. d.swap(i, j)
  84. }
  85. // Less reports whether the element with
  86. // index i should sort before the element with index j.
  87. func (d *Dict) Less(i, j int) bool {
  88. return d.less(i, j)
  89. }
  90. // Get get data from key.
  91. func (d *Dict) Get(key string) interface{} {
  92. return d.get(key)
  93. }
  94. // GetBytes get data from key.
  95. func (d *Dict) GetBytes(key []byte) interface{} {
  96. return d.Get(strconv.B2S(key))
  97. }
  98. // Set set new key.
  99. func (d *Dict) Set(key string, value interface{}) {
  100. d.set(key, value)
  101. }
  102. // SetBytes set new key.
  103. func (d *Dict) SetBytes(key []byte, value interface{}) {
  104. d.Set(strconv.B2S(key), value)
  105. }
  106. // Del delete key.
  107. func (d *Dict) Del(key string) {
  108. d.del(key)
  109. }
  110. // DelBytes delete key.
  111. func (d *Dict) DelBytes(key []byte) {
  112. d.Del(strconv.B2S(key))
  113. }
  114. // Has check if key exists.
  115. func (d *Dict) Has(key string) bool {
  116. return d.has(key)
  117. }
  118. // HasBytes check if key exists.
  119. func (d *Dict) HasBytes(key []byte) bool {
  120. return d.Has(strconv.B2S(key))
  121. }
  122. // Reset reset dict.
  123. func (d *Dict) Reset() {
  124. d.reset()
  125. }
  126. // Map convert to map.
  127. func (d *Dict) Map(dst DictMap) {
  128. for i := range d.D {
  129. kv := &d.D[i]
  130. sd, ok := kv.Value.(*Dict)
  131. if ok {
  132. subDst := make(DictMap)
  133. sd.Map(subDst)
  134. dst[kv.Key] = subDst
  135. } else {
  136. dst[kv.Key] = kv.Value
  137. }
  138. }
  139. }
  140. // Parse convert map to Dict.
  141. func (d *Dict) Parse(src DictMap) {
  142. d.reset()
  143. for k, v := range src {
  144. sv, ok := v.(map[string]interface{})
  145. if ok {
  146. subDict := new(Dict)
  147. subDict.Parse(sv)
  148. d.append(k, subDict)
  149. } else {
  150. d.append(k, v)
  151. }
  152. }
  153. }