| package model |
| |
| import "sync" |
| |
| type Set struct { |
| m map[string]bool |
| sync.RWMutex |
| } |
| |
| func NewSet() *Set { |
| return &Set{ |
| m: make(map[string]bool), |
| } |
| } |
| |
| // Add add |
| func (s *Set) Add(item string) { |
| s.Lock() |
| defer s.Unlock() |
| s.m[item] = true |
| } |
| |
| // Remove deletes the specified item from the map |
| func (s *Set) Remove(item string) { |
| s.Lock() |
| defer s.Unlock() |
| delete(s.m, item) |
| } |
| |
| // Has looks for the existence of an item |
| func (s *Set) Has(item string) bool { |
| s.RLock() |
| defer s.RUnlock() |
| _, ok := s.m[item] |
| return ok |
| } |
| |
| // Len returns the number of items in a set. |
| func (s *Set) Len() int { |
| return len(s.List()) |
| } |
| |
| // Clear removes all items from the set |
| func (s *Set) Clear() { |
| s.Lock() |
| defer s.Unlock() |
| s.m = make(map[string]bool) |
| } |
| |
| // IsEmpty checks for emptiness |
| func (s *Set) IsEmpty() bool { |
| if s.Len() == 0 { |
| return true |
| } |
| return false |
| } |
| |
| // Set returns a slice of all items |
| func (s *Set) List() []string { |
| s.RLock() |
| defer s.RUnlock() |
| list := make([]string, 0) |
| for item := range s.m { |
| list = append(list, item) |
| } |
| return list |
| } |
| |
| |