// license that can be found in the LICENSE file.
// This package provides heap operations for any type that implements
-// HeapInterface.
+// heap.Interface.
//
package heap
import "sort"
-// Any type that implements HeapInterface may be used as a
+// Any type that implements heap.Interface may be used as a
// heap with the following invariants (established after Init
// has been called):
//
// h.Less(i, j) for 0 <= i < h.Len() and j = 2*i+1 or 2*i+2 and j < h.Len()
//
-type HeapInterface interface {
- sort.SortInterface;
+type Interface interface {
+ sort.Interface;
Push(x interface{});
Pop() interface{};
}
// and may be called whenever the heap invariants may have been invalidated.
// Its complexity is O(n*log(n)) where n = h.Len().
//
-func Init(h HeapInterface) {
+func Init(h Interface) {
sort.Sort(h);
}
// Push pushes the element x onto the heap. The complexity is
// O(log(n)) where n = h.Len().
//
-func Push(h HeapInterface, x interface{}) {
+func Push(h Interface, x interface{}) {
h.Push(x);
up(h, h.Len() - 1);
}
// Pop removes the minimum element (according to Less) from the heap
// and returns it. The complexity is O(log(n)) where n = h.Len().
//
-func Pop(h HeapInterface) interface{} {
+func Pop(h Interface) interface{} {
n := h.Len() - 1;
h.Swap(0, n);
down(h, 0, n);
// Remove removes the element at index i from the heap.
// The complexity is O(log(n)) where n = h.Len().
//
-func Remove(h HeapInterface, i int) interface{} {
+func Remove(h Interface, i int) interface{} {
n := h.Len() - 1;
if n != i {
h.Swap(n, i);
}
-func up(h HeapInterface, j int) {
+func up(h Interface, j int) {
for {
i := (j-1)/2;
if i == j || h.Less(i, j) {
}
-func down(h HeapInterface, i, n int) {
+func down(h Interface, i, n int) {
for {
j := 2*i + 1;
if j >= n {
// and user-defined collections.
package sort
-// SortInterface is the interface that a type, typically a collection,
-// must implement for its contents to be sorted in increasing order.
-// Its methods require that the elements of the collection be enumerated
-// by an integer index.
-type SortInterface interface {
+// A type, typically a collection, that satisfies sort.Interface can be
+// sorted by the routines in this package. The methods require that the
+// elements of the collection be enumerated by an integer index.
+type Interface interface {
// Len is the number of elements in the collection.
Len() int;
// Less returns whether the element with index i is should sort
}
// Insertion sort
-func insertionSort(data SortInterface, a, b int) {
+func insertionSort(data Interface, a, b int) {
for i := a+1; i < b; i++ {
for j := i; j > a && data.Less(j, j-1); j-- {
data.Swap(j, j-1);
// ``Engineering a Sort Function,'' SP&E November 1993.
// Move the median of the three values data[a], data[b], data[c] into data[a].
-func medianOfThree(data SortInterface, a, b, c int) {
+func medianOfThree(data Interface, a, b, c int) {
m0 := b;
m1 := a;
m2 := c;
// now data[m0] <= data[m1] <= data[m2]
}
-func swapRange(data SortInterface, a, b, n int) {
+func swapRange(data Interface, a, b, n int) {
for i := 0; i < n; i++ {
data.Swap(a+i, b+i);
}
}
-func doPivot(data SortInterface, lo, hi int) (midlo, midhi int) {
+func doPivot(data Interface, lo, hi int) (midlo, midhi int) {
m := (lo+hi)/2;
if hi-lo > 40 {
// Tukey's ``Ninther,'' median of three medians of three.
return lo+b-a, hi-(d-c);
}
-func quickSort(data SortInterface, a, b int) {
+func quickSort(data Interface, a, b int) {
if b-a > 7 {
mlo, mhi := doPivot(data, a, b);
quickSort(data, a, mlo);
}
}
-func Sort(data SortInterface) {
+func Sort(data Interface) {
quickSort(data, 0, data.Len());
}
-func IsSorted(data SortInterface) bool {
+func IsSorted(data Interface) bool {
n := data.Len();
for i := n-1; i > 0; i-- {
if data.Less(i, i-1) {
// Convenience types for common cases
-// IntArray attaches the methods of SortInterface to []int, sorting in increasing order.
+// IntArray attaches the methods of Interface to []int, sorting in increasing order.
type IntArray []int
func (p IntArray) Len() int {
}
-// FloatArray attaches the methods of SortInterface to []float, sorting in increasing order.
+// FloatArray attaches the methods of Interface to []float, sorting in increasing order.
type FloatArray []float
func (p FloatArray) Len() int {
}
-// StringArray attaches the methods of SortInterface to []string, sorting in increasing order.
+// StringArray attaches the methods of Interface to []string, sorting in increasing order.
type StringArray []string
func (p StringArray) Len() int {