1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-30 18:53:30 +00:00
arikawa/internal/moreatomic/snowflake_set.go
Maximilian von Lindern 32789bb6e2 *: Separate utils and internal (#129)
* Utils: move package utils/heart to internal/heart

* Utils: move package utils/moreatomic to internal/moreatomic

* Utils: move package utils/zlib to internal/zlib
2020-07-29 16:58:33 -07:00

52 lines
1 KiB
Go

package moreatomic
import (
"sync"
"github.com/diamondburned/arikawa/discord"
)
type SnowflakeSet struct {
set map[discord.Snowflake]struct{}
mut sync.Mutex
}
// NewSnowflakeSet creates a new SnowflakeSet.
func NewSnowflakeSet() *SnowflakeSet {
return &SnowflakeSet{
set: make(map[discord.Snowflake]struct{}),
}
}
// Add adds the passed discord.Snowflake to the set.
func (s *SnowflakeSet) Add(flake discord.Snowflake) {
s.mut.Lock()
s.set[flake] = struct{}{}
s.mut.Unlock()
}
// Contains checks whether the passed discord.Snowflake is present in the set.
func (s *SnowflakeSet) Contains(flake discord.Snowflake) (ok bool) {
s.mut.Lock()
defer s.mut.Unlock()
_, ok = s.set[flake]
return
}
// Delete deletes the passed discord.Snowflake from the set and returns true if
// the element is present. If not, Delete is a no-op and returns false.
func (s *SnowflakeSet) Delete(flake discord.Snowflake) bool {
s.mut.Lock()
defer s.mut.Unlock()
if _, ok := s.set[flake]; ok {
delete(s.set, flake)
return true
}
return false
}