collections

package module
v0.0.0-...-6aa3a9d Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT Imports: 4 Imported by: 8

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

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]) Capacity

func (l *List[T]) Capacity() int

Capacity returns the current capacity of this List.

func (*List[T]) Contains

func (l *List[T]) Contains(t T) bool

Contains returns true if the given element is in the List.

func (*List[T]) Copy

func (l *List[T]) Copy() *List[T]

Copy returns a copy of the given List.

func (*List[T]) CopyPart

func (l *List[T]) CopyPart(low, high int) (*List[T], error)

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

func (l *List[T]) Count(f func(int, T) bool) int

Count counts the number of elements t in this List such that f(index(t), t) == true.

func (*List[T]) Filter

func (l *List[T]) Filter(f func(int, T) bool) *List[T]

Filter returns a new List containing only the elements t in this List such that f(index(t), t) == false.

func (*List[T]) Find

func (l *List[T]) Find(t T) (pos int, err error)

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

func (l *List[T]) Get(pos int) (t T, err error)

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

func (l *List[T]) Insert(t T, pos int) error

Insert inserts t at position pos in this List. It returns an error if the given index is out of bounds.

func (*List[T]) IsEmpty

func (l *List[T]) IsEmpty() bool

IsEmpty returns true if this List is empty.

func (*List[T]) Len

func (l *List[T]) Len() int

Len returns the number of elements in this List.

func (*List[T]) Remove

func (l *List[T]) Remove(pos int) (t T, err error)

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

func (l *List[T]) Set(pos int, t T) error

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]) Size

func (l *List[T]) Size() int

Size returns the number of elements in this List.

func (*List[T]) Slice

func (l *List[T]) Slice(low, high int) error

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.

func (*List[T]) Sort

func (l *List[T]) Sort(less func(s, t T) bool)

Sort sorts this List according to the provided less function.

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]) Contains

func (m *Map[K, V]) Contains(k K) bool

Contains returns true if the given key is in the Map.

func (*Map[K, V]) Copy

func (m *Map[K, V]) Copy() *Map[K, V]

Copy returns a copy of the given Map.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(k K) (v V, err error)

Get returns the value associated with k. If k is not a key in this Map, Get returns an error.

func (*Map[K, V]) IsEmpty

func (m *Map[K, V]) IsEmpty() bool

IsEmpty returns true if this Map is empty.

func (*Map[K, V]) Iterate

func (m *Map[K, V]) Iterate(keyOrder func(K, K) bool) Iterator2[K, V]

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]) Keys

func (m *Map[K, V]) Keys() *List[K]

Keys returns all keys present in this Map.

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(k K) bool

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.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(k K, v V)

Set adds the given key-value pair to this Map. If there is already a value associated with k, it will be overwritten.

func (*Map[K, V]) Size

func (m *Map[K, V]) Size() int

Size returns the number of entries in this Map.

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue is an implementation of a queue using a slice.

func AsQueue

func AsQueue[T any](elems []T) *Queue[T]

AsQueue returns a Queue backed by the given slice.

func NewQueue

func NewQueue[T any](capacity int) *Queue[T]

NewQueue makes a new Queue with the specified initial capacity.

func (*Queue[T]) AsSlice

func (q *Queue[T]) AsSlice() []T

AsSlice returns the underlying slice for this Queue.

func (*Queue[T]) Capacity

func (q *Queue[T]) Capacity() int

Capacity returns the current capacity of this Queue.

func (*Queue[T]) Copy

func (q *Queue[T]) Copy() *Queue[T]

Copy returns a copy of the given Queue.

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (t T, err error)

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.

func (*Queue[T]) IsEmpty

func (q *Queue[T]) IsEmpty() bool

IsEmpty returns true if this Queue is empty.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() (t T, err error)

Peek returns the front element of this Queue, without removing it from the Queue. It returns an error if the Queue is empty.

func (*Queue[T]) Size

func (q *Queue[T]) Size() int

Size returns the number of elements in 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]) Contains

func (s *Set[T]) Contains(t T) bool

Contains returns true if the given element is in the List.

func (*Set[T]) Copy

func (s *Set[T]) Copy() *Set[T]

Copy returns a copy of the given Set.

func (*Set[T]) IsEmpty

func (s *Set[T]) IsEmpty() bool

IsEmpty returns true if this Set is empty.

func (*Set[T]) Remove

func (s *Set[T]) Remove(t T) bool

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.

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size returns the number of elements in this Set.

func (*Set[T]) Slice

func (s *Set[T]) Slice() []T

Slice returns a slice containing the elements of this Set.

func (*Set[T]) String

func (s *Set[T]) String() string

String returns a string representation of this Set.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack is an implementation of a stack using a slice.

func AsStack

