Merge pull request #574 from cmasone-attic/issue564

Add IterAllP() to all typed Lists and Sets
This commit is contained in:
cmasone-attic
2015-11-05 09:22:03 -08:00
29 changed files with 425 additions and 153 deletions
+6
View File
@@ -416,6 +416,12 @@ func (l ListOfRefOfIncident) IterAll(cb ListOfRefOfIncidentIterAllCallback) {
})
}
func (l ListOfRefOfIncident) IterAllP(concurrency int, cb ListOfRefOfIncidentIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(RefOfIncident), i)
})
}
type ListOfRefOfIncidentFilterCallback func(v RefOfIncident, i uint64) (keep bool)
func (l ListOfRefOfIncident) Filter(cb ListOfRefOfIncidentFilterCallback) ListOfRefOfIncident {
+6
View File
@@ -533,6 +533,12 @@ func (s SetOfString) IterAll(cb SetOfStringIterAllCallback) {
})
}
func (s SetOfString) IterAllP(concurrency int, cb SetOfStringIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(types.String).String())
})
}
type SetOfStringFilterCallback func(p string) (keep bool)
func (s SetOfString) Filter(cb SetOfStringFilterCallback) SetOfString {
+12
View File
@@ -674,6 +674,12 @@ func (l ListOfNode) IterAll(cb ListOfNodeIterAllCallback) {
})
}
func (l ListOfNode) IterAllP(concurrency int, cb ListOfNodeIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(Node), i)
})
}
type ListOfNodeFilterCallback func(v Node, i uint64) (keep bool)
func (l ListOfNode) Filter(cb ListOfNodeFilterCallback) ListOfNode {
@@ -935,6 +941,12 @@ func (l ListOfRefOfValue) IterAll(cb ListOfRefOfValueIterAllCallback) {
})
}
func (l ListOfRefOfValue) IterAllP(concurrency int, cb ListOfRefOfValueIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(RefOfValue), i)
})
}
type ListOfRefOfValueFilterCallback func(v RefOfValue, i uint64) (keep bool)
func (l ListOfRefOfValue) Filter(cb ListOfRefOfValueFilterCallback) ListOfRefOfValue {
+6
View File
@@ -533,6 +533,12 @@ func (s SetOfString) IterAll(cb SetOfStringIterAllCallback) {
})
}
func (s SetOfString) IterAllP(concurrency int, cb SetOfStringIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(types.String).String())
})
}
type SetOfStringFilterCallback func(p string) (keep bool)
func (s SetOfString) Filter(cb SetOfStringFilterCallback) SetOfString {
+6
View File
@@ -635,6 +635,12 @@ func (s SetOfRefOfRemotePhoto) IterAll(cb SetOfRefOfRemotePhotoIterAllCallback)
})
}
func (s SetOfRefOfRemotePhoto) IterAllP(concurrency int, cb SetOfRefOfRemotePhotoIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(RefOfRemotePhoto))
})
}
type SetOfRefOfRemotePhotoFilterCallback func(p RefOfRemotePhoto) (keep bool)
func (s SetOfRefOfRemotePhoto) Filter(cb SetOfRefOfRemotePhotoFilterCallback) SetOfRefOfRemotePhoto {
+6
View File
@@ -305,6 +305,12 @@ func (l ListOfSong) IterAll(cb ListOfSongIterAllCallback) {
})
}
func (l ListOfSong) IterAllP(concurrency int, cb ListOfSongIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(Song), i)
})
}
type ListOfSongFilterCallback func(v Song, i uint64) (keep bool)
func (l ListOfSong) Filter(cb ListOfSongFilterCallback) ListOfSong {
+12
View File
@@ -548,6 +548,12 @@ func (s SetOfRemotePhoto) IterAll(cb SetOfRemotePhotoIterAllCallback) {
})
}
func (s SetOfRemotePhoto) IterAllP(concurrency int, cb SetOfRemotePhotoIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(RemotePhoto))
})
}
type SetOfRemotePhotoFilterCallback func(p RemotePhoto) (keep bool)
func (s SetOfRemotePhoto) Filter(cb SetOfRemotePhotoFilterCallback) SetOfRemotePhoto {
@@ -792,6 +798,12 @@ func (s SetOfRefOfRemotePhoto) IterAll(cb SetOfRefOfRemotePhotoIterAllCallback)
})
}
func (s SetOfRefOfRemotePhoto) IterAllP(concurrency int, cb SetOfRefOfRemotePhotoIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(RefOfRemotePhoto))
})
}
type SetOfRefOfRemotePhotoFilterCallback func(p RefOfRemotePhoto) (keep bool)
func (s SetOfRefOfRemotePhoto) Filter(cb SetOfRefOfRemotePhotoFilterCallback) SetOfRefOfRemotePhoto {
+6
View File
@@ -533,6 +533,12 @@ func (s SetOfString) IterAll(cb SetOfStringIterAllCallback) {
})
}
func (s SetOfString) IterAllP(concurrency int, cb SetOfStringIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(types.String).String())
})
}
type SetOfStringFilterCallback func(p string) (keep bool)
func (s SetOfString) Filter(cb SetOfStringFilterCallback) SetOfString {
+12
View File
@@ -250,6 +250,12 @@ func (l ListOfMapOfStringToValue) IterAll(cb ListOfMapOfStringToValueIterAllCall
})
}
func (l ListOfMapOfStringToValue) IterAllP(concurrency int, cb ListOfMapOfStringToValueIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(MapOfStringToValue), i)
})
}
type ListOfMapOfStringToValueFilterCallback func(v MapOfStringToValue, i uint64) (keep bool)
func (l ListOfMapOfStringToValue) Filter(cb ListOfMapOfStringToValueFilterCallback) ListOfMapOfStringToValue {
@@ -763,6 +769,12 @@ func (l ListOfPitch) IterAll(cb ListOfPitchIterAllCallback) {
})
}
func (l ListOfPitch) IterAllP(concurrency int, cb ListOfPitchIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(Pitch), i)
})
}
type ListOfPitchFilterCallback func(v Pitch, i uint64) (keep bool)
func (l ListOfPitch) Filter(cb ListOfPitchFilterCallback) ListOfPitch {
+6
View File
@@ -533,6 +533,12 @@ func (s SetOfString) IterAll(cb SetOfStringIterAllCallback) {
})
}
func (s SetOfString) IterAllP(concurrency int, cb SetOfStringIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(types.String).String())
})
}
type SetOfStringFilterCallback func(p string) (keep bool)
func (s SetOfString) Filter(cb SetOfStringFilterCallback) SetOfString {
+6
View File
@@ -228,6 +228,12 @@ func (s SetOfRefOfRemotePhoto) IterAll(cb SetOfRefOfRemotePhotoIterAllCallback)
})
}
func (s SetOfRefOfRemotePhoto) IterAllP(concurrency int, cb SetOfRefOfRemotePhotoIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(RefOfRemotePhoto))
})
}
type SetOfRefOfRemotePhotoFilterCallback func(p RefOfRemotePhoto) (keep bool)
func (s SetOfRefOfRemotePhoto) Filter(cb SetOfRefOfRemotePhotoFilterCallback) SetOfRefOfRemotePhoto {
+6
View File
@@ -350,6 +350,12 @@ func (s SetOfRefOfCommit) IterAll(cb SetOfRefOfCommitIterAllCallback) {
})
}
func (s SetOfRefOfCommit) IterAllP(concurrency int, cb SetOfRefOfCommitIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(v.(RefOfCommit))
})
}
type SetOfRefOfCommitFilterCallback func(p RefOfCommit) (keep bool)
func (s SetOfRefOfCommit) Filter(cb SetOfRefOfCommitFilterCallback) SetOfRefOfCommit {
+6
View File
@@ -125,6 +125,12 @@ func (l {{.Name}}) IterAll(cb {{.Name}}IterAllCallback) {
})
}
func (l {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) {
l.l.IterAllP(concurrency, func(v {{$typesPackage}}Value, i uint64) {
cb({{valueToUser "v" .ElemType}}, i)
})
}
type {{.Name}}FilterCallback func(v {{userType .ElemType}}, i uint64) (keep bool)
func (l {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} {
+6
View File
@@ -96,6 +96,12 @@ func (s {{.Name}}) IterAll(cb {{.Name}}IterAllCallback) {
})
}
func (s {{.Name}}) IterAllP(concurrency int, cb {{.Name}}IterAllCallback) {
s.s.IterAllP(concurrency, func(v {{$typesPackage}}Value) {
cb({{valueToUser "v" .ElemType}})
})
}
type {{.Name}}FilterCallback func(p {{userType .ElemType}}) (keep bool)
func (s {{.Name}}) Filter(cb {{.Name}}FilterCallback) {{.Name}} {
@@ -130,6 +130,12 @@ func (l ListOfInt64) IterAll(cb ListOfInt64IterAllCallback) {
})
}
func (l ListOfInt64) IterAllP(concurrency int, cb ListOfInt64IterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(int64(v.(types.Int64)), i)
})
}
type ListOfInt64FilterCallback func(v int64, i uint64) (keep bool)
func (l ListOfInt64) Filter(cb ListOfInt64FilterCallback) ListOfInt64 {
+18
View File
@@ -285,6 +285,12 @@ func (l ListOfRefOfFloat32) IterAll(cb ListOfRefOfFloat32IterAllCallback) {
})
}
func (l ListOfRefOfFloat32) IterAllP(concurrency int, cb ListOfRefOfFloat32IterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(RefOfFloat32), i)
})
}
type ListOfRefOfFloat32FilterCallback func(v RefOfFloat32, i uint64) (keep bool)
func (l ListOfRefOfFloat32) Filter(cb ListOfRefOfFloat32FilterCallback) ListOfRefOfFloat32 {
@@ -471,6 +477,12 @@ func (l ListOfString) IterAll(cb ListOfStringIterAllCallback) {
})
}
func (l ListOfString) IterAllP(concurrency int, cb ListOfStringIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(types.String).String(), i)
})
}
type ListOfStringFilterCallback func(v string, i uint64) (keep bool)
func (l ListOfString) Filter(cb ListOfStringFilterCallback) ListOfString {
@@ -628,6 +640,12 @@ func (s SetOfFloat32) IterAll(cb SetOfFloat32IterAllCallback) {
})
}
func (s SetOfFloat32) IterAllP(concurrency int, cb SetOfFloat32IterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(float32(v.(types.Float32)))
})
}
type SetOfFloat32FilterCallback func(p float32) (keep bool)
func (s SetOfFloat32) Filter(cb SetOfFloat32FilterCallback) SetOfFloat32 {
+6
View File
@@ -101,6 +101,12 @@ func (s SetOfBool) IterAll(cb SetOfBoolIterAllCallback) {
})
}
func (s SetOfBool) IterAllP(concurrency int, cb SetOfBoolIterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(bool(v.(types.Bool)))
})
}
type SetOfBoolFilterCallback func(p bool) (keep bool)
func (s SetOfBool) Filter(cb SetOfBoolFilterCallback) SetOfBool {
+6
View File
@@ -250,6 +250,12 @@ func (l ListOfStruct) IterAll(cb ListOfStructIterAllCallback) {
})
}
func (l ListOfStruct) IterAllP(concurrency int, cb ListOfStructIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(Struct), i)
})
}
type ListOfStructFilterCallback func(v Struct, i uint64) (keep bool)
func (l ListOfStruct) Filter(cb ListOfStructFilterCallback) ListOfStruct {
@@ -233,6 +233,12 @@ func (l ListOfTree) IterAll(cb ListOfTreeIterAllCallback) {
})
}
func (l ListOfTree) IterAllP(concurrency int, cb ListOfTreeIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(Tree), i)
})
}
type ListOfTreeFilterCallback func(v Tree, i uint64) (keep bool)
func (l ListOfTree) Filter(cb ListOfTreeFilterCallback) ListOfTree {
@@ -109,3 +109,144 @@ func (s StructWithDupList) SetL(val ListOfUInt8) StructWithDupList {
s.ref = &ref.Ref{}
return s
}
// ListOfUInt8
type ListOfUInt8 struct {
l types.List
ref *ref.Ref
}
func NewListOfUInt8() ListOfUInt8 {
return ListOfUInt8{types.NewList(), &ref.Ref{}}
}
type ListOfUInt8Def []uint8
func (def ListOfUInt8Def) New() ListOfUInt8 {
l := make([]types.Value, len(def))
for i, d := range def {
l[i] = types.UInt8(d)
}
return ListOfUInt8{types.NewList(l...), &ref.Ref{}}
}
func (l ListOfUInt8) Def() ListOfUInt8Def {
d := make([]uint8, l.Len())
for i := uint64(0); i < l.Len(); i++ {
d[i] = uint8(l.l.Get(i).(types.UInt8))
}
return d
}
func (l ListOfUInt8) Equals(other types.Value) bool {
return other != nil && __typeRefForListOfUInt8.Equals(other.TypeRef()) && l.Ref() == other.Ref()
}
func (l ListOfUInt8) Ref() ref.Ref {
return types.EnsureRef(l.ref, l)
}
func (l ListOfUInt8) Chunks() (chunks []ref.Ref) {
chunks = append(chunks, l.TypeRef().Chunks()...)
chunks = append(chunks, l.l.Chunks()...)
return
}
// A Noms Value that describes ListOfUInt8.
var __typeRefForListOfUInt8 types.TypeRef
func (m ListOfUInt8) TypeRef() types.TypeRef {
return __typeRefForListOfUInt8
}
func init() {
__typeRefForListOfUInt8 = types.MakeCompoundTypeRef(types.ListKind, types.MakePrimitiveTypeRef(types.UInt8Kind))
types.RegisterValue(__typeRefForListOfUInt8, builderForListOfUInt8, readerForListOfUInt8)
}
func builderForListOfUInt8(v types.Value) types.Value {
return ListOfUInt8{v.(types.List), &ref.Ref{}}
}
func readerForListOfUInt8(v types.Value) types.Value {
return v.(ListOfUInt8).l
}
func (l ListOfUInt8) Len() uint64 {
return l.l.Len()
}
func (l ListOfUInt8) Empty() bool {
return l.Len() == uint64(0)
}
func (l ListOfUInt8) Get(i uint64) uint8 {
return uint8(l.l.Get(i).(types.UInt8))
}
func (l ListOfUInt8) Slice(idx uint64, end uint64) ListOfUInt8 {
return ListOfUInt8{l.l.Slice(idx, end), &ref.Ref{}}
}
func (l ListOfUInt8) Set(i uint64, val uint8) ListOfUInt8 {
return ListOfUInt8{l.l.Set(i, types.UInt8(val)), &ref.Ref{}}
}
func (l ListOfUInt8) Append(v ...uint8) ListOfUInt8 {
return ListOfUInt8{l.l.Append(l.fromElemSlice(v)...), &ref.Ref{}}
}
func (l ListOfUInt8) Insert(idx uint64, v ...uint8) ListOfUInt8 {
return ListOfUInt8{l.l.Insert(idx, l.fromElemSlice(v)...), &ref.Ref{}}
}
func (l ListOfUInt8) Remove(idx uint64, end uint64) ListOfUInt8 {
return ListOfUInt8{l.l.Remove(idx, end), &ref.Ref{}}
}
func (l ListOfUInt8) RemoveAt(idx uint64) ListOfUInt8 {
return ListOfUInt8{(l.l.RemoveAt(idx)), &ref.Ref{}}
}
func (l ListOfUInt8) fromElemSlice(p []uint8) []types.Value {
r := make([]types.Value, len(p))
for i, v := range p {
r[i] = types.UInt8(v)
}
return r
}
type ListOfUInt8IterCallback func(v uint8, i uint64) (stop bool)
func (l ListOfUInt8) Iter(cb ListOfUInt8IterCallback) {
l.l.Iter(func(v types.Value, i uint64) bool {
return cb(uint8(v.(types.UInt8)), i)
})
}
type ListOfUInt8IterAllCallback func(v uint8, i uint64)
func (l ListOfUInt8) IterAll(cb ListOfUInt8IterAllCallback) {
l.l.IterAll(func(v types.Value, i uint64) {
cb(uint8(v.(types.UInt8)), i)
})
}
func (l ListOfUInt8) IterAllP(concurrency int, cb ListOfUInt8IterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(uint8(v.(types.UInt8)), i)
})
}
type ListOfUInt8FilterCallback func(v uint8, i uint64) (keep bool)
func (l ListOfUInt8) Filter(cb ListOfUInt8FilterCallback) ListOfUInt8 {
nl := NewListOfUInt8()
l.IterAll(func(v uint8, i uint64) {
if cb(v, i) {
nl = nl.Append(v)
}
})
return nl
}
@@ -299,6 +299,12 @@ func (l ListOfD) IterAll(cb ListOfDIterAllCallback) {
})
}
func (l ListOfD) IterAllP(concurrency int, cb ListOfDIterAllCallback) {
l.l.IterAllP(concurrency, func(v types.Value, i uint64) {
cb(v.(D), i)
})
}
type ListOfDFilterCallback func(v D, i uint64) (keep bool)
func (l ListOfD) Filter(cb ListOfDFilterCallback) ListOfD {
@@ -163,138 +163,3 @@ func (s StructWithList) SetI(val int64) StructWithList {
s.ref = &ref.Ref{}
return s
}
// ListOfUInt8
type ListOfUInt8 struct {
l types.List
ref *ref.Ref
}
func NewListOfUInt8() ListOfUInt8 {
return ListOfUInt8{types.NewList(), &ref.Ref{}}
}
type ListOfUInt8Def []uint8
func (def ListOfUInt8Def) New() ListOfUInt8 {
l := make([]types.Value, len(def))
for i, d := range def {
l[i] = types.UInt8(d)
}
return ListOfUInt8{types.NewList(l...), &ref.Ref{}}
}
func (l ListOfUInt8) Def() ListOfUInt8Def {
d := make([]uint8, l.Len())
for i := uint64(0); i < l.Len(); i++ {
d[i] = uint8(l.l.Get(i).(types.UInt8))
}
return d
}
func (l ListOfUInt8) Equals(other types.Value) bool {
return other != nil && __typeRefForListOfUInt8.Equals(other.TypeRef()) && l.Ref() == other.Ref()
}
func (l ListOfUInt8) Ref() ref.Ref {
return types.EnsureRef(l.ref, l)
}
func (l ListOfUInt8) Chunks() (chunks []ref.Ref) {
chunks = append(chunks, l.TypeRef().Chunks()...)
chunks = append(chunks, l.l.Chunks()...)
return
}
// A Noms Value that describes ListOfUInt8.
var __typeRefForListOfUInt8 types.TypeRef
func (m ListOfUInt8) TypeRef() types.TypeRef {
return __typeRefForListOfUInt8
}
func init() {
__typeRefForListOfUInt8 = types.MakeCompoundTypeRef(types.ListKind, types.MakePrimitiveTypeRef(types.UInt8Kind))
types.RegisterValue(__typeRefForListOfUInt8, builderForListOfUInt8, readerForListOfUInt8)
}
func builderForListOfUInt8(v types.Value) types.Value {
return ListOfUInt8{v.(types.List), &ref.Ref{}}
}
func readerForListOfUInt8(v types.Value) types.Value {
return v.(ListOfUInt8).l
}
func (l ListOfUInt8) Len() uint64 {
return l.l.Len()
}
func (l ListOfUInt8) Empty() bool {
return l.Len() == uint64(0)
}
func (l ListOfUInt8) Get(i uint64) uint8 {
return uint8(l.l.Get(i).(types.UInt8))
}
func (l ListOfUInt8) Slice(idx uint64, end uint64) ListOfUInt8 {
return ListOfUInt8{l.l.Slice(idx, end), &ref.Ref{}}
}
func (l ListOfUInt8) Set(i uint64, val uint8) ListOfUInt8 {
return ListOfUInt8{l.l.Set(i, types.UInt8(val)), &ref.Ref{}}
}
func (l ListOfUInt8) Append(v ...uint8) ListOfUInt8 {
return ListOfUInt8{l.l.Append(l.fromElemSlice(v)...), &ref.Ref{}}
}
func (l ListOfUInt8) Insert(idx uint64, v ...uint8) ListOfUInt8 {
return ListOfUInt8{l.l.Insert(idx, l.fromElemSlice(v)...), &ref.Ref{}}
}
func (l ListOfUInt8) Remove(idx uint64, end uint64) ListOfUInt8 {
return ListOfUInt8{l.l.Remove(idx, end), &ref.Ref{}}
}
func (l ListOfUInt8) RemoveAt(idx uint64) ListOfUInt8 {
return ListOfUInt8{(l.l.RemoveAt(idx)), &ref.Ref{}}
}
func (l ListOfUInt8) fromElemSlice(p []uint8) []types.Value {
r := make([]types.Value, len(p))
for i, v := range p {
r[i] = types.UInt8(v)
}
return r
}
type ListOfUInt8IterCallback func(v uint8, i uint64) (stop bool)
func (l ListOfUInt8) Iter(cb ListOfUInt8IterCallback) {
l.l.Iter(func(v types.Value, i uint64) bool {
return cb(uint8(v.(types.UInt8)), i)
})
}
type ListOfUInt8IterAllCallback func(v uint8, i uint64)
func (l ListOfUInt8) IterAll(cb ListOfUInt8IterAllCallback) {
l.l.IterAll(func(v types.Value, i uint64) {
cb(uint8(v.(types.UInt8)), i)
})
}
type ListOfUInt8FilterCallback func(v uint8, i uint64) (keep bool)
func (l ListOfUInt8) Filter(cb ListOfUInt8FilterCallback) ListOfUInt8 {
nl := NewListOfUInt8()
l.IterAll(func(v uint8, i uint64) {
if cb(v, i) {
nl = nl.Append(v)
}
})
return nl
}
@@ -357,6 +357,12 @@ func (s SetOfUInt8) IterAll(cb SetOfUInt8IterAllCallback) {
})
}
func (s SetOfUInt8) IterAllP(concurrency int, cb SetOfUInt8IterAllCallback) {
s.s.IterAllP(concurrency, func(v types.Value) {
cb(uint8(v.(types.UInt8)))
})
}
type SetOfUInt8FilterCallback func(p uint8) (keep bool)
func (s SetOfUInt8) Filter(cb SetOfUInt8FilterCallback) SetOfUInt8 {
+20
View File
@@ -1,6 +1,7 @@
package test
import (
"sync"
"testing"
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
@@ -48,6 +49,25 @@ func TestListIter(t *testing.T) {
assert.Equal(gen.ListOfInt64Def{0, 1, 2}, acc)
}
func TestListIterAllP(t *testing.T) {
assert := assert.New(t)
l := gen.ListOfInt64Def{0, 1, 2, 3, 4}
mu := sync.Mutex{}
visited := map[int64]bool{}
l.New().IterAllP(2, func(v int64, index uint64) {
assert.EqualValues(v, index)
mu.Lock()
defer mu.Unlock()
visited[v] = true
})
if assert.Len(visited, len(l)) {
for _, e := range l {
assert.True(visited[e])
}
}
}
func TestListFilter(t *testing.T) {
assert := assert.New(t)
l := gen.ListOfInt64Def{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.New()
+12
View File
@@ -252,6 +252,12 @@ func (l ListOfUInt64) IterAll(cb ListOfUInt64IterAllCallback) {
})
}
func (l ListOfUInt64) IterAllP(concurrency int, cb ListOfUInt64IterAllCallback) {
l.l.IterAllP(concurrency, func(v Value, i uint64) {
cb(uint64(v.(UInt64)), i)
})
}
type ListOfUInt64FilterCallback func(v uint64, i uint64) (keep bool)
func (l ListOfUInt64) Filter(cb ListOfUInt64FilterCallback) ListOfUInt64 {
@@ -387,6 +393,12 @@ func (l ListOfRefOfBlob) IterAll(cb ListOfRefOfBlobIterAllCallback) {
})
}
func (l ListOfRefOfBlob) IterAllP(concurrency int, cb ListOfRefOfBlobIterAllCallback) {
l.l.IterAllP(concurrency, func(v Value, i uint64) {
cb(v.(RefOfBlob), i)
})
}
type ListOfRefOfBlobFilterCallback func(v RefOfBlob, i uint64) (keep bool)
func (l ListOfRefOfBlob) Filter(cb ListOfRefOfBlobFilterCallback) ListOfRefOfBlob {
+3 -4
View File
@@ -78,16 +78,15 @@ func (l List) IterAllP(concurrency int, f listIterAllFunc) {
func (l List) iterInternal(sem chan int, lf listIterAllFunc, offset uint64) {
wg := sync.WaitGroup{}
for idx := uint64(0); idx < l.Len(); idx++ {
for idx := range l.values {
wg.Add(1)
sem <- 1
go func(idx uint64) {
defer wg.Done()
v := l.values[idx]
lf(v, idx+offset)
lf(l.values[idx], idx+offset)
<-sem
}(idx)
}(uint64(idx))
}
wg.Wait()
+2
View File
@@ -101,6 +101,8 @@ func (m Map) IterAll(cb mapIterAllCallback) {
}
}
// TODO: Implement IterAllP() BUG 573
type mapFilterCallback func(key, value Value) (keep bool)
func (m Map) Filter(cb mapFilterCallback) Map {
+35 -14
View File
@@ -1,7 +1,9 @@
package types
import (
"runtime"
"sort"
"sync"
"github.com/attic-labs/noms/ref"
)
@@ -14,6 +16,18 @@ type Set struct {
ref *ref.Ref
}
type setIterCallback func(v Value) bool
type setIterAllCallback func(v Value)
type setFilterCallback func(v Value) (keep bool)
var setTypeRef = MakeCompoundTypeRef(SetKind, MakePrimitiveTypeRef(ValueKind))
func init() {
RegisterFromValFunction(setTypeRef, func(v Value) Value {
return v.(Set)
})
}
func NewSet(v ...Value) Set {
return newSetFromData(buildSetData(setData{}, v), setTypeRef)
}
@@ -72,8 +86,6 @@ func (s Set) Subtract(others ...Set) Set {
return result
}
type setIterCallback func(v Value) bool
func (s Set) Iter(cb setIterCallback) {
for _, v := range s.data {
if cb(v) {
@@ -82,15 +94,33 @@ func (s Set) Iter(cb setIterCallback) {
}
}
type setIterAllCallback func(v Value)
func (s Set) IterAll(cb setIterAllCallback) {
for _, v := range s.data {
cb(v)
}
}
type setFilterCallback func(v Value) (keep bool)
func (s Set) IterAllP(concurrency int, f setIterAllCallback) {
if concurrency == 0 {
concurrency = runtime.NumCPU()
}
sem := make(chan int, concurrency)
wg := sync.WaitGroup{}
for idx := range s.data {
wg.Add(1)
sem <- 1
go func(idx int) {
defer wg.Done()
f(s.data[idx])
<-sem
}(idx)
}
wg.Wait()
}
func (s Set) Filter(cb setFilterCallback) Set {
data := setData{}
@@ -124,9 +154,6 @@ func (s Set) Chunks() (chunks []ref.Ref) {
}
return
}
var setTypeRef = MakeCompoundTypeRef(SetKind, MakePrimitiveTypeRef(ValueKind))
func (s Set) TypeRef() TypeRef {
return s.t
}
@@ -135,12 +162,6 @@ func (s Set) elemType() TypeRef {
return s.t.Desc.(CompoundDesc).ElemTypes[0]
}
func init() {
RegisterFromValFunction(setTypeRef, func(v Value) Value {
return v.(Set)
})
}
func newSetFromData(m setData, t TypeRef) Set {
return Set{m, t, &ref.Ref{}}
}
+56
View File
@@ -1,6 +1,8 @@
package types
import (
"runtime"
"sync"
"testing"
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
@@ -157,6 +159,60 @@ func TestSetIterAll(t *testing.T) {
assert.True(s.Equals(acc))
}
func TestSetIterAllP(t *testing.T) {
assert := assert.New(t)
testIter := func(concurrency, setLen int) {
values := make([]Value, setLen)
for i := 0; i < setLen; i++ {
values[i] = UInt64(i)
}
s := NewSet(values...)
cur := 0
mu := sync.Mutex{}
getCur := func() int {
mu.Lock()
defer mu.Unlock()
return cur
}
expectConcurreny := concurrency
if concurrency == 0 {
expectConcurreny = runtime.NumCPU()
}
visited := make([]bool, setLen)
sf := func(v Value) {
mu.Lock()
cur++
mu.Unlock()
for getCur() < expectConcurreny {
}
visited[v.(UInt64)] = true
}
if concurrency == 1 {
s.IterAll(sf)
} else {
s.IterAllP(concurrency, sf)
}
numVisited := 0
for _, visit := range visited {
if visit {
numVisited++
}
}
assert.Equal(setLen, numVisited, "IterAllP was not called with every index")
}
testIter(0, 100)
testIter(10, 1000)
testIter(1, 100000)
testIter(64, 100000)
}
func TestSetFilter(t *testing.T) {
assert := assert.New(t)