sets

package
v0.0.0-...-dc23390 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2018 License: BSD-3-Clause Imports: 2 Imported by: 0

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

func NewExpirableSet(initialCapacity int, ttl time.Duration) Set

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

func NewSet(initialCapacity int) Set

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

func NewSetFromStrings(strings ...string) Set

NewSetFromStrings creates a new set from the specified strings.

func NewThreadSafeExpirableSet

func NewThreadSafeExpirableSet(initialCapacity int, ttl time.Duration) Set

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL