You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/core/collection/set.go

236 lines
4.2 KiB
Go

4 years ago
package collection
import (
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/logx"
4 years ago
)
const (
unmanaged = iota
untyped
intType
int64Type
uintType
uint64Type
stringType
)
4 years ago
// Set is not thread-safe, for concurrent use, make sure to use it with synchronization.
4 years ago
type Set struct {
data map[interface{}]lang.PlaceholderType
tp int
}
// NewSet returns a managed Set, can only put the values with the same type.
4 years ago
func NewSet() *Set {
return &Set{
data: make(map[interface{}]lang.PlaceholderType),
tp: untyped,
}
}
// NewUnmanagedSet returns an unmanaged Set, which can put values with different types.
4 years ago
func NewUnmanagedSet() *Set {
return &Set{
data: make(map[interface{}]lang.PlaceholderType),
tp: unmanaged,
}
}
// Add adds i into s.
4 years ago
func (s *Set) Add(i ...interface{}) {
for _, each := range i {
s.add(each)
}
}
// AddInt adds int values ii into s.
4 years ago
func (s *Set) AddInt(ii ...int) {
for _, each := range ii {
s.add(each)
}
}
// AddInt64 adds int64 values ii into s.
4 years ago
func (s *Set) AddInt64(ii ...int64) {
for _, each := range ii {
s.add(each)
}
}
// AddUint adds uint values ii into s.
4 years ago
func (s *Set) AddUint(ii ...uint) {
for _, each := range ii {
s.add(each)
}
}
// AddUint64 adds uint64 values ii into s.
4 years ago
func (s *Set) AddUint64(ii ...uint64) {
for _, each := range ii {
s.add(each)
}
}
// AddStr adds string values ss into s.
4 years ago
func (s *Set) AddStr(ss ...string) {
for _, each := range ss {
s.add(each)
}
}
// Contains checks if i is in s.
4 years ago
func (s *Set) Contains(i interface{}) bool {
if len(s.data) == 0 {
return false
}
s.validate(i)
_, ok := s.data[i]
return ok
}
// Keys returns the keys in s.
4 years ago
func (s *Set) Keys() []interface{} {
var keys []interface{}
for key := range s.data {
keys = append(keys, key)
}
return keys
}
// KeysInt returns the int keys in s.
4 years ago
func (s *Set) KeysInt() []int {
var keys []int
for key := range s.data {
3 years ago
if intKey, ok := key.(int); ok {
4 years ago
keys = append(keys, intKey)
}
}
return keys
}
// KeysInt64 returns int64 keys in s.
4 years ago
func (s *Set) KeysInt64() []int64 {
var keys []int64
for key := range s.data {
3 years ago
if intKey, ok := key.(int64); ok {
4 years ago
keys = append(keys, intKey)
}
}
return keys
}
// KeysUint returns uint keys in s.
4 years ago
func (s *Set) KeysUint() []uint {
var keys []uint
for key := range s.data {
3 years ago
if intKey, ok := key.(uint); ok {
4 years ago
keys = append(keys, intKey)
}
}
return keys
}
// KeysUint64 returns uint64 keys in s.
4 years ago
func (s *Set) KeysUint64() []uint64 {
var keys []uint64
for key := range s.data {
3 years ago
if intKey, ok := key.(uint64); ok {
4 years ago
keys = append(keys, intKey)
}
}
return keys
}
// KeysStr returns string keys in s.
4 years ago
func (s *Set) KeysStr() []string {
var keys []string
for key := range s.data {
3 years ago
if strKey, ok := key.(string); ok {
4 years ago
keys = append(keys, strKey)
}
}
return keys
}
// Remove removes i from s.
4 years ago
func (s *Set) Remove(i interface{}) {
s.validate(i)
delete(s.data, i)
}
// Count returns the number of items in s.
4 years ago
func (s *Set) Count() int {
return len(s.data)
}
func (s *Set) add(i interface{}) {
switch s.tp {
case unmanaged:
// do nothing
case untyped:
s.setType(i)
default:
s.validate(i)
}
s.data[i] = lang.Placeholder
}
func (s *Set) setType(i interface{}) {
4 years ago
// s.tp can only be untyped here
4 years ago
switch i.(type) {
case int:
s.tp = intType
case int64:
s.tp = int64Type
case uint:
s.tp = uintType
case uint64:
s.tp = uint64Type
case string:
s.tp = stringType
}
}
func (s *Set) validate(i interface{}) {
if s.tp == unmanaged {
return
}
switch i.(type) {
case int:
if s.tp != intType {
logx.Errorf("Error: element is int, but set contains elements with type %d", s.tp)
}
case int64:
if s.tp != int64Type {
logx.Errorf("Error: element is int64, but set contains elements with type %d", s.tp)
}
case uint:
if s.tp != uintType {
logx.Errorf("Error: element is uint, but set contains elements with type %d", s.tp)
}
case uint64:
if s.tp != uint64Type {
logx.Errorf("Error: element is uint64, but set contains elements with type %d", s.tp)
}
case string:
if s.tp != stringType {
logx.Errorf("Error: element is string, but set contains elements with type %d", s.tp)
}
}
}