cort

package
v0.0.0-...-f5c3557 Latest Latest
Warning

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

Go to latest
Published: May 2, 2018 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cort implements extra sort helpers.

"cort" is pronounced "sort".

Example (Fix)
package main

import (
	"fmt"

	"github.com/iheartradio/cog/cort"
)

type IntSlice []int

func (p IntSlice) Len() int           { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
func (p IntSlice) Move(i, j, a0, a1, b0, b1 int) {
	e := p[i]
	copy(p[a0:a1], p[b0:b1])
	p[j] = e
}

func main() {
	s := IntSlice{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

	const i = 4

	for i := 1; i <= 5; i++ {
		s[i] = 10 * i
		fmt.Println("i = ", i)
		fmt.Println("Before:\t", s)
		cort.Fix(i, s)
		fmt.Println("After:\t", s)
		fmt.Println()
	}

}
Output:

i =  1
Before:	 [0 10 2 3 4 5 6 7 8 9]
After:	 [0 2 3 4 5 6 7 8 9 10]

i =  2
Before:	 [0 2 20 4 5 6 7 8 9 10]
After:	 [0 2 4 5 6 7 8 9 10 20]

i =  3
Before:	 [0 2 4 30 6 7 8 9 10 20]
After:	 [0 2 4 6 7 8 9 10 20 30]

i =  4
Before:	 [0 2 4 6 40 8 9 10 20 30]
After:	 [0 2 4 6 8 9 10 20 30 40]

i =  5
Before:	 [0 2 4 6 8 50 10 20 30 40]
After:	 [0 2 4 6 8 10 20 30 40 50]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fix

func Fix(i int, s Mover)

Fix updates an already-sorted slice after an element has been updated. Rather than calling sort.Sort again (which is painfully slow for already-sorted slices), this very quickly finds a new home for the changed element.

Types

type Mover

type Mover interface {
	sort.Interface
	Move(i, j, a0, a1, b0, b1 int)
}

A Mover moves a single value around inside of itself, given a subslice of itself to shift.

Jump to

Keyboard shortcuts

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