Commit 8acb8fb7 authored by Rob Pike's avatar Rob Pike

reduce stutter: sort.SortInterface -> sort.Interface.

ditto for heap.HeapInterface

R=gri,rsc
DELTA=31  (0 added, 1 deleted, 30 changed)
OCL=35665
CL=35673
parent fd4767f2
...@@ -3,20 +3,20 @@ ...@@ -3,20 +3,20 @@
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// This package provides heap operations for any type that implements // This package provides heap operations for any type that implements
// HeapInterface. // heap.Interface.
// //
package heap package heap
import "sort" 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 // heap with the following invariants (established after Init
// has been called): // 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() // 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 { type Interface interface {
sort.SortInterface; sort.Interface;
Push(x interface{}); Push(x interface{});
Pop() interface{}; Pop() interface{};
} }
...@@ -27,7 +27,7 @@ type HeapInterface interface { ...@@ -27,7 +27,7 @@ type HeapInterface interface {
// and may be called whenever the heap invariants may have been invalidated. // and may be called whenever the heap invariants may have been invalidated.
// Its complexity is O(n*log(n)) where n = h.Len(). // Its complexity is O(n*log(n)) where n = h.Len().
// //
func Init(h HeapInterface) { func Init(h Interface) {
sort.Sort(h); sort.Sort(h);
} }
...@@ -35,7 +35,7 @@ func Init(h HeapInterface) { ...@@ -35,7 +35,7 @@ func Init(h HeapInterface) {
// Push pushes the element x onto the heap. The complexity is // Push pushes the element x onto the heap. The complexity is
// O(log(n)) where n = h.Len(). // O(log(n)) where n = h.Len().
// //
func Push(h HeapInterface, x interface{}) { func Push(h Interface, x interface{}) {
h.Push(x); h.Push(x);
up(h, h.Len() - 1); up(h, h.Len() - 1);
} }
...@@ -44,7 +44,7 @@ func Push(h HeapInterface, x interface{}) { ...@@ -44,7 +44,7 @@ func Push(h HeapInterface, x interface{}) {
// Pop removes the minimum element (according to Less) from the heap // Pop removes the minimum element (according to Less) from the heap
// and returns it. The complexity is O(log(n)) where n = h.Len(). // 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; n := h.Len() - 1;
h.Swap(0, n); h.Swap(0, n);
down(h, 0, n); down(h, 0, n);
...@@ -55,7 +55,7 @@ func Pop(h HeapInterface) interface{} { ...@@ -55,7 +55,7 @@ func Pop(h HeapInterface) interface{} {
// Remove removes the element at index i from the heap. // Remove removes the element at index i from the heap.
// The complexity is O(log(n)) where n = h.Len(). // 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; n := h.Len() - 1;
if n != i { if n != i {
h.Swap(n, i); h.Swap(n, i);
...@@ -66,7 +66,7 @@ func Remove(h HeapInterface, i int) interface{} { ...@@ -66,7 +66,7 @@ func Remove(h HeapInterface, i int) interface{} {
} }
func up(h HeapInterface, j int) { func up(h Interface, j int) {
for { for {
i := (j-1)/2; i := (j-1)/2;
if i == j || h.Less(i, j) { if i == j || h.Less(i, j) {
...@@ -78,7 +78,7 @@ func up(h HeapInterface, j int) { ...@@ -78,7 +78,7 @@ func up(h HeapInterface, j int) {
} }
func down(h HeapInterface, i, n int) { func down(h Interface, i, n int) {
for { for {
j := 2*i + 1; j := 2*i + 1;
if j >= n { if j >= n {
......
...@@ -93,7 +93,7 @@ func (p *IntVector) AppendVector(x *IntVector) { ...@@ -93,7 +93,7 @@ func (p *IntVector) AppendVector(x *IntVector) {
} }
// SortInterface support // sort.Interface support
// Less returns a boolean denoting whether the i'th element is less than the j'th element. // Less returns a boolean denoting whether the i'th element is less than the j'th element.
func (p *IntVector) Less(i, j int) bool { func (p *IntVector) Less(i, j int) bool {
return p.At(i) < p.At(j); return p.At(i) < p.At(j);
......
...@@ -92,7 +92,7 @@ func (p *StringVector) AppendVector(x *StringVector) { ...@@ -92,7 +92,7 @@ func (p *StringVector) AppendVector(x *StringVector) {
} }
// SortInterface support // sort.Interface support
// Less returns a boolean denoting whether the i'th element is less than the j'th element. // Less returns a boolean denoting whether the i'th element is less than the j'th element.
func (p *StringVector) Less(i, j int) bool { func (p *StringVector) Less(i, j int) bool {
return p.At(i) < p.At(j); return p.At(i) < p.At(j);
......
...@@ -207,9 +207,9 @@ func (p *Vector) AppendVector(x *Vector) { ...@@ -207,9 +207,9 @@ func (p *Vector) AppendVector(x *Vector) {
} }
// Partial SortInterface support // Partial sort.Interface support
// LessInterface provides partial support of the SortInterface. // LessInterface provides partial support of the sort.Interface.
type LessInterface interface { type LessInterface interface {
Less(y Element) bool; Less(y Element) bool;
} }
......
...@@ -82,7 +82,7 @@ func (e *Error) String() string { ...@@ -82,7 +82,7 @@ func (e *Error) String() string {
type ErrorList []*Error type ErrorList []*Error
// ErrorList implements the SortInterface. // ErrorList implements the sort Interface.
func (p ErrorList) Len() int { func (p ErrorList) Len() int {
return len(p); return len(p);
} }
......
...@@ -6,11 +6,10 @@ ...@@ -6,11 +6,10 @@
// and user-defined collections. // and user-defined collections.
package sort package sort
// SortInterface is the interface that a type, typically a collection, // A type, typically a collection, that satisfies sort.Interface can be
// must implement for its contents to be sorted in increasing order. // sorted by the routines in this package. The methods require that the
// Its methods require that the elements of the collection be enumerated // elements of the collection be enumerated by an integer index.
// by an integer index. type Interface interface {
type SortInterface interface {
// Len is the number of elements in the collection. // Len is the number of elements in the collection.
Len() int; Len() int;
// Less returns whether the element with index i is should sort // Less returns whether the element with index i is should sort
...@@ -29,7 +28,7 @@ func min(a, b int) int { ...@@ -29,7 +28,7 @@ func min(a, b int) int {
} }
// Insertion 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 i := a+1; i < b; i++ {
for j := i; j > a && data.Less(j, j-1); j-- { for j := i; j > a && data.Less(j, j-1); j-- {
data.Swap(j, j-1); data.Swap(j, j-1);
...@@ -41,7 +40,7 @@ func insertionSort(data SortInterface, a, b int) { ...@@ -41,7 +40,7 @@ func insertionSort(data SortInterface, a, b int) {
// ``Engineering a Sort Function,'' SP&E November 1993. // ``Engineering a Sort Function,'' SP&E November 1993.
// Move the median of the three values data[a], data[b], data[c] into data[a]. // 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; m0 := b;
m1 := a; m1 := a;
m2 := c; m2 := c;
...@@ -58,13 +57,13 @@ func medianOfThree(data SortInterface, a, b, c int) { ...@@ -58,13 +57,13 @@ func medianOfThree(data SortInterface, a, b, c int) {
// now data[m0] <= data[m1] <= data[m2] // 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++ { for i := 0; i < n; i++ {
data.Swap(a+i, b+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; m := (lo+hi)/2;
if hi-lo > 40 { if hi-lo > 40 {
// Tukey's ``Ninther,'' median of three medians of three. // Tukey's ``Ninther,'' median of three medians of three.
...@@ -123,7 +122,7 @@ func doPivot(data SortInterface, lo, hi int) (midlo, midhi int) { ...@@ -123,7 +122,7 @@ func doPivot(data SortInterface, lo, hi int) (midlo, midhi int) {
return lo+b-a, hi-(d-c); 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 { if b-a > 7 {
mlo, mhi := doPivot(data, a, b); mlo, mhi := doPivot(data, a, b);
quickSort(data, a, mlo); quickSort(data, a, mlo);
...@@ -133,12 +132,12 @@ func quickSort(data SortInterface, a, b int) { ...@@ -133,12 +132,12 @@ func quickSort(data SortInterface, a, b int) {
} }
} }
func Sort(data SortInterface) { func Sort(data Interface) {
quickSort(data, 0, data.Len()); quickSort(data, 0, data.Len());
} }
func IsSorted(data SortInterface) bool { func IsSorted(data Interface) bool {
n := data.Len(); n := data.Len();
for i := n-1; i > 0; i-- { for i := n-1; i > 0; i-- {
if data.Less(i, i-1) { if data.Less(i, i-1) {
...@@ -151,7 +150,7 @@ func IsSorted(data SortInterface) bool { ...@@ -151,7 +150,7 @@ func IsSorted(data SortInterface) bool {
// Convenience types for common cases // 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 type IntArray []int
func (p IntArray) Len() int { func (p IntArray) Len() int {
...@@ -170,7 +169,7 @@ func (p IntArray) Sort() { ...@@ -170,7 +169,7 @@ func (p IntArray) Sort() {
} }
// 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 type FloatArray []float
func (p FloatArray) Len() int { func (p FloatArray) Len() int {
...@@ -189,7 +188,7 @@ func (p FloatArray) Sort() { ...@@ -189,7 +188,7 @@ func (p FloatArray) Sort() {
} }
// 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 type StringArray []string
func (p StringArray) Len() int { func (p StringArray) Len() int {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment