1
0
mirror of https://github.com/golang/go synced 2024-11-23 17:10:03 -07:00

sort: change IntArray etc. to IntSlice for better name hygiene.

R=golang-dev, gri
CC=golang-dev
https://golang.org/cl/4602054
This commit is contained in:
Rob Pike 2011-06-11 09:25:18 +10:00
parent 18333f2de5
commit 4b1170d2b1
12 changed files with 62 additions and 62 deletions

View File

@ -934,12 +934,12 @@ We can apply <code>Sort</code> to any type that implements <code>Len</code>, <co
The <code>sort</code> package includes the necessary methods to allow sorting of
arrays of integers, strings, etc.; here's the code for arrays of <code>int</code>
<p>
<pre> <!-- progs/sort.go /type.*IntArray/ /Swap/ -->
33 type IntArray []int
<pre> <!-- progs/sort.go /type.*IntSlice/ /Swap/ -->
33 type IntSlice []int
35 func (p IntArray) Len() int { return len(p) }
36 func (p IntArray) Less(i, j int) bool { return p[i] &lt; p[j] }
37 func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
35 func (p IntSlice) Len() int { return len(p) }
36 func (p IntSlice) Less(i, j int) bool { return p[i] &lt; p[j] }
37 func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
</pre>
<p>
Here we see methods defined for non-<code>struct</code> types. You can define methods
@ -952,7 +952,7 @@ to test that the result is sorted.
<pre> <!-- progs/sortmain.go /func.ints/ /^}/ -->
12 func ints() {
13 data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
14 a := sort.IntArray(data)
14 a := sort.IntSlice(data)
15 sort.Sort(a)
16 if !sort.IsSorted(a) {
17 panic(&quot;fail&quot;)

View File

@ -628,7 +628,7 @@ We can apply "Sort" to any type that implements "Len", "Less", and "Swap".
The "sort" package includes the necessary methods to allow sorting of
arrays of integers, strings, etc.; here's the code for arrays of "int"
--PROG progs/sort.go /type.*IntArray/ /Swap/
--PROG progs/sort.go /type.*IntSlice/ /Swap/
Here we see methods defined for non-"struct" types. You can define methods
for any type you define and name in your package.

View File

@ -30,34 +30,34 @@ func IsSorted(data Interface) bool {
// Convenience types for common cases
type IntArray []int
type IntSlice []int
func (p IntArray) Len() int { return len(p) }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
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] }
type Float64Array []float64
type Float64Slice []float64
func (p Float64Array) Len() int { return len(p) }
func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] }
func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p Float64Slice) Len() int { return len(p) }
func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type StringArray []string
type StringSlice []string
func (p StringArray) Len() int { return len(p) }
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p StringSlice) Len() int { return len(p) }
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Convenience wrappers for common cases
func SortInts(a []int) { Sort(IntArray(a)) }
func SortFloat64s(a []float64) { Sort(Float64Array(a)) }
func SortStrings(a []string) { Sort(StringArray(a)) }
func SortInts(a []int) { Sort(IntSlice(a)) }
func SortFloat64s(a []float64) { Sort(Float64Slice(a)) }
func SortStrings(a []string) { Sort(StringSlice(a)) }
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) }
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }

View File

