aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2009-02-06 14:32:09 -0800
committerRob Pike <r@golang.org>2009-02-06 14:32:09 -0800
commit39d05ae808849f09cceb07b7970e6f493a0822e2 (patch)
tree4046133efbbaa96c581fa2140c74dc48fd8f1c24 /src
parent0970c4686350f772b481a270bd48767a953d3eb8 (diff)
downloadgo-39d05ae808849f09cceb07b7970e6f493a0822e2.tar.xz
add a trivial visitor method, just for fun
R=gri DELTA=31 (30 added, 1 deleted, 0 changed) OCL=24568 CL=24575
Diffstat (limited to 'src')
-rw-r--r--src/lib/container/array/array.go7
-rw-r--r--src/lib/container/array/array_test.go24
2 files changed, 30 insertions, 1 deletions
diff --git a/src/lib/container/array/array.go b/src/lib/container/array/array.go
index 768e46d3a4..e29736fb65 100644
--- a/src/lib/container/array/array.go
+++ b/src/lib/container/array/array.go
@@ -140,6 +140,13 @@ func (p *Array) Slice(i, j int) *Array {
}
+func (p *Array) Do(f func(elem Element)) {
+ for i := 0; i < len(p.a); i++ {
+ f(p.a[i]) // not too safe if f changes the Array
+ }
+}
+
+
// Convenience wrappers
func (p *Array) Push(x Element) {
diff --git a/src/lib/container/array/array_test.go b/src/lib/container/array/array_test.go
index df9fee673c..43ac702abc 100644
--- a/src/lib/container/array/array_test.go
+++ b/src/lib/container/array/array_test.go
@@ -139,7 +139,6 @@ func TestInsertArray(t *testing.T) {
verify_pattern(t, a, 8, 1000, 2);
}
-
func TestSorting(t *testing.T) {
const n = 100;
a := array.NewIntArray(n);
@@ -148,3 +147,26 @@ func TestSorting(t *testing.T) {
}
if sort.IsSorted(a) { t.Error("not sorted") }
}
+
+
+func TestDo(t *testing.T) {
+ const n = 25;
+ const salt = 17;
+ a := array.NewIntArray(n);
+ for i := 0; i < n; i++ {
+ a.Set(i, salt * i);
+ }
+ count := 0;
+ a.Do(
+ func(e array.Element) {
+ i := e.(int);
+ if i != count*salt {
+ t.Error("value at", count, "should be", count*salt, "not", i)
+ }
+ count++;
+ }
+ );
+ if count != n {
+ t.Error("should visit", n, "values; did visit", count)
+ }
+}