github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/utils/set/strings.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package set
     5  
     6  import (
     7  	"sort"
     8  )
     9  
    10  // Strings represents the classic "set" data structure, and contains
    11  // strings.
    12  type Strings struct {
    13  	values map[string]bool
    14  }
    15  
    16  // NewStrings creates and initializes a Strings and populates it with
    17  // initial values as specified in the parameters.
    18  func NewStrings(initial ...string) Strings {
    19  	result := Strings{values: make(map[string]bool)}
    20  	for _, value := range initial {
    21  		result.Add(value)
    22  	}
    23  	return result
    24  }
    25  
    26  // Size returns the number of elements in the set.
    27  func (s Strings) Size() int {
    28  	return len(s.values)
    29  }
    30  
    31  // IsEmpty is true for empty or uninitialized sets.
    32  func (s Strings) IsEmpty() bool {
    33  	return len(s.values) == 0
    34  }
    35  
    36  // Add puts a value into the set.
    37  func (s *Strings) Add(value string) {
    38  	if s.values == nil {
    39  		s.values = make(map[string]bool)
    40  	}
    41  	s.values[value] = true
    42  }
    43  
    44  // Remove takes a value out of the set.  If value wasn't in the set to start
    45  // with, this method silently succeeds.
    46  func (s *Strings) Remove(value string) {
    47  	delete(s.values, value)
    48  }
    49  
    50  // Contains returns true if the value is in the set, and false otherwise.
    51  func (s Strings) Contains(value string) bool {
    52  	_, exists := s.values[value]
    53  	return exists
    54  }
    55  
    56  // Values returns an unordered slice containing all the values in the set.
    57  func (s Strings) Values() []string {
    58  	result := make([]string, len(s.values))
    59  	i := 0
    60  	for key := range s.values {
    61  		result[i] = key
    62  		i++
    63  	}
    64  	return result
    65  }
    66  
    67  // SortedValues returns an ordered slice containing all the values in the set.
    68  func (s Strings) SortedValues() []string {
    69  	values := s.Values()
    70  	sort.Strings(values)
    71  	return values
    72  }
    73  
    74  // Union returns a new Strings representing a union of the elments in the
    75  // method target and the parameter.
    76  func (s Strings) Union(other Strings) Strings {
    77  	result := NewStrings()
    78  	// Use the internal map rather than going through the friendlier functions
    79  	// to avoid extra allocation of slices.
    80  	for value := range s.values {
    81  		result.values[value] = true
    82  	}
    83  	for value := range other.values {
    84  		result.values[value] = true
    85  	}
    86  	return result
    87  }
    88  
    89  // Intersection returns a new Strings representing a intersection of the elments in the
    90  // method target and the parameter.
    91  func (s Strings) Intersection(other Strings) Strings {
    92  	result := NewStrings()
    93  	// Use the internal map rather than going through the friendlier functions
    94  	// to avoid extra allocation of slices.
    95  	for value := range s.values {
    96  		if other.Contains(value) {
    97  			result.values[value] = true
    98  		}
    99  	}
   100  	return result
   101  }
   102  
   103  // Difference returns a new Strings representing all the values in the
   104  // target that are not in the parameter.
   105  func (s Strings) Difference(other Strings) Strings {
   106  	result := NewStrings()
   107  	// Use the internal map rather than going through the friendlier functions
   108  	// to avoid extra allocation of slices.
   109  	for value := range s.values {
   110  		if !other.Contains(value) {
   111  			result.values[value] = true
   112  		}
   113  	}
   114  	return result
   115  }