github.com/alexandrestein/gods@v1.0.1/sets/hashset/hashset.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package hashset implements a set backed by a hash table.
     6  //
     7  // Structure is not thread safe.
     8  //
     9  // References: http://en.wikipedia.org/wiki/Set_%28abstract_data_type%29
    10  package hashset
    11  
    12  import (
    13  	"fmt"
    14  	"github.com/alexandrestein/gods/sets"
    15  	"strings"
    16  )
    17  
    18  func assertSetImplementation() {
    19  	var _ sets.Set = (*Set)(nil)
    20  }
    21  
    22  // Set holds elements in go's native map
    23  type Set struct {
    24  	items map[interface{}]struct{}
    25  }
    26  
    27  var itemExists = struct{}{}
    28  
    29  // New instantiates a new empty set
    30  func New() *Set {
    31  	return &Set{items: make(map[interface{}]struct{})}
    32  }
    33  
    34  // Add adds the items (one or more) to the set.
    35  func (set *Set) Add(items ...interface{}) {
    36  	for _, item := range items {
    37  		set.items[item] = itemExists
    38  	}
    39  }
    40  
    41  // Remove removes the items (one or more) from the set.
    42  func (set *Set) Remove(items ...interface{}) {
    43  	for _, item := range items {
    44  		delete(set.items, item)
    45  	}
    46  }
    47  
    48  // Contains check if items (one or more) are present in the set.
    49  // All items have to be present in the set for the method to return true.
    50  // Returns true if no arguments are passed at all, i.e. set is always superset of empty set.
    51  func (set *Set) Contains(items ...interface{}) bool {
    52  	for _, item := range items {
    53  		if _, contains := set.items[item]; !contains {
    54  			return false
    55  		}
    56  	}
    57  	return true
    58  }
    59  
    60  // Empty returns true if set does not contain any elements.
    61  func (set *Set) Empty() bool {
    62  	return set.Size() == 0
    63  }
    64  
    65  // Size returns number of elements within the set.
    66  func (set *Set) Size() int {
    67  	return len(set.items)
    68  }
    69  
    70  // Clear clears all values in the set.
    71  func (set *Set) Clear() {
    72  	set.items = make(map[interface{}]struct{})
    73  }
    74  
    75  // Values returns all items in the set.
    76  func (set *Set) Values() []interface{} {
    77  	values := make([]interface{}, set.Size())
    78  	count := 0
    79  	for item := range set.items {
    80  		values[count] = item
    81  		count++
    82  	}
    83  	return values
    84  }
    85  
    86  // String returns a string representation of container
    87  func (set *Set) String() string {
    88  	str := "HashSet\n"
    89  	items := []string{}
    90  	for k := range set.items {
    91  		items = append(items, fmt.Sprintf("%v", k))
    92  	}
    93  	str += strings.Join(items, ", ")
    94  	return str
    95  }