protobuf-go/internal/set/strings.go
Joe Tsai d55639e713 internal/set: add set package for set data structures
Package set provides simple set data structures for uint64 and string types.

High-level API:
	type Set(T {}) xxx
	func (Set) Len() int
	func (Set) Has(T) bool
	func (Set) Set(T)
	func (Set) Clear(T)

These data structures are useful for implementing required fields efficiently
or ensuring that protobuf identifiers do not conflict.

Change-Id: If846630a9034909a43121b3e0f6720275f4b7aaf
Reviewed-on: https://go-review.googlesource.com/128898
Reviewed-by: Chris Manghane <cmang@golang.org>
2018-08-09 21:46:35 +00:00

26 lines
538 B
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package set
// Strings represents a set of strings.
type Strings map[string]struct{}
func (ss *Strings) Len() int {
return len(*ss)
}
func (ss *Strings) Has(s string) bool {
_, ok := (*ss)[s]
return ok
}
func (ss *Strings) Set(s string) {
if *ss == nil {
*ss = make(map[string]struct{})
}
(*ss)[s] = struct{}{}
}
func (ss *Strings) Clear(s string) {
delete(*ss, s)
}