Skip to content

Commit 73f6ec5

Browse files
committed
Set dims in C code
1 parent 657c3b4 commit 73f6ec5

2 files changed

Lines changed: 82 additions & 53 deletions

File tree

R/read_npy.R

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -227,60 +227,60 @@ convert_bytes_to_array <- function(bytes, what, shape, size, endian) {
227227
for (j in seq_len(n_records)) {
228228
res[[j]] <- lapply(res_fields, `[[`, j)
229229
}
230-
} else {
231-
if (is.na(endian)) {
232-
endian <- .Platform$endian
233-
}
234-
# FIXME: optimize this
235-
if (what != "unicode" && endian != .Platform$endian) {
236-
ind <- rep_len(rev(seq_len(size)), length(bytes)) +
237-
(seq_along(bytes) - 1L) %/% size * size
238-
bytes <- bytes[ind]
239-
}
240-
res <- switch(
241-
what,
242-
float = .Call(
243-
C_type_convert_float,
244-
bytes,
245-
size,
246-
PACKAGE = "grumpy"
247-
),
248-
int = .Call(
249-
C_type_convert_int,
250-
bytes,
251-
size,
252-
PACKAGE = "grumpy"
253-
),
254-
uint = .Call(
255-
C_type_convert_uint,
256-
bytes,
257-
size,
258-
PACKAGE = "grumpy"
259-
),
260-
bool = .Call(
261-
C_type_convert_bool,
262-
bytes,
263-
size,
264-
PACKAGE = "grumpy"
265-
),
266-
string = .Call(
267-
C_type_convert_string,
268-
bytes,
269-
size,
270-
PACKAGE = "grumpy"
271-
),
272-
unicode = .Call(
273-
C_type_convert_unicode,
274-
bytes,
275-
size,
276-
endian,
277-
PACKAGE = "grumpy"
278-
),
279-
stop("Unsupported data type: ", what, call. = FALSE)
280-
)
230+
dim(res) <- shape
231+
return(res)
281232
}
282233

283-
dim(res) <- shape
234+
if (is.na(endian)) {
235+
endian <- .Platform$endian
236+
}
237+
# FIXME: optimize this
238+
if (what != "unicode" && endian != .Platform$endian) {
239+
ind <- rep_len(rev(seq_len(size)), length(bytes)) +
240+
(seq_along(bytes) - 1L) %/% size * size
241+
bytes <- bytes[ind]
242+
}
243+
res <- switch(
244+
what,
245+
float = .Call(
246+
C_type_convert_float,
247+
bytes,
248+
size,
249+
PACKAGE = "grumpy"
250+
),
251+
int = .Call(
252+
C_type_convert_int,
253+
bytes,
254+
size,
255+
PACKAGE = "grumpy"
256+
),
257+
uint = .Call(
258+
C_type_convert_uint,
259+
bytes,
260+
size,
261+
PACKAGE = "grumpy"
262+
),
263+
bool = .Call(
264+
C_type_convert_bool,
265+
bytes,
266+
size,
267+
PACKAGE = "grumpy"
268+
),
269+
string = .Call(
270+
C_type_convert_string,
271+
bytes,
272+
size,
273+
PACKAGE = "grumpy"
274+
),
275+
unicode = .Call(
276+
C_type_convert_unicode,
277+
bytes,
278+
size,
279+
endian,
280+
PACKAGE = "grumpy"
281+
),
282+
stop("Unsupported data type: ", what, call. = FALSE)
283+
)
284284

285285
return(res)
286286
}

src/type_conversion.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ SEXP type_convert_int(SEXP input, SEXP _n_bytes) {
3535
}
3636
}
3737

38+
/* Set dim attribute if chunk_dim is not NULL / NA */
39+
if (!isNull(chunk_dim) && xlength(chunk_dim) > 0) {
40+
setAttrib(data, R_DimSymbol, chunk_dim);
41+
}
3842
UNPROTECT(1);
3943
return(data);
4044
}
@@ -73,6 +77,11 @@ SEXP type_convert_uint(SEXP input, SEXP _n_bytes) {
7377
}
7478
}
7579

80+
/* Set dim attribute if chunk_dim is not NULL / NA */
81+
if (!isNull(chunk_dim) && xlength(chunk_dim) > 0) {
82+
setAttrib(data, R_DimSymbol, chunk_dim);
83+
}
84+
7685
UNPROTECT(1);
7786
return(data);
7887
}
@@ -111,6 +120,11 @@ SEXP type_convert_float(SEXP input, SEXP _n_bytes){
111120
error("%d byte floating point values are not currently supported\n", n_bytes);
112121
}
113122

123+
/* Set dim attribute if chunk_dim is not NULL / NA */
124+
if (!isNull(chunk_dim) && xlength(chunk_dim) > 0) {
125+
setAttrib(data, R_DimSymbol, chunk_dim);
126+
}
127+
114128
UNPROTECT(1);
115129
return(data);
116130
}
@@ -133,6 +147,11 @@ SEXP type_convert_bool(SEXP input, SEXP _n_bytes) {
133147
p_data[i] = ((const int8_t *)raw_buffer)[i];
134148
}
135149

150+
/* Set dim attribute if chunk_dim is not NULL / NA */
151+
if (!isNull(chunk_dim) && xlength(chunk_dim) > 0) {
152+
setAttrib(data, R_DimSymbol, chunk_dim);
153+
}
154+
136155
UNPROTECT(1);
137156
return(data);
138157
}
@@ -171,6 +190,11 @@ SEXP type_convert_string(SEXP input, SEXP _n_bytes) {
171190
SET_STRING_ELT(data, i, mkCharCE(field, CE_BYTES));
172191
}
173192

193+
/* Set dim attribute if chunk_dim is not NULL / NA */
194+
if (!isNull(chunk_dim) && xlength(chunk_dim) > 0) {
195+
setAttrib(data, R_DimSymbol, chunk_dim);
196+
}
197+
174198
UNPROTECT(1);
175199
return(data);
176200
}
@@ -232,8 +256,13 @@ SEXP type_convert_unicode(SEXP input, SEXP _n_bytes, SEXP _endian) {
232256

233257
SET_STRING_ELT(data, i, mkCharCE(utf8_buf, CE_UTF8));
234258
}
235-
236259
Riconv_close(cd);
260+
261+
/* Set dim attribute if chunk_dim is not NULL / NA */
262+
if (!isNull(chunk_dim) && xlength(chunk_dim) > 0) {
263+
setAttrib(data, R_DimSymbol, chunk_dim);
264+
}
265+
237266
UNPROTECT(1);
238267
return(data);
239268
}

0 commit comments

Comments
 (0)