Documentation ¶
Overview ¶
Package sets provides set data structures. The set data structures in this package do not replace the recommended implementation in Effective Go: A set can be implemented as a map with value type bool. Set the map entry to true to put the value in the set, and then test it by simple indexing.
attended := map[string]bool{ "Ann": true, "Joe": true, ... } if attended[person] { // will be false if person is not in the map fmt.Println(person, "was at the meeting") }
However, this package provide set types that are more efficient for Add, Remove, and Contains calls; and more functionality on top of that. Due to the lack of generics in Go, it's recommended to choose the map[string]bool when the set must be iterated many times.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set interface { // Add adds the specified element to the set. Add(element interface{}) // Clear empties the set of all elements. Clear() // Contains checks whether or not the specified element belongs to the set. Contains(element interface{}) bool // Remove removes the specified element from the set. Remove(element interface{}) // Size return the number of elements in the set. Size() int // ToSlice converts the set into a slice. ToSlice() []interface{} // ToStringSlice converts the set into a string slice. ToStringSlice() []string }
Set is a set data structure. It does not replace the recommended implementation in Effective Go: A set can be implemented as a map with value type bool. Set the map entry to true to put the value in the set, and then test it by simple indexing.
attended := map[string]bool{ "Ann": true, "Joe": true, ... } if attended[person] { // will be false if person is not in the map fmt.Println(person, "was at the meeting") }
However, this set type is more efficient for Add, Remove, and Contains calls. Due to the lack of generics in Go, it's recommended to choose the map[string]bool when the set must be iterated many times.
func NewExpirableSet ¶
NewExpirableSet creates a new set. The initial capacity does not bound the set's size: sets grow to accommodate the number of elements to store. TTL (time to live) specifies the duration after which an element expires. TODO(Geish): shrink the size on expiry
func NewSet ¶
NewSet creates a new set. The initial capacity does not bound the set's size: sets grow to accommodate the number of elements to store.
func NewSetFromStrings ¶
NewSetFromStrings creates a new set from the specified strings.
func NewThreadSafeExpirableSet ¶
NewThreadSafeExpirableSet creates a new thread-safe set using a read-write mutex. The initial capacity does not bound the set's size: sets grow to accommodate the number of elements to store. TTL (time to live) specifies the duration after which an element expires.