From 0d54e01dda81d6e96f8f31a9113af2f4a75b2f89 Mon Sep 17 00:00:00 2001 From: Chris Masone Date: Tue, 15 Dec 2015 17:05:48 -0800 Subject: [PATCH 1/3] Implement compoundSet.Filter() Implementation is based on newTypedSet(), so hopefully has similar performance characteristics. --- types/compound_set.go | 12 +++++++++++- types/compound_set_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/types/compound_set.go b/types/compound_set.go index a961a0a099..ec8649bf55 100644 --- a/types/compound_set.go +++ b/types/compound_set.go @@ -66,7 +66,17 @@ func (cs compoundSet) Subtract(others ...Set) Set { } func (cs compoundSet) Filter(cb setFilterCallback) Set { - panic("not implemented") + seq := newEmptySequenceChunker(makeSetLeafChunkFn(cs.t, cs.cs), newSetMetaSequenceChunkFn(cs.t, cs.cs), newSetLeafBoundaryChecker(), newOrderedMetaSequenceBoundaryChecker) + + // Could conceivably use IterAllP() here. + cs.IterAll(func(v Value) { + if cb(v) { + seq.Append(v) + } + }) + + s := seq.Done() + return internalValueFromType(s, s.Type()).(Set) } // TODO: seek should return false if it failed to find the value diff --git a/types/compound_set_test.go b/types/compound_set_test.go index d96220db05..a1ae2cdb8e 100644 --- a/types/compound_set_test.go +++ b/types/compound_set_test.go @@ -167,3 +167,27 @@ func TestCompoundSetIterAll(t *testing.T) { doTest(getTestRefToNativeOrderSet()) doTest(getTestRefToValueOrderSet()) } + +func TestCompoundSetFilter(t *testing.T) { + assert := assert.New(t) + + doTest := func(ts testSet) { + set := ts.toCompoundSet(chunks.NewMemoryStore()) + sort.Sort(ts) + pivot := ts.values[10] + actual := set.Filter(func(v Value) bool { + return ts.less(v, pivot) + }) + + idx := 0 + actual.IterAll(func(v Value) { + assert.True(ts.values[idx].Equals(v), "%v != %v", v, ts.values[idx]) + idx++ + }) + } + + doTest(getTestNativeOrderSet()) + doTest(getTestRefValueOrderSet()) + doTest(getTestRefToNativeOrderSet()) + doTest(getTestRefToValueOrderSet()) +} From 8171014b200ddbfaf60757e025cba87d984e0b61 Mon Sep 17 00:00:00 2001 From: Chris Masone Date: Tue, 15 Dec 2015 17:37:25 -0800 Subject: [PATCH 2/3] remove comment --- types/compound_set.go | 1 - 1 file changed, 1 deletion(-) diff --git a/types/compound_set.go b/types/compound_set.go index ec8649bf55..6cf73f8e15 100644 --- a/types/compound_set.go +++ b/types/compound_set.go @@ -68,7 +68,6 @@ func (cs compoundSet) Subtract(others ...Set) Set { func (cs compoundSet) Filter(cb setFilterCallback) Set { seq := newEmptySequenceChunker(makeSetLeafChunkFn(cs.t, cs.cs), newSetMetaSequenceChunkFn(cs.t, cs.cs), newSetLeafBoundaryChecker(), newOrderedMetaSequenceBoundaryChecker) - // Could conceivably use IterAllP() here. cs.IterAll(func(v Value) { if cb(v) { seq.Append(v) From 18c446e9344c255f122e2797f474e56c81a9e2e4 Mon Sep 17 00:00:00 2001 From: Chris Masone Date: Tue, 15 Dec 2015 20:08:51 -0800 Subject: [PATCH 3/3] Add set-equivalence check to unit test --- types/compound_set_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/types/compound_set_test.go b/types/compound_set_test.go index a1ae2cdb8e..9bab35e0bc 100644 --- a/types/compound_set_test.go +++ b/types/compound_set_test.go @@ -174,10 +174,12 @@ func TestCompoundSetFilter(t *testing.T) { doTest := func(ts testSet) { set := ts.toCompoundSet(chunks.NewMemoryStore()) sort.Sort(ts) - pivot := ts.values[10] + pivotPoint := 10 + pivot := ts.values[pivotPoint] actual := set.Filter(func(v Value) bool { return ts.less(v, pivot) }) + assert.True(newTypedSet(set.cs, set.t, ts.values[:pivotPoint+1]...).Equals(actual)) idx := 0 actual.IterAll(func(v Value) {