~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/pkg/sort/sort.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
 
5
 
// Package sort provides primitives for sorting arrays and user-defined
 
5
// Package sort provides primitives for sorting slices and user-defined
6
6
// collections.
7
7
package sort
8
8
 
82
82
        //      data[d <= i < hi] = pivot
83
83
        //
84
84
        // Once b meets c, can swap the "= pivot" sections
85
 
        // into the middle of the array.
 
85
        // into the middle of the slice.
86
86
        pivot := lo
87
87
        a, b, c, d := lo+1, lo+1, hi, hi
88
88
        for b < c {
155
155
 
156
156
// Convenience types for common cases
157
157
 
158
 
// IntArray attaches the methods of Interface to []int, sorting in increasing order.
159
 
type IntArray []int
160
 
 
161
 
func (p IntArray) Len() int           { return len(p) }
162
 
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
163
 
func (p IntArray) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
164
 
 
165
 
// Sort is a convenience method.
166
 
func (p IntArray) Sort() { Sort(p) }
167
 
 
168
 
 
169
 
// Float64Array attaches the methods of Interface to []float64, sorting in increasing order.
170
 
type Float64Array []float64
171
 
 
172
 
func (p Float64Array) Len() int           { return len(p) }
173
 
func (p Float64Array) Less(i, j int) bool { return p[i] < p[j] }
174
 
func (p Float64Array) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
175
 
 
176
 
// Sort is a convenience method.
177
 
func (p Float64Array) Sort() { Sort(p) }
178
 
 
179
 
 
180
 
// StringArray attaches the methods of Interface to []string, sorting in increasing order.
181
 
type StringArray []string
182
 
 
183
 
func (p StringArray) Len() int           { return len(p) }
184
 
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
185
 
func (p StringArray) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
186
 
 
187
 
// Sort is a convenience method.
188
 
func (p StringArray) Sort() { Sort(p) }
 
158
// IntSlice attaches the methods of Interface to []int, sorting in increasing order.
 
159
type IntSlice []int
 
160
 
 
161
func (p IntSlice) Len() int           { return len(p) }
 
162
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
 
163
func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
 
164
 
 
165
// Sort is a convenience method.
 
166
func (p IntSlice) Sort() { Sort(p) }
 
167
 
 
168
 
 
169
// Float64Slice attaches the methods of Interface to []float64, sorting in increasing order.
 
170
type Float64Slice []float64
 
171
 
 
172
func (p Float64Slice) Len() int           { return len(p) }
 
173
func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] }
 
174
func (p Float64Slice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
 
175
 
 
176
// Sort is a convenience method.
 
177
func (p Float64Slice) Sort() { Sort(p) }
 
178
 
 
179
 
 
180
// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
 
181
type StringSlice []string
 
182
 
 
183
func (p StringSlice) Len() int           { return len(p) }
 
184
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
 
185
func (p StringSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
 
186
 
 
187
// Sort is a convenience method.
 
188
func (p StringSlice) Sort() { Sort(p) }
189
189
 
190
190
 
191
191
// Convenience wrappers for common cases
192
192
 
193
 
// SortInts sorts an array of ints in increasing order.
194
 
func SortInts(a []int) { Sort(IntArray(a)) }
195
 
// SortFloat64s sorts an array of float64s in increasing order.
196
 
func SortFloat64s(a []float64) { Sort(Float64Array(a)) }
197
 
// SortStrings sorts an array of strings in increasing order.
198
 
func SortStrings(a []string) { Sort(StringArray(a)) }
199
 
 
200
 
 
201
 
// IntsAreSorted tests whether an array of ints is sorted in increasing order.
202
 
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
203
 
// Float64sAreSorted tests whether an array of float64s is sorted in increasing order.
204
 
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Array(a)) }
205
 
// StringsAreSorted tests whether an array of strings is sorted in increasing order.
206
 
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
 
193
// Ints sorts a slice of ints in increasing order.
 
194
func Ints(a []int) { Sort(IntSlice(a)) }
 
195
// Float64s sorts a slice of float64s in increasing order.
 
196
func Float64s(a []float64) { Sort(Float64Slice(a)) }
 
197
// Strings sorts a slice of strings in increasing order.
 
198
func Strings(a []string) { Sort(StringSlice(a)) }
 
199
 
 
200
 
 
201
// IntsAreSorted tests whether a slice of ints is sorted in increasing order.
 
202
func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) }
 
203
// Float64sAreSorted tests whether a slice of float64s is sorted in increasing order.
 
204
func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) }
 
205
// StringsAreSorted tests whether a slice of strings is sorted in increasing order.
 
206
func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) }