mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-26 03:25:35 +00:00
dc7aa98b76
I didn't clean up the more idiomatic map[T]bool with true values, at least yet. I just converted the relatively awkward struct{}-valued maps. Updates #cleanup Change-Id: I758abebd2bb1f64bc7a9d0f25c32298f4679c14f Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package set contains set types.
|
|
package set
|
|
|
|
// Set is a set of T.
|
|
type Set[T comparable] map[T]struct{}
|
|
|
|
// Add adds e to the set.
|
|
func (s Set[T]) Add(e T) { s[e] = struct{}{} }
|
|
|
|
// Delete removes e from the set.
|
|
func (s Set[T]) Delete(e T) { delete(s, e) }
|
|
|
|
// Contains reports whether s contains e.
|
|
func (s Set[T]) Contains(e T) bool {
|
|
_, ok := s[e]
|
|
return ok
|
|
}
|
|
|
|
// Len reports the number of items in s.
|
|
func (s Set[T]) Len() int { return len(s) }
|
|
|
|
// HandleSet is a set of T.
|
|
//
|
|
// It is not safe for concurrent use.
|
|
type HandleSet[T any] map[Handle]T
|
|
|
|
// Handle is a opaque comparable value that's used as the map key
|
|
// in a HandleSet. The only way to get one is to call HandleSet.Add.
|
|
type Handle struct {
|
|
v *byte
|
|
}
|
|
|
|
// Add adds the element (map value) e to the set.
|
|
//
|
|
// It returns the handle (map key) with which e can be removed, using a map
|
|
// delete.
|
|
func (s *HandleSet[T]) Add(e T) Handle {
|
|
h := Handle{new(byte)}
|
|
if *s == nil {
|
|
*s = make(HandleSet[T])
|
|
}
|
|
(*s)[h] = e
|
|
return h
|
|
}
|