func AsStack[T any](elems []T) *Stack[T]

AsStack returns a Stack backed by the given slice.

func NewStack

func NewStack[T any](capacity int) *Stack[T]

NewStack makes a new Stack with the specified initial capacity.

func (*Stack[T]) AsSlice

func (s *Stack[T]) AsSlice() []T

AsSlice returns the underlying slice for this Stack.

func (*Stack[T]) Capacity

func (s *Stack[T]) Capacity() int

Capacity returns the current capacity of this Stack.

func (*Stack[T]) Copy

func (s *Stack[T]) Copy() *Stack[T]

Copy returns a copy of the given Stack.

func (*Stack[T]) IsEmpty

func (s *Stack[T]) IsEmpty() bool

IsEmpty returns true if this Stack is empty.

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (t T, err error)

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

func (s *Stack[T]) Pop() (t T, err error)

Pop removes the top element of the Stack and returns it. It returns an error if the Stack is empty.

func (*Stack[T]) Push

func (s *Stack[T]) Push(t T)

Push adds the given element to the top of this Stack.

func (*Stack[T]) Size

func (s *Stack[T]) Size() int

Size returns the number of elements in this Stack.

type String

type String string

String is a wrapper around a string.

func (*String) Clone

func (s *String) Clone() string

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

func (s *String) Compare(b string) int

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) Contains

func (s *String) Contains(substr string) bool

Contains reports whether substr is within s.

func (*String) ContainsAny

func (s *String) ContainsAny(chars string) bool

ContainsAny reports whether any Unicode code points in chars are within s.

func (*String) ContainsRune

func (s *String) ContainsRune(r rune) bool

ContainsRune reports whether the Unicode code point r is within s.

func (*String) Count

func (s *String) Count(substr string) int

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

func (s *String) Cut(sep string) (before, after string, found bool)

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

func (s *String) EqualFold(t string) bool

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

func (s *String) Fields() []string

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

func (s *String) FieldsFunc(f func(rune) bool) []string

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) HasPrefix

func (s *String) HasPrefix(prefix string) bool

HasPrefix tests whether the string s begins with prefix.

func (*String) HasSuffix

func (s *String) HasSuffix(suffix string) bool

HasSuffix tests whether the string s ends with suffix.

func (*String) Index

func (s *String) Index(substr string) int

Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.

func (*String) IndexAny

func (s *String) IndexAny(chars string) int

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

func (s *String) IndexByte(c byte) int

IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.

func (*String) IndexFunc

func (s *String) IndexFunc(f func(rune) bool) int

IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.

func (*String) IndexRune

func (s *String) IndexRune(r rune) int

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

func (s *String) LastIndex(substr string) int

LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.

func (*String) LastIndexAny

func (s *String) LastIndexAny(chars string) int

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

func (s *String) LastIndexByte(c byte) int

LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.

func (*String) LastIndexFunc

func (s *String) LastIndexFunc(f func(rune) bool) int

LastIndexFunc returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.

func (*String) NewReader

func (s *String) NewReader() *strings.Reader

NewReader returns a new Reader reading from s. It is similar to bytes.NewBufferString but more efficient and read-only.

func (*String) Repeat

func (s *String) Repeat(count int) string

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

func (s *String) Replace(old, new string, n int)

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

func (s *String) ReplaceAll(old, new string)

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

func (s *String) Split(sep string) []string

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

func (s *String) SplitAfter(sep string) []string

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

func (s *String) SplitAfterN(sep string, n int) []string

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

func (s *String) SplitN(sep string, n int) []string

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) Title deprecated

func (s *String) Title()

Title modifies s so that it has all Unicode letters that begin words mapped to their Unicode title case.

Deprecated: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.

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

func (s *String) ToValidUTF8(replacement string)

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

func (s *String) Trim(cutset string)

Trim modifies s so that it has all leading and trailing Unicode code points contained in cutset removed.

func (*String) TrimFunc

func (s *String) TrimFunc(f func(rune) bool)

TrimFunc modifies s so that it has all leading and trailing Unicode code points c satisfying f(c) removed.

func (*String) TrimLeft

func (s *String) TrimLeft(cutset string)

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

func (s *String) TrimLeftFunc(f func(rune) bool)

TrimLeftFunc modifies s so that it has all leading Unicode code points c satisfying f(c) removed.

func (*String) TrimPrefix

func (s *String) TrimPrefix(prefix string)

TrimPrefix removes from s the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.

func (*String) TrimRight

func (s *String) TrimRight(cutset string)

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

func (s *String) TrimRightFunc(f func(rune) bool)

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

func (s *String) TrimSuffix(suffix string)

TrimSuffix removes from s the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.

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.

Jump to

Keyboard shortcuts

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