mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-30 10:45:18 -06:00
Also added IterAllP() to types.Set so it could be used by generated set code. Towards #564
152 lines
3.8 KiB
Cheetah
152 lines
3.8 KiB
Cheetah
{{$typesPackage := .TypesPackage}}
|
|
|
|
// {{.Name}}
|
|
|
|
type {{.Name}} struct {
|
|
s {{$typesPackage}}Set
|
|
ref *ref.Ref
|
|
}
|
|
|
|
func New{{.Name}}() {{.Name}} {
|
|
return {{.Name}}{ {{$typesPackage}}NewSet(), &ref.Ref{}}
|
|
}
|
|
|
|
{{if .CanUseDef}}
|
|
type {{.Name}}Def map[{{defType .ElemType}}]bool
|
|
|
|
func (def {{.Name}}Def) New() {{.Name}} {
|
|
l := make([]{{$typesPackage}}Value, len(def))
|
|
i := 0
|
|
for d, _ := range def {
|
|
l[i] = {{defToValue "d" .ElemType}}
|
|
i++
|
|
}
|
|
return {{.Name}}{ {{$typesPackage}}NewSet(l...), &ref.Ref{}}
|
|
}
|
|
|
|
func (s {{.Name}}) Def() {{.Name}}Def {
|
|
def := make(map[{{defType .ElemType}}]bool, s.Len())
|
|
s.s.Iter(func(v {{$typesPackage}}Value) bool {
|
|
def[{{valueToDef "v" .ElemType}}] = true
|
|
return false
|
|
})
|
|
return def
|
|
}
|
|
{{end}}
|
|
|
|
func (s {{.Name}}) Equals(other {{$typesPackage}}Value) bool {
|
|
return other != nil && __typeRefFor{{.Name}}.Equals(other.TypeRef()) && s.Ref() == other.Ref()
|
|
}
|
|
|
|
func (s {{.Name}}) Ref() ref.Ref {
|
|
return {{$typesPackage}}EnsureRef(s.ref, s)
|
|
}
|
|
|
|
func (s {{.Name}}) Chunks() (chunks []ref.Ref) {
|
|
chunks = append(chunks, s.TypeRef().Chunks()...)
|
|
chunks = append(chunks, s.s.Chunks()...)
|
|
return
|
|
}
|
|
|
|
// A Noms Value that describes {{.Name}}.
|
|
var __typeRefFor{{.Name}} {{$typesPackage}}TypeRef
|
|
|
|
func (m {{.Name}}) TypeRef() {{$typesPackage}}TypeRef {
|
|
return __typeRefFor{{.Name}}
|
|
}
|
|
|
|
func init() {
|
|
__typeRefFor{{.Name}} = {{toTypesTypeRef .Type .FileID .PackageName}}
|
|
{{$typesPackage}}RegisterValue(__typeRefFor{{.Name}}, builderFor{{.Name}}, readerFor{{.Name}})
|
|
}
|
|
|
|
func builderFor{{.Name}}(v {{$typesPackage}}Value) {{$typesPackage}}Value {
|
|
return {{.Name}}{v.({{$typesPackage}}Set), &ref.Ref{}}
|
|
}
|
|
|
|
func readerFor{{.Name}}(v {{$typesPackage}}Value) {{$typesPackage}}Value {
|
|
return v.({{.Name}}).s
|
|
}
|
|
|
|
func (s {{.Name}}) Empty() bool {
|
|
return s.s.Empty()
|
|
}
|
|
|
|
func (s {{.Name}}) Len() uint64 {
|
|
return s.s.Len()
|
|
}
|
|
|
|
func (s {{.Name}}) Has(p {{userType .ElemType}}) bool {
|
|
return s.s.Has({{userToValue "p" .ElemType}})
|
|
}
|
|
|
|
type {{.Name}}IterCallback func(p {{userType .ElemType}}) (stop bool)
|
|
|
|
func (s {{.Name}}) Iter(cb {{.Name}}IterCallback) {
|
|
s.s.Iter(func(v {{$typesPackage}}Value) bool {
|
|
return cb({{valueToUser "v" .ElemType}})
|
|
})
|
|
}
|
|
|
|
type {{.Name}}IterAllCallback func(p {{userType .ElemType}})
|
|
|
|
func (s {{.Name}}) IterAll(cb {{.Name}}IterAllCallback) {
|
|
s.s.IterAll(func(v {{$typesPackage}}Value) {
|
|
cb({{valueToUser "v" .ElemType}})
|
|
})
|
|
}
|
|
|
|
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}} {
|
|
ns := New{{.Name}}()
|
|
s.IterAll(func(v {{userType .ElemType}}) {
|
|
if cb(v) {
|
|
ns = ns.Insert(v)
|
|
}
|
|
})
|
|
return ns
|
|
}
|
|
|
|
func (s {{.Name}}) Insert(p ...{{userType .ElemType}}) {{.Name}} {
|
|
return {{.Name}}{s.s.Insert(s.fromElemSlice(p)...), &ref.Ref{}}
|
|
}
|
|
|
|
func (s {{.Name}}) Remove(p ...{{userType .ElemType}}) {{.Name}} {
|
|
return {{.Name}}{s.s.Remove(s.fromElemSlice(p)...), &ref.Ref{}}
|
|
}
|
|
|
|
func (s {{.Name}}) Union(others ...{{.Name}}) {{.Name}} {
|
|
return {{.Name}}{s.s.Union(s.fromStructSlice(others)...), &ref.Ref{}}
|
|
}
|
|
|
|
func (s {{.Name}}) Subtract(others ...{{.Name}}) {{.Name}} {
|
|
return {{.Name}}{s.s.Subtract(s.fromStructSlice(others)...), &ref.Ref{}}
|
|
}
|
|
|
|
func (s {{.Name}}) Any() {{userType .ElemType}} {
|
|
return {{valueToUser "s.s.Any()" .ElemType}}
|
|
}
|
|
|
|
func (s {{.Name}}) fromStructSlice(p []{{.Name}}) []{{$typesPackage}}Set {
|
|
r := make([]{{$typesPackage}}Set, len(p))
|
|
for i, v := range p {
|
|
r[i] = v.s
|
|
}
|
|
return r
|
|
}
|
|
|
|
func (s {{.Name}}) fromElemSlice(p []{{userType .ElemType}}) []{{$typesPackage}}Value {
|
|
r := make([]{{$typesPackage}}Value, len(p))
|
|
for i, v := range p {
|
|
r[i] = {{userToValue "v" .ElemType}}
|
|
}
|
|
return r
|
|
}
|