set.go 4.1 KB

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