Documentation
¶
Overview ¶
Package collections provides a thread-safe implementation of a FIFO (First-In, First-Out) queue data structure.
Package collections provides a thread-safe implementation of a LIFO (Last-In, First-Out) stack data structure.
Index ¶
- func AddIfNotExists[T comparable](vs []T, t T) []T
- func All[T any](vs []T, f func(T) bool) bool
- func Any[T any](vs []T, f func(T) bool) bool
- func BitMaskInclude(src, flag int) bool
- func Concat[T any](slices ...[]T) []T
- func Distinct[T comparable](vs []T) []T
- func Filter[T any](vs []T, f func(T) bool) []T
- func InSet[T comparable](set map[T]struct{}, item T) bool
- func InSetAny[T comparable](haystack map[T]struct{}, needles []T) bool
- func Include[T comparable](vs []T, t T) bool
- func IncludeMask(vs []int, t int) bool
- func Index[T comparable](vs []T, t T) int
- func Intersect[T comparable](slices ...[]T) []T
- func JoinN(slice []int, sep string) string
- func MakeSet[T comparable](items []T) map[T]struct{}
- func Map[T any, U any](vs []T, f func(T) U) []U
- func Remove[T comparable](vs []T, t T) []T
- type ConcurrentStringMap
- func (c *ConcurrentStringMap[T]) Delete(key string)
- func (c *ConcurrentStringMap[T]) DeleteAll()
- func (c *ConcurrentStringMap[T]) Get(key string) (T, bool)
- func (c *ConcurrentStringMap[T]) Keys() []string
- func (c *ConcurrentStringMap[T]) KeysAndValues() ([]string, []T)
- func (c *ConcurrentStringMap[T]) Put(key string, val T)
- func (c *ConcurrentStringMap[T]) Values() []T
- type LookupTable
- type Queue
- type Stack
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddIfNotExists ¶
func AddIfNotExists[T comparable](vs []T, t T) []T
AddIfNotExists adds an element to the slice if it is not already present.
Type Parameters:
T: The type of the elements in the slice, which must be comparable.
Parameters:
vs: The slice to add to. t: The element to add.
Returns:
A new slice with the element added if it was not present, or the original slice.
func All ¶
All returns `true` if all elements in the slice `vs` satisfy the predicate `f`.
Type Parameters:
T: The type of the elements in the slice.
Parameters:
vs: The slice to check. f: The predicate function.
Returns:
`true` if all elements satisfy `f`, `false` otherwise.
func Any ¶
Any returns `true` if at least one element in the slice `vs` satisfies the predicate `f`.
Type Parameters:
T: The type of the elements in the slice.
Parameters:
vs: The slice to check. f: The predicate function.
Returns:
`true` if any element satisfies `f`, `false` otherwise.
func BitMaskInclude ¶
BitMaskInclude checks if a source bitmask includes a given flag.
Parameters:
src: The source bitmask. flag: The flag to check for.
Returns:
`true` if the source bitmask includes the flag, `false` otherwise.
func Concat ¶
func Concat[T any](slices ...[]T) []T
Concat concatenates multiple slices into a single slice.
Type Parameters:
T: The type of the elements in the slices.
Parameters:
slices: The slices to concatenate.
Returns:
A new slice containing all elements from the input slices.
func Distinct ¶
func Distinct[T comparable](vs []T) []T
Distinct returns a new slice with all duplicate values removed.
Type Parameters:
T: The type of the elements in the slice, which must be comparable.
Parameters:
vs: The slice to remove duplicates from.
Returns:
A new slice with unique elements.
func Filter ¶
Filter returns a new slice containing all elements from `vs` that satisfy the predicate `f`.
Type Parameters:
T: The type of the elements in the slice.
Parameters:
vs: The slice to filter. f: The predicate function.
Returns:
A new slice with the filtered elements.
func InSet ¶ added in v1.2.151
func InSet[T comparable](set map[T]struct{}, item T) bool
InSet checks if an item is present in a set.
Type Parameters:
T: The type of the item, which must be comparable.
Parameters:
set: The set to check for membership in. item: The item to check for.
Returns:
`true` if the item is in the set, `false` otherwise.
func InSetAny ¶ added in v1.2.151
func InSetAny[T comparable](haystack map[T]struct{}, needles []T) bool
InSetAny checks if any of the items in a slice are present in a set. It returns true as soon as the first match is found.
Type Parameters:
T: The type of the items, which must be comparable.
Parameters:
haystack: The set to search within. needles: A slice of items to search for.
Returns:
`true` if any of the `needles` are found in the `haystack`, `false` otherwise.
func Include ¶
func Include[T comparable](vs []T, t T) bool
Include returns `true` if the target value `t` is in the slice `vs`.
Type Parameters:
T: The type of the elements in the slice, which must be comparable.
Parameters:
vs: The slice to search in. t: The value to search for.
Returns:
`true` if `t` is found in `vs`, `false` otherwise.
func IncludeMask ¶
IncludeMask checks if any integer in the slice `vs` has all the bits of the mask `t` set.
Parameters:
vs: The slice of integers to check. t: The bitmask to check against.
Returns:
`true` if any integer in `vs` includes the mask `t`, `false` otherwise.
func Index ¶
func Index[T comparable](vs []T, t T) int
Index returns the first index of the target value `t` in the slice `vs`, or -1 if no match is found.
Type Parameters:
T: The type of the elements in the slice, which must be comparable.
Parameters:
vs: The slice to search in. t: The value to search for.
Returns:
The index of the first occurrence of `t`, or -1 if not found.
func Intersect ¶
func Intersect[T comparable](slices ...[]T) []T
Intersect returns a new slice containing only the elements that exist in all given slices.
Type Parameters:
T: The type of the elements in the slices, which must be comparable.
Parameters:
slices: The slices to find the intersection of.
Returns:
A new slice with the common elements.
func JoinN ¶
JoinN converts a slice of integers to a string, with elements separated by a given separator.
Parameters:
slice: The slice of integers to join. sep: The separator string.
Returns:
A string representation of the integer slice.
func MakeSet ¶ added in v1.2.151
func MakeSet[T comparable](items []T) map[T]struct{}
MakeSet creates a set from a slice of items. A set is represented as a map with the items as keys and empty structs as values, which is a common and memory-efficient way to implement sets in Go.
Type Parameters:
T: The type of the items, which must be comparable.
Parameters:
items: A slice of items to be converted into a set.
Returns:
A map representing the set of items.
func Map ¶
Map returns a new slice containing the results of applying the function `f` to each element in the original slice `vs`.
Type Parameters:
T: The type of the elements in the input slice. U: The type of the elements in the output slice.
Parameters:
vs: The slice to map. f: The mapping function.
Returns:
A new slice with the mapped elements.
func Remove ¶
func Remove[T comparable](vs []T, t T) []T
Remove removes the first occurrence of an element from the slice.
Type Parameters:
T: The type of the elements in the slice, which must be comparable.
Parameters:
vs: The slice to remove from. t: The element to remove.
Returns:
A new slice with the element removed.
Types ¶
type ConcurrentStringMap ¶
ConcurrentStringMap is a thread-safe map with string keys. It uses a RWMutex to allow for concurrent read access and exclusive write access.
Type Parameters:
T: The type of the values stored in the map.
func NewConcurrentStringMap ¶
func NewConcurrentStringMap[T any]() *ConcurrentStringMap[T]
NewConcurrentStringMap creates and returns a new ConcurrentStringMap.
Type Parameters:
T: The type of the values to be stored in the map.
Returns:
An initialized ConcurrentStringMap.
func (*ConcurrentStringMap[T]) Delete ¶ added in v1.2.98
func (c *ConcurrentStringMap[T]) Delete(key string)
Delete removes a key-value pair from the map. It is safe for concurrent use.
Parameters:
key: The key of the item to delete.
func (*ConcurrentStringMap[T]) DeleteAll ¶ added in v1.2.98
func (c *ConcurrentStringMap[T]) DeleteAll()
DeleteAll removes all key-value pairs from the map. It is safe for concurrent use.
func (*ConcurrentStringMap[T]) Get ¶
func (c *ConcurrentStringMap[T]) Get(key string) (T, bool)
Get retrieves a value from the map by its key. It is safe for concurrent use.
Parameters:
key: The key of the value to retrieve.
Returns:
The value associated with the key, and a boolean indicating if the key was found.
func (*ConcurrentStringMap[T]) Keys ¶ added in v1.2.63
func (c *ConcurrentStringMap[T]) Keys() []string
Keys returns a slice of all keys in the map. The order of the keys is not guaranteed. It is safe for concurrent use.
Returns:
A slice of strings containing all the keys in the map.
func (*ConcurrentStringMap[T]) KeysAndValues ¶ added in v1.2.63
func (c *ConcurrentStringMap[T]) KeysAndValues() ([]string, []T)
KeysAndValues returns two slices: one with all the keys and one with all the values. The order of keys and values is not guaranteed, but the correspondence between a key and a value at the same index is maintained. It is safe for concurrent use.
Returns:
A slice of keys and a slice of values.
func (*ConcurrentStringMap[T]) Put ¶
func (c *ConcurrentStringMap[T]) Put(key string, val T)
Put adds or updates a value in the map. It is safe for concurrent use.
Parameters:
key: The key of the value to set. val: The value to set.
func (*ConcurrentStringMap[T]) Values ¶ added in v1.2.63
func (c *ConcurrentStringMap[T]) Values() []T
Values returns a slice of all values in the map. The order of the values is not guaranteed. It is safe for concurrent use.
Returns:
A slice containing all the values in the map.
type LookupTable ¶ added in v1.2.161
LookupTable is a generic lookup table, essentially a map with string keys and values of a specified type. It provides a set of convenient methods for common map operations.
Type Parameters:
T: The type of the values stored in the lookup table.
func (LookupTable[T]) Clear ¶ added in v1.2.163
func (lt LookupTable[T]) Clear()
Clear removes all entries from the lookup table, making it empty.
func (LookupTable[T]) Contains ¶ added in v1.2.163
func (lt LookupTable[T]) Contains(key string) bool
Contains checks if the given key exists in the lookup table.
Parameters:
key: The key to check for.
Returns:
`true` if the key exists, `false` otherwise.
func (LookupTable[T]) Delete ¶ added in v1.2.162
func (lt LookupTable[T]) Delete(key string)
Delete removes the entry for the given key from the lookup table. If the key does not exist, this is a no-op.
Parameters:
key: The key of the entry to delete.
func (LookupTable[T]) Get ¶ added in v1.2.161
func (lt LookupTable[T]) Get(key string) (T, bool)
Get retrieves the value associated with the given key.
Parameters:
key: The key to look up.
Returns:
The value associated with the key, and a boolean indicating if the key was found.
func (LookupTable[T]) Keys ¶ added in v1.2.161
func (lt LookupTable[T]) Keys() []string
Keys returns a slice containing all the keys in the lookup table. The order of the keys is not guaranteed.
Returns:
A slice of strings representing the keys.
func (LookupTable[T]) Len ¶ added in v1.2.162
func (lt LookupTable[T]) Len() int
Len returns the number of entries in the lookup table.
Returns:
The number of entries.
func (LookupTable[T]) Values ¶ added in v1.2.161
func (lt LookupTable[T]) Values() []T
Values returns a slice containing all the values in the lookup table. The order of the values is not guaranteed.
Returns:
A slice containing the values.
type Queue ¶
type Queue[T any] interface { // Push adds an item to the end of the queue. Push(v T) // Pop removes and returns the item from the front of the queue. // It also returns a boolean indicating if an item was successfully popped. Pop() (T, bool) // Length returns the number of items in the queue. Length() int }
Queue defines the interface for a generic, thread-safe queue.
Type Parameters:
T: The type of the items stored in the queue.
type Stack ¶
type Stack[T any] interface { // Push adds an item to the top of the stack. Push(v T) // Pop removes and returns the item from the top of the stack. // It also returns a boolean indicating if an item was successfully popped. Pop() (T, bool) // PopMany removes and returns a specified number of items from the top of the stack. // If the stack contains fewer items than requested, it returns all the items in the stack. PopMany(count int) ([]T, bool) // PopAll removes and returns all items from the stack. PopAll() ([]T, bool) // Peek returns the item at the top of the stack without removing it. Peek() (T, bool) // Length returns the number of items in the stack. Length() int // IsEmpty checks if the stack is empty. IsEmpty() bool }
Stack defines the interface for a generic, thread-safe stack.
Type Parameters:
T: The type of the items stored in the stack.