sortutil: modernc.org/sortutil Index | Examples | Files

package sortutil

import "modernc.org/sortutil"

Package sortutil provides utilities supplementing the standard 'sort' package.

Changelog

2019-10-24: v1.1.0: Added topological sort.

2015-06-17: Added utils for math/big.{Int,Rat}.

Index

Examples

Package Files

sortutil.go topsort.go

func Dedupe Uses

func Dedupe(data sort.Interface) (n int)

Dedupe returns n, the number of distinct elements in data. The resulting elements are sorted in elements [0, n) or data[:n] for a slice.

Code:

a := []int{4, 1, 2, 1, 3, 4, 2}
fmt.Println(a[:Dedupe(sort.IntSlice(a))])

b := []string{"foo", "bar", "baz", "bar", "foo", "qux", "qux"}
fmt.Println(b[:Dedupe(sort.StringSlice(b))])

Output:

[1 2 3 4]
[bar baz foo qux]

func SearchBigInts Uses

func SearchBigInts(a []*big.Int, x *big.Int) int

SearchBigInts searches for x in a sorted slice of *big.Int and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchBigRats Uses

func SearchBigRats(a []*big.Rat, x *big.Rat) int

SearchBigRats searches for x in a sorted slice of *big.Int and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchBytes Uses

func SearchBytes(a []byte, x byte) int

SearchBytes searches for x in a sorted slice of bytes and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchFloat32s Uses

func SearchFloat32s(a []float32, x float32) int

SearchFloat32s searches for x in a sorted slice of float32 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchInt16s Uses

func SearchInt16s(a []int16, x int16) int

SearchInt16s searches for x in a sorted slice of int16 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchInt32s Uses

func SearchInt32s(a []int32, x int32) int

SearchInt32s searches for x in a sorted slice of int32 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchInt64s Uses

func SearchInt64s(a []int64, x int64) int

SearchInt64s searches for x in a sorted slice of int64 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchInt8s Uses

func SearchInt8s(a []int8, x int8) int

SearchInt8s searches for x in a sorted slice of int8 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchRunes Uses

func SearchRunes(a []rune, x rune) int

SearchRunes searches for x in a sorted slice of uint64 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchUint16s Uses

func SearchUint16s(a []uint16, x uint16) int

SearchUint16s searches for x in a sorted slice of uint16 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchUint32s Uses

func SearchUint32s(a []uint32, x uint32) int

SearchUint32s searches for x in a sorted slice of uint32 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchUint64s Uses

func SearchUint64s(a []uint64, x uint64) int

SearchUint64s searches for x in a sorted slice of uint64 and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

func SearchUints Uses

func SearchUints(a []uint, x uint) int

SearchUints searches for x in a sorted slice of uints and returns the index as specified by sort.Search. The slice must be sorted in ascending order.

type BigIntSlice Uses

type BigIntSlice []*big.Int

BigIntSlice attaches the methods of sort.Interface to []*big.Int, sorting in increasing order.

func (BigIntSlice) Len Uses

func (s BigIntSlice) Len() int

func (BigIntSlice) Less Uses

func (s BigIntSlice) Less(i, j int) bool

func (BigIntSlice) Sort Uses

func (s BigIntSlice) Sort()

Sort is a convenience method.

func (BigIntSlice) Swap Uses

func (s BigIntSlice) Swap(i, j int)

type BigRatSlice Uses

type BigRatSlice []*big.Rat

BigRatSlice attaches the methods of sort.Interface to []*big.Rat, sorting in increasing order.

func (BigRatSlice) Len Uses

func (s BigRatSlice) Len() int

func (BigRatSlice) Less Uses

func (s BigRatSlice) Less(i, j int) bool

func (BigRatSlice) Sort Uses

func (s BigRatSlice) Sort()

Sort is a convenience method.

func (BigRatSlice) Swap Uses

func (s BigRatSlice) Swap(i, j int)

type ByteSlice Uses

type ByteSlice []byte

ByteSlice attaches the methods of sort.Interface to []byte, sorting in increasing order.

func (ByteSlice) Len Uses

func (s ByteSlice) Len() int

func (ByteSlice) Less Uses

func (s ByteSlice) Less(i, j int) bool

func (ByteSlice) Sort Uses

func (s ByteSlice) Sort()

Sort is a convenience method.

func (ByteSlice) Swap Uses

func (s ByteSlice) Swap(i, j int)

type Float32Slice Uses

type Float32Slice []float32

Float32Slice attaches the methods of sort.Interface to []float32, sorting in increasing order.

func (Float32Slice) Len Uses

func (s Float32Slice) Len() int

func (Float32Slice) Less Uses

func (s Float32Slice) Less(i, j int) bool

func (Float32Slice) Sort Uses

func (s Float32Slice) Sort()

Sort is a convenience method.

func (Float32Slice) Swap Uses

func (s Float32Slice) Swap(i, j int)

type Int16Slice Uses

type Int16Slice []int16

Int16Slice attaches the methods of sort.Interface to []int16, sorting in increasing order.

func (Int16Slice) Len Uses

