Documentation ¶
Overview ¶
Package collections provides Go implementations of basic collections and other data structures.
Instantiate a collection X using the collections.NewX functions, which take an initial capacity as argument:
l := collections.NewList[string](10) m := collections.NewMap[string, int](10) s := collections.NewSet[string](10) q := collections.NewQueue[int](10) k := collections.NewStack[byte](5)
Or convert your existing slices/maps to collections using the collections.AsX functions:
l := collections.AsList([]int{0, 1}) m := collections.AsMap(map[int]int{0: 0, 1: 1}) q := collections.AsQueue([]int{0, 1}) k := collections.AsStack([]int{0, 1})
Code generated by github.com/barrettj12/collections/internal/gen. DO NOT EDIT. Generated from package "strings", version go1.19.4
Index ¶
- type Iterator
- type Iterator2
- type List
- func (l *List[T]) Append(t ...T)
- func (l *List[T]) AsSlice() []T
- func (l *List[T]) Capacity() int
- func (l *List[T]) Contains(t T) bool
- func (l *List[T]) Copy() *List[T]
- func (l *List[T]) CopyPart(low, high int) (*List[T], error)
- func (l *List[T]) Count(f func(int, T) bool) int
- func (l *List[T]) Filter(f func(int, T) bool) *List[T]
- func (l *List[T]) Find(t T) (pos int, err error)
- func (l *List[T]) Get(pos int) (t T, err error)
- func (l *List[T]) Insert(t T, pos int) error
- func (l *List[T]) IsEmpty() bool
- func (l *List[T]) Len() int
- func (l *List[T]) Remove(pos int) (t T, err error)
- func (l *List[T]) RemoveAll(t T)
- func (l *List[T]) Set(pos int, t T) error
- func (l *List[T]) Shuffle()
- func (l *List[T]) Size() int
- func (l *List[T]) Slice(low, high int) error
- func (l *List[T]) Sort(less func(s, t T) bool)
- type Map
- func (m *Map[K, V]) AsSlice() map[K]V
- func (m *Map[K, V]) Contains(k K) bool
- func (m *Map[K, V]) Copy() *Map[K, V]
- func (m *Map[K, V]) Get(k K) (v V, err error)
- func (m *Map[K, V]) IsEmpty() bool
- func (m *Map[K, V]) Iterate(keyOrder func(K, K) bool) Iterator2[K, V]
- func (m *Map[K, V]) Keys() *List[K]
- func (m *Map[K, V]) Remove(k K) bool
- func (m *Map[K, V]) Set(k K, v V)
- func (m *Map[K, V]) Size() int
- type Queue
- type Set
- func AsSet[T comparable](elems []T) *Set[T]
- func Difference[T comparable](s1, s2 *Set[T]) *Set[T]
- func Intersection[T comparable](sets ...*Set[T]) *Set[T]
- func NewSet[T comparable](capacity int) *Set[T]
- func SymmetricDifference[T comparable](s1, s2 *Set[T]) *Set[T]
- func Union[T comparable](sets ...*Set[T]) *Set[T]
- type Stack
- type String
- func (s *String) Clone() string
- func (s *String) Compare(b string) int
- func (s *String) Contains(substr string) bool
- func (s *String) ContainsAny(chars string) bool
- func (s *String) ContainsRune(r rune) bool
- func (s *String) Count(substr string) int
- func (s *String) Cut(sep string) (before, after string, found bool)
- func (s *String) EqualFold(t string) bool
- func (s *String) Fields() []string
- func (s *String) FieldsFunc(f func(rune) bool) []string
- func (s *String) HasPrefix(prefix string) bool
- func (s *String) HasSuffix(suffix string) bool
- func (s *String) Index(substr string) int
- func (s *String) IndexAny(chars string) int
- func (s *String) IndexByte(c byte) int
- func (s *String) IndexFunc(f func(rune) bool) int
- func (s *String) IndexRune(r rune) int
- func (s *String) LastIndex(substr string) int
- func (s *String) LastIndexAny(chars string) int
- func (s *String) LastIndexByte(c byte) int
- func (s *String) LastIndexFunc(f func(rune) bool) int
- func (s *String) NewReader() *strings.Reader
- func (s *String) Repeat(count int) string
- func (s *String) Replace(old, new string, n int)
- func (s *String) ReplaceAll(old, new string)
- func (s *String) Split(sep string) []string
- func (s *String) SplitAfter(sep string) []string
- func (s *String) SplitAfterN(sep string, n int) []string
- func (s *String) SplitN(sep string, n int) []string
- func (s *String) Title()deprecated
- func (s *String) ToLower()
- func (s *String) ToTitle()
- func (s *String) ToUpper()
- func (s *String) ToValidUTF8(replacement string)
- func (s *String) Trim(cutset string)
- func (s *String) TrimFunc(f func(rune) bool)
- func (s *String) TrimLeft(cutset string)
- func (s *String) TrimLeftFunc(f func(rune) bool)
- func (s *String) TrimPrefix(prefix string)
- func (s *String) TrimRight(cutset string)
- func (s *String) TrimRightFunc(f func(rune) bool)
- func (s *String) TrimSpace()
- func (s *String) TrimSuffix(suffix string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Iterator ¶
type Iterator[T any] interface { // HasNext returns true if there are more values in the iterator. HasNext() bool // Next returns the next value in the iterator, and advances iteration. // Implementations of Next may panic if there are no more values to return. // To be safe, always call HasNext before calling Next. Next() T }
Iterator allows iteration over a collection.
type Iterator2 ¶
type Iterator2[T, U any] interface { // HasNext returns true if there are more values in the iterator. HasNext() bool // Next returns the next value in the iterator, and advances iteration. // Implementations of Next may panic if there are no more values to return. // To be safe, always call HasNext before calling Next. Next() (T, U) }
Iterator2 allows iteration over a bivalent collection (e.g. a Map).
type List ¶
type List[T comparable] []T
List is an implementation of a list using a slice.
func AsList ¶
func AsList[T comparable](s []T) *List[T]
AsList returns a List backed by the given slice.
func NewList ¶
func NewList[T comparable](capacity int) *List[T]
NewList makes a new List with the specified initial capacity.
func (*List[T]) Append ¶
func (l *List[T]) Append(t ...T)
Append appends the given elements to the end of the List.
func (*List[T]) AsSlice ¶
func (l *List[T]) AsSlice() []T
AsSlice returns the underlying slice for this List.
func (*List[T]) CopyPart ¶
CopyPart returns a partial copy of the given List, i.e. it copies the part of the List starting at low and ending at high-1. It returns an error if either index is out of bounds, or if low is greater than high.
CopyPart accepts "Python-style" indices which can be negative: -1 refers to the last element of the List, -2 the second last, etc.
func (*List[T]) Count ¶
Count counts the number of elements t in this List such that f(index(t), t) == true.
func (*List[T]) Filter ¶
Filter returns a new List containing only the elements t in this List such that f(index(t), t) == false.
func (*List[T]) Find ¶
Find returns the first index at which the given element appears, or returns an error if the element is not found.
func (*List[T]) Get ¶
Get returns the element at index pos in this List. It returns an error if the given index is out of bounds.
func (*List[T]) Insert ¶
Insert inserts t at position pos in this List. It returns an error if the given index is out of bounds.
func (*List[T]) Remove ¶
Remove removes the element at the given index in this List, shifting all other elements down to "fill in the gap". It returns the removed element, or an error if the given index is out of bounds.
func (*List[T]) RemoveAll ¶
func (l *List[T]) RemoveAll(t T)
RemoveAll removes all occurrences of the given element in the List.
func (*List[T]) Set ¶
Set replaces the element at position pos with t. It returns an error if the given index is out of bounds.
func (*List[T]) Shuffle ¶
func (l *List[T]) Shuffle()
Shuffle randomises the order of elements using rand.Shuffle.
func (*List[T]) Slice ¶
Slice slices this List at the given indices, removing all elements with index smaller than low or at least high. It returns an error if either index is out of bounds, or if low is greater than high.
Slice accepts "Python-style" indices which can be negative: -1 refers to the last element of the List, -2 the second last, etc.
type Map ¶
type Map[K comparable, V any] map[K]V
Map is an implementation of a map using Go's hashmap.
func AsMap ¶
func AsMap[K comparable, V any](m map[K]V) *Map[K, V]
AsMap returns a Map backed by the given map.
func NewMap ¶
func NewMap[K comparable, V any](capacity int) *Map[K, V]
NewMap makes a new Map with the specified initial capacity.
func (*Map[K, V]) AsSlice ¶
func (m *Map[K, V]) AsSlice() map[K]V
Underlying returns the underlying map for this Map.
func (*Map[K, V]) Get ¶
Get returns the value associated with k. If k is not a key in this Map, Get returns an error.
func (*Map[K, V]) Iterate ¶
Iterate returns an Iterator2 iterating over the given map. If a non-nil comparator keyOrder is provided, then the iteration will be in the order determined on the keys.
func (*Map[K, V]) Remove ¶
Remove removes k and its associated value from this Map. It returns false if k was not in the Map to begin with, and returns true if k was removed.
type Queue ¶
type Queue[T any] struct { // contains filtered or unexported fields }
Queue is an implementation of a queue using a slice.
func (*Queue[T]) AsSlice ¶
func (q *Queue[T]) AsSlice() []T
AsSlice returns the underlying slice for this Queue.
func (*Queue[T]) Dequeue ¶
Dequeue removes the front element of the Queue and returns it. It returns an error if the Queue is empty.
func (*Queue[T]) Enqueue ¶
func (q *Queue[T]) Enqueue(t T)
Enqueue adds the given element to the back of this Queue.
type Set ¶
type Set[T comparable] map[T]o
Set is a implementation of a set using a Go hashmap.
func AsSet ¶
func AsSet[T comparable](elems []T) *Set[T]
AsSet returns a Set containing the elements of the given slice.
func Difference ¶
func Difference[T comparable](s1, s2 *Set[T]) *Set[T]
Difference returns the set of all elements in s1 which are not in s2.
func Intersection ¶
func Intersection[T comparable](sets ...*Set[T]) *Set[T]
Intersection returns the Set of elements which are in all of the given sets.
func NewSet ¶
func NewSet[T comparable](capacity int) *Set[T]
NewSet makes a new Set with the specified initial capacity.
func SymmetricDifference ¶
func SymmetricDifference[T comparable](s1, s2 *Set[T]) *Set[T]
SymmetricDifference returns the set of all elements which are in exactly one of s1 and s2.
func Union ¶
func Union[T comparable](sets ...*Set[T]) *Set[T]
Union returns the Set of all elements which are in any of the given Sets.
func (*Set[T]) Add ¶
func (s *Set[T]) Add(t T)
Add adds t to this Set, if it is not already in the Set.
func (*Set[T]) Remove ¶
Remove removes t from this Set. It returns false if t was not in the Set to begin with, and returns true if t was removed from the Set.
type Stack ¶
type Stack[T any] struct { // contains filtered or unexported fields }
Stack is an implementation of a stack using a slice.
func (*Stack[T]) AsSlice ¶
func (s *Stack[T]) AsSlice() []T
AsSlice returns the underlying slice for this Stack.
func (*Stack[T]) Peek ¶
Peek returns the top element of this Stack, without removing it from the Stack. It returns an error if the Stack is empty.
func (*Stack[T]) Pop ¶
Pop removes the top element of the Stack and returns it. It returns an error if the Stack is empty.
type String ¶
type String string
String is a wrapper around a string.
func (*String) Clone ¶
Clone returns a fresh copy of s. It guarantees to make a copy of s into a new allocation, which can be important when retaining only a small substring of a much larger string. Using Clone can help such programs use less memory. Of course, since using Clone makes a copy, overuse of Clone can make programs use more memory. Clone should typically be used only rarely, and only when profiling indicates that it is needed. For strings of length zero the string "" will be returned and no allocation is made.
func (*String) Compare ¶
Compare returns an integer comparing two strings lexicographically. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
Compare is included only for symmetry with package bytes. It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.
func (*String) ContainsAny ¶
ContainsAny reports whether any Unicode code points in chars are within s.
func (*String) ContainsRune ¶
ContainsRune reports whether the Unicode code point r is within s.
func (*String) Count ¶
Count counts the number of non-overlapping instances of substr in s. If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
func (*String) Cut ¶
Cut slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, "", false.
func (*String) EqualFold ¶
EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.
func (*String) Fields ¶
Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.
func (*String) FieldsFunc ¶
FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned.
FieldsFunc makes no guarantees about the order in which it calls f(c) and assumes that f always returns the same value for a given c.
func (*String) Index ¶
Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
func (*String) IndexAny ¶
IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
func (*String) IndexByte ¶
IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
func (*String) IndexFunc ¶
IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.
func (*String) IndexRune ¶
IndexRune returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s. If r is utf8.RuneError, it returns the first instance of any invalid UTF-8 byte sequence.
func (*String) LastIndex ¶
LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
func (*String) LastIndexAny ¶
LastIndexAny returns the index of the last instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
func (*String) LastIndexByte ¶
LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
func (*String) LastIndexFunc ¶
LastIndexFunc returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.
func (*String) NewReader ¶
NewReader returns a new Reader reading from s. It is similar to bytes.NewBufferString but more efficient and read-only.
func (*String) Repeat ¶
Repeat returns a new string consisting of count copies of the string s.
It panics if count is negative or if the result of (len(s) * count) overflows.
func (*String) Replace ¶
Replace modifies s so that it has the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.
func (*String) ReplaceAll ¶
ReplaceAll modifies s so that it has all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string.
func (*String) Split ¶
Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.
If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.
It is equivalent to SplitN with a count of -1.
To split around the first instance of a separator, see Cut.
func (*String) SplitAfter ¶
SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings.
If s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s.
If sep is empty, SplitAfter splits after each UTF-8 sequence. If both s and sep are empty, SplitAfter returns an empty slice.
It is equivalent to SplitAfterN with a count of -1.
func (*String) SplitAfterN ¶
SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings.
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for SplitAfter.
func (*String) SplitN ¶
SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
Edge cases for s and sep (for example, empty strings) are handled as described in the documentation for Split.
To split around the first instance of a separator, see Cut.
func (*String) ToLower ¶
func (s *String) ToLower()
ToLower modifies s so that it has all Unicode letters mapped to their lower case.
func (*String) ToTitle ¶
func (s *String) ToTitle()
ToTitle modifies s so that it has all Unicode letters mapped to their Unicode title case.
func (*String) ToUpper ¶
func (s *String) ToUpper()
ToUpper modifies s so that it has all Unicode letters mapped to their upper case.
func (*String) ToValidUTF8 ¶
ToValidUTF8 modifies s so that it has each run of invalid UTF-8 byte sequences replaced by the replacement string, which may be empty.
func (*String) Trim ¶
Trim modifies s so that it has all leading and trailing Unicode code points contained in cutset removed.
func (*String) TrimFunc ¶
TrimFunc modifies s so that it has all leading and trailing Unicode code points c satisfying f(c) removed.
func (*String) TrimLeft ¶
TrimLeft modifies s so that it has all leading Unicode code points contained in cutset removed.
To remove a prefix, use TrimPrefix instead.
func (*String) TrimLeftFunc ¶
TrimLeftFunc modifies s so that it has all leading Unicode code points c satisfying f(c) removed.
func (*String) TrimPrefix ¶
TrimPrefix removes from s the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.
func (*String) TrimRight ¶
TrimRight modifies s so that it has all trailing Unicode code points contained in cutset removed.
To remove a suffix, use TrimSuffix instead.
func (*String) TrimRightFunc ¶
TrimRightFunc modifies s so that it has all trailing Unicode code points c satisfying f(c) removed.
func (*String) TrimSpace ¶
func (s *String) TrimSpace()
TrimSpace modifies s so that it has all leading and trailing white space removed, as defined by Unicode.
func (*String) TrimSuffix ¶
TrimSuffix removes from s the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
gen
This package contains code-generation tools, particularly those which generate methods on data types from standard library or experimental packages.
|
This package contains code-generation tools, particularly those which generate methods on data types from standard library or experimental packages. |