-
Notifications
You must be signed in to change notification settings - Fork 149
/
ints.go
81 lines (71 loc) · 2.33 KB
/
ints.go
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
// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hb
import "github.com/cpmech/gosl/chk"
// PutInts puts a slice of integers into file
// Input:
// path -- HDF5 path such as "/myvec" or "/group/myvec"
// v -- slice of integers
func (o *File) PutInts(path string, v []int) {
if len(v) < 1 {
chk.Panic("cannot put empty slice in HDF file. path = %q", path)
}
o.putInts(path, []int{len(v)}, v)
}
// GetInts gets a slice of ints from file. Memory will be allocated
func (o *File) GetInts(path string) (v []int) {
_, v = o.getInts(path, false, false)
return
}
// PutInt puts one integer into file
// Input:
// path -- HDF5 path such as "/myvec" or "/group/myvec"
// val -- value
// Note: this is a convenience function wrapping PutInts
func (o *File) PutInt(path string, val int) {
o.putInts(path, []int{1}, []int{val})
}
// GetInt gets one integer from file
// Note: this is a convenience function wrapping GetInts
func (o *File) GetInt(path string) int {
_, v := o.getInts(path, true, false)
if len(v) != 1 {
chk.Panic("failed to get ONE integer\n")
}
return v[0]
}
// auxiliary methods ///////////////////////////////////////////////////////////////////////////
// putInts puts an array of integers into file
func (o *File) putInts(path string, dims []int, dat []int) {
if o.gobReading {
chk.Panic("cannot put %q because file is open for READONLY", path)
}
o.gobEnc.Encode("putInts")
o.gobEnc.Encode(path)
o.gobEnc.Encode(len(dims))
o.gobEnc.Encode(dims)
o.gobEnc.Encode(dat)
}
// putIntsNoGroup puts integers into file without creating groups
func (o *File) putIntsNoGroup(path string, dat []int) {
o.putInts(path, []int{len(dat)}, dat)
}
// getInts gets an array of integers from file
func (o *File) getInts(path string, isScalar, isMatrix bool) (dims, dat []int) {
var cmd string
o.gobDec.Decode(&cmd)
if cmd != "putInts" {
chk.Panic("wrong command => %q\n(r/w commands need to be called in the same order)", cmd)
}
var rpath string
o.gobDec.Decode(&rpath)
if rpath != path {
chk.Panic("cannot read path: %s != %s\n(r/w commands need to be called in the same order)", path, rpath)
}
var length int
_, dims, length = o.deGobRnkDims()
dat = make([]int, length)
o.gobDec.Decode(&dat)
return
}