Skip to content

Latest commit

 

History

History
864 lines (596 loc) · 15.4 KB

README.md

File metadata and controls

864 lines (596 loc) · 15.4 KB

contalgo

Generic Container and Algorithm Library for Go.

点击这里查看中文文档

Build Status codebeat badge Coverage Status

This library depends on go generics, which is introduced in 1.18+.

Usage

func AllOf

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

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

func Average[T Numeric](a []T) T

Average returns the average value of a.

func Compare

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

func Copy[T any](a []T) []T

Copy make a copy of slice a.

Complexity: O(len(a)).

func Count

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

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

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

func Equals[T comparable](a, b T) bool

Equals wraps the '==' operator for comparable types.

func Find

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

func Generate[T any](a []T, gen func() T)

Generate fill each element of a`` with gen()`.

Complexity: O(len(a)).

func Index

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

func IsSorted[T Ordered](a []T) bool

IsSorted returns whether the slice a is sorted in ascending order.

Complexity: O(len(a)).

func Less

func Less[T Ordered](a, b T) bool

Less wraps the '<' operator for ordered types.

func Max

func Max[T Ordered](a, b T) T

Max return the larger value between a and b.

Complexity: O(1).

func MaxN

func MaxN[T Ordered](a ...T) T

MaxN return the maximum value in the sequence a.

Complexity: O(len(a)).

func Min

func Min[T Ordered](a, b T) T

Min return the smaller value between a and b.

Complexity: O(1).

func MinMax

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

func MinMaxN[T Ordered](a ...T) (min, max T)

MinMaxN returns both min and max in slice a.

Complexity: O(len(a))

func MinN

func MinN[T Ordered](a ...T) T

MinN return the minimum value in the sequence a.

Complexity: O(len(a)).

func NoneOf

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

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

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

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

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

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

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

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

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

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

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

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

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

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

type DList[T any] struct {
}

DList is a doubly linked list.

func NewDList

func NewDList[T any]() *DList[T]

NewDList make a new DList object

func NewDListOf

func NewDListOf[T any](vs ...T) *DList[T]

NewDListOf make a new DList from a serial of values

func (*DList[T]) Clean

func (l *DList[T]) Clean()

Clean cleanup the list

func (*DList[T]) ForEach

func (l *DList[T]) ForEach(cb func(val T))

ForEach iterate the list, apply each element to the cb callback function

func (*DList[T]) ForEachIf

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 (*DList[T]) IsEmpty

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

IsEmpty return whether the list is empty

func (*DList[T]) Len

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

Len return the length of the list

func (*DList[T]) PopBack

func (l *DList[T]) PopBack() (T, bool)

func (*DList[T]) PopFront

func (l *DList[T]) PopFront() (T, bool)

func (*DList[T]) PushBack

func (l *DList[T]) PushBack(val T)

func (*DList[T]) PushFront

func (l *DList[T]) PushFront(val T)

func (*DList[T]) String

func (l *DList[T]) String() string

String convert the list to string

type Float

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

type HashFn[T any] func(t T) uint64

HashFn is a function that returns the hash of 't'.

type Integer

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

type LessFn[T any] func(a, b T) bool

LessFn is a function that returns whether 'a' is less than 'b'.

type Numeric

type Numeric interface {
	Integer | Float
}

Numeric is a constraint that permits any numeric type.

type Ordered

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

type Queue[T any] struct {
}

Queue is a FIFO container

func NewQueue

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

NewQueue create a new Queue object.

func (*Queue[T]) Clean

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

func (*Queue[T]) IsEmpty

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

func (*Queue[T]) Len

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

func (*Queue[T]) PopBack

func (q *Queue[T]) PopBack() (T, bool)

func (*Queue[T]) PopFront

func (q *Queue[T]) PopFront() (T, bool)

func (*Queue[T]) PushBack

func (q *Queue[T]) PushBack(val T)

func (*Queue[T]) PushFront

func (q *Queue[T]) PushFront(val T)

func (*Queue[T]) String

func (q *Queue[T]) String() string

type Set

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

func MakeSetOf[K comparable](ks ...K) Set[K]

MakeSetOf creates a new Set object with the initial content from ks.

func (*Set[K]) Add

func (s *Set[K]) Add(k K)

func (*Set[K]) AddN

func (s *Set[K]) AddN(ks ...K)

func (*Set[K]) Clean

func (s *Set[K]) Clean()

func (*Set[K]) Del

func (s *Set[K]) Del(k K)

func (*Set[K]) DelN

func (s *Set[K]) DelN(ks ...K)

func (*Set[K]) ForEach

func (s *Set[K]) ForEach(cb func(k K))

func (*Set[K]) ForEachIf

func (s *Set[K]) ForEachIf(cb func(k K) bool)

func (*Set[K]) Has

func (s *Set[K]) Has(k K) bool

func (*Set[K]) IsEmpty

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

func (*Set[K]) Keys

func (s *Set[K]) Keys() []K

func (*Set[K]) Len

func (s *Set[K]) Len() int

func (Set[K]) String

func (s Set[K]) String() string

type Signed

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

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

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

NewStack creates a new Stack object.

func NewStackCap

func NewStackCap[T any](capicity int) *Stack[T]

NewStackCap creates a new Stack object with the specified capicity.

func (*Stack[T]) Cap

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

func (*Stack[T]) Clean

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

func (*Stack[T]) IsEmpty

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

func (*Stack[T]) Len

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

func (*Stack[T]) MustPop

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

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (val T, ok bool)

func (*Stack[T]) Push

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

type Unsigned

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

type Vector[T any] []T

Vector is a sequence container representing array that can change in size.

func MakeVector

func MakeVector[T any]() Vector[T]

MakeVector creates an empty Vector object.

func MakeVectorCap

func MakeVectorCap[T any](c int) Vector[T]

MakeVectorCap creates an empty Vector object with specified capacity.

func MakeVectorOf

func MakeVectorOf[T any](v ...T) Vector[T]

MakeVectorOf creates an Vector object with initial values.

func (*Vector[T]) Append

func (v *Vector[T]) Append(x ...T)

Append appends the values x... to the tail of the vector.

func (*Vector[T]) At

func (v *Vector[T]) At(i int) T

func (*Vector[T]) Cap

func (v *Vector[T]) Cap() int

func (*Vector[T]) Clean

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 (*Vector[T]) Insert

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 (*Vector[T]) IsEmpty

func (v *Vector[T]) IsEmpty() bool

func (*Vector[T]) Len

func (v *Vector[T]) Len() int

func (*Vector[T]) PushBack

func (v *Vector[T]) PushBack(x T)

func (*Vector[T]) Remove

func (v *Vector[T]) Remove(i int)

Remove removes 1 element in the vector.

Complexity: O(len(s) - i).

func (*Vector[T]) RemoveLength

func (v *Vector[T]) RemoveLength(i int, len int)

Remove removes the elements in the range[i, i+len) from the vector.

func (*Vector[T]) RemoveRange

func (v *Vector[T]) RemoveRange(i, j int)

Remove removes the elements in the range[i, j) from the vector.

func (*Vector[T]) Reserve

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 (*Vector[T]) Set

func (v *Vector[T]) Set(i int, x T)

func (*Vector[T]) Shrink

func (v *Vector[T]) Shrink()

Shrink removes unused capacity from the vector.

Reference