From 4268ded39cbeb0913e5264094c353caf04114197 Mon Sep 17 00:00:00 2001 From: Dan Willhite Date: Tue, 15 Dec 2015 17:53:51 -0800 Subject: [PATCH] Implement Map and MapP on compoundList. --- types/compound_list.go | 33 ++++++++++++++++++++++++++++-- types/compound_list_test.go | 40 ++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/types/compound_list.go b/types/compound_list.go index 84e964e0da..54888445c2 100644 --- a/types/compound_list.go +++ b/types/compound_list.go @@ -111,11 +111,40 @@ func (cl compoundList) Slice(start uint64, end uint64) List { } func (cl compoundList) Map(mf MapFunc) []interface{} { - panic("not implemented") + start := uint64(0) + + results := make([]interface{}, 0, cl.Len()) + iterateMetaSequenceLeaf(cl, cl.cs, func(l Value) bool { + list := l.(listLeaf) + for i, v := range list.values { + res := mf(v, start+uint64(i)) + results = append(results, res) + } + start += list.Len() + return false + }) + return results } func (cl compoundList) MapP(concurrency int, mf MapFunc) []interface{} { - panic("not implemented") + start := uint64(0) + + var limit chan int + if concurrency == 0 { + limit = make(chan int, runtime.NumCPU()) + } else { + limit = make(chan int, concurrency) + } + + results := make([]interface{}, 0, cl.Len()) + iterateMetaSequenceLeaf(cl, cl.cs, func(l Value) bool { + list := l.(listLeaf) + nl := list.mapInternal(limit, mf, start) + results = append(results, nl...) + start += list.Len() + return false + }) + return results } func (cl compoundList) Set(idx uint64, v Value) List { diff --git a/types/compound_list_test.go b/types/compound_list_test.go index 181eeec7d1..05b99a9140 100644 --- a/types/compound_list_test.go +++ b/types/compound_list_test.go @@ -61,7 +61,7 @@ func getTestSimpleListUnique() testSimpleList { for len(uniques) < length { uniques[s.Int63()] = true } - values := []Value{} + values := make([]Value, 0, length) for k, _ := range uniques { values = append(values, Int64(k)) } @@ -158,6 +158,44 @@ func TestCompoundListIterAllP(t *testing.T) { assert.Equal(len(simpleList), len(visited)) } +func TestCompoundListMap(t *testing.T) { + assert := assert.New(t) + cs := chunks.NewMemoryStore() + + simpleList := getTestSimpleList() + tr := MakeCompoundType(ListKind, MakePrimitiveType(Int64Kind)) + cl := NewTypedList(cs, tr, simpleList...) + + l := cl.Map(func(v Value, i uint64) interface{} { + v1 := v.(Int64) + return v1 + Int64(i) + }) + + assert.Equal(uint64(len(l)), cl.Len()) + for i := 0; i < len(l); i++ { + assert.Equal(l[i], cl.Get(uint64(i)).(Int64)+Int64(i)) + } +} + +func TestCompoundListMapP(t *testing.T) { + assert := assert.New(t) + cs := chunks.NewMemoryStore() + + simpleList := getTestSimpleList() + tr := MakeCompoundType(ListKind, MakePrimitiveType(Int64Kind)) + cl := NewTypedList(cs, tr, simpleList...) + + l := cl.MapP(64, func(v Value, i uint64) interface{} { + v1 := v.(Int64) + return v1 + Int64(i) + }) + + assert.Equal(uint64(len(l)), cl.Len()) + for i := 0; i < len(l); i++ { + assert.Equal(l[i], cl.Get(uint64(i)).(Int64)+Int64(i)) + } +} + func TestCompoundListLen(t *testing.T) { assert := assert.New(t)