@ -11,7 +11,7 @@ import (
func ints() {
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
a := sort.IntArray(data)
a := sort.IntSlice(data)
sort.Sort(a)
if !sort.IsSorted(a) {
panic("fail")
@ -20,7 +20,7 @@ func ints() {
func strings() {
data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
a := sort.StringArray(data)
a := sort.StringSlice(data)
sort.Sort(a)
if !sort.IsSorted(a) {
panic("fail")

View File

@ -218,7 +218,7 @@ type Flag struct {
// sortFlags returns the flags as a slice in lexicographical sorted order.
func sortFlags(flags map[string]*Flag) []*Flag {
list := make(sort.StringArray, len(flags))
list := make(sort.StringSlice, len(flags))
i := 0
for _, f := range flags {
list[i] = f.Name

View File

@ -99,7 +99,7 @@ func SearchStrings(a []string, x string) int {
// Search returns the result of applying SearchInts to the receiver and x.
func (p IntArray) Search(x int) int { return SearchInts(p, x) }
func (p IntSlice) Search(x int) int { return SearchInts(p, x) }
// Search returns the result of applying SearchFloat64s to the receiver and x.
@ -107,4 +107,4 @@ func (p Float64Array) Search(x float64) int { return SearchFloat64s(p, x) }
// Search returns the result of applying SearchStrings to the receiver and x.
func (p StringArray) Search(x string) int { return SearchStrings(p, x) }
func (p StringSlice) Search(x string) int { return SearchStrings(p, x) }

View File

@ -107,9 +107,9 @@ var wrappertests = []struct {
{"SearchInts", SearchInts(data, 11), 8},
{"SearchFloat64s", SearchFloat64s(fdata, 2.1), 4},
{"SearchStrings", SearchStrings(sdata, ""), 0},
{"IntArray.Search", IntArray(data).Search(0), 2},
{"IntSlice.Search", IntSlice(data).Search(0), 2},
{"Float64Array.Search", Float64Array(fdata).Search(2.0), 3},
{"StringArray.Search", StringArray(sdata).Search("x"), 3},
{"StringSlice.Search", StringSlice(sdata).Search("x"), 3},
}

View File

@ -155,15 +155,15 @@ func IsSorted(data Interface) bool {
// Convenience types for common cases
// IntArray attaches the methods of Interface to []int, sorting in increasing order.
type IntArray []int
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
type IntSlice []int
func (p IntArray) Len() int { return len(p) }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
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] }
// Sort is a convenience method.
func (p IntArray) Sort() { Sort(p) }
func (p IntSlice) Sort() { Sort(p) }
// Float64Array attaches the methods of Interface to []float64, sorting in increasing order.
@ -177,30 +177,30 @@ func (p Float64Array) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p Float64Array) Sort() { Sort(p) }
// StringArray attaches the methods of Interface to []string, sorting in increasing order.
type StringArray []string
// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
type StringSlice []string
func (p StringArray) Len() int { return len(p) }
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p StringSlice) Len() int { return len(p) }
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Sort is a convenience method.
func (p StringArray) Sort() { Sort(p) }
func (p StringSlice) Sort() { Sort(p) }
// Convenience wrappers for common cases
// SortInts sorts an array of ints in increasing order.
func SortInts(a []int) { Sort(IntArray(a)) }
func SortInts(a []int) { Sort(IntSlice(a)) }
// SortFloat64s sorts an array of float64s in increasing order.
func SortFloat64s(a []float64) { Sort(Float64Array(a)) }
// SortStrings sorts an array of strings in increasing order.
func SortStrings(a []string) { Sort(StringArray(a)) }
func SortStrings(a []string) { Sort(StringSlice(a)) }
// IntsAreSorted tests whether an array of ints is sorted in increasing order.
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
// Float64sAreSorted tests whether an array of float64s is sorted in increasing order.
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) }
// StringsAreSorted tests whether an array of strings is sorted in increasing order.
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }

View File

@ -16,9 +16,9 @@ var ints = [...]int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984,
var float64s = [...]float64{74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8}
var strings = [...]string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"}
func TestSortIntArray(t *testing.T) {
func TestSortIntSlice(t *testing.T) {
data := ints
a := IntArray(data[0:])
a := IntSlice(data[0:])
Sort(a)
if !IsSorted(a) {
t.Errorf("sorted %v", ints)
@ -36,9 +36,9 @@ func TestSortFloat64Array(t *testing.T) {
}
}
func TestSortStringArray(t *testing.T) {
func TestSortStringSlice(t *testing.T) {
data := strings
a := StringArray(data[0:])
a := StringSlice(data[0:])
Sort(a)
if !IsSorted(a) {
t.Errorf("sorted %v", strings)

View File

@ -62,7 +62,7 @@ var ForkLock sync.RWMutex
// Convert array of string to array
// of NUL-terminated byte pointer.
func StringArrayPtr(ss []string) []*byte {
func StringSlicePtr(ss []string) []*byte {
bb := make([]*byte, len(ss)+1)
for i := 0; i < len(ss); i++ {
bb[i] = StringBytePtr(ss[i])
@ -364,7 +364,7 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err Error)
// Convert args to C form.
argv0p := StringBytePtr(argv0)
argvp := StringArrayPtr(argv)
argvp := StringSlicePtr(argv)
var chroot *byte
if attr.Chroot != "" {
@ -514,7 +514,7 @@ func Exec(argv0 string, argv []string, envv []string) (err Error) {
_, _, e := Syscall(SYS_EXEC,
uintptr(unsafe.Pointer(StringBytePtr(argv0))),
uintptr(unsafe.Pointer(&StringArrayPtr(argv)[0])),
uintptr(unsafe.Pointer(&StringSlicePtr(argv)[0])),
0)
return NewError(e)

View File

@ -62,7 +62,7 @@ var ForkLock sync.RWMutex
// Convert array of string to array
// of NUL-terminated byte pointer.
func StringArrayPtr(ss []string) []*byte {
func StringSlicePtr(ss []string) []*byte {
bb := make([]*byte, len(ss)+1)
for i := 0; i < len(ss); i++ {
bb[i] = StringBytePtr(ss[i])
@ -293,8 +293,8 @@ func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err int) {
// Convert args to C form.
argv0p := StringBytePtr(argv0)
argvp := StringArrayPtr(argv)
envvp := StringArrayPtr(attr.Env)
argvp := StringSlicePtr(argv)
envvp := StringSlicePtr(attr.Env)
if OS == "freebsd" && len(argv[0]) > len(argv0) {
argvp[0] = argv0p
@ -378,7 +378,7 @@ func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid, handle int,
func Exec(argv0 string, argv []string, envv []string) (err int) {
_, _, err1 := RawSyscall(SYS_EXECVE,
uintptr(unsafe.Pointer(StringBytePtr(argv0))),
uintptr(unsafe.Pointer(&StringArrayPtr(argv)[0])),
uintptr(unsafe.Pointer(&StringArrayPtr(envv)[0])))
uintptr(unsafe.Pointer(&StringSlicePtr(argv)[0])),
uintptr(unsafe.Pointer(&StringSlicePtr(envv)[0])))
return int(err1)
}

View File

@ -344,7 +344,7 @@ func printCategories() {
fmt.Print("}\n\n")
}
decl := make(sort.StringArray, len(list))
decl := make(sort.StringSlice, len(list))
ndecl := 0
for _, name := range list {
if _, ok := category[name]; !ok {
@ -665,7 +665,7 @@ func printScriptOrProperty(doProps bool) {
fmt.Print("}\n\n")
}
decl := make(sort.StringArray, len(list))
decl := make(sort.StringSlice, len(list))
ndecl := 0
for _, name := range list {
if doProps {