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

Side by Side 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, 1 month 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package infra_util
6
7 import (
8 "bytes"
9 "fmt"
10 )
11
12 type StringSet map[string]struct{}
13
14 func StringSetFrom(elems ...string) StringSet {
15 ret := make(StringSet, len(elems))
16 ret.Add(elems...)
17 return ret
18 }
19
20 func (s StringSet) Cardinality() int { return len(s) }
21
22 func (s StringSet) Add(elems ...string) (ret int) {
23 for _, e := range elems {
24 if !s.Has(e) {
25 ret++
26 s[e] = struct{}{}
27 }
28 }
29 return
30 }
31
32 func (s StringSet) Remove(elems ...string) (ret int) {
33 for _, e := range elems {
34 if s.Has(e) {
35 delete(s, e)
36 ret++
37 }
38 }
39 return
40 }
41
42 func (s StringSet) Has(elem string) bool {
43 _, has := s[elem]
44 return has
45 }
46
47 func (s StringSet) String() string {
48 buf := &bytes.Buffer{}
49 fmt.Fprintf(buf, "set(")
50 first := true
51 for e := range s {
52 if !first {
53 fmt.Fprint(buf, ",")
54 }
55 first = false
56 fmt.Fprintf(buf, "%#v", e)
57 }
58 fmt.Fprintf(buf, ")")
59 return buf.String()
60 }
OLDNEW
« 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