mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-06 18:59:02 -06:00
Implement Filter on compoundList.
This commit is contained in:
@@ -186,7 +186,14 @@ func (cl compoundList) sequenceChunkerAtIndex(idx uint64) *sequenceChunker {
|
||||
}
|
||||
|
||||
func (cl compoundList) Filter(cb listFilterCallback) List {
|
||||
panic("not implemented")
|
||||
seq := newEmptySequenceChunker(makeListLeafChunkFn(cl.t, cl.cs), newMetaSequenceChunkFn(cl.t, cl.cs), newListLeafBoundaryChecker(), newMetaSequenceBoundaryChecker)
|
||||
cl.IterAll(func(v Value, idx uint64) {
|
||||
if cb(v, idx) {
|
||||
seq.Append(v)
|
||||
}
|
||||
})
|
||||
s := seq.Done()
|
||||
return internalValueFromType(s, s.Type()).(List)
|
||||
}
|
||||
|
||||
func (cl compoundList) Remove(start uint64, end uint64) List {
|
||||
|
||||
@@ -548,3 +548,39 @@ func TestCompoundListSlice(t *testing.T) {
|
||||
assert.True(cl8.Equals(cl.Slice(cl.Len()/2, cl.Len()+1)))
|
||||
assert.True(cl8.Equals(cl.Slice(cl.Len()/2, cl.Len()*2)))
|
||||
}
|
||||
|
||||
func TestCompoundListFilter(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
simple := getTestSimpleList()
|
||||
filterCb := func(v Value, idx uint64) bool {
|
||||
return v.(Int64)%5 != 0
|
||||
}
|
||||
|
||||
expected := testSimpleList{}
|
||||
for i, v := range simple {
|
||||
if filterCb(v, uint64(i)) {
|
||||
expected = append(expected, v)
|
||||
}
|
||||
|
||||
}
|
||||
cl := simple.ToNomsList(cs)
|
||||
|
||||
res := cl.Filter(filterCb)
|
||||
assert.Equal(len(expected), int(res.Len()))
|
||||
res.IterAll(func(v Value, idx uint64) {
|
||||
assert.Equal(expected[idx], v)
|
||||
})
|
||||
assert.True(expected.ToNomsList(cs).Equals(res))
|
||||
}
|
||||
|
||||
func TestCompoundListFilterEmpty(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
cs := chunks.NewMemoryStore()
|
||||
empty := testSimpleList{}.ToNomsList(cs)
|
||||
filterCb := func(v Value, idx uint64) bool {
|
||||
return v.(Int64)%5 != 0
|
||||
}
|
||||
res1 := empty.Filter(filterCb)
|
||||
assert.True(res1.Empty())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user