sortp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2020 License: BSD-3-Clause Imports: 1 Imported by: 11

README

sort GoSearch

Plus to the standard sort package.

Godoc

// InterfaceStruct is a struct implementing sort.Interface given closures
type InterfaceStruct struct {...}

// SortF calls sort.Sort by closures. Since Interface.Len always returns a constant,
// it is an int parameter rather than a closure here.
func SortF(Len int, Less func(i, j int) bool, Swap func(i, j int)) {...}

// The bubble sort algorithm. It is especially useful for almost sorted list.
// Bubble sort is a stable sort algorithm.
func Bubble(data sort.Interface) {...}

LICENSE

BSD license

Documentation

Overview

Package sortp is a plus to the standard "sort" package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bubble

func Bubble(data sort.Interface)

The bubble sort algorithm. It is especially useful for almost sorted list. Bubble sort is a stable sort algorithm.

func BubbleF

func BubbleF(Len int, Less func(i, j int) bool, Swap func(i, j int))

Similar to Bubble but using closures other than sort.Interface.

Example
data := []int{5, 3, 1, 8, 0}

BubbleF(len(data), func(i, j int) bool {
	return data[i] < data[j]
}, func(i, j int) {
	data[i], data[j] = data[j], data[i]
})

fmt.Println(data)
Output:

[0 1 3 5 8]

func DiffSortedList

func DiffSortedList(LeftLen, RightLen int, Compare func(l, r int) int, OutputLeft, OutputRight func(index int))

DiffSortedList compares the difference of two sorted list. LeftLen and RightLen are lengths of the left/right lists, respectively. Compare returns -1/1/0 if l-th element of left list is less/greater/equal to the r-th element of the right list, both are zero-based. OutputLeft is called with the index of the element existing in the left list but not in the right list. OutputLeft will be called with ascending indexes. OutputRight is similar. Please make no assumption of the calling order of OutputLeft/OutputRight.

Example
from := []string{"a", "b", "d", "f"}
to := []string{"b", "c", "d", "g", "h"}

var extra, missing []string
DiffSortedList(len(from), len(to), func(f, t int) int {
	if from[f] < to[t] {
		return -1
	}
	if from[f] > to[t] {
		return 1
	}
	return 0
}, func(f int) {
	extra = append(extra, from[f])
}, func(t int) {
	missing = append(missing, to[t])
})
fmt.Println("extra:", extra)
fmt.Println("missing:", missing)

from, to = to, from
extra, missing = nil, nil
DiffSortedList(len(from), len(to), func(f, t int) int {
	if from[f] < to[t] {
		return -1
	}
	if from[f] > to[t] {
		return 1
	}
	return 0
}, func(f int) {
	extra = append(extra, from[f])
}, func(t int) {
	missing = append(missing, to[t])
})
fmt.Println("extra:", extra)
fmt.Println("missing:", missing)

to = from
extra, missing = nil, nil
DiffSortedList(len(from), len(to), func(f, t int) int {
	if from[f] < to[t] {
		return -1
	}
	if from[f] > to[t] {
		return 1
	}
	return 0
}, func(f int) {
	extra = append(extra, from[f])
}, func(t int) {
	missing = append(missing, to[t])
})
fmt.Println("extra:", extra)
fmt.Println("missing:", missing)
Output:

extra: [a f]
missing: [c g h]
extra: [c g h]
missing: [a f]
extra: []
missing: []

func IndexSort

func IndexSort(data sort.Interface) []int

IndexSort returns a slice of indexes l, so that data[l[i]], i = 0...N-1, are sorted.

func IndexSortF

func IndexSortF(Len int, Less func(i, j int) bool) []int

IndexSortF is similar to IndexSort but with funcs as the input.

func IndexStable

func IndexStable(data sort.Interface) []int

IndexSort returns a slice of indexes l, so that data[l[i]], i = 0...N-1, are sorted.

func IndexStableF

func IndexStableF(Len int, Less func(i, j int) bool) []int

IndexStableF is similar to IndexStable but with funcs as the input.

func IsSortedF

func IsSortedF(Len int, Less func(i, j int) bool) bool

IsSortedF is similar to sort.IsSorted but with closure as arguments.

func Merge

func Merge(LeftLen, RightLen int, Less func(l, r int) bool, AppendLeft func(l int), AppendRight func(r int))

Merge merges two sorted list.

@param LeftLen is the length of the left list. @param RightLen is the length of the right list. @param Less compares the l-th element of left list and the r-th element of the right list. @param AppendLeft appends the l-th element of the left list to the result list. @param AppendRight appends the r-th element of the right list to the result list.

Example
left := []int{1, 3, 5, 7}
right := []int{4, 6, 8, 10, 11}

var merged []int
Merge(len(left), len(right), func(l, r int) bool {
	return left[l] < right[r]
}, func(l int) {
	merged = append(merged, left[l])
}, func(r int) {
	merged = append(merged, right[r])
})
fmt.Println(merged)

left, right = right, left
merged = nil
Merge(len(left), len(right), func(l, r int) bool {
	return left[l] < right[r]
}, func(l int) {
	merged = append(merged, left[l])
}, func(r int) {
	merged = append(merged, right[r])
})
fmt.Println(merged)
Output:

[1 3 4 5 6 7 8 10 11]
[1 3 4 5 6 7 8 10 11]

func ReverseLess

func ReverseLess(Less func(i, j int) bool) func(i, j int) bool

ReverseLess returns a func which can be used to sort data in a reverse order.

func SortF

func SortF(Len int, Less func(i, j int) bool, Swap func(i, j int))

SortF calls sort.Sort by closures. Since Interface.Len always returns a constant, it is an int parameter rather than a closure here.

func StableF

func StableF(Len int, Less func(i, j int) bool, Swap func(i, j int))

StableF calls sort.Stable by closures. Since Interface.Len always returns a constant, it is an int parameter rather than a closure here.

Types

type InterfaceStruct

type InterfaceStruct struct {
	// Len is the number of elements in the collection.
	LenF func() int
	// Less reports whether the element with
	// index i should sort before the element with index j.
	LessF func(i, j int) bool
	// Swap swaps the elements with indexes i and j.
	SwapF func(i, j int)
}

InterfaceStruct is a struct implementing sort.Interface given closures

Example
data := []int{5, 3, 1, 8, 0}

sort.Sort(InterfaceStruct{
	LenF: func() int {
		return len(data)
	}, LessF: func(i, j int) bool {
		return data[i] < data[j]
	}, SwapF: func(i, j int) {
		data[i], data[j] = data[j], data[i]
	},
})

fmt.Println(data)
Output:

[0 1 3 5 8]

func (InterfaceStruct) Len

func (is InterfaceStruct) Len() int

sort.Interface.Len

func (InterfaceStruct) Less

func (is InterfaceStruct) Less(i, j int) bool

sort.Interface.Less

func (InterfaceStruct) Swap

func (is InterfaceStruct) Swap(i, j int)

sort.Interface.Swap

Directories

Path Synopsis
Package less can generates some useful less functions.
Package less can generates some useful less functions.

Jump to

Keyboard shortcuts

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