set.go 3.5 KB

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