github.com/diamondburned/arikawa/v2@v2.1.0/internal/moreatomic/snowflake_set.go (about)

     1  package moreatomic
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/diamondburned/arikawa/v2/discord"
     7  )
     8  
     9  type SnowflakeSet struct {
    10  	set map[discord.Snowflake]struct{}
    11  	mut sync.Mutex
    12  }
    13  
    14  // NewSnowflakeSet creates a new SnowflakeSet.
    15  func NewSnowflakeSet() *SnowflakeSet {
    16  	return &SnowflakeSet{
    17  		set: make(map[discord.Snowflake]struct{}),
    18  	}
    19  }
    20  
    21  // Add adds the passed discord.Snowflake to the set.
    22  func (s *SnowflakeSet) Add(flake discord.Snowflake) {
    23  	s.mut.Lock()
    24  
    25  	s.set[flake] = struct{}{}
    26  
    27  	s.mut.Unlock()
    28  }
    29  
    30  // Contains checks whether the passed discord.Snowflake is present in the set.
    31  func (s *SnowflakeSet) Contains(flake discord.Snowflake) (ok bool) {
    32  	s.mut.Lock()
    33  	defer s.mut.Unlock()
    34  
    35  	_, ok = s.set[flake]
    36  	return
    37  }
    38  
    39  // Delete deletes the passed discord.Snowflake from the set and returns true if
    40  // the element is present. If not, Delete is a no-op and returns false.
    41  func (s *SnowflakeSet) Delete(flake discord.Snowflake) bool {
    42  	s.mut.Lock()
    43  	defer s.mut.Unlock()
    44  
    45  	if _, ok := s.set[flake]; ok {
    46  		delete(s.set, flake)
    47  		return true
    48  	}
    49  
    50  	return false
    51  }