From b54fd91c82090fa8f498bcac680d89576d131beb Mon Sep 17 00:00:00 2001 From: Chris Masone Date: Thu, 5 Nov 2015 13:11:17 -0800 Subject: [PATCH] Add Filter() to List And update collection templates to be backed by the untyped Filter() implementations. Fixes #578 --- nomdl/codegen/list.tmpl | 9 +++------ nomdl/codegen/map.tmpl | 9 +++------ nomdl/codegen/set.tmpl | 9 +++------ types/list.go | 12 ++++++++++++ 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/nomdl/codegen/list.tmpl b/nomdl/codegen/list.tmpl index 5a219c92bf..b97a77dad2 100644 --- a/nomdl/codegen/list.tmpl +++ b/nomdl/codegen/list.tmpl @@ -134,11 +134,8 @@ func (l {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) { type {{.Name}}FilterCallback func(v {{userType .ElemType}}, i uint64) (keep bool) func (l {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} { - nl := New{{.Name}}() - l.IterAll(func(v {{userType .ElemType}}, i uint64) { - if cb(v, i) { - nl = nl.Append(v) - } + out := l.l.Filter(func(v {{$typesPackage}}Value, i uint64) bool { + return cb({{valueToUser "v" .ElemType}}, i) }) - return nl + return {{.Name}}{out, &ref.Ref{}} } diff --git a/nomdl/codegen/map.tmpl b/nomdl/codegen/map.tmpl index ce9cb89f57..a6e1c58011 100644 --- a/nomdl/codegen/map.tmpl +++ b/nomdl/codegen/map.tmpl @@ -125,11 +125,8 @@ func (m {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) { type {{.Name}}FilterCallback func(k {{userType .KeyType}}, v {{userType .ValueType}}) (keep bool) func (m {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} { - nm := New{{.Name}}() - m.IterAll(func(k {{userType .KeyType}}, v {{userType .ValueType}}) { - if cb(k, v) { - nm = nm.Set(k, v) - } + out := m.m.Filter(func(k, v {{$typesPackage}}Value) bool { + return cb({{valueToUser "k" .KeyType}}, {{valueToUser "v" .ValueType}}) }) - return nm + return {{.Name}}{out, &ref.Ref{}} } diff --git a/nomdl/codegen/set.tmpl b/nomdl/codegen/set.tmpl index 4975bc8d95..1724944009 100644 --- a/nomdl/codegen/set.tmpl +++ b/nomdl/codegen/set.tmpl @@ -105,13 +105,10 @@ func (s {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) { type {{.Name}}FilterCallback func(p {{userType .ElemType}}) (keep bool) func (s {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} { - ns := New{{.Name}}() - s.IterAll(func(v {{userType .ElemType}}) { - if cb(v) { - ns = ns.Insert(v) - } + out := s.s.Filter(func(v {{$typesPackage}}Value) bool { + return cb({{valueToUser "v" .ElemType}}) }) - return ns + return {{.Name}}{out, &ref.Ref{}} } func (s {{.Name}}) Insert(p ...{{userType .ElemType}}) {{.Name}} { diff --git a/types/list.go b/types/list.go index d0e2898fd7..b28cafc82c 100644 --- a/types/list.go +++ b/types/list.go @@ -86,6 +86,18 @@ func (l List) iterInternal(sem chan int, lf listIterAllFunc, offset uint64) { wg.Wait() } +type listFilterCallback func(v Value, index uint64) (keep bool) + +func (l List) Filter(cb listFilterCallback) List { + data := []Value{} + for i, v := range l.values { + if cb(v, uint64(i)) { + data = append(data, v) + } + } + return newListNoCopy(data, l.t) +} + type MapFunc func(v Value, index uint64) interface{} func (l List) Map(mf MapFunc) []interface{} {