forked from cpmech/gosl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ints.go
160 lines (141 loc) · 4.07 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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 h5
/*
#include "hdf5.h"
#include "hdf5_hl.h"
#include "stdlib.h"
// constants from H5Tpublic.h
#ifdef WIN32
hid_t H5LONG() { return H5T_NATIVE_LLONG; }
#else
hid_t H5LONG() { return H5T_NATIVE_LONG; }
#endif
*/
import "C"
import (
"unsafe"
"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) {
// GOB
if o.useGob {
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)
return
}
// HDF5
rnk := C.int(len(dims))
o.hierarchCreate(path, func(cp *C.char) C.herr_t {
return C.H5LTmake_dataset(o.hdfHandle, cp, rnk, (*C.hsize_t)(unsafe.Pointer(&dims[0])), C.H5LONG(), unsafe.Pointer(&dat[0]))
})
}
// putIntsNoGroup puts integers into file without creating groups
func (o *File) putIntsNoGroup(path string, dat []int) {
// GOB
if o.useGob {
o.putInts(path, []int{len(dat)}, dat)
return
}
// HDF5
cpth := C.CString(path)
defer C.free(unsafe.Pointer(cpth))
dims := []int{len(dat)}
st := C.H5LTmake_dataset(o.hdfHandle, cpth, 1, (*C.hsize_t)(unsafe.Pointer(&dims[0])), C.H5LONG(), unsafe.Pointer(&dat[0]))
if st < 0 {
chk.Panic("cannot put int array with path=%q in file <%s>", path, o.furl)
}
}
// getInts gets an array of integers from file
func (o *File) getInts(path string, isScalar, isMatrix bool) (dims, dat []int) {
// GOB
if o.useGob {
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
}
// HDF5
o.filterPath(path)
cpth := C.CString(path)
defer C.free(unsafe.Pointer(cpth))
rank := 1
if isMatrix {
rank = 2
}
dims = make([]int, rank)
st := C.H5LTget_dataset_info(o.hdfHandle, cpth, (*C.hsize_t)(unsafe.Pointer(&dims[0])), nil, nil)
if st < 0 {
chk.Panic("cannot read dimensions with path=%q and file <%s>", path, o.furl)
}
if len(dims) != rank {
chk.Panic("size of dims=%d is incorrectly read: %d != %d. path=%q. file <%s>", dims, len(dims), rank, path, o.furl)
}
if isMatrix {
dat = make([]int, dims[0]*dims[1])
} else {
if isScalar {
if dims[0] == 0 { // TODO: check why Matlab/Octave scalars set this to zero
dims[0] = 1
}
}
dat = make([]int, dims[0])
}
st = C.H5LTread_dataset(o.hdfHandle, cpth, C.H5LONG(), unsafe.Pointer(&dat[0]))
if st < 0 {
chk.Panic("cannot read dataset with path=%q in file=<%s>", path, o.furl)
}
return
}