cubefs/util/set.go
shuqiang-zheng 68cf97ab68 fix(master):fix the problem of TestCreateColdVol
Signed-off-by: shuqiang-zheng <zhengshuqiang@oppo.com>
2024-07-18 16:57:38 +08:00

74 lines
1.3 KiB
Go

// Copyright 2018 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package util
import "sync"
type (
Null struct{}
Set struct {
sync.RWMutex
m map[string]Null
}
)
func NewSet() *Set {
return &Set{
m: map[string]Null{},
}
}
func (s *Set) Add(val string) {
s.Lock()
defer s.Unlock()
s.m[val] = Null{}
}
func (s *Set) Remove(val string) {
s.Lock()
defer s.Unlock()
delete(s.m, val)
}
func (s *Set) Has(key string) bool {
s.RLock()
defer s.RUnlock()
_, ok := s.m[key]
return ok
}
func (s *Set) Len() int {
s.RLock()
defer s.RUnlock()
return len(s.m)
}
func (s *Set) Clear() {
s.Lock()
defer s.Unlock()
s.m = make(map[string]Null)
}
func (s *Set) Range(fun func(key interface{}) bool) {
s.RLock()
defer s.RUnlock()
for k := range s.m {
if !fun(k) {
return
}
}
}