Files
dolt/nomdl/codegen/map.tmpl
Erik Arvidsson 30cc7d518f Make Equals compare by Ref (after comparing TypeRef)
This changes equal to compare by ref. Since computing the ref can be
expensive we first check that the type refs are equal.

Fixes #532
2015-10-30 16:50:49 -04:00

126 lines
3.4 KiB
Cheetah

{{$typesPackage := .TypesPackage}}
// {{.Name}}
type {{.Name}} struct {
m {{$typesPackage}}Map
ref *ref.Ref
}
func New{{.Name}}() {{.Name}} {
return {{.Name}}{ {{$typesPackage}}NewMap(), &ref.Ref{}}
}
{{if .CanUseDef}}
type {{.Name}}Def map[{{defType .KeyType}}]{{defType .ValueType}}
func (def {{.Name}}Def) New() {{.Name}} {
kv := make([]{{$typesPackage}}Value, 0, len(def)*2)
for k, v := range def {
kv = append(kv, {{defToValue "k" .KeyType}}, {{defToValue "v" .ValueType}})
}
return {{.Name}}{ {{$typesPackage}}NewMap(kv...), &ref.Ref{}}
}
func (m {{.Name}}) Def() {{.Name}}Def {
def := make(map[{{defType .KeyType}}]{{defType .ValueType}})
m.m.Iter(func(k, v {{$typesPackage}}Value) bool {
def[{{valueToDef "k" .KeyType}}] = {{valueToDef "v" .ValueType}}
return false
})
return def
}
{{end}}
func {{.Name}}FromVal(val {{$typesPackage}}Value) {{.Name}} {
// TODO: Do we still need FromVal?
if val, ok := val.({{.Name}}); ok {
return val
}
// TODO: Validate here
return {{.Name}}{val.({{$typesPackage}}Map), &ref.Ref{}}
}
func (m {{.Name}}) InternalImplementation() {{$typesPackage}}Map {
return m.m
}
func (m {{.Name}}) Equals(other {{$typesPackage}}Value) bool {
return other != nil && __typeRefFor{{.Name}}.Equals(other.TypeRef()) && m.Ref() == other.Ref()
}
func (m {{.Name}}) Ref() ref.Ref {
return {{$typesPackage}}EnsureRef(m.ref, m)
}
func (m {{.Name}}) Chunks() (chunks []ref.Ref) {
chunks = append(chunks, m.TypeRef().Chunks()...)
chunks = append(chunks, m.m.Chunks()...)
return
}
{{template "type_ref.tmpl" .}}
func (m {{.Name}}) Empty() bool {
return m.m.Empty()
}
func (m {{.Name}}) Len() uint64 {
return m.m.Len()
}
func (m {{.Name}}) Has(p {{userType .KeyType}}) bool {
return m.m.Has({{userToValue "p" .KeyType}})
}
func (m {{.Name}}) Get(p {{userType .KeyType}}) {{userType .ValueType}} {
return {{valueToUser (printf "m.m.Get(%s)" (userToValue "p" .KeyType)) .ValueType}}
}
func (m {{.Name}}) MaybeGet(p {{userType .KeyType}}) ({{userType .ValueType}}, bool) {
v, ok := m.m.MaybeGet({{userToValue "p" .KeyType}})
if !ok {
return {{userZero .ValueType}}, false
}
return {{valueToUser "v" .ValueType}}, ok
}
func (m {{.Name}}) Set(k {{userType .KeyType}}, v {{userType .ValueType}}) {{.Name}} {
return {{.Name}}{m.m.Set({{userToValue "k" .KeyType}}, {{userToValue "v" .ValueType}}), &ref.Ref{}}
}
// TODO: Implement SetM?
func (m {{.Name}}) Remove(p {{userType .KeyType}}) {{.Name}} {
return {{.Name}}{m.m.Remove({{userToValue "p" .KeyType}}), &ref.Ref{}}
}
type {{.Name}}IterCallback func(k {{userType .KeyType}}, v {{userType .ValueType}}) (stop bool)
func (m {{.Name}}) Iter(cb {{.Name}}IterCallback) {
m.m.Iter(func(k, v {{$typesPackage}}Value) bool {
return cb({{valueToUser "k" .KeyType}}, {{valueToUser "v" .ValueType}})
})
}
type {{.Name}}IterAllCallback func(k {{userType .KeyType}}, v {{userType .ValueType}})
func (m {{.Name}}) IterAll(cb {{.Name}}IterAllCallback) {
m.m.IterAll(func(k, v {{$typesPackage}}Value) {
cb({{valueToUser "k" .KeyType}}, {{valueToUser "v" .ValueType}})
})
}
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)
}
})
return nm
}