dict.go 2.8 KB

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