Skip to content

Commit c8eb571

Browse files
committed
wire up chr
1 parent 7a89b14 commit c8eb571

File tree

3 files changed

+99
-11
lines changed

3 files changed

+99
-11
lines changed

r/src/vctr_builder.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "vctr_builder.h"
3131
#include "vctr_builder_base.h"
32+
#include "vctr_builder_chr.h"
3233
#include "vctr_builder_dbl.h"
3334
#include "vctr_builder_int.h"
3435
#include "vctr_builder_int64.h"

r/src/vctr_builder_chr.h

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#ifndef R_NANOARROW_VCTR_BUILDER_CHR_H_INCLUDED
19+
#define R_NANOARROW_VCTR_BUILDER_CHR_H_INCLUDED
20+
21+
#define R_NO_REMAP
22+
#include <R.h>
23+
#include <Rinternals.h>
24+
25+
#include <inttypes.h>
26+
#include <stdint.h>
27+
28+
#include "vctr_builder_base.h"
29+
30+
class ChrBuilder : public VctrBuilder {
31+
public:
32+
explicit ChrBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_CHR, ptype_sexp) {}
33+
34+
SEXP GetPtype() override { return Rf_allocVector(STRSXP, 0); }
35+
36+
ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override {
37+
NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error));
38+
SEXP value = PROTECT(Rf_allocVector(STRSXP, n));
39+
SetValue(value);
40+
UNPROTECT(1);
41+
return NANOARROW_OK;
42+
}
43+
44+
ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override {
45+
NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error));
46+
R_xlen_t length = array_view_.length;
47+
48+
switch (array_view_.storage_type) {
49+
case NANOARROW_TYPE_NA:
50+
for (R_xlen_t i = 0; i < length; i++) {
51+
SET_STRING_ELT(value_, value_size_ + i, NA_STRING);
52+
}
53+
return NANOARROW_OK;
54+
55+
case NANOARROW_TYPE_INT8:
56+
case NANOARROW_TYPE_UINT8:
57+
case NANOARROW_TYPE_INT16:
58+
case NANOARROW_TYPE_UINT16:
59+
case NANOARROW_TYPE_INT32:
60+
case NANOARROW_TYPE_UINT32:
61+
case NANOARROW_TYPE_INT64: {
62+
char buf[64];
63+
for (R_xlen_t i = 0; i < length; i++) {
64+
if (ArrowArrayViewIsNull(&array_view_, i)) {
65+
SET_STRING_ELT(value_, value_size_ + i, NA_STRING);
66+
} else {
67+
int n_chars = snprintf(buf, sizeof(buf), "%" PRId64,
68+
ArrowArrayViewGetIntUnsafe(&array_view_, i));
69+
SET_STRING_ELT(value_, value_size_ + i,
70+
Rf_mkCharLenCE(buf, n_chars, CE_UTF8));
71+
}
72+
}
73+
return NANOARROW_OK;
74+
}
75+
76+
case NANOARROW_TYPE_STRING:
77+
case NANOARROW_TYPE_LARGE_STRING: {
78+
struct ArrowStringView item;
79+
for (R_xlen_t i = 0; i < length; i++) {
80+
if (ArrowArrayViewIsNull(&array_view_, i)) {
81+
SET_STRING_ELT(value_, value_size_ + i, NA_STRING);
82+
} else {
83+
item = ArrowArrayViewGetStringUnsafe(&array_view_, i);
84+
SET_STRING_ELT(value_, value_size_ + i,
85+
Rf_mkCharLenCE(item.data, (int)item.size_bytes, CE_UTF8));
86+
}
87+
}
88+
89+
return NANOARROW_OK;
90+
}
91+
92+
default:
93+
return ENOTSUP;
94+
}
95+
}
96+
};
97+
98+
#endif

r/src/vctr_builder_primitive.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424

2525
#include "vctr_builder_base.h"
2626

27-
class ChrBuilder : public VctrBuilder {
28-
public:
29-
explicit ChrBuilder(SEXP ptype_sexp)
30-
: VctrBuilder(VECTOR_TYPE_CHR, ptype_sexp),
31-
use_altrep_(VCTR_BUILDER_USE_ALTREP_DEFAULT) {}
32-
33-
SEXP GetPtype() override { return Rf_allocVector(STRSXP, 0); }
34-
35-
VctrBuilderUseAltrep use_altrep_;
36-
};
37-
3827
class BlobBuilder : public VctrBuilder {
3928
public:
4029
explicit BlobBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_BLOB, ptype_sexp) {}

0 commit comments

Comments
 (0)