cgorithm

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 2 Imported by: 0

README

cgorithm

C++ <algorithm> implementation in Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](arr []T, predicate func(int, T) bool) bool

Tests if every element of `arr` matches a predicate that accept index and value as parameters, and returns true, if element match, and false otherwise. The function returns true if every element matches, false otherwise.

func AllSatisfy

func AllSatisfy[T any,
	U any,
](arr1 []T, arr2 []U, predicate func(int, int, T, U) bool) bool

Test if all pairs of elements in two slices satisfy a predicate that accept Index-Value pair of two slices, and returns true, if element match, and false otherwise. The function returns true, if all pairs of elements matches, false otherwise.

func Any

func Any[T any](arr []T, predicate func(T) bool) bool

Tests if any element of `arr` matches a predicate that accept index and value as parameters, and returns true, if element match, and false otherwise. The function returns true if any element matches, false otherwise.

func AnySatisfy

func AnySatisfy[T any,
	U any,
](arr1 []T, arr2 []U, predicate func(int, int, T, U) bool) bool

Test if any pair of elements in two slices satisfy a predicate that accept Index-Value pair of two slices, and returns true, if element match, and false otherwise. The function returns true, if any pair of elements matches, false otherwise.

func Concatenate

func Concatenate(elements ...string) string

Concatenates all arguments into one string.

func ConcatenateSlice

func ConcatenateSlice(elements []string) string

Concatenates strings from the slice into one.

func Count

func Count[T comparable](arr []T, element T) int

Counts element in slice.

func CountIf

func CountIf[T any](arr []T, predicate func(int, T) bool) int

Counts elements in slice that match predicate. The predicate accept Index-Value pair, and returns true, if element need to be counted, false otherwise. The function returns count of counted values.

func Filter

func Filter[T any](arr []T, predicate func(int, T) bool) []T

Filters a slice from elements that not match predicate. The predicate accept Index-Value pair, and returns true, if element need to be skipped from resulting slice. The function returns filtered slice.

func Find

func Find[T comparable](arr []T, element T) int

Find element in the slice. Returns -1 if not found, index of element otherwise if found.

func FindIf

func FindIf[T any](arr []T, predicate func(int, T) bool) int

Finds element in the slice. Returns first element that matches predicate, that accept Index-Value pair, and returns true, if element was finded, false otherwise. The function returns -1 if none element match predicate.

func Foreach

func Foreach[T any](arr []T, predicate func(int, T) ForeachAction) bool

Calls predicate for each element in the slice. The predicate returns action. Returns true if loop was success (the predicates returned values ​​in the range 0-2), false otherwise.

func Generate

func Generate[T any](count int, predicate func(int) T) []T

Generates a array filled with `count` values returned by predicate, and return it.

func MAll

func MAll[TK comparable, TV any](m map[TK]TV, predicate func(TK, TV) bool) bool

Same as `All`, but only for maps, and the predicate accepts Key-Value pair.

func MAllSatisfy

func MAllSatisfy[TK comparable,
	TV any,
	UK comparable,
	UV any,
](m1 map[TK]TV, m2 map[UK]UV, predicate func(TK, UK, TV, UV) bool) bool

Same as `AllSatisfy`, but only for maps, and the predicate accepts two Key-Value pairs of both maps.

func MAny

func MAny[TK comparable, TV any](m map[TK]TV, predicate func(TK, TV) bool) bool

Same as `Any`, but only for maps, and the predicate accepts Key-Value pair.

func MAnySatisfy

func MAnySatisfy[TK comparable,
	TV any,
	UK comparable,
	UV any,
](m1 map[TK]TV, m2 map[UK]UV, predicate func(TK, UK, TV, UV) bool) bool

Same as `AnySatisfy`, but only for maps, and the predicate accepts two Key-Value pairs of both maps.

func MCount

func MCount[TK comparable, TV comparable](m map[TK]TV, element TV) int

