1
// Copyright 2013 Canonical Ltd.
2
// Licensed under the LGPLv3, see LICENCE file for details.
10
// Strings represents the classic "set" data structure, and contains strings.
11
type Strings map[string]bool
13
// NewStrings creates and initializes a Strings and populates it with
14
// initial values as specified in the parameters.
15
func NewStrings(initial ...string) Strings {
16
result := make(Strings)
17
for _, value := range initial {
23
// Size returns the number of elements in the set.
24
func (s Strings) Size() int {
28
// IsEmpty is true for empty or uninitialized sets.
29
func (s Strings) IsEmpty() bool {
33
// Add puts a value into the set.
34
func (s Strings) Add(value string) {
36
panic("uninitalised set")
41
// Remove takes a value out of the set. If value wasn't in the set to start
42
// with, this method silently succeeds.
43
func (s Strings) Remove(value string) {
47
// Contains returns true if the value is in the set, and false otherwise.
48
func (s Strings) Contains(value string) bool {
53
// Values returns an unordered slice containing all the values in the set.
54
func (s Strings) Values() []string {
55
result := make([]string, len(s))
64
// SortedValues returns an ordered slice containing all the values in the set.
65
func (s Strings) SortedValues() []string {
71
// Union returns a new Strings representing a union of the elments in the
72
// method target and the parameter.
73
func (s Strings) Union(other Strings) Strings {
74
result := make(Strings)
75
// Use the internal map rather than going through the friendlier functions
76
// to avoid extra allocation of slices.
77
for value := range s {
80
for value := range other {
86
// Intersection returns a new Strings representing a intersection of the elments in the
87
// method target and the parameter.
88
func (s Strings) Intersection(other Strings) Strings {
89
result := make(Strings)
90
// Use the internal map rather than going through the friendlier functions
91
// to avoid extra allocation of slices.
92
for value := range s {
93
if other.Contains(value) {
100
// Difference returns a new Strings representing all the values in the
101
// target that are not in the parameter.
102
func (s Strings) Difference(other Strings) Strings {
103
result := make(Strings)
104
// Use the internal map rather than going through the friendlier functions
105
// to avoid extra allocation of slices.
106
for value := range s {
107
if !other.Contains(value) {