Merge pull request #779 from willhite/panics

Implement Map and MapP on compoundList.
This commit is contained in:
Dan Willhite
2015-12-16 09:55:42 -08:00
2 changed files with 70 additions and 3 deletions

View File

@@ -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 {

View File

@@ -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)