summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2008-09-09 18:13:08 -0700
committerRobert Griesemer <gri@golang.org>2008-09-09 18:13:08 -0700
commit5a0ea48bf2c6d08d823eac7f9e9ae97961fd365a (patch)
tree2fbb1fde592c3586ad9aa47a93199cb87a65fb0f
parent90d23cd83ee59d537f580fe3d149f37254399843 (diff)
downloadgolang-5a0ea48bf2c6d08d823eac7f9e9ae97961fd365a.tar.gz
- added convenience wrappers for sort
(work now with Ken's latest compiler fix) - exoanded test cases accordingly - fixed a type in the spec (thx r) R=r DELTA=65 (62 added, 2 deleted, 1 changed) OCL=15050 CL=15050
-rw-r--r--doc/go_spec.txt2
-rw-r--r--src/lib/sort.go14
-rw-r--r--test/sorting.go50
3 files changed, 63 insertions, 3 deletions
diff --git a/doc/go_spec.txt b/doc/go_spec.txt
index 380dbbfaa..32eeaeea2 100644
--- a/doc/go_spec.txt
+++ b/doc/go_spec.txt
@@ -2269,7 +2269,7 @@ representation of the integer.
5b) Converting an array of uint8s yields a string whose successive
bytes are those of the array. (Recall byte is a synonym for uint8.)
- string([]byte('h', 'e', 'l', 'l', 'o')) // "hello"
+ string([]byte{'h', 'e', 'l', 'l', 'o'}) // "hello"
Note that there is no linguistic mechanism to convert between pointers
and integers. A library may be provided under restricted circumstances
diff --git a/src/lib/sort.go b/src/lib/sort.go
index 72b0ddd47..fb5f77f47 100644
--- a/src/lib/sort.go
+++ b/src/lib/sort.go
@@ -65,8 +65,6 @@ export func IsSorted(data SortInterface) bool {
// Convenience types for common cases
-// TODO: Once we can associate methods with all types, this can be simplified
-// since we cann associate the methods with the arrays directly.
export type IntArray struct {
data *[]int;
@@ -93,3 +91,15 @@ export type StringArray struct {
func (p *StringArray) len() int { return len(p.data); }
func (p *StringArray) less(i, j int) bool { return p.data[i] < p.data[j]; }
func (p *StringArray) swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i]; }
+
+
+// Convenience wrappers for common cases
+
+export func SortInts(a *[]int) { Sort(&IntArray{a}); }
+export func SortFloats(a *[]float) { Sort(&FloatArray{a}); }
+export func SortStrings(a *[]string) { Sort(&StringArray{a}); }
+
+
+export func IntsAreSorted(a *[]int) bool { return IsSorted(&IntArray{a}); }
+export func FloatsAreSorted(a *[]float) bool { return IsSorted(&FloatArray{a}); }
+export func StringsAreSorted(a *[]string) bool { return IsSorted(&StringArray{a}); }
diff --git a/test/sorting.go b/test/sorting.go
index f55e9fddd..ae9dafb75 100644
--- a/test/sorting.go
+++ b/test/sorting.go
@@ -59,4 +59,54 @@ func main() {
panic();
}
}
+
+ // Same tests again, this time using the convenience wrappers
+
+ { data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586};
+
+ Sort.SortInts(&data);
+
+ /*
+ for i := 0; i < len(data); i++ {
+ print(data[i], " ");
+ }
+ print("\n");
+ */
+
+ if !Sort.IntsAreSorted(&data) {
+ panic();
+ }
+ }
+
+ { data := []float{74.3, 59.0, 238.2, -784.0, 2.3, 9845.768, -959.7485, 905, 7.8, 7.8};
+
+ Sort.SortFloats(&data);
+
+ /*
+ for i := 0; i < len(data); i++ {
+ print(data[i], " ");
+ }
+ print("\n");
+ */
+
+ if !Sort.FloatsAreSorted(&data) {
+ panic();
+ }
+ }
+
+ { data := []string{"", "Hello", "foo", "bar", "foo", "f00", "%*&^*&^&", "***"};
+
+ Sort.SortStrings(&data);
+
+ /*
+ for i := 0; i < len(data); i++ {
+ print(data[i], " ");
+ }
+ print("\n");
+ */
+
+ if !Sort.StringsAreSorted(&data) {
+ panic();
+ }
+ }
}