Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Unified Diff: go/src/infra/libs/infra_util/stringSet.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: more tests and refactors Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « go/src/infra/libs/infra_util/errors.go ('k') | go/src/infra/libs/infra_util/test_utils.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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()
+}
« no previous file with comments | « go/src/infra/libs/infra_util/errors.go ('k') | go/src/infra/libs/infra_util/test_utils.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698