Debian Codegraph @

dstream/loadsave_gen.go

raw symbols SCIP
1 // GENERATED CODE, DO NOT EDIT
2
3 package dstream
4
5 import (
6 "encoding/gob"
7 "io"
8 "time"
9 )
10
11 // Save saves the dstream to a file. It can subsequently be reloaded with Load.
12 func Save(ds Dstream, w io.Writer) {
13
14 ds.Reset()
15
16 enc := gob.NewEncoder(w)
17
18 err := enc.Encode(ds.Names())
19 if err != nil {
20 panic(err)
21 }
22
23 first := true
24 for ds.Next() {
25
26 if first {
27 var dtypes []Dtype
28 for j := 0; j < ds.NumVar(); j++ {
29 switch ds.GetPos(j).(type) {
30 case []string:
31 dtypes = append(dtypes, String)
32 case []time.Time:
33 dtypes = append(dtypes, Time)
34 case []uint8:
35 dtypes = append(dtypes, Uint8)
36 case []uint16:
37 dtypes = append(dtypes, Uint16)
38 case []uint32:
39 dtypes = append(dtypes, Uint32)
40 case []uint64:
41 dtypes = append(dtypes, Uint64)
42 case []int8:
43 dtypes = append(dtypes, Int8)
44 case []int16:
45 dtypes = append(dtypes, Int16)
46 case []int32:
47 dtypes = append(dtypes, Int32)
48 case []int64:
49 dtypes = append(dtypes, Int64)
50 case []float32:
51 dtypes = append(dtypes, Float32)
52 case []float64:
53 dtypes = append(dtypes, Float64)
54 }
55 }
56
57 err := enc.Encode(&dtypes)
58 if err != nil {
59 panic(err)
60 }
61
62 first = false
63 }
64
65 for j := 0; j < ds.NumVar(); j++ {
66 switch x := ds.GetPos(j).(type) {
67 case []string:
68 err := enc.Encode(&x)
69 if err != nil {
70 panic(err)
71 }
72 case []time.Time:
73 err := enc.Encode(&x)
74 if err != nil {
75 panic(err)
76 }
77 case []uint8:
78 err := enc.Encode(&x)
79 if err != nil {
80 panic(err)
81 }
82 case []uint16:
83 err := enc.Encode(&x)
84 if err != nil {
85 panic(err)
86 }
87 case []uint32:
88 err := enc.Encode(&x)
89 if err != nil {
90 panic(err)
91 }
92 case []uint64:
93 err := enc.Encode(&x)
94 if err != nil {
95 panic(err)
96 }
97 case []int8:
98 err := enc.Encode(&x)
99 if err != nil {
100 panic(err)
101 }
102 case []int16:
103 err := enc.Encode(&x)
104 if err != nil {
105 panic(err)
106 }
107 case []int32:
108 err := enc.Encode(&x)
109 if err != nil {
110 panic(err)
111 }
112 case []int64:
113 err := enc.Encode(&x)
114 if err != nil {
115 panic(err)
116 }
117 case []float32:
118 err := enc.Encode(&x)
119 if err != nil {
120 panic(err)
121 }
122 case []float64:
123 err := enc.Encode(&x)
124 if err != nil {
125 panic(err)
126 }
127 }
128 }
129 }
130 }
131
132 // load is a dstream that reads its data from a file that has been constructed using Save.
133 type load struct {
134
135 // done becomes true when the whole data source has been read
136 done bool
137
138 // The variable names
139 names []string
140
141 // The number of observations, not relevant until reading is complete.
142 nobs int
143
144 // The data types of the variables
145 dtypes []Dtype
146
147 // Use this to decode gob data from the file.
148 dec *gob.Decoder
149
150 // namespos maps variable names to their column positions
151 namespos map[string]int
152
153 // This holds the data for the current chunk
154 data []interface{}
155 }
156
157 // NewLoad returns a dstream that loads data from the given file. The file must
158 // have been created using the Save function.
159 func NewLoad(r io.Reader) Dstream {
160
161 var ld load
162 ld.init(r)
163
164 return &ld
165 }
166
167 func (ld *load) init(r io.Reader) {
168
169 ld.dec = gob.NewDecoder(r)
170
171 err := ld.dec.Decode(&ld.names)
172 if err != nil {
173 panic(err)
174 }
175
176 ld.namespos = make(map[string]int)
177 for k, na := range ld.names {
178 ld.namespos[na] = k
179 }
180
181 err = ld.dec.Decode(&ld.dtypes)
182 if err != nil {
183 panic(err)
184 }
185
186 ld.data = make([]interface{}, len(ld.names))
187 }
188
189 // Reset panics since this concrete type of Dstream cannot be reset.
190 func (ld *load) Reset() {
191 panic("A dstream created using NewLoad cannot be reset.\n")
192 }
193
194 // GetPos returns the data for the variable at the given position in the current chunk.
195 func (ld *load) GetPos(j int) interface{} {
196 return ld.data[j]
197 }
198
199 // Names returns the variable names.
200 func (ld *load) Names() []string {
201 return ld.names
202 }
203
204 // NumObs returns the number of observations (data rows) in the dstream. A -1 is
205 // returned if the source has not been completely read.
206 func (ld *load) NumObs() int {
207 if !ld.done {
208 return -1
209 }
210 return ld.nobs
211 }
212
213 // Get returns the data for the given named variable in the current chunk.
214 func (ld *load) Get(na string) interface{} {
215 return ld.GetPos(ld.namespos[na])
216 }
217
218 // Close does nothing, it is here to satisfy the Dstream interface.
219 func (ld *load) Close() {
220 }
221
222 // NumVar returns the number of variables in the dstream.
223 func (ld *load) NumVar() int {
224 return len(ld.names)
225 }
226
227 // Next advances to the next chunk and returns true, or returns false if
228 // all chunks have been read.
229 func (ld *load) Next() bool {
230
231 // Loop over variables within chunks
232 for j := 0; j < ld.NumVar(); j++ {
233 switch ld.dtypes[j] {
234 case String:
235 var x []string
236 err := ld.dec.Decode(&x)
237 if err == io.EOF {
238 if j != 0 {
239 panic("File is corrupt")
240 }
241 ld.done = true
242 return false
243 } else if err != nil {
244 panic(err)
245 }
246 ld.data[j] = x
247 if j == 0 {
248 ld.nobs += len(x)
249 }
250 case Time:
251 var x []time.Time
252 err := ld.dec.Decode(&x)
253 if err == io.EOF {
254 if j != 0 {
255 panic("File is corrupt")
256 }
257 ld.done = true
258 return false
259 } else if err != nil {
260 panic(err)
261 }
262 ld.data[j] = x
263 if j == 0 {
264 ld.nobs += len(x)
265 }
266 case Uint8:
267 var x []uint8
268 err := ld.dec.Decode(&x)
269 if err == io.EOF {
270 if j != 0 {
271 panic("File is corrupt")
272 }
273 ld.done = true
274 return false
275 } else if err != nil {
276 panic(err)
277 }
278 ld.data[j] = x
279 if j == 0 {
280 ld.nobs += len(x)
281 }
282 case Uint16:
283 var x []uint16
284 err := ld.dec.Decode(&x)
285 if err == io.EOF {
286 if j != 0 {
287 panic("File is corrupt")
288 }
289 ld.done = true
290 return false
291 } else if err != nil {
292 panic(err)
293 }
294 ld.data[j] = x
295 if j == 0 {
296 ld.nobs += len(x)
297 }
298 case Uint32:
299 var x []uint32
300 err := ld.dec.Decode(&x)
301 if err == io.EOF {
302 if j != 0 {
303 panic("File is corrupt")
304 }
305 ld.done = true
306 return false
307 } else if err != nil {
308 panic(err)
309 }
310 ld.data[j] = x
311 if j == 0 {
312 ld.nobs += len(x)
313 }
314 case Uint64:
315 var x []uint64
316 err := ld.dec.Decode(&x)
317 if err == io.EOF {
318 if j != 0 {
319 panic("File is corrupt")
320 }
321 ld.done = true
322 return false
323 } else if err != nil {
324 panic(err)
325 }
326 ld.data[j] = x
327 if j == 0 {
328 ld.nobs += len(x)
329 }
330 case Int8:
331 var x []int8
332 err := ld.dec.Decode(&x)
333 if err == io.EOF {
334 if j != 0 {
335 panic("File is corrupt")
336 }
337 ld.done = true
338 return false
339 } else if err != nil {
340 panic(err)
341 }
342 ld.data[j] = x
343 if j == 0 {
344 ld.nobs += len(x)
345 }
346 case Int16:
347 var x []int16
348 err := ld.dec.Decode(&x)
349 if err == io.EOF {
350 if j != 0 {
351 panic("File is corrupt")
352 }
353 ld.done = true
354 return false
355 } else if err != nil {
356 panic(err)
357 }
358 ld.data[j] = x
359 if j == 0 {
360 ld.nobs += len(x)
361 }
362 case Int32:
363 var x []int32
364 err := ld.dec.Decode(&x)
365 if err == io.EOF {
366 if j != 0 {
367 panic("File is corrupt")
368 }
369 ld.done = true
370 return false
371 } else if err != nil {
372 panic(err)
373 }
374 ld.data[j] = x
375 if j == 0 {
376 ld.nobs += len(x)
377 }
378 case Int64:
379 var x []int64
380 err := ld.dec.Decode(&x)
381 if err == io.EOF {
382 if j != 0 {
383 panic("File is corrupt")
384 }
385 ld.done = true
386 return false
387 } else if err != nil {
388 panic(err)
389 }
390 ld.data[j] = x
391 if j == 0 {
392 ld.nobs += len(x)
393 }
394 case Float32:
395 var x []float32
396 err := ld.dec.Decode(&x)
397 if err == io.EOF {
398 if j != 0 {
399 panic("File is corrupt")
400 }
401 ld.done = true
402 return false
403 } else if err != nil {
404 panic(err)
405 }
406 ld.data[j] = x
407 if j == 0 {
408 ld.nobs += len(x)
409 }
410 case Float64:
411 var x []float64
412 err := ld.dec.Decode(&x)
413 if err == io.EOF {
414 if j != 0 {
415 panic("File is corrupt")
416 }
417 ld.done = true
418 return false
419 } else if err != nil {
420 panic(err)
421 }
422 ld.data[j] = x
423 if j == 0 {
424 ld.nobs += len(x)
425 }
426 }
427 }
428
429 return true
430 }