|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Copyright 2018 H2O.ai |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | +// copy of this software and associated documentation files (the "Software"), |
| 6 | +// to deal in the Software without restriction, including without limitation |
| 7 | +// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | +// and/or sell copies of the Software, and to permit persons to whom the |
| 9 | +// Software is furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | +// IN THE SOFTWARE. |
| 21 | +//------------------------------------------------------------------------------ |
| 22 | +#include "../datatable/include/datatable.h" |
| 23 | +#include "datatable.h" |
| 24 | +#include "frame/py_frame.h" |
| 25 | +#include "rowindex.h" |
| 26 | +#include "py_rowindex.h" |
| 27 | +extern "C" { |
| 28 | + |
| 29 | + |
| 30 | +//------------------------------------------------------------------------------ |
| 31 | +// Helper functions |
| 32 | +//------------------------------------------------------------------------------ |
| 33 | + |
| 34 | +static int _column_index_oob(DataTable* dt, size_t i) { |
| 35 | + if (i < dt->ncols) return 0; |
| 36 | + PyErr_Format(PyExc_IndexError, "Column %zu does not exist in the Frame", i); |
| 37 | + return -1; |
| 38 | +} |
| 39 | + |
| 40 | +static DataTable* _extract_dt(PyObject* pydt) { |
| 41 | + return static_cast<py::Frame*>(pydt)->get_datatable(); |
| 42 | +} |
| 43 | + |
| 44 | +static RowIndex* _extract_ri(PyObject* pyri) { |
| 45 | + if (pyri == Py_None) return nullptr; |
| 46 | + return static_cast<pyrowindex::obj*>(pyri)->ref; |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +size_t DtABIVersion() { |
| 51 | + return 1; |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +//------------------------------------------------------------------------------ |
| 57 | +// Frame |
| 58 | +//------------------------------------------------------------------------------ |
| 59 | + |
| 60 | +int DtFrame_Check(PyObject* ob) { |
| 61 | + if (ob == nullptr) return 0; |
| 62 | + auto typeptr = reinterpret_cast<PyObject*>(&py::Frame::Type::type); |
| 63 | + int ret = PyObject_IsInstance(ob, typeptr); |
| 64 | + if (ret == -1) { |
| 65 | + PyErr_Clear(); |
| 66 | + ret = 0; |
| 67 | + } |
| 68 | + return ret; |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +size_t DtFrame_NColumns(PyObject* pydt) { |
| 73 | + auto dt = _extract_dt(pydt); |
| 74 | + return dt->ncols; |
| 75 | +} |
| 76 | + |
| 77 | +size_t DtFrame_NRows(PyObject* pydt) { |
| 78 | + auto dt = _extract_dt(pydt); |
| 79 | + return dt->nrows; |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +int DtFrame_ColumnStype(PyObject* pydt, size_t i) { |
| 84 | + auto dt = _extract_dt(pydt); |
| 85 | + if (_column_index_oob(dt, i)) return -1; |
| 86 | + return static_cast<int>(dt->columns[i]->stype()); // stype() is noexcept |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +PyObject* DtFrame_ColumnRowindex(PyObject* pydt, size_t i) { |
| 91 | + auto dt = _extract_dt(pydt); |
| 92 | + if (_column_index_oob(dt, i)) return nullptr; |
| 93 | + const RowIndex& ri = dt->columns[i]->rowindex(); // rowindex() is noexcept |
| 94 | + return ri? pyrowindex::wrap(ri) : py::None().release(); |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +const void* DtFrame_ColumnDataR(PyObject* pydt, size_t i) { |
| 99 | + auto dt = _extract_dt(pydt); |
| 100 | + if (_column_index_oob(dt, i)) return nullptr; |
| 101 | + try { |
| 102 | + return dt->columns[i]->data(); |
| 103 | + } catch (const std::exception& e) { |
| 104 | + exception_to_python(e); |
| 105 | + return nullptr; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +void* DtFrame_ColumnDataW(PyObject* pydt, size_t i) { |
| 110 | + auto dt = _extract_dt(pydt); |
| 111 | + if (_column_index_oob(dt, i)) return nullptr; |
| 112 | + try { |
| 113 | + return dt->columns[i]->data_w(); |
| 114 | + } catch (const std::exception& e) { |
| 115 | + exception_to_python(e); |
| 116 | + return nullptr; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +const char* DtFrame_ColumnStringDataR(PyObject* pydt, size_t i) { |
| 122 | + auto dt = _extract_dt(pydt); |
| 123 | + if (_column_index_oob(dt, i)) return nullptr; |
| 124 | + SType st = dt->columns[i]->stype(); |
| 125 | + try { |
| 126 | + if (st == SType::STR32) { |
| 127 | + auto scol = static_cast<StringColumn<uint32_t>*>(dt->columns[i]); |
| 128 | + return scol->strdata(); |
| 129 | + } |
| 130 | + if (st == SType::STR64) { |
| 131 | + auto scol = static_cast<StringColumn<uint64_t>*>(dt->columns[i]); |
| 132 | + return scol->strdata(); |
| 133 | + } |
| 134 | + } catch (const std::exception& e) { |
| 135 | + exception_to_python(e); |
| 136 | + return nullptr; |
| 137 | + } |
| 138 | + PyErr_Format(PyExc_TypeError, "Column %zu is not of string type", i); |
| 139 | + return nullptr; |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +//------------------------------------------------------------------------------ |
| 145 | +// Rowindex |
| 146 | +//------------------------------------------------------------------------------ |
| 147 | + |
| 148 | +int DtRowindex_Check(PyObject* ob) { |
| 149 | + if (ob == nullptr) return 0; |
| 150 | + if (ob == Py_None) return 1; |
| 151 | + auto typeptr = reinterpret_cast<PyObject*>(&pyrowindex::type); |
| 152 | + int ret = PyObject_IsInstance(ob, typeptr); |
| 153 | + if (ret == -1) { |
| 154 | + PyErr_Clear(); |
| 155 | + ret = 0; |
| 156 | + } |
| 157 | + return ret; |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +int DtRowindex_Type(PyObject* pyri) { |
| 162 | + RowIndex* ri = _extract_ri(pyri); |
| 163 | + if (!ri) return 0; |
| 164 | + return static_cast<int>(ri->type()); |
| 165 | +} |
| 166 | + |
| 167 | + |
| 168 | +size_t DtRowindex_Size(PyObject* pyri) { |
| 169 | + RowIndex* ri = _extract_ri(pyri); |
| 170 | + if (!ri) return 0; |
| 171 | + return ri->size(); |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +int DtRowindex_UnpackSlice( |
| 176 | + PyObject* pyri, size_t* start, size_t* length, size_t* step) |
| 177 | +{ |
| 178 | + RowIndex* ri = _extract_ri(pyri); |
| 179 | + if (!ri || ri->type() != RowIndexType::SLICE) { |
| 180 | + PyErr_Format(PyExc_TypeError, "expected a slice rowindex"); |
| 181 | + return -1; |
| 182 | + } |
| 183 | + *start = ri->slice_start(); |
| 184 | + *length = ri->size(); |
| 185 | + *step = ri->slice_step(); |
| 186 | + return 0; |
| 187 | +} |
| 188 | + |
| 189 | + |
| 190 | +const void* DtRowindex_ArrayData(PyObject* pyri) { |
| 191 | + RowIndex* ri = _extract_ri(pyri); |
| 192 | + if (ri && ri->type() == RowIndexType::ARR32) { |
| 193 | + return ri->indices32(); |
| 194 | + } |
| 195 | + if (ri && ri->type() == RowIndexType::ARR64) { |
| 196 | + return ri->indices64(); |
| 197 | + } |
| 198 | + PyErr_Format(PyExc_TypeError, "expected an array rowindex"); |
| 199 | + return nullptr; |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | +//------------------------------------------------------------------------------ |
| 205 | +// Deprecated |
| 206 | +//------------------------------------------------------------------------------ |
| 207 | + |
| 208 | +void* datatable_get_column_data(void* dt_, size_t column) { |
| 209 | + DataTable *dt = static_cast<DataTable*>(dt_); |
| 210 | + return dt->columns[column]->data_w(); |
| 211 | +} |
| 212 | + |
| 213 | +void datatable_unpack_slicerowindex(void* dt_, size_t* start, size_t* step) { |
| 214 | + DataTable *dt = static_cast<DataTable*>(dt_); |
| 215 | + RowIndex ri(dt->rowindex); |
| 216 | + *start = ri.slice_start(); |
| 217 | + *step = ri.slice_step(); |
| 218 | +} |
| 219 | + |
| 220 | +void datatable_unpack_arrayrowindex(void *dt_, void **indices) { |
| 221 | + DataTable *dt = static_cast<DataTable*>(dt_); |
| 222 | + RowIndex ri(dt->rowindex); |
| 223 | + *indices = const_cast<int32_t*>(ri.indices32()); |
| 224 | +} |
| 225 | + |
| 226 | + |
| 227 | +} // extern "C" |
0 commit comments