forked from gonum/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5pt_table.go
199 lines (170 loc) · 5.29 KB
/
h5pt_table.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright ©2017 The Gonum 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 hdf5
// #include "hdf5.h"
// #include "hdf5_hl.h"
// #include <stdint.h>
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"fmt"
"reflect"
"unsafe"
"gonum.org/v1/hdf5/cmem"
)
// Table is an hdf5 packet-table.
type Table struct {
Identifier
}
func newPacketTable(id C.hid_t) *Table {
return &Table{Identifier{id}}
}
// Close closes an open packet table.
func (t *Table) Close() error {
return t.closeWith(h5ptclose)
}
func h5ptclose(id C.hid_t) C.herr_t {
return C.H5PTclose(id)
}
// IsValid returns whether or not an indentifier points to a packet table.
func (t *Table) IsValid() bool {
return C.H5PTis_valid(t.id) >= 0
}
// ReadPackets reads a number of packets from a packet table.
func (t *Table) ReadPackets(start, nrecords int, data interface{}) error {
c_start := C.hsize_t(start)
c_nrecords := C.size_t(nrecords)
rv := reflect.Indirect(reflect.ValueOf(data))
rt := rv.Type()
c_data := unsafe.Pointer(nil)
switch rt.Kind() {
case reflect.Array:
if rv.Len() < nrecords {
panic(fmt.Errorf("not enough capacity in array (cap=%d)", rv.Len()))
}
c_data = unsafe.Pointer(rv.Index(0).UnsafeAddr())
case reflect.Slice:
if rv.Len() < nrecords {
panic(fmt.Errorf("not enough capacity in slice (cap=%d)", rv.Len()))
}
slice := (*reflect.SliceHeader)(unsafe.Pointer(rv.UnsafeAddr()))
c_data = unsafe.Pointer(slice.Data)
default:
panic(fmt.Errorf("unhandled kind (%s), need slice or array", rt.Kind()))
}
err := C.H5PTread_packets(t.id, c_start, c_nrecords, c_data)
return h5err(err)
}
// Append appends packets to the end of a packet table.
//
// Struct values must only have exported fields, otherwise Append will panic.
func (t *Table) Append(args ...interface{}) error {
if len(args) == 0 {
return fmt.Errorf("hdf5: no arguments passed to packet table append.")
}
var enc cmem.Encoder
for _, arg := range args {
if err := enc.Encode(arg); err != nil {
return err
}
}
if len(enc.Buf) <= 0 {
return fmt.Errorf("hdf5: invalid empty buffer")
}
return h5err(C.H5PTappend(t.id, C.size_t(len(args)), unsafe.Pointer(&enc.Buf[0])))
}
// Next reads packets from a packet table starting at the current index into the value pointed at by data.
// i.e. data is a pointer to an array or a slice.
func (t *Table) Next(data interface{}) error {
rt := reflect.TypeOf(data)
if rt.Kind() != reflect.Ptr {
return fmt.Errorf("hdf5: invalid value type. got=%v, want pointer", rt.Kind())
}
rt = rt.Elem()
rv := reflect.Indirect(reflect.ValueOf(data))
n := C.size_t(0)
cdata := unsafe.Pointer(nil)
switch rt.Kind() {
case reflect.Array:
if rv.Cap() <= 0 {
panic(fmt.Errorf("not enough capacity in array (cap=%d)", rv.Cap()))
}
cdata = unsafe.Pointer(rv.UnsafeAddr())
n = C.size_t(rv.Cap())
case reflect.Slice:
if rv.Cap() <= 0 {
panic(fmt.Errorf("not enough capacity in slice (cap=%d)", rv.Cap()))
}
slice := (*reflect.SliceHeader)(unsafe.Pointer(rv.UnsafeAddr()))
cdata = unsafe.Pointer(slice.Data)
n = C.size_t(rv.Cap())
default:
panic(fmt.Errorf("unsupported kind (%s), need slice or array", rt.Kind()))
}
err := C.H5PTget_next(t.id, n, cdata)
return h5err(err)
}
// NumPackets returns the number of packets in a packet table.
func (t *Table) NumPackets() (int, error) {
c_nrecords := C.hsize_t(0)
err := C.H5PTget_num_packets(t.id, &c_nrecords)
return int(c_nrecords), h5err(err)
}
// CreateIndex resets a packet table's index to the first packet.
func (t *Table) CreateIndex() error {
err := C.H5PTcreate_index(t.id)
return h5err(err)
}
// SetIndex sets a packet table's index.
func (t *Table) SetIndex(index int) error {
c_idx := C.hsize_t(index)
err := C.H5PTset_index(t.id, c_idx)
return h5err(err)
}
// Type returns an identifier for a copy of the datatype for a dataset. The returned
// datatype must be closed by the user when it is no longer needed.
func (t *Table) Type() (*Datatype, error) {
hid := C.H5Dget_type(t.id)
if err := checkID(hid); err != nil {
return nil, err
}
return NewDatatype(hid), nil
}
func createTable(id C.hid_t, name string, dtype *Datatype, chunkSize, compression int) (*Table, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
chunk := C.hsize_t(chunkSize)
compr := C.int(compression)
hid := C.H5PTcreate_fl(id, c_name, dtype.id, chunk, compr)
if err := checkID(hid); err != nil {
return nil, err
}
return newPacketTable(hid), nil
}
func createTableFrom(id C.hid_t, name string, dtype interface{}, chunkSize, compression int) (*Table, error) {
var err error
switch dt := dtype.(type) {
case reflect.Type:
if hdfDtype, err := NewDataTypeFromType(dt); err == nil {
return createTable(id, name, hdfDtype, chunkSize, compression)
}
case *Datatype:
return createTable(id, name, dt, chunkSize, compression)
default:
if hdfDtype, err := NewDataTypeFromType(reflect.TypeOf(dtype)); err == nil {
return createTable(id, name, hdfDtype, chunkSize, compression)
}
}
return nil, err
}
func openTable(id C.hid_t, name string) (*Table, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
hid := C.H5PTopen(id, c_name)
if err := checkID(hid); err != nil {
return nil, err
}
return newPacketTable(hid), nil
}