set.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package collection
  2. import (
  3. "github.com/tal-tech/go-zero/core/lang"
  4. "github.com/tal-tech/go-zero/core/logx"
  5. )
  6. const (
  7. unmanaged = iota
  8. untyped
  9. intType
  10. int64Type
  11. uintType
  12. uint64Type
  13. stringType
  14. )
  15. // Set is not thread-safe, for concurrent use, make sure to use it with synchronization.
  16. type Set struct {
  17. data map[interface{}]lang.PlaceholderType
  18. tp int
  19. }
  20. func NewSet() *Set {
  21. return &Set{
  22. data: make(map[interface{}]lang.PlaceholderType),
  23. tp: untyped,
  24. }
  25. }
  26. func NewUnmanagedSet() *Set {
  27. return &Set{
  28. data: make(map[interface{}]lang.PlaceholderType),
  29. tp: unmanaged,
  30. }
  31. }
  32. func (s *Set) Add(i ...interface{}) {
  33. for _, each := range i {
  34. s.add(each)
  35. }
  36. }
  37. func (s *Set) AddInt(ii ...int) {
  38. for _, each := range ii {
  39. s.add(each)
  40. }
  41. }
  42. func (s *Set) AddInt64(ii ...int64) {
  43. for _, each := range ii {
  44. s.add(each)
  45. }
  46. }
  47. func (s *Set) AddUint(ii ...uint) {
  48. for _, each := range ii {
  49. s.add(each)
  50. }
  51. }
  52. func (s *Set) AddUint64(ii ...uint64) {
  53. for _, each := range ii {
  54. s.add(each)
  55. }
  56. }
  57. func (s *Set) AddStr(ss ...string) {
  58. for _, each := range ss {
  59. s.add(each)
  60. }
  61. }
  62. func (s *Set) Contains(i interface{}) bool {
  63. if len(s.data) == 0 {
  64. return false
  65. }
  66. s.validate(i)
  67. _, ok := s.data[i]
  68. return ok
  69. }
  70. func (s *Set) Keys() []interface{} {
  71. var keys []interface{}
  72. for key := range s.data {
  73. keys = append(keys, key)
  74. }
  75. return keys
  76. }
  77. func (s *Set) KeysInt() []int {
  78. var keys []int
  79. for key := range s.data {
  80. if intKey, ok := key.(int); !ok {
  81. continue
  82. } else {
  83. keys = append(keys, intKey)
  84. }
  85. }
  86. return keys
  87. }
  88. func (s *Set) KeysInt64() []int64 {
  89. var keys []int64
  90. for key := range s.data {
  91. if intKey, ok := key.(int64); !ok {
  92. continue
  93. } else {
  94. keys = append(keys, intKey)
  95. }
  96. }
  97. return keys
  98. }
  99. func (s *Set) KeysUint() []uint {
  100. var keys []uint
  101. for key := range s.data {
  102. if intKey, ok := key.(uint); !ok {
  103. continue
  104. } else {
  105. keys = append(keys, intKey)
  106. }
  107. }
  108. return keys
  109. }
  110. func (s *Set) KeysUint64() []uint64 {
  111. var keys []uint64
  112. for key := range s.data {
  113. if intKey, ok := key.(uint64); !ok {
  114. continue
  115. } else {
  116. keys = append(keys, intKey)
  117. }
  118. }
  119. return keys
  120. }
  121. func (s *Set) KeysStr() []string {
  122. var keys []string
  123. for key := range s.data {
  124. if strKey, ok := key.(string); !ok {
  125. continue
  126. } else {
  127. keys = append(keys, strKey)
  128. }
  129. }
  130. return keys
  131. }
  132. func (s *Set) Remove(i interface{}) {
  133. s.validate(i)
  134. delete(s.data, i)
  135. }
  136. func (s *Set) Count() int {
  137. return len(s.data)
  138. }
  139. func (s *Set) add(i interface{}) {
  140. switch s.tp {
  141. case unmanaged:
  142. // do nothing
  143. case untyped:
  144. s.setType(i)
  145. default:
  146. s.validate(i)
  147. }
  148. s.data[i] = lang.Placeholder
  149. }
  150. func (s *Set) setType(i interface{}) {
  151. // s.tp can only be untyped here
  152. switch i.(type) {
  153. case int:
  154. s.tp = intType
  155. case int64:
  156. s.tp = int64Type
  157. case uint:
  158. s.tp = uintType
  159. case uint64:
  160. s.tp = uint64Type
  161. case string:
  162. s.tp = stringType
  163. }
  164. }
  165. func (s *Set) validate(i interface{}) {
  166. if s.tp == unmanaged {
  167. return
  168. }
  169. switch i.(type) {
  170. case int:
  171. if s.tp != intType {
  172. logx.Errorf("Error: element is int, but set contains elements with type %d", s.tp)
  173. }
  174. case int64:
  175. if s.tp != int64Type {
  176. logx.Errorf("Error: element is int64, but set contains elements with type %d", s.tp)
  177. }
  178. case uint:
  179. if s.tp != uintType {
  180. logx.Errorf("Error: element is uint, but set contains elements with type %d", s.tp)
  181. }
  182. case uint64:
  183. if s.tp != uint64Type {
  184. logx.Errorf("Error: element is uint64, but set contains elements with type %d", s.tp)
  185. }
  186. case string:
  187. if s.tp != stringType {
  188. logx.Errorf("Error: element is string, but set contains elements with type %d", s.tp)
  189. }
  190. }
  191. }