func (s Int16Slice) Len() int

func (Int16Slice) Less Uses

func (s Int16Slice) Less(i, j int) bool

func (Int16Slice) Sort Uses

func (s Int16Slice) Sort()

Sort is a convenience method.

func (Int16Slice) Swap Uses

func (s Int16Slice) Swap(i, j int)

type Int32Slice Uses

type Int32Slice []int32

Int32Slice attaches the methods of sort.Interface to []int32, sorting in increasing order.

func (Int32Slice) Len Uses

func (s Int32Slice) Len() int

func (Int32Slice) Less Uses

func (s Int32Slice) Less(i, j int) bool

func (Int32Slice) Sort Uses

func (s Int32Slice) Sort()

Sort is a convenience method.

func (Int32Slice) Swap Uses

func (s Int32Slice) Swap(i, j int)

type Int64Slice Uses

type Int64Slice []int64

Int64Slice attaches the methods of sort.Interface to []int64, sorting in increasing order.

func (Int64Slice) Len Uses

func (s Int64Slice) Len() int

func (Int64Slice) Less Uses

func (s Int64Slice) Less(i, j int) bool

func (Int64Slice) Sort Uses

func (s Int64Slice) Sort()

Sort is a convenience method.

func (Int64Slice) Swap Uses

func (s Int64Slice) Swap(i, j int)

type Int8Slice Uses

type Int8Slice []int8

Int8Slice attaches the methods of sort.Interface to []int8, sorting in increasing order.

func (Int8Slice) Len Uses

func (s Int8Slice) Len() int

func (Int8Slice) Less Uses

func (s Int8Slice) Less(i, j int) bool

func (Int8Slice) Sort Uses

func (s Int8Slice) Sort()

Sort is a convenience method.

func (Int8Slice) Swap Uses

func (s Int8Slice) Swap(i, j int)

type RuneSlice Uses

type RuneSlice []rune

RuneSlice attaches the methods of sort.Interface to []rune, sorting in increasing order.

func (RuneSlice) Len Uses

func (s RuneSlice) Len() int

func (RuneSlice) Less Uses

func (s RuneSlice) Less(i, j int) bool

func (RuneSlice) Sort Uses

func (s RuneSlice) Sort()

Sort is a convenience method.

func (RuneSlice) Swap Uses

func (s RuneSlice) Swap(i, j int)

type TopologicalSortNode Uses

type TopologicalSortNode interface {
    // Edges return the list of nodes this node points to.
    Edges() []TopologicalSortNode
}

TopologicalSortNode represents a node of a graph for TopologicalSort. Implementations of TopologicalSortNode must be comparable.

func TopologicalSort Uses

func TopologicalSort(graph []TopologicalSortNode) []TopologicalSortNode

TopologicalSort returns a reversed topological ordering of a directed acyclic graph or nil if graph is not a DAG.

It implements the Depth-first search algorithm:

https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search

type Uint16Slice Uses

type Uint16Slice []uint16

Uint16Slice attaches the methods of sort.Interface to []uint16, sorting in increasing order.

func (Uint16Slice) Len Uses

func (s Uint16Slice) Len() int

func (Uint16Slice) Less Uses

func (s Uint16Slice) Less(i, j int) bool

func (Uint16Slice) Sort Uses

func (s Uint16Slice) Sort()

Sort is a convenience method.

func (Uint16Slice) Swap Uses

func (s Uint16Slice) Swap(i, j int)

type Uint32Slice Uses

type Uint32Slice []uint32

Uint32Slice attaches the methods of sort.Interface to []uint32, sorting in increasing order.

func (Uint32Slice) Len Uses

func (s Uint32Slice) Len() int

func (Uint32Slice) Less Uses

func (s Uint32Slice) Less(i, j int) bool

func (Uint32Slice) Sort Uses

func (s Uint32Slice) Sort()

Sort is a convenience method.

func (Uint32Slice) Swap Uses

func (s Uint32Slice) Swap(i, j int)

type Uint64Slice Uses

type Uint64Slice []uint64

Uint64Slice attaches the methods of sort.Interface to []uint64, sorting in increasing order.

func (Uint64Slice) Len Uses

func (s Uint64Slice) Len() int

func (Uint64Slice) Less Uses

func (s Uint64Slice) Less(i, j int) bool

func (Uint64Slice) Sort Uses

func (s Uint64Slice) Sort()

Sort is a convenience method.

func (Uint64Slice) Swap Uses

func (s Uint64Slice) Swap(i, j int)

type UintSlice Uses

type UintSlice []uint

UintSlice attaches the methods of sort.Interface to []uint, sorting in increasing order.

func (UintSlice) Len Uses

func (s UintSlice) Len() int

func (UintSlice) Less Uses

func (s UintSlice) Less(i, j int) bool

func (UintSlice) Sort Uses

func (s UintSlice) Sort()

Sort is a convenience method.

func (UintSlice) Swap Uses

func (s UintSlice) Swap(i, j int)

Package sortutil imports 2 packages (graph) and is imported by 8 packages. Updated 2019-10-24. Refresh now. Tools for package owners.