| 1 |
// NOTE: This file can't be compiled on its own, it must be #included |
| 2 |
// with certain #defines set, as described below. |
| 3 |
// |
| 4 |
// Implementation of a vec class specialized to a specific item type, such |
| 5 |
// as vec[i64] or vec[float]. Assume that certain #defines are provided that |
| 6 |
// provide all the item type specific definitions: |
| 7 |
// |
| 8 |
// VEC vec C type (e.g. VecI32) |
| 9 |
// VEC_TYPE boxed vec type object (e.g. VecI32Type) |
| 10 |
// VEC_OBJECT boxed Python object struct (e.g. VecI32Object) |
| 11 |
// BUF_OBJECT buffer Python object struct (e.g. VecI32BufObject) |
| 12 |
// BUF_TYPE buffer type object (e.g. VecI32BufType) |
| 13 |
// NAME(suffix) macro to create prefixed name with given suffix (e.g. VecI32##suffix) |
| 14 |
// FUNC(suffix) macro to create prefixed function name with suffix (e.g. VecI32_##suffix) |
| 15 |
// ITEM_TYPE_STR vec item type as C string literal (e.g. "i32") |
| 16 |
// ITEM_TYPE_MAGIC integer constant corresponding to the item type (e.g. VEC_ITEM_TYPE_I32) |
| 17 |
// ITEM_C_TYPE C type used for items (e.g. int32_t) |
| 18 |
// FEATURES capsule API struct name (e.g. Vec_I32API) |
| 19 |
// BOX_ITEM C function to box item (e.g. VecI32_BoxItem) |
| 20 |
// UNBOX_ITEM C function to unbox item (e.g. VecI32_UnboxItem) |
| 21 |
// IS_UNBOX_ERROR C function to check for unbox error (e.g. VecI32_IsUnboxError) |
| 22 |
|
| 23 |
#ifndef VEC |
| 24 |
#error "VEC must be defined" |
| 25 |
#endif |
| 26 |
|
| 27 |
#define PY_SSIZE_T_CLEAN |
| 28 |
#include <Python.h> |
| 29 |
#include "librt_vecs.h" |
| 30 |
#include "vecs_internal.h" |
| 31 |
#include "mypyc_util.h" |
| 32 |
|
| 33 |
#define VEC_BUF(v) ((BUF_OBJECT *)((char *)(v).items - offsetof(BUF_OBJECT, items))) |
| 34 |
#define VEC_CAP(v) (VEC_BUF(v)->ob_base.ob_size) |
| 35 |
#define VEC_INCREF(v) do { if ((v).items) Py_INCREF(VEC_BUF(v)); } while (0) |
| 36 |
#define VEC_DECREF(v) do { if ((v).items) Py_DECREF(VEC_BUF(v)); } while (0) |
| 37 |
|
| 38 |
inline static VEC vec_error() { |
| 39 |
VEC v = { .len = -1 }; |
| 40 |
return v; |
| 41 |
} |
| 42 |
|
| 43 |
// Alloc a partially initialized vec. Caller *must* initialize len. |
| 44 |
static VEC vec_alloc(Py_ssize_t size) |
| 45 |
{ |
| 46 |
BUF_OBJECT *buf; |
| 47 |
/* TODO: Check for overflow */ |
| 48 |
if (size == 0) { |
| 49 |
buf = NULL; |
| 50 |
} else { |
| 51 |
buf = PyObject_NewVar(BUF_OBJECT, &BUF_TYPE, size); |
| 52 |
if (buf == NULL) |
| 53 |
return vec_error(); |
| 54 |
} |
| 55 |
VEC res = { .items = (buf != NULL) ? buf->items : NULL }; |
| 56 |
return res; |
| 57 |
} |
| 58 |
|
| 59 |
static void vec_dealloc(VEC_OBJECT *self) { |
| 60 |
if (self->vec.items) { |
| 61 |
Py_DECREF(VEC_BUF(self->vec)); |
| 62 |
self->vec.items = NULL; |
| 63 |
} |
| 64 |
PyObject_Del(self); |
| 65 |
} |
| 66 |
|
| 67 |
// Box a vec[<itemtype>] value, stealing 'vec'. On error, decref 'vec'. |
| 68 |
PyObject *FUNC(Box)(VEC vec) { |
| 69 |
VEC_OBJECT *obj = PyObject_New(VEC_OBJECT, &VEC_TYPE); |
| 70 |
if (obj == NULL) { |
| 71 |
VEC_DECREF(vec); |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
obj->vec = vec; |
| 75 |
return (PyObject *)obj; |
| 76 |
} |
| 77 |
|
| 78 |
VEC FUNC(Unbox)(PyObject *obj) { |
| 79 |
if (obj->ob_type == &VEC_TYPE) { |
| 80 |
VEC result = ((VEC_OBJECT *)obj)->vec; |
| 81 |
VEC_INCREF(result); // TODO: Should we borrow instead? |
| 82 |
return result; |
| 83 |
} else { |
| 84 |
PyErr_SetString(PyExc_TypeError, "vec[" ITEM_TYPE_STR "] expected"); |
| 85 |
return vec_error(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
VEC FUNC(ConvertFromNested)(VecNestedBufItem item) { |
| 90 |
return (VEC) { item.len, (ITEM_C_TYPE *)item.items }; |
| 91 |
} |
| 92 |
|
| 93 |
VEC FUNC(New)(Py_ssize_t size, Py_ssize_t cap) { |
| 94 |
if (cap < 0) { |
| 95 |
PyErr_SetString(PyExc_ValueError, "capacity must not be negative"); |
| 96 |
return vec_error(); |
| 97 |
} |
| 98 |
if (cap < size) |
| 99 |
cap = size; |
| 100 |
VEC vec = vec_alloc(cap); |
| 101 |
if (VEC_IS_ERROR(vec)) |
| 102 |
return vec; |
| 103 |
for (Py_ssize_t i = 0; i < cap; i++) { |
| 104 |
vec.items[i] = 0; |
| 105 |
} |
| 106 |
vec.len = size; |
| 107 |
return vec; |
| 108 |
} |
| 109 |
|
| 110 |
#ifdef BUFFER_FORMAT_CHAR_OK |
| 111 |
inline static int buffer_format_matches(const char *fmt) { |
| 112 |
char c = *fmt; |
| 113 |
if (c == '@' || c == '=') { |
| 114 |
c = fmt[1]; |
| 115 |
} |
| 116 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 117 |
else if (c == '<') { c = fmt[1]; } |
| 118 |
else if (c == '>' || c == '!') { return 0; } |
| 119 |
#else |
| 120 |
else if (c == '>') { c = fmt[1]; } |
| 121 |
else if (c == '<' || c == '!') { return 0; } |
| 122 |
#endif |
| 123 |
return c != '\0' && BUFFER_FORMAT_CHAR_OK(c); |
| 124 |
} |
| 125 |
|
| 126 |
// Try to get a compatible buffer view from 'obj'. Return 1 if successful |
| 127 |
// (view is filled and caller must call PyBuffer_Release), 0 if the object |
| 128 |
// doesn't support buffer protocol or the format doesn't match (no cleanup |
| 129 |
// needed), or -1 on error. |
| 130 |
inline static int vec_get_buffer(PyObject *obj, Py_buffer *view) { |
| 131 |
if (PyObject_GetBuffer(obj, view, PyBUF_C_CONTIGUOUS | PyBUF_FORMAT) != 0) { |
| 132 |
PyErr_Clear(); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
if (view->ndim == 1 |
| 136 |
&& view->itemsize == sizeof(ITEM_C_TYPE) |
| 137 |
&& buffer_format_matches(view->format)) { |
| 138 |
return 1; |
| 139 |
} |
| 140 |
PyBuffer_Release(view); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
#endif |
| 144 |
|
| 145 |
static inline VEC vec_from_sequence(PyObject *seq, int64_t cap, const int is_list) { |
| 146 |
Py_ssize_t n = is_list ? PyList_GET_SIZE(seq) : PyTuple_GET_SIZE(seq); |
| 147 |
Py_ssize_t alloc_size = n > cap ? n : cap; |
| 148 |
VEC v = vec_alloc(alloc_size); |
| 149 |
if (VEC_IS_ERROR(v)) |
| 150 |
return vec_error(); |
| 151 |
#ifdef Py_GIL_DISABLED |
| 152 |
if (is_list) { |
| 153 |
// Other threads could be mutating the list concurrently, so use strong references |
| 154 |
Py_ssize_t actual = 0; |
| 155 |
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(seq) && i < n; i++) { |
| 156 |
PyObject *item = PyList_GetItemRef(seq, i); |
| 157 |
if (unlikely(item == NULL)) { |
| 158 |
// Race condition: list shrank between size read and get item |
| 159 |
VEC_DECREF(v); |
| 160 |
return vec_error(); |
| 161 |
} |
| 162 |
ITEM_C_TYPE x = UNBOX_ITEM(item); |
| 163 |
Py_DECREF(item); |
| 164 |
if (IS_UNBOX_ERROR(x)) { |
| 165 |
VEC_DECREF(v); |
| 166 |
return vec_error(); |
| 167 |
} |
| 168 |
v.items[actual++] = x; |
| 169 |
} |
| 170 |
v.len = actual; |
| 171 |
return v; |
| 172 |
} |
| 173 |
// Tuples are immutable, so fall through to the shared loop below |
| 174 |
#endif |
| 175 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 176 |
PyObject *item = is_list ? PyList_GET_ITEM(seq, i) : PyTuple_GET_ITEM(seq, i); |
| 177 |
ITEM_C_TYPE x = UNBOX_ITEM(item); |
| 178 |
if (IS_UNBOX_ERROR(x)) { |
| 179 |
VEC_DECREF(v); |
| 180 |
return vec_error(); |
| 181 |
} |
| 182 |
v.items[i] = x; |
| 183 |
} |
| 184 |
v.len = n; |
| 185 |
return v; |
| 186 |
} |
| 187 |
|
| 188 |
VEC FUNC(FromIterable)(PyObject *iterable, int64_t cap) { |
| 189 |
if (cap < 0) { |
| 190 |
PyErr_SetString(PyExc_ValueError, "capacity must not be negative"); |
| 191 |
return vec_error(); |
| 192 |
} |
| 193 |
|
| 194 |
if (ITEM_TYPE_MAGIC == VEC_ITEM_TYPE_U8 && PyBytes_CheckExact(iterable)) { |
| 195 |
Py_ssize_t n = PyBytes_GET_SIZE(iterable); |
| 196 |
Py_ssize_t alloc_size = n > cap ? n : cap; |
| 197 |
VEC v = vec_alloc(alloc_size); |
| 198 |
if (VEC_IS_ERROR(v)) |
| 199 |
return vec_error(); |
| 200 |
if (n > 0) |
| 201 |
memcpy(v.items, PyBytes_AS_STRING(iterable), n); |
| 202 |
v.len = n; |
| 203 |
return v; |
| 204 |
} |
| 205 |
|
| 206 |
#ifdef BUFFER_FORMAT_CHAR_OK |
| 207 |
Py_buffer view; |
| 208 |
int buf_ok = vec_get_buffer(iterable, &view); |
| 209 |
if (buf_ok < 0) |
| 210 |
return vec_error(); |
| 211 |
if (buf_ok) { |
| 212 |
Py_ssize_t n = view.len / (Py_ssize_t)sizeof(ITEM_C_TYPE); |
| 213 |
Py_ssize_t alloc_size = n > cap ? n : cap; |
| 214 |
VEC v = vec_alloc(alloc_size); |
| 215 |
if (VEC_IS_ERROR(v)) { |
| 216 |
PyBuffer_Release(&view); |
| 217 |
return vec_error(); |
| 218 |
} |
| 219 |
if (n > 0) { |
| 220 |
memcpy(v.items, view.buf, n * sizeof(ITEM_C_TYPE)); |
| 221 |
} |
| 222 |
v.len = n; |
| 223 |
PyBuffer_Release(&view); |
| 224 |
return v; |
| 225 |
} |
| 226 |
#endif |
| 227 |
|
| 228 |
if (PyList_CheckExact(iterable)) { |
| 229 |
return vec_from_sequence(iterable, cap, 1); |
| 230 |
} else if (PyTuple_CheckExact(iterable)) { |
| 231 |
return vec_from_sequence(iterable, cap, 0); |
| 232 |
} |
| 233 |
|
| 234 |
VEC v = vec_alloc(cap); |
| 235 |
if (VEC_IS_ERROR(v)) |
| 236 |
return vec_error(); |
| 237 |
if (cap > 0) { |
| 238 |
memset(v.items, 0, sizeof(ITEM_C_TYPE) * cap); |
| 239 |
} |
| 240 |
v.len = 0; |
| 241 |
|
| 242 |
PyObject *iter = PyObject_GetIter(iterable); |
| 243 |
if (iter == NULL) { |
| 244 |
VEC_DECREF(v); |
| 245 |
return vec_error(); |
| 246 |
} |
| 247 |
PyObject *item; |
| 248 |
while ((item = PyIter_Next(iter)) != NULL) { |
| 249 |
ITEM_C_TYPE x = UNBOX_ITEM(item); |
| 250 |
Py_DECREF(item); |
| 251 |
if (IS_UNBOX_ERROR(x)) { |
| 252 |
Py_DECREF(iter); |
| 253 |
VEC_DECREF(v); |
| 254 |
return vec_error(); |
| 255 |
} |
| 256 |
v = FUNC(Append)(v, x); |
| 257 |
if (VEC_IS_ERROR(v)) { |
| 258 |
Py_DECREF(iter); |
| 259 |
VEC_DECREF(v); |
| 260 |
return vec_error(); |
| 261 |
} |
| 262 |
} |
| 263 |
Py_DECREF(iter); |
| 264 |
if (PyErr_Occurred()) { |
| 265 |
VEC_DECREF(v); |
| 266 |
return vec_error(); |
| 267 |
} |
| 268 |
return v; |
| 269 |
} |
| 270 |
|
| 271 |
static PyObject *vec_new(PyTypeObject *self, PyObject *args, PyObject *kw) { |
| 272 |
static char *kwlist[] = {"", "capacity", NULL}; |
| 273 |
PyObject *init = NULL; |
| 274 |
int64_t cap = 0; |
| 275 |
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OL:vec", kwlist, &init, &cap)) { |
| 276 |
return NULL; |
| 277 |
} |
| 278 |
if (cap < 0) { |
| 279 |
PyErr_SetString(PyExc_ValueError, "capacity must not be negative"); |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
if (init == NULL) { |
| 283 |
return FUNC(Box)(FUNC(New)(0, cap)); |
| 284 |
} else { |
| 285 |
VEC v = FUNC(FromIterable)(init, cap); |
| 286 |
if (VEC_IS_ERROR(v)) |
| 287 |
return NULL; |
| 288 |
return FUNC(Box)(v); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static PyObject *vec_repr(PyObject *self) { |
| 293 |
return Vec_GenericRepr(self, ITEM_TYPE_MAGIC, 0, 1); |
| 294 |
} |
| 295 |
|
| 296 |
static PyObject *vec_get_item(PyObject *o, Py_ssize_t i) { |
| 297 |
VEC v = ((VEC_OBJECT *)o)->vec; |
| 298 |
if ((size_t)i < (size_t)v.len) { |
| 299 |
return BOX_ITEM(v.items[i]); |
| 300 |
} else if ((size_t)i + (size_t)v.len < (size_t)v.len) { |
| 301 |
return BOX_ITEM(v.items[i + v.len]); |
| 302 |
} else { |
| 303 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 304 |
return NULL; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
VEC FUNC(Slice)(VEC vec, int64_t start, int64_t end) { |
| 309 |
if (start < 0) |
| 310 |
start += vec.len; |
| 311 |
if (end < 0) |
| 312 |
end += vec.len; |
| 313 |
if (start < 0) |
| 314 |
start = 0; |
| 315 |
if (start >= vec.len) |
| 316 |
start = vec.len; |
| 317 |
if (end < start) |
| 318 |
end = start; |
| 319 |
if (end > vec.len) |
| 320 |
end = vec.len; |
| 321 |
int64_t slicelength = end - start; |
| 322 |
VEC res = vec_alloc(slicelength); |
| 323 |
if (VEC_IS_ERROR(res)) |
| 324 |
return res; |
| 325 |
res.len = slicelength; |
| 326 |
for (Py_ssize_t i = 0; i < slicelength; i++) |
| 327 |
res.items[i] = vec.items[start + i]; |
| 328 |
return res; |
| 329 |
} |
| 330 |
|
| 331 |
static PyObject *vec_subscript(PyObject *self, PyObject *item) { |
| 332 |
VEC vec = ((VEC_OBJECT *)self)->vec; |
| 333 |
if (PyIndex_Check(item)) { |
| 334 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 335 |
if (i == -1 && PyErr_Occurred()) |
| 336 |
return NULL; |
| 337 |
if ((size_t)i < (size_t)vec.len) { |
| 338 |
return BOX_ITEM(vec.items[i]); |
| 339 |
} else if ((size_t)i + (size_t)vec.len < (size_t)vec.len) { |
| 340 |
return BOX_ITEM(vec.items[i + vec.len]); |
| 341 |
} else { |
| 342 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 343 |
return NULL; |
| 344 |
} |
| 345 |
} else if (PySlice_Check(item)) { |
| 346 |
Py_ssize_t start, stop, step; |
| 347 |
if (PySlice_Unpack(item, &start, &stop, &step) < 0) |
| 348 |
return NULL; |
| 349 |
Py_ssize_t slicelength = PySlice_AdjustIndices(vec.len, &start, &stop, step); |
| 350 |
VEC res = vec_alloc(slicelength); |
| 351 |
if (VEC_IS_ERROR(res)) |
| 352 |
return NULL; |
| 353 |
res.len = slicelength; |
| 354 |
Py_ssize_t j = start; |
| 355 |
for (Py_ssize_t i = 0; i < slicelength; i++) { |
| 356 |
res.items[i] = vec.items[j]; |
| 357 |
j += step; |
| 358 |
} |
| 359 |
return FUNC(Box)(res); |
| 360 |
} else { |
| 361 |
PyErr_Format(PyExc_TypeError, "vec indices must be integers or slices, not %.100s", |
| 362 |
item->ob_type->tp_name); |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
static int vec_ass_item(PyObject *self, Py_ssize_t i, PyObject *o) { |
| 368 |
ITEM_C_TYPE x = UNBOX_ITEM(o); |
| 369 |
if (IS_UNBOX_ERROR(x)) |
| 370 |
return -1; |
| 371 |
VEC v = ((VEC_OBJECT *)self)->vec; |
| 372 |
if ((size_t)i < (size_t)v.len) { |
| 373 |
v.items[i] = x; |
| 374 |
return 0; |
| 375 |
} else if ((size_t)i + (size_t)v.len < (size_t)v.len) { |
| 376 |
v.items[i + v.len] = x; |
| 377 |
return 0; |
| 378 |
} else { |
| 379 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 380 |
return -1; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
static int vec_contains(PyObject *self, PyObject *value) { |
| 385 |
ITEM_C_TYPE x = UNBOX_ITEM(value); |
| 386 |
if (unlikely(IS_UNBOX_ERROR(x))) { |
| 387 |
if (PyErr_Occurred()) |
| 388 |
PyErr_Clear(); |
| 389 |
// Fall back to boxed comparison (e.g. 2.0 == 2) |
| 390 |
VEC v = ((VEC_OBJECT *)self)->vec; |
| 391 |
for (Py_ssize_t i = 0; i < v.len; i++) { |
| 392 |
PyObject *boxed = BOX_ITEM(v.items[i]); |
| 393 |
if (boxed == NULL) |
| 394 |
return -1; |
| 395 |
int cmp = PyObject_RichCompareBool(boxed, value, Py_EQ); |
| 396 |
Py_DECREF(boxed); |
| 397 |
if (cmp != 0) |
| 398 |
return cmp; // 1 if equal, -1 on error |
| 399 |
} |
| 400 |
return 0; |
| 401 |
} |
| 402 |
VEC v = ((VEC_OBJECT *)self)->vec; |
| 403 |
for (Py_ssize_t i = 0; i < v.len; i++) { |
| 404 |
if (v.items[i] == x) |
| 405 |
return 1; |
| 406 |
} |
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
static Py_ssize_t vec_length(PyObject *o) { |
| 411 |
return ((VEC_OBJECT *)o)->vec.len; |
| 412 |
} |
| 413 |
|
| 414 |
static PyObject *vec_richcompare(PyObject *self, PyObject *other, int op) { |
| 415 |
int cmp = 1; |
| 416 |
PyObject *res; |
| 417 |
if (op == Py_EQ || op == Py_NE) { |
| 418 |
if (other->ob_type != &VEC_TYPE) |
| 419 |
cmp = 0; |
| 420 |
else { |
| 421 |
VEC x = ((VEC_OBJECT *)self)->vec; |
| 422 |
VEC y = ((VEC_OBJECT *)other)->vec; |
| 423 |
if (x.len != y.len) { |
| 424 |
cmp = 0; |
| 425 |
} else { |
| 426 |
for (Py_ssize_t i = 0; i < x.len; i++) { |
| 427 |
if (x.items[i] != y.items[i]) { |
| 428 |
cmp = 0; |
| 429 |
break; |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
if (op == Py_NE) |
| 435 |
cmp = cmp ^ 1; |
| 436 |
res = cmp ? Py_True : Py_False; |
| 437 |
} else |
| 438 |
res = Py_NotImplemented; |
| 439 |
Py_INCREF(res); |
| 440 |
return res; |
| 441 |
} |
| 442 |
|
| 443 |
// Append item to 'vec', stealing 'vec'. Return 'vec' with item appended. |
| 444 |
VEC FUNC(Append)(VEC vec, ITEM_C_TYPE x) { |
| 445 |
if (vec.items && vec.len < VEC_CAP(vec)) { |
| 446 |
vec.items[vec.len] = x; |
| 447 |
vec.len++; |
| 448 |
return vec; |
| 449 |
} else { |
| 450 |
Py_ssize_t cap = vec.items ? VEC_CAP(vec) : 0; |
| 451 |
Py_ssize_t new_size = Vec_GrowCapacity(cap); |
| 452 |
VEC new = vec_alloc(new_size); |
| 453 |
if (VEC_IS_ERROR(new)) { |
| 454 |
// The input v is being consumed/stolen by this function, so on error |
| 455 |
// we must decref it to avoid leaking the buffer. |
| 456 |
VEC_DECREF(vec); |
| 457 |
return vec_error(); |
| 458 |
} |
| 459 |
new.len = vec.len + 1; |
| 460 |
if (vec.len > 0) |
| 461 |
memcpy(new.items, vec.items, sizeof(ITEM_C_TYPE) * vec.len); |
| 462 |
new.items[vec.len] = x; |
| 463 |
VEC_DECREF(vec); |
| 464 |
return new; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
inline static int vec_memory_overlaps(const void *p1, Py_ssize_t len1, |
| 469 |
const void *p2, Py_ssize_t len2) { |
| 470 |
if (len1 <= 0 || len2 <= 0) |
| 471 |
return 0; |
| 472 |
uintptr_t a = (uintptr_t)p1, b = (uintptr_t)p2; |
| 473 |
if (a <= b) |
| 474 |
return b - a < (uintptr_t)len1; |
| 475 |
return a - b < (uintptr_t)len2; |
| 476 |
} |
| 477 |
|
| 478 |
// Extend 'dst' by appending 'n' items from 'items', stealing 'dst'. |
| 479 |
// Caller guarantees n > 0 and that 'items' remains valid for the call. |
| 480 |
// If force_alloc is true, always allocate a new buffer even when dst has capacity. |
| 481 |
inline static VEC vec_extend_items( |
| 482 |
VEC dst, const ITEM_C_TYPE *items, Py_ssize_t n, int force_alloc |
| 483 |
) { |
| 484 |
if (unlikely(n > PY_SSIZE_T_MAX - dst.len)) { |
| 485 |
PyErr_NoMemory(); |
| 486 |
VEC_DECREF(dst); |
| 487 |
return vec_error(); |
| 488 |
} |
| 489 |
Py_ssize_t new_len = dst.len + n; |
| 490 |
Py_ssize_t cap = dst.items ? VEC_CAP(dst) : 0; |
| 491 |
if (!force_alloc && new_len <= cap) { |
| 492 |
memcpy(dst.items + dst.len, items, sizeof(ITEM_C_TYPE) * n); |
| 493 |
dst.len = new_len; |
| 494 |
return dst; |
| 495 |
} |
| 496 |
Py_ssize_t new_cap = Vec_GrowCapacityTo(cap, new_len); |
| 497 |
VEC new = vec_alloc(new_cap); |
| 498 |
if (VEC_IS_ERROR(new)) { |
| 499 |
VEC_DECREF(dst); |
| 500 |
return vec_error(); |
| 501 |
} |
| 502 |
if (dst.len > 0) |
| 503 |
memcpy(new.items, dst.items, sizeof(ITEM_C_TYPE) * dst.len); |
| 504 |
memcpy(new.items + dst.len, items, sizeof(ITEM_C_TYPE) * n); |
| 505 |
new.len = new_len; |
| 506 |
VEC_DECREF(dst); |
| 507 |
return new; |
| 508 |
} |
| 509 |
|
| 510 |
// Extend 'vec' with items from 'iterable', stealing 'vec'. |
| 511 |
// Return extended 'vec', or error vec on failure. |
| 512 |
VEC FUNC(Extend)(VEC vec, PyObject *iterable) { |
| 513 |
if (Py_TYPE(iterable) == &VEC_TYPE) { |
| 514 |
return FUNC(ExtendVec)(vec, ((VEC_OBJECT *)iterable)->vec); |
| 515 |
} |
| 516 |
|
| 517 |
if (ITEM_TYPE_MAGIC == VEC_ITEM_TYPE_U8 && PyBytes_CheckExact(iterable)) { |
| 518 |
Py_ssize_t n = PyBytes_GET_SIZE(iterable); |
| 519 |
if (n > 0) |
| 520 |
return vec_extend_items(vec, (const ITEM_C_TYPE *)PyBytes_AS_STRING(iterable), n, 0); |
| 521 |
return vec; |
| 522 |
} |
| 523 |
|
| 524 |
#ifdef BUFFER_FORMAT_CHAR_OK |
| 525 |
Py_buffer view; |
| 526 |
int buf_ok = vec_get_buffer(iterable, &view); |
| 527 |
if (buf_ok < 0) { |
| 528 |
VEC_DECREF(vec); |
| 529 |
return vec_error(); |
| 530 |
} |
| 531 |
if (buf_ok) { |
| 532 |
Py_ssize_t n = view.len / (Py_ssize_t)sizeof(ITEM_C_TYPE); |
| 533 |
if (n > 0) { |
| 534 |
Py_ssize_t dst_bytes = n * (Py_ssize_t)sizeof(ITEM_C_TYPE); |
| 535 |
int force_alloc = vec.items != NULL |
| 536 |
&& n <= VEC_CAP(vec) - vec.len |
| 537 |
&& vec_memory_overlaps(view.buf, view.len, |
| 538 |
vec.items + vec.len, dst_bytes); |
| 539 |
vec = vec_extend_items(vec, (const ITEM_C_TYPE *)view.buf, n, force_alloc); |
| 540 |
} |
| 541 |
PyBuffer_Release(&view); |
| 542 |
return vec; |
| 543 |
} |
| 544 |
#endif |
| 545 |
|
| 546 |
PyObject *iter = PyObject_GetIter(iterable); |
| 547 |
if (iter == NULL) { |
| 548 |
VEC_DECREF(vec); |
| 549 |
return vec_error(); |
| 550 |
} |
| 551 |
PyObject *item; |
| 552 |
while ((item = PyIter_Next(iter)) != NULL) { |
| 553 |
ITEM_C_TYPE x = UNBOX_ITEM(item); |
| 554 |
Py_DECREF(item); |
| 555 |
if (IS_UNBOX_ERROR(x)) { |
| 556 |
Py_DECREF(iter); |
| 557 |
VEC_DECREF(vec); |
| 558 |
return vec_error(); |
| 559 |
} |
| 560 |
vec = FUNC(Append)(vec, x); |
| 561 |
if (VEC_IS_ERROR(vec)) { |
| 562 |
Py_DECREF(iter); |
| 563 |
return vec_error(); |
| 564 |
} |
| 565 |
} |
| 566 |
Py_DECREF(iter); |
| 567 |
if (PyErr_Occurred()) { |
| 568 |
VEC_DECREF(vec); |
| 569 |
return vec_error(); |
| 570 |
} |
| 571 |
return vec; |
| 572 |
} |
| 573 |
|
| 574 |
// Extend 'dst' with items from 'src' vec, stealing 'dst', borrowing 'src'. |
| 575 |
// Return extended vec, or error vec on failure. |
| 576 |
VEC FUNC(ExtendVec)(VEC dst, VEC src) { |
| 577 |
if (src.len == 0) |
| 578 |
return dst; |
| 579 |
return vec_extend_items(dst, src.items, src.len, dst.items == src.items); |
| 580 |
} |
| 581 |
|
| 582 |
// Convert vec to list, stealing 'v'. |
| 583 |
PyObject *FUNC(ToList)(VEC v) { |
| 584 |
Py_ssize_t n = v.len; |
| 585 |
PyObject *list = PyList_New(n); |
| 586 |
if (list == NULL) { |
| 587 |
VEC_DECREF(v); |
| 588 |
return NULL; |
| 589 |
} |
| 590 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 591 |
PyObject *item = BOX_ITEM(v.items[i]); |
| 592 |
if (item == NULL) { |
| 593 |
Py_DECREF(list); |
| 594 |
VEC_DECREF(v); |
| 595 |
return NULL; |
| 596 |
} |
| 597 |
PyList_SET_ITEM(list, i, item); |
| 598 |
} |
| 599 |
VEC_DECREF(v); |
| 600 |
return list; |
| 601 |
} |
| 602 |
|
| 603 |
// Convert vec to tuple, stealing 'v'. |
| 604 |
PyObject *FUNC(ToTuple)(VEC v) { |
| 605 |
Py_ssize_t n = v.len; |
| 606 |
PyObject *tuple = PyTuple_New(n); |
| 607 |
if (tuple == NULL) { |
| 608 |
VEC_DECREF(v); |
| 609 |
return NULL; |
| 610 |
} |
| 611 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 612 |
PyObject *item = BOX_ITEM(v.items[i]); |
| 613 |
if (item == NULL) { |
| 614 |
Py_DECREF(tuple); |
| 615 |
VEC_DECREF(v); |
| 616 |
return NULL; |
| 617 |
} |
| 618 |
PyTuple_SET_ITEM(tuple, i, item); |
| 619 |
} |
| 620 |
VEC_DECREF(v); |
| 621 |
return tuple; |
| 622 |
} |
| 623 |
|
| 624 |
// Remove item from 'vec', stealing 'vec'. Return 'vec' with item removed. |
| 625 |
VEC FUNC(Remove)(VEC v, ITEM_C_TYPE x) { |
| 626 |
for (Py_ssize_t i = 0; i < v.len; i++) { |
| 627 |
if (v.items[i] == x) { |
| 628 |
for (; i < v.len - 1; i++) { |
| 629 |
v.items[i] = v.items[i + 1]; |
| 630 |
} |
| 631 |
v.len--; |
| 632 |
// Return the stolen reference without INCREF |
| 633 |
return v; |
| 634 |
} |
| 635 |
} |
| 636 |
PyErr_SetString(PyExc_ValueError, "vec.remove(x): x not in vec"); |
| 637 |
// The input v is being consumed/stolen by this function, so on error |
| 638 |
// we must decref it to avoid leaking the buffer. |
| 639 |
VEC_DECREF(v); |
| 640 |
return vec_error(); |
| 641 |
} |
| 642 |
|
| 643 |
// Pop item from 'vec', stealing 'vec'. Return struct with modified 'vec' and the popped item. |
| 644 |
NAME(PopResult) FUNC(Pop)(VEC v, Py_ssize_t index) { |
| 645 |
NAME(PopResult) result; |
| 646 |
|
| 647 |
if (index < 0) |
| 648 |
index += v.len; |
| 649 |
|
| 650 |
if (index < 0 || index >= v.len) { |
| 651 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 652 |
// The input v is being consumed/stolen by this function, so on error |
| 653 |
// we must decref it to avoid leaking the buffer. |
| 654 |
VEC_DECREF(v); |
| 655 |
result.f0 = vec_error(); |
| 656 |
result.f1 = 0; |
| 657 |
return result; |
| 658 |
} |
| 659 |
|
| 660 |
result.f1 = v.items[index]; |
| 661 |
for (Py_ssize_t i = index; i < v.len - 1; i++) { |
| 662 |
v.items[i] = v.items[i + 1]; |
| 663 |
} |
| 664 |
|
| 665 |
v.len--; |
| 666 |
// Return the stolen reference without INCREF |
| 667 |
result.f0 = v; |
| 668 |
return result; |
| 669 |
} |
| 670 |
|
| 671 |
static PyMappingMethods vec_mapping_methods = { |
| 672 |
.mp_length = vec_length, |
| 673 |
.mp_subscript = vec_subscript, |
| 674 |
}; |
| 675 |
|
| 676 |
#ifdef BUFFER_FORMAT |
| 677 |
static int vec_getbuffer(VEC_OBJECT *self, Py_buffer *view, int flags) { |
| 678 |
if (view == NULL) { |
| 679 |
PyErr_SetString(PyExc_BufferError, |
| 680 |
"vec_getbuffer: view==NULL argument is obsolete"); |
| 681 |
return -1; |
| 682 |
} |
| 683 |
if ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) { |
| 684 |
PyErr_SetString(PyExc_BufferError, "Object is not writable"); |
| 685 |
view->obj = NULL; |
| 686 |
return -1; |
| 687 |
} |
| 688 |
|
| 689 |
view->obj = (PyObject *)self; |
| 690 |
Py_INCREF(self); |
| 691 |
view->buf = (void *)self->vec.items; |
| 692 |
view->len = self->vec.len * (Py_ssize_t)sizeof(ITEM_C_TYPE); |
| 693 |
view->readonly = 1; |
| 694 |
view->itemsize = sizeof(ITEM_C_TYPE); |
| 695 |
view->format = NULL; |
| 696 |
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 697 |
view->format = BUFFER_FORMAT; |
| 698 |
view->ndim = 1; |
| 699 |
view->shape = NULL; |
| 700 |
if ((flags & PyBUF_ND) == PyBUF_ND) |
| 701 |
view->shape = &self->vec.len; |
| 702 |
view->strides = NULL; |
| 703 |
if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) |
| 704 |
view->strides = &view->itemsize; |
| 705 |
view->suboffsets = NULL; |
| 706 |
view->internal = NULL; |
| 707 |
|
| 708 |
return 0; |
| 709 |
} |
| 710 |
|
| 711 |
static PyBufferProcs vec_buffer_procs = { |
| 712 |
.bf_getbuffer = (getbufferproc)vec_getbuffer, |
| 713 |
}; |
| 714 |
#endif |
| 715 |
|
| 716 |
static PySequenceMethods vec_sequence_methods = { |
| 717 |
.sq_item = vec_get_item, |
| 718 |
.sq_ass_item = vec_ass_item, |
| 719 |
.sq_contains = vec_contains, |
| 720 |
}; |
| 721 |
|
| 722 |
static PyMethodDef vec_methods[] = { |
| 723 |
{NULL, NULL, 0, NULL}, /* Sentinel */ |
| 724 |
}; |
| 725 |
|
| 726 |
// Iterator type for specialized vec types |
| 727 |
|
| 728 |
typedef struct { |
| 729 |
PyObject_HEAD |
| 730 |
VEC vec; // Unboxed vec (keeps buffer alive via items reference) |
| 731 |
Py_ssize_t index; // Current iteration index |
| 732 |
} NAME(IterObject); |
| 733 |
|
| 734 |
PyTypeObject NAME(IterType); |
| 735 |
|
| 736 |
static PyObject *vec_iter(PyObject *self) { |
| 737 |
NAME(IterObject) *it = PyObject_New(NAME(IterObject), &NAME(IterType)); |
| 738 |
if (it == NULL) |
| 739 |
return NULL; |
| 740 |
it->vec = ((VEC_OBJECT *)self)->vec; |
| 741 |
VEC_INCREF(it->vec); |
| 742 |
it->index = 0; |
| 743 |
return (PyObject *)it; |
| 744 |
} |
| 745 |
|
| 746 |
static void vec_iter_dealloc(NAME(IterObject) *self) { |
| 747 |
VEC_DECREF(self->vec); |
| 748 |
PyObject_Del(self); |
| 749 |
} |
| 750 |
|
| 751 |
static PyObject *vec_iter_next(NAME(IterObject) *self) { |
| 752 |
if (self->vec.items == NULL) |
| 753 |
return NULL; |
| 754 |
if (self->index < self->vec.len) { |
| 755 |
PyObject *item = BOX_ITEM(self->vec.items[self->index]); |
| 756 |
if (item == NULL) |
| 757 |
return NULL; |
| 758 |
self->index++; |
| 759 |
return item; |
| 760 |
} |
| 761 |
VEC_DECREF(self->vec); |
| 762 |
self->vec.items = NULL; |
| 763 |
return NULL; // StopIteration |
| 764 |
} |
| 765 |
|
| 766 |
static PyObject *vec_iter_len(NAME(IterObject) *self, PyObject *Py_UNUSED(ignored)) { |
| 767 |
if (self->vec.items == NULL) |
| 768 |
return PyLong_FromSsize_t(0); |
| 769 |
Py_ssize_t remaining = self->vec.len - self->index; |
| 770 |
if (remaining < 0) |
| 771 |
remaining = 0; |
| 772 |
return PyLong_FromSsize_t(remaining); |
| 773 |
} |
| 774 |
|
| 775 |
static PyMethodDef vec_iter_methods[] = { |
| 776 |
{"__length_hint__", (PyCFunction)vec_iter_len, METH_NOARGS, NULL}, |
| 777 |
{NULL, NULL, 0, NULL}, |
| 778 |
}; |
| 779 |
|
| 780 |
PyTypeObject NAME(IterType) = { |
| 781 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 782 |
.tp_name = "vec_iterator[" ITEM_TYPE_STR "]", |
| 783 |
.tp_basicsize = sizeof(NAME(IterObject)), |
| 784 |
.tp_itemsize = 0, |
| 785 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 786 |
.tp_dealloc = (destructor)vec_iter_dealloc, |
| 787 |
.tp_iter = PyObject_SelfIter, |
| 788 |
.tp_iternext = (iternextfunc)vec_iter_next, |
| 789 |
.tp_methods = vec_iter_methods, |
| 790 |
}; |
| 791 |
|
| 792 |
PyTypeObject BUF_TYPE = { |
| 793 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 794 |
.tp_name = "vecbuf[" ITEM_TYPE_STR "]", |
| 795 |
.tp_doc = "Internal data buffer used by vec objects", |
| 796 |
.tp_basicsize = sizeof(BUF_OBJECT) - sizeof(ITEM_C_TYPE), |
| 797 |
.tp_itemsize = sizeof(ITEM_C_TYPE), |
| 798 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 799 |
//.tp_new = ?? |
| 800 |
.tp_free = PyObject_Del, |
| 801 |
}; |
| 802 |
|
| 803 |
PyTypeObject VEC_TYPE = { |
| 804 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 805 |
.tp_name = "vec[" ITEM_TYPE_STR "]", |
| 806 |
.tp_doc = "Mutable sequence-like container optimized for compilation with mypyc", |
| 807 |
.tp_basicsize = sizeof(VEC_OBJECT), |
| 808 |
.tp_itemsize = 0, |
| 809 |
.tp_base = &VecType, // Inherit from base vec type for isinstance() support |
| 810 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 811 |
.tp_new = vec_new, |
| 812 |
//.tp_free = PyObject_Del, |
| 813 |
.tp_dealloc = (destructor)vec_dealloc, |
| 814 |
.tp_repr = (reprfunc)vec_repr, |
| 815 |
.tp_iter = vec_iter, |
| 816 |
.tp_as_sequence = &vec_sequence_methods, |
| 817 |
.tp_as_mapping = &vec_mapping_methods, |
| 818 |
#ifdef BUFFER_FORMAT |
| 819 |
.tp_as_buffer = &vec_buffer_procs, |
| 820 |
#endif |
| 821 |
.tp_richcompare = vec_richcompare, |
| 822 |
.tp_methods = vec_methods, |
| 823 |
}; |
| 824 |
|
| 825 |
NAME(API) FEATURES = { |
| 826 |
&VEC_TYPE, |
| 827 |
&BUF_TYPE, |
| 828 |
FUNC(New), |
| 829 |
FUNC(Box), |
| 830 |
FUNC(Unbox), |
| 831 |
FUNC(ConvertFromNested), |
| 832 |
FUNC(Append), |
| 833 |
FUNC(Pop), |
| 834 |
FUNC(Remove), |
| 835 |
FUNC(Slice), |
| 836 |
FUNC(FromIterable), |
| 837 |
FUNC(Extend), |
| 838 |
FUNC(ExtendVec), |
| 839 |
FUNC(ToList), |
| 840 |
FUNC(ToTuple), |
| 841 |
}; |
| 842 |
|
| 843 |
#undef VEC_BUF |
| 844 |
#undef VEC_CAP |
| 845 |
#undef VEC_INCREF |
| 846 |
#undef VEC_DECREF |