| Index: go/src/infra/libs/infra_util/stringSet.go
|
| diff --git a/go/src/infra/libs/infra_util/stringSet.go b/go/src/infra/libs/infra_util/stringSet.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2b417edb1d50a4cc84c86d7a3bde54d7f1aa32d4
|
| --- /dev/null
|
| +++ b/go/src/infra/libs/infra_util/stringSet.go
|
| @@ -0,0 +1,60 @@
|
| +// Copyright 2014 The Chromium 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 infra_util
|
| +
|
| +import (
|
| + "bytes"
|
| + "fmt"
|
| +)
|
| +
|
| +type StringSet map[string]struct{}
|
| +
|
| +func StringSetFrom(elems ...string) StringSet {
|
| + ret := make(StringSet, len(elems))
|
| + ret.Add(elems...)
|
| + return ret
|
| +}
|
| +
|
| +func (s StringSet) Cardinality() int { return len(s) }
|
| +
|
| +func (s StringSet) Add(elems ...string) (ret int) {
|
| + for _, e := range elems {
|
| + if !s.Has(e) {
|
| + ret++
|
| + s[e] = struct{}{}
|
| + }
|
| + }
|
| + return
|
| +}
|
| +
|
| +func (s StringSet) Remove(elems ...string) (ret int) {
|
| + for _, e := range elems {
|
| + if s.Has(e) {
|
| + delete(s, e)
|
| + ret++
|
| + }
|
| + }
|
| + return
|
| +}
|
| +
|
| +func (s StringSet) Has(elem string) bool {
|
| + _, has := s[elem]
|
| + return has
|
| +}
|
| +
|
| +func (s StringSet) String() string {
|
| + buf := &bytes.Buffer{}
|
| + fmt.Fprintf(buf, "set(")
|
| + first := true
|
| + for e := range s {
|
| + if !first {
|
| + fmt.Fprint(buf, ",")
|
| + }
|
| + first = false
|
| + fmt.Fprintf(buf, "%#v", e)
|
| + }
|
| + fmt.Fprintf(buf, ")")
|
| + return buf.String()
|
| +}
|
|
|