package dstream import ( "fmt" "time" ) {{- range . }} // Resize returns a slice of the given type having size n, // re-using the provided slice if it is big enough. func resize{{ .Utype }}(x []{{ .Type }}, n int) []{{ .Type }} { if cap(x) < n { x = make([]{{ .Type }}, n) } return x[0:n] } {{ end }} // VarTypes returns a map relating each variable by name to its corresponding // data type. The dstream should be readable before calling (Reset and call // Next if needed). func VarTypes(d Dstream) map[string]string { types := make(map[string]string) for k, na := range d.Names() { v := d.GetPos(k) switch v.(type) { {{- range . }} case []{{ .Type }}: types[na] = "{{ .Type }}" {{- end }} default: types[na] = "unknown type" } } return types } func ilen(x interface{}) int { switch x := x.(type) { {{- range . }} case []{{ .Type }}: return len(x) {{- end }} case nil: return 0 default: msg := fmt.Sprintf("unknown type: %T", x) panic(msg) } } func truncate(z []interface{}) { for j, x := range z { if x != nil { switch x := x.(type) { {{- range . }} case []{{ .Type }}: z[j] = x[0:0] {{- end }} default: msg := fmt.Sprintf("unknown type %T", x) panic(msg) } } } } // GetCol returns a copy of the data for one variable. The data // are returned as a slice. The column is returned starting with the // current chunk, call Reset to ensure that the column is extracted // from the first chunk. func GetCol(da Dstream, na string) interface{} { vn := da.Names() for j, x := range vn { if na == x { return GetColPos(da, j) } } panic(fmt.Sprintf("GetCol: variable '%s' not found", na)) return nil } // GetColPos returns a copy of the data for one variable. // The data are returned as a slice, which is a coy of the // underlying data. func GetColPos(da Dstream, j int) interface{} { da.Reset() if !da.Next() { return nil } // Get the first chunk so that we have the data type. v := da.GetPos(j) switch v := v.(type) { {{- range . }} case []{{ .Type }}: var x []{{ .Type }} x = append(x, v...) for da.Next() { y := da.GetPos(j).([]{{ .Type }}) x = append(x, y...) } return x {{- end }} case nil: return nil default: msg := fmt.Sprintf("GetCol: unknown data type %T.\n", v) panic(msg) } } // equalSlice returns true if x and y are slices of the same type and // hold equal values. func equalSlice(x, y interface{}) bool { switch u := x.(type) { {{- range . }} case []{{ .Type }}: v, ok := y.([]{{ .Type }}) if !ok { return false } if len(u) != len(v) { return false } for i := range u { if u[i] != v[i] { return false } } {{- end }} default: panic("unkown type") } return true }