Same as `Count`, but only for maps, and the element argument is value of pair to be counted.

func MCountIf

func MCountIf[TK comparable, TV any](m map[TK]TV, predicate func(TK, TV) bool) int

Same as `CountIf`, but only for maps, and the predicate accept Key-Value pair.

func MFilter

func MFilter[TK comparable, TV any](m map[TK]TV, predicate func(TK, TV) bool) map[TK]TV

Same as `Filter`, but only for maps, and the predicate accept Key-Value pair.

func MFindIf

func MFindIf[TK comparable, TV any](m map[TK]TV, predicate func(TK, TV) bool) []TK

Same as `FindIf`, but only for maps, and the predicate accept Key-Value pair. The function returns array of keys, values of that matches the predicate.

func MFindK

func MFindK[TK comparable, TV comparable](m map[TK]TV, element TV) []TK

Same as `Find`, but only for maps, and the predicate accept Key-Value pair.

func MFindV

func MFindV[TK comparable, TV comparable](m map[TK]TV, element TV) []TK

func MForeach

func MForeach[TK comparable, TV any](m map[TK]TV, predicate func(TK, TV) ForeachAction) bool

Same as `Foreach`, but only for maps, and the predicate accept Key-Value pair.

func MReduce

func MReduce[TK comparable, TV any, U any](m map[TK]TV, init U, predicate func(TK, U, TV) U) U

Same as `Reduce`, but only for maps.

func MTransform

func MTransform[TK comparable, TV any, UK comparable, UV any](m map[TK]TV, predicate func(TK, TV) (UK, UV)) map[UK]UV

Same as `Transform`, but only for maps, and the predicate accept Key-Value pair, and returns Key-Value pair, which will stored in map.

func MTransformReduce

func MTransformReduce[TK comparable, TV any, U any](arr map[TK]TV, init U, reduce func(TK, U, U) U, transform func(TK, TV) U) U

Same as `TransformReduce`, but only for maps.

func Max

func Max[T constraints.Ordered](x, y T) T

Finds max value of x and y values.

func Min

func Min[T constraints.Ordered](x, y T) T

Finds min value of x and y values.

func Qsort

func Qsort[T any](arr []T, predicate func(int, int, T, T) int) []T

Sorts slice. The function does same as `qsort` in C, but the predicate accepts indexes.

func Reduce

func Reduce[T any, U any](arr []T, init U, predicate func(int, U, T) U) U

Reduces the slice using the predicate. The predicate accept index, result, and element to be reduced, and returns the result. The function returns result of predicates.

func RepeatArray

func RepeatArray[T any](count int, elements []T) []T

Repeats elements in one slice N times.

func RepeatElement

func RepeatElement[T any](count int, element T) []T

Repeats a one element N times and writes in the slice.

func Sort

func Sort[T constraints.Ordered](arr []T) []T

func Sum

func Sum[T constraints.Ordered](arr []T, init T) T

Sums the slice.

func Transform

func Transform[T any, U any](arr []T, predicate func(int, T) U) []U

Apply predicate to every element in the slice. The predicate accepts Index-Value pair, and returns changed value with (un-)changed type. The function returns slice of elements with predicate applied.

func TransformReduce

func TransformReduce[T any, U any](arr []T, init U, reduce func(int, U, U) U, transform func(int, T) U) U

Same as `Reduce` and `Transform`.

func Zip

func Zip[T any, U any](arr1 []T, arr2 []U, predicate func(int, T, U) ForeachAction) bool

Same as `Foreach`, but accepts a two slices and the predicate accepts index, and elements of two slices.

Types

type ForeachAction

type ForeachAction int

ForEach loop action.

const (
	// Do nothing.
	ForeachNoOp ForeachAction = 0
	// Break from loop.
	ForeachBreak ForeachAction = 1
	// Continues loop.
	ForeachContinue ForeachAction = 2
)

Jump to

Keyboard shortcuts

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