Generic Container and Algorithm Library for Go.
点击这里查看中文文档。
This library depends on go generics, which is introduced in 1.18+.
func AllOf[T any](a []T, pred func(T) bool) bool
AllOf return true if pred(e) returns true for all emements e in a.
Complexity: O(len(a)).
func AnyOf[T any](a []T, pred func(T) bool) bool
AnyOf return true if pred(e) returns true for any emements e in a.
Complexity: O(len(a)).
func Average[T Numeric](a []T) T
Average returns the average value of a.
func Compare[E Ordered](a, b []E) int
Compare compares each elements in a and b.
return 0 if they are equals, return 1 if a > b, return -1 if a < b.
Complexity: O(min(len(a), len(b))).
func Copy[T any](a []T) []T
Copy make a copy of slice a.
Complexity: O(len(a)).
func Count[T comparable](a []T, x T) int
Count returns the number of elements in the slice equals to x.
Complexity: O(len(a)).
func CountIf[T comparable](a []T, pred func(T) bool) int
CountIf returns the number of elements in the slice which pred returns true.
Complexity: O(len(a)).
func Equal[T comparable](a, b []T) bool
Equal returns whether two slices are equal. Return true if they are the same length and all elements are equal.
Complexity: O(min(len(a), len(b))).
func Equals[T comparable](a, b T) bool
Equals wraps the '==' operator for comparable types.
func Find[T comparable](a []T, x T) (index int, ok bool)
Find find the value x in the given slice a linearly. return (index, true) if found, return (_, false) if not found. Complexity: O(len(a)).
func Generate[T any](a []T, gen func() T)
Generate fill each element of a`` with
gen()`.
Complexity: O(len(a)).
func Index[T comparable](a []T, x T) int
Index find the value x in the given slice a linearly.
Return index if found, -1 if not found.
Complexity: O(len(a)).
func IsSorted[T Ordered](a []T) bool
IsSorted returns whether the slice a is sorted in ascending order.
Complexity: O(len(a)).
func Less[T Ordered](a, b T) bool
Less wraps the '<' operator for ordered types.
func Max[T Ordered](a, b T) T
Max return the larger value between a
and b
.
Complexity: O(1).
func MaxN[T Ordered](a ...T) T
MaxN return the maximum value in the sequence a
.
Complexity: O(len(a)).
func Min[T Ordered](a, b T) T
Min return the smaller value between a
and b
.
Complexity: O(1).
func MinMax[T Ordered](a, b T) (min, max T)
MinMax returns both min and max between a and b.
Complexity: O(1).
func MinMaxN[T Ordered](a ...T) (min, max T)
MinMaxN returns both min and max in slice a.
Complexity: O(len(a))
func MinN[T Ordered](a ...T) T
MinN return the minimum value in the sequence a
.
Complexity: O(len(a)).
func NoneOf[T any](a []T, pred func(T) bool) bool
NoneOf return true pred(e) returns true for none emements e in a.
Complexity: O(len(a)).
func Range[T Numeric](first, last T) []T
Range make a []T filled with values in the [first, last)
sequence.
Complexity: O(last-first).
func Remove[T comparable](a []T, x T) []T
Remove remove the elements which equals to x from the input slice. return the processed slice, and the content of the input slice is also changed.
Complexity: O(len(a)).
func RemoveCopy[T comparable](a []T, x T) []T
RemoveCopy remove all elements which equals to x from the input slice. return the processed slice, and the content of the input slice is also changed.
Complexity: O(len(a)).
func RemoveIf[T any](a []T, cond func(T) bool) []T
RemoveIf remove each element which make cond(x) returns true from the input slice, copy other elements to a new slice and return it. The input slice is kept unchanged.
Complexity: O(len(a)).
func RemoveIfCopy[T any](a []T, cond func(T) bool) []T
RemoveIfCopy drops each element which make cond(x) returns true from the input slice, copy other elements to a new slice and return it. The input slice is kept unchanged.
Complexity: O(len(a)).
func Sum[T Numeric](a []T) T
Sum summarize all elements in a. returns the result as type R, you should use SumAs if T can't hold the result. Complexity: O(len(a)).
func SumAs[R, T Numeric](a []T) R
SumAs summarize all elements in a. returns the result as type R, this is useful when T is too small to hold the result. Complexity: O(len(a)).
func Transform[T any](a []T, op func(T) T)
Transform applies the function op to each element in slice a and set it back to the same place in a.
Complexity: O(len(a)).
func TransformCopy[R any, T any](a []T, op func(T) R) []R
TransformCopy applies the function op to each element in slice a and return all the result as a slice.
Complexity: O(len(a)).
func TransformTo[R any, T any](a []T, op func(T) R, b []R)
TransformTo applies the function op to each element in slice a and fill it to slice b.
The len(b) must not lesser than len(a).
Complexity: O(len(a)).
func Unique[T comparable](a []T) []T
Unique remove adjacent repeated elements from the input slice. return the processed slice, and the content of the input slice is also changed.
Complexity: O(len(a)).
func UniqueCopy[T comparable](a []T) []T
UniqueCopy remove adjacent repeated elements from the input slice. return the result slice, and the input slice is kept unchanged.
Complexity: O(len(a)).
type Container[T any] interface {
IsEmpty() bool // IsEmpty checks if the container has no elements.
Len() int // Len returns the number of elements in the container.
Clean() // Clean erases all elements from the container. After this call, Len() returns zero.
}
Container is a holder object that stores a collection of other objects.
type DList[T any] struct {
}
DList is a doubly linked list.
func NewDList[T any]() *DList[T]
NewDList make a new DList object
func NewDListOf[T any](vs ...T) *DList[T]
NewDListOf make a new DList from a serial of values
func (l *DList[T]) Clean()
Clean cleanup the list
func (l *DList[T]) ForEach(cb func(val T))
ForEach iterate the list, apply each element to the cb callback function
func (l *DList[T]) ForEachIf(cb func(val T) bool)
ForEach iterate the list, apply each element to the cb callback function, stop if cb returns false.
func (l *DList[T]) IsEmpty() bool
IsEmpty return whether the list is empty
func (l *DList[T]) Len() int
Len return the length of the list
func (l *DList[T]) PopBack() (T, bool)
func (l *DList[T]) PopFront() (T, bool)
func (l *DList[T]) PushBack(val T)
func (l *DList[T]) PushFront(val T)
func (l *DList[T]) String() string
String convert the list to string
type Float interface {
~float32 | ~float64
}
Float is a constraint that permits any floating-point type. If future releases of Go add new predeclared floating-point types, this constraint will be modified to include them.
type HashFn[T any] func(t T) uint64
HashFn is a function that returns the hash of 't'.
type Integer interface {
Signed | Unsigned
}
Integer is a constraint that permits any integer type. If future releases of Go add new predeclared integer types, this constraint will be modified to include them.
type LessFn[T any] func(a, b T) bool
LessFn is a function that returns whether 'a' is less than 'b'.
type Numeric interface {
Integer | Float
}
Numeric is a constraint that permits any numeric type.
type Ordered interface {
Integer | Float | ~string
}
Ordered is a constraint that permits any ordered type: any type that supports the operators < <= >= >. If future releases of Go add new ordered types, this constraint will be modified to include them.
type Queue[T any] struct {
}
Queue is a FIFO container
func NewQueue[T any]() *Queue[T]
NewQueue create a new Queue object.
func (q *Queue[T]) Clean()
func (q *Queue[T]) IsEmpty() bool
func (q *Queue[T]) Len() int
func (q *Queue[T]) PopBack() (T, bool)
func (q *Queue[T]) PopFront() (T, bool)
func (q *Queue[T]) PushBack(val T)
func (q *Queue[T]) PushFront(val T)
func (q *Queue[T]) String() string
type Set[K comparable] map[K]bool
Set is an associative container that contains a unordered set of unique objects of type K.
func MakeSetOf[K comparable](ks ...K) Set[K]
MakeSetOf creates a new Set object with the initial content from ks.
func (s *Set[K]) Add(k K)
func (s *Set[K]) AddN(ks ...K)
func (s *Set[K]) Clean()
func (s *Set[K]) Del(k K)
func (s *Set[K]) DelN(ks ...K)
func (s *Set[K]) ForEach(cb func(k K))
func (s *Set[K]) ForEachIf(cb func(k K) bool)
func (s *Set[K]) Has(k K) bool
func (s *Set[K]) IsEmpty() bool
func (s *Set[K]) Keys() []K
func (s *Set[K]) Len() int
func (s Set[K]) String() string
type Signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
Signed is a constraint that permits any signed integer type. If future releases of Go add new predeclared signed integer types, this constraint will be modified to include them.
type Stack[T any] struct {
}
Stack s is a container adaptor that provides the functionality of a stack, a LIFO (last-in, first-out) data structure.
func NewStack[T any]() *Stack[T]
NewStack creates a new Stack object.
func NewStackCap[T any](capicity int) *Stack[T]
NewStackCap creates a new Stack object with the specified capicity.
func (s *Stack[T]) Cap() int
func (s *Stack[T]) Clean()
func (s *Stack[T]) IsEmpty() bool
func (s *Stack[T]) Len() int
func (s *Stack[T]) MustPop() T
func (s *Stack[T]) Pop() (val T, ok bool)
func (s *Stack[T]) Push(t T)
type Unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
Unsigned is a constraint that permits any unsigned integer type. If future releases of Go add new predeclared unsigned integer types, this constraint will be modified to include them.
type Vector[T any] []T
Vector is a sequence container representing array that can change in size.
func MakeVector[T any]() Vector[T]
MakeVector creates an empty Vector object.
func MakeVectorCap[T any](c int) Vector[T]
MakeVectorCap creates an empty Vector object with specified capacity.
func MakeVectorOf[T any](v ...T) Vector[T]
MakeVectorOf creates an Vector object with initial values.
func (v *Vector[T]) Append(x ...T)
Append appends the values x... to the tail of the vector.
func (v *Vector[T]) At(i int) T
func (v *Vector[T]) Cap() int
func (v *Vector[T]) Clean()
Clean erases all elements from the vector. After this call, Len() returns zero. Leaves the Cap() of the vector unchanged.
func (v *Vector[T]) Insert(i int, x ...T)
Insert inserts the values x... into the vector at index i. After the insertion, (*v)[i] == x[0]. Insert panics if i is out of range.
Complexity: O(len(s) + len(v)).
func (v *Vector[T]) IsEmpty() bool
func (v *Vector[T]) Len() int
func (v *Vector[T]) PushBack(x T)
func (v *Vector[T]) Remove(i int)
Remove removes 1 element in the vector.
Complexity: O(len(s) - i).
func (v *Vector[T]) RemoveLength(i int, len int)
Remove removes the elements in the range[i, i+len) from the vector.
func (v *Vector[T]) RemoveRange(i, j int)
Remove removes the elements in the range[i, j) from the vector.
func (v *Vector[T]) Reserve(l int)
Reserve increases the capacity of the vector (the total number of elements that the vector can hold without requiring reallocation)to a value that's greater or equal to l. If l is greater than the current Cap(), new storage is allocated, otherwise the function does nothing.
Reserve() does not change the size of the vector.
func (v *Vector[T]) Set(i int, x T)
func (v *Vector[T]) Shrink()
Shrink removes unused capacity from the vector.