File: utils.tmpl

package info (click to toggle)
golang-github-kshedden-dstream 0.0~git20190512.c4c4106-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 596 kB
  • sloc: makefile: 30
file content (142 lines) | stat: -rw-r--r-- 3,283 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
}