| OLD | NEW |
| (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 } |
| OLD | NEW |