|
| 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_LGL_H_INCLUDED |
| 19 | +#define R_NANOARROW_VCTR_BUILDER_LGL_H_INCLUDED |
| 20 | + |
| 21 | +#define R_NO_REMAP |
| 22 | +#include <R.h> |
| 23 | +#include <Rinternals.h> |
| 24 | + |
| 25 | +#include "vctr_builder_base.h" |
| 26 | + |
| 27 | +class LglBuilder : public VctrBuilder { |
| 28 | + public: |
| 29 | + explicit LglBuilder(SEXP ptype_sexp) : VctrBuilder(VECTOR_TYPE_LGL, ptype_sexp) {} |
| 30 | + |
| 31 | + SEXP GetPtype() override { return Rf_allocVector(LGLSXP, 0); } |
| 32 | + |
| 33 | + ArrowErrorCode Reserve(R_xlen_t n, ArrowError* error) override { |
| 34 | + NANOARROW_RETURN_NOT_OK(VctrBuilder::Reserve(n, error)); |
| 35 | + SEXP value = PROTECT(Rf_allocVector(LGLSXP, n)); |
| 36 | + SetValue(value); |
| 37 | + UNPROTECT(1); |
| 38 | + return NANOARROW_OK; |
| 39 | + } |
| 40 | + |
| 41 | + ArrowErrorCode PushNext(const ArrowArray* array, ArrowError* error) override { |
| 42 | + NANOARROW_RETURN_NOT_OK(VctrBuilder::PushNext(array, error)); |
| 43 | + |
| 44 | + // True for all the types supported here |
| 45 | + const uint8_t* is_valid = array_view_.buffer_views[0].data.as_uint8; |
| 46 | + const uint8_t* data_buffer = array_view_.buffer_views[1].data.as_uint8; |
| 47 | + |
| 48 | + int64_t raw_src_offset = array_view_.offset; |
| 49 | + R_xlen_t length = array->length; |
| 50 | + int* result = LOGICAL(value_); |
| 51 | + |
| 52 | + // Fill the buffer |
| 53 | + switch (array_view_.storage_type) { |
| 54 | + case NANOARROW_TYPE_NA: |
| 55 | + for (R_xlen_t i = 0; i < length; i++) { |
| 56 | + result[value_size_ + i] = NA_LOGICAL; |
| 57 | + } |
| 58 | + break; |
| 59 | + case NANOARROW_TYPE_BOOL: |
| 60 | + ArrowBitsUnpackInt32(data_buffer, raw_src_offset, length, result + value_size_); |
| 61 | + |
| 62 | + // Set any nulls to NA_LOGICAL |
| 63 | + if (is_valid != NULL && array_view_.null_count != 0) { |
| 64 | + for (R_xlen_t i = 0; i < length; i++) { |
| 65 | + if (!ArrowBitGet(is_valid, raw_src_offset + i)) { |
| 66 | + result[value_size_ + i] = NA_LOGICAL; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + break; |
| 71 | + case NANOARROW_TYPE_INT8: |
| 72 | + case NANOARROW_TYPE_UINT8: |
| 73 | + case NANOARROW_TYPE_INT16: |
| 74 | + case NANOARROW_TYPE_UINT16: |
| 75 | + case NANOARROW_TYPE_INT32: |
| 76 | + case NANOARROW_TYPE_UINT32: |
| 77 | + case NANOARROW_TYPE_INT64: |
| 78 | + case NANOARROW_TYPE_UINT64: |
| 79 | + case NANOARROW_TYPE_FLOAT: |
| 80 | + case NANOARROW_TYPE_DOUBLE: |
| 81 | + for (R_xlen_t i = 0; i < length; i++) { |
| 82 | + result[value_size_ + i] = ArrowArrayViewGetIntUnsafe(&array_view_, i) != 0; |
| 83 | + } |
| 84 | + |
| 85 | + // Set any nulls to NA_LOGICAL |
| 86 | + if (is_valid != NULL && array_view_.null_count != 0) { |
| 87 | + for (R_xlen_t i = 0; i < length; i++) { |
| 88 | + if (!ArrowBitGet(is_valid, raw_src_offset + i)) { |
| 89 | + result[value_size_ + i] = NA_LOGICAL; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + break; |
| 94 | + |
| 95 | + default: |
| 96 | + return EINVAL; |
| 97 | + } |
| 98 | + |
| 99 | + return NANOARROW_OK; |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +#endif |
0 commit comments