summaryrefslogtreecommitdiff
path: root/libgo/go/sort
diff options
context:
space:
mode:
authorian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2012-03-02 20:01:37 +0000
committerian <ian@138bc75d-0d04-0410-961f-82ee72b054a4>2012-03-02 20:01:37 +0000
commitef10b1dda693467283a3645a5da610e810149764 (patch)
tree3eeb8918d39d675108073c8b76d6dd10586a608c /libgo/go/sort
parentea6ad4ae9afbd45b5c19aadab39ddbf1db0d50f8 (diff)
downloadgcc-ef10b1dda693467283a3645a5da610e810149764.tar.gz
libgo: Update to weekly.2012-02-22 release.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@184819 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/sort')
-rw-r--r--libgo/go/sort/example_interface_test.go77
-rw-r--r--libgo/go/sort/example_reverse_test.go30
-rw-r--r--libgo/go/sort/example_test.go2
3 files changed, 108 insertions, 1 deletions
diff --git a/libgo/go/sort/example_interface_test.go b/libgo/go/sort/example_interface_test.go
new file mode 100644
index 00000000000..4c88821be7c
--- /dev/null
+++ b/libgo/go/sort/example_interface_test.go
@@ -0,0 +1,77 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package sort_test
+
+import (
+ "fmt"
+ "sort"
+)
+
+type Grams int
+
+func (g Grams) String() string { return fmt.Sprintf("%dg", int(g)) }
+
+type Organ struct {
+ Name string
+ Weight Grams
+}
+
+type Organs []*Organ
+
+func (s Organs) Len() int { return len(s) }
+func (s Organs) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+
+// ByName implements sort.Interface by providing Less and using the Len and
+// Swap methods of the embedded Organs value.
+type ByName struct{ Organs }
+
+func (s ByName) Less(i, j int) bool { return s.Organs[i].Name < s.Organs[j].Name }
+
+// ByWeight implements sort.Interface by providing Less and using the Len and
+// Swap methods of the embedded Organs value.
+type ByWeight struct{ Organs }
+
+func (s ByWeight) Less(i, j int) bool { return s.Organs[i].Weight < s.Organs[j].Weight }
+
+func ExampleInterface() {
+ s := []*Organ{
+ {"brain", 1340},
+ {"heart", 290},
+ {"liver", 1494},
+ {"pancreas", 131},
+ {"prostate", 62},
+ {"spleen", 162},
+ }
+
+ sort.Sort(ByWeight{s})
+ fmt.Println("Organs by weight:")
+ printOrgans(s)
+
+ sort.Sort(ByName{s})
+ fmt.Println("Organs by name:")
+ printOrgans(s)
+
+ // Output:
+ // Organs by weight:
+ // prostate (62g)
+ // pancreas (131g)
+ // spleen (162g)
+ // heart (290g)
+ // brain (1340g)
+ // liver (1494g)
+ // Organs by name:
+ // brain (1340g)
+ // heart (290g)
+ // liver (1494g)
+ // pancreas (131g)
+ // prostate (62g)
+ // spleen (162g)
+}
+
+func printOrgans(s []*Organ) {
+ for _, o := range s {
+ fmt.Printf("%-8s (%v)\n", o.Name, o.Weight)
+ }
+}
diff --git a/libgo/go/sort/example_reverse_test.go b/libgo/go/sort/example_reverse_test.go
new file mode 100644
index 00000000000..7c7f05bf3a2
--- /dev/null
+++ b/libgo/go/sort/example_reverse_test.go
@@ -0,0 +1,30 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package sort_test
+
+import (
+ "fmt"
+ "sort"
+)
+
+// Reverse embeds a sort.Interface value and implements a reverse sort over
+// that value.
+type Reverse struct {
+ // This embedded Interface permits Reverse to use the methods of
+ // another Interface implementation.
+ sort.Interface
+}
+
+// Less returns the opposite of the embedded implementation's Less method.
+func (r Reverse) Less(i, j int) bool {
+ return r.Interface.Less(j, i)
+}
+
+func ExampleInterface_reverse() {
+ s := []int{5, 2, 6, 3, 1, 4} // unsorted
+ sort.Sort(Reverse{sort.IntSlice(s)})
+ fmt.Println(s)
+ // Output: [6 5 4 3 2 1]
+}
diff --git a/libgo/go/sort/example_test.go b/libgo/go/sort/example_test.go
index 2224db7e13c..f57d02546f7 100644
--- a/libgo/go/sort/example_test.go
+++ b/libgo/go/sort/example_test.go
@@ -9,9 +9,9 @@ import (
"sort"
)
-// [1 2 3 4 5 6]
func ExampleInts() {
s := []int{5, 2, 6, 3, 1, 4} // unsorted
sort.Ints(s)
fmt.Println(s)
+ // Output: [1 2 3 4 5 6]
}