Debian Codegraph @

dstream/gen/utils.tmpl

raw
1 package dstream
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 {{- range . }}
9 // Resize returns a slice of the given type having size n,
10 // re-using the provided slice if it is big enough.
11 func resize{{ .Utype }}(x []{{ .Type }}, n int) []{{ .Type }} {
12 if cap(x) < n {
13 x = make([]{{ .Type }}, n)
14 }
15 return x[0:n]
16 }
17 {{ end }}
18
19 // VarTypes returns a map relating each variable by name to its corresponding
20 // data type. The dstream should be readable before calling (Reset and call
21 // Next if needed).
22 func VarTypes(d Dstream) map[string]string {
23 types := make(map[string]string)
24 for k, na := range d.Names() {
25 v := d.GetPos(k)
26 switch v.(type) {
27 {{- range . }}
28 case []{{ .Type }}:
29 types[na] = "{{ .Type }}"
30 {{- end }}
31 default:
32 types[na] = "unknown type"
33 }
34 }
35 return types
36 }
37
38 func ilen(x interface{}) int {
39 switch x := x.(type) {
40 {{- range . }}
41 case []{{ .Type }}:
42 return len(x)
43 {{- end }}
44 case nil:
45 return 0
46 default:
47 msg := fmt.Sprintf("unknown type: %T", x)
48 panic(msg)
49 }
50 }
51
52 func truncate(z []interface{}) {
53 for j, x := range z {
54 if x != nil {
55 switch x := x.(type) {
56 {{- range . }}
57 case []{{ .Type }}:
58 z[j] = x[0:0]
59 {{- end }}
60 default:
61 msg := fmt.Sprintf("unknown type %T", x)
62 panic(msg)
63 }
64 }
65 }
66 }
67
68 // GetCol returns a copy of the data for one variable. The data
69 // are returned as a slice. The column is returned starting with the
70 // current chunk, call Reset to ensure that the column is extracted
71 // from the first chunk.
72 func GetCol(da Dstream, na string) interface{} {
73
74 vn := da.Names()
75 for j, x := range vn {
76 if na == x {
77 return GetColPos(da, j)
78 }
79 }
80 panic(fmt.Sprintf("GetCol: variable '%s' not found", na))
81 return nil
82 }
83
84 // GetColPos returns a copy of the data for one variable.
85 // The data are returned as a slice, which is a coy of the
86 // underlying data.
87 func GetColPos(da Dstream, j int) interface{} {
88
89 da.Reset()
90
91 if !da.Next() {
92 return nil
93 }
94
95 // Get the first chunk so that we have the data type.
96 v := da.GetPos(j)
97
98 switch v := v.(type) {
99 {{- range . }}
100 case []{{ .Type }}:
101 var x []{{ .Type }}
102 x = append(x, v...)
103 for da.Next() {
104 y := da.GetPos(j).([]{{ .Type }})
105 x = append(x, y...)
106 }
107 return x
108 {{- end }}
109 case nil:
110 return nil
111 default:
112 msg := fmt.Sprintf("GetCol: unknown data type %T.\n", v)
113 panic(msg)
114 }
115 }
116
117 // equalSlice returns true if x and y are slices of the same type and
118 // hold equal values.
119 func equalSlice(x, y interface{}) bool {
120
121 switch u := x.(type) {
122 {{- range . }}
123 case []{{ .Type }}:
124 v, ok := y.([]{{ .Type }})
125 if !ok {
126 return false
127 }
128 if len(u) != len(v) {
129 return false
130 }
131 for i := range u {
132 if u[i] != v[i] {
133 return false
134 }
135 }
136 {{- end }}
137 default:
138 panic("unkown type")
139 }
140
141 return true
142 }