algorithm

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2022 License: MIT Imports: 1 Imported by: 1

README

go-algorithm

Some algorithm functions found in the c++ STL developed with generics (golang 1.18).

Examples

Sorting

import (
    . "github.com/AlexandreChamard/go-builtin"
)

sliceInt := []int{42, 12, 4, -5, 0, 69, 20}
Sort(sliceInt, Less[int])
// sliceInt = []int{-5, 0, 4, 12, 20, 42, 69}

Unique

import (
    . "github.com/AlexandreChamard/go-builtin"
)

type t struct {
    i, j int
}

eqf := func(a, b t) bool { return a.j == b.j }

slice := []t{{0, 42}, {1, 12}, {2, 4}, {3, -5}, {4, 0}, {5, 69}, {6, 4}, {7, 20}}

Unique(slice) // Returns true

UniqueF(slice, eqf) // Returns false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[N Number](n N) N

func All

func All[T any](slice []T, pred Eqf[T]) bool

Returns true if ALL pred(element) = true

func Any

func Any[T any](slice []T, pred Eqf[T]) bool

Returns true if AT LEASE ONE pred(element) = true

func Max

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

Returns the biggest element

func MaxN

func MaxN[T Ord](xs ...T) T

Returns the biggest element

func Min

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

Returns the lowest element

func MinN

func MinN[T Ord](xs ...T) T

Returns the lowest element

func Partition

func Partition[T any](slice []T, comp Ordf[T], low, high int) int

func QuickSort

func QuickSort[T any](slice []T, comp Ordf[T])

Generic QuickSort with a predicate

func SliceEqual

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

Returns true if all the elements are equals

func SliceEqualF

func SliceEqualF[T any](a, b []T, comp Compf[T]) bool

func Sort

func Sort[T any](slice []T, comp Ordf[T])

Generic sort with a predicate

func Unique

func Unique[T comparable](slice []T) bool

Returns true if all elements are unique

func UniqueF

func UniqueF[T any](slice []T, pred Compf[T]) bool

Same as Unique with a predicate

Types

This section is empty.

Jump to

Keyboard shortcuts

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