| 1 |
// Implementation of generic vec[t], when t is a plain type object (possibly optional). |
| 2 |
// |
| 3 |
// Examples of types supported: |
| 4 |
// |
| 5 |
// - vec[str] |
| 6 |
// - vec[str | None] |
| 7 |
// - vec[object] |
| 8 |
// - vec[UserClass] |
| 9 |
|
| 10 |
#define PY_SSIZE_T_CLEAN |
| 11 |
#include <Python.h> |
| 12 |
#include "librt_vecs.h" |
| 13 |
#include "vecs_internal.h" |
| 14 |
|
| 15 |
#define VEC_BUF(v) ((VecTBufObject *)((char *)(v).items - offsetof(VecTBufObject, items))) |
| 16 |
#define VEC_CAP(v) (VEC_BUF(v)->ob_base.ob_size) |
| 17 |
#define VEC_INCREF(v) do { if ((v).items) Py_INCREF(VEC_BUF(v)); } while (0) |
| 18 |
#define VEC_DECREF(v) do { if ((v).items) Py_DECREF(VEC_BUF(v)); } while (0) |
| 19 |
|
| 20 |
static inline VecT vec_error() { |
| 21 |
VecT v = { .len = -1 }; |
| 22 |
return v; |
| 23 |
} |
| 24 |
|
| 25 |
static inline VecTBufObject *alloc_buf(Py_ssize_t size, size_t item_type) { |
| 26 |
VecTBufObject *buf = PyObject_GC_NewVar(VecTBufObject, &VecTBufType, size); |
| 27 |
if (buf == NULL) |
| 28 |
return NULL; |
| 29 |
buf->item_type = item_type; |
| 30 |
Py_INCREF(VEC_BUF_ITEM_TYPE(buf)); |
| 31 |
return buf; |
| 32 |
} |
| 33 |
|
| 34 |
static inline void vec_track_buffer(VecT *vec) { |
| 35 |
if (vec->items != NULL) { |
| 36 |
PyObject_GC_Track(VEC_T_BUF(*vec)); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Alloc a partially initialized vec. If size > 0, caller *must* immediately initialize len, |
| 41 |
// and items. Caller *must* also call vec_track_buffer on the returned vec but only |
| 42 |
// after initializing the items. |
| 43 |
static VecT vec_alloc(Py_ssize_t size, size_t item_type) { |
| 44 |
VecTBufObject *buf; |
| 45 |
|
| 46 |
if (size == 0) { |
| 47 |
buf = NULL; |
| 48 |
} else { |
| 49 |
buf = alloc_buf(size, item_type); |
| 50 |
if (buf == NULL) |
| 51 |
return vec_error(); |
| 52 |
} |
| 53 |
return (VecT) { .items = (buf != NULL) ? buf->items : NULL }; |
| 54 |
} |
| 55 |
|
| 56 |
// Box a VecT value, stealing 'vec'. On failure, return NULL and decref 'vec'. |
| 57 |
PyObject *VecT_Box(VecT vec, size_t item_type) { |
| 58 |
// An unboxed empty vec may have NULL items, but a boxed vec must have a buf |
| 59 |
// allocated, since it contains the item type |
| 60 |
if (vec.items == NULL) { |
| 61 |
VecTBufObject *buf = alloc_buf(0, item_type); |
| 62 |
if (buf == NULL) |
| 63 |
return NULL; |
| 64 |
vec.items = buf->items; |
| 65 |
vec_track_buffer(&vec); |
| 66 |
} |
| 67 |
VecTObject *obj = PyObject_GC_New(VecTObject, &VecTType); |
| 68 |
if (obj == NULL) { |
| 69 |
// items is always defined, so no need for a NULL check |
| 70 |
Py_DECREF(VEC_BUF(vec)); |
| 71 |
return NULL; |
| 72 |
} |
| 73 |
obj->vec = vec; |
| 74 |
PyObject_GC_Track(obj); |
| 75 |
return (PyObject *)obj; |
| 76 |
} |
| 77 |
|
| 78 |
VecT VecT_Unbox(PyObject *obj, size_t item_type) { |
| 79 |
if (obj->ob_type == &VecTType) { |
| 80 |
VecT result = ((VecTObject *)obj)->vec; |
| 81 |
if (VEC_BUF(result)->item_type == item_type) { |
| 82 |
VEC_INCREF(result); // TODO: Should we borrow instead? |
| 83 |
return result; |
| 84 |
} |
| 85 |
} |
| 86 |
// TODO: Better error message, with name of type |
| 87 |
PyErr_SetString(PyExc_TypeError, "vec[t] expected"); |
| 88 |
return vec_error(); |
| 89 |
} |
| 90 |
|
| 91 |
VecT VecT_ConvertFromNested(VecNestedBufItem item) { |
| 92 |
return (VecT) { item.len, (PyObject **)item.items }; |
| 93 |
} |
| 94 |
|
| 95 |
VecT VecT_New(Py_ssize_t size, Py_ssize_t cap, size_t item_type) { |
| 96 |
if (cap < 0) { |
| 97 |
PyErr_SetString(PyExc_ValueError, "capacity must not be negative"); |
| 98 |
return vec_error(); |
| 99 |
} |
| 100 |
if (cap < size) |
| 101 |
cap = size; |
| 102 |
VecT vec = vec_alloc(cap, item_type); |
| 103 |
if (VEC_IS_ERROR(vec)) |
| 104 |
return vec; |
| 105 |
for (Py_ssize_t i = 0; i < cap; i++) { |
| 106 |
vec.items[i] = NULL; |
| 107 |
} |
| 108 |
vec_track_buffer(&vec); |
| 109 |
vec.len = size; |
| 110 |
return vec; |
| 111 |
} |
| 112 |
|
| 113 |
static PyObject *vec_repr(PyObject *self) { |
| 114 |
VecTObject *v = (VecTObject *)self; |
| 115 |
return Vec_GenericRepr(self, VEC_BUF(v->vec)->item_type, 0, 1); |
| 116 |
} |
| 117 |
|
| 118 |
static PyObject *vec_get_item(PyObject *o, Py_ssize_t i) { |
| 119 |
VecT v = ((VecTObject *)o)->vec; |
| 120 |
if ((size_t)i < (size_t)v.len) { |
| 121 |
PyObject *item = v.items[i]; |
| 122 |
Py_INCREF(item); |
| 123 |
return item; |
| 124 |
} else if ((size_t)i + (size_t)v.len < (size_t)v.len) { |
| 125 |
PyObject *item = v.items[i + v.len]; |
| 126 |
Py_INCREF(item); |
| 127 |
return item; |
| 128 |
} else { |
| 129 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
VecT VecT_Slice(VecT vec, int64_t start, int64_t end) { |
| 135 |
if (start < 0) |
| 136 |
start += vec.len; |
| 137 |
if (end < 0) |
| 138 |
end += vec.len; |
| 139 |
if (start < 0) |
| 140 |
start = 0; |
| 141 |
if (start >= vec.len) |
| 142 |
start = vec.len; |
| 143 |
if (end < start) |
| 144 |
end = start; |
| 145 |
if (end > vec.len) |
| 146 |
end = vec.len; |
| 147 |
int64_t slicelength = end - start; |
| 148 |
if (slicelength == 0) |
| 149 |
return (VecT) { .len = 0, .items = NULL }; |
| 150 |
VecT res = vec_alloc(slicelength, VEC_BUF(vec)->item_type); |
| 151 |
if (VEC_IS_ERROR(res)) |
| 152 |
return res; |
| 153 |
res.len = slicelength; |
| 154 |
for (Py_ssize_t i = 0; i < slicelength; i++) { |
| 155 |
PyObject *item = vec.items[start + i]; |
| 156 |
Py_INCREF(item); |
| 157 |
res.items[i] = item; |
| 158 |
} |
| 159 |
vec_track_buffer(&res); |
| 160 |
return res; |
| 161 |
} |
| 162 |
|
| 163 |
static PyObject *vec_subscript(PyObject *self, PyObject *item) { |
| 164 |
VecT vec = ((VecTObject *)self)->vec; |
| 165 |
if (PyIndex_Check(item)) { |
| 166 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 167 |
if (i == -1 && PyErr_Occurred()) |
| 168 |
return NULL; |
| 169 |
if ((size_t)i < (size_t)vec.len) { |
| 170 |
PyObject *result = vec.items[i]; |
| 171 |
Py_INCREF(result); |
| 172 |
return result; |
| 173 |
} else if ((size_t)i + (size_t)vec.len < (size_t)vec.len) { |
| 174 |
PyObject *result = vec.items[i + vec.len]; |
| 175 |
Py_INCREF(result); |
| 176 |
return result; |
| 177 |
} else { |
| 178 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 179 |
return NULL; |
| 180 |
} |
| 181 |
} else if (PySlice_Check(item)) { |
| 182 |
Py_ssize_t start, stop, step; |
| 183 |
if (PySlice_Unpack(item, &start, &stop, &step) < 0) |
| 184 |
return NULL; |
| 185 |
Py_ssize_t slicelength = PySlice_AdjustIndices(vec.len, &start, &stop, step); |
| 186 |
VecT res = vec_alloc(slicelength, VEC_BUF(vec)->item_type); |
| 187 |
if (VEC_IS_ERROR(res)) |
| 188 |
return NULL; |
| 189 |
res.len = slicelength; |
| 190 |
Py_ssize_t j = start; |
| 191 |
for (Py_ssize_t i = 0; i < slicelength; i++) { |
| 192 |
PyObject *item = vec.items[j]; |
| 193 |
Py_INCREF(item); |
| 194 |
res.items[i] = item; |
| 195 |
j += step; |
| 196 |
} |
| 197 |
vec_track_buffer(&res); |
| 198 |
PyObject *result = VecT_Box(res, VEC_BUF(vec)->item_type); |
| 199 |
if (result == NULL) { |
| 200 |
VEC_DECREF(res); |
| 201 |
} |
| 202 |
return result; |
| 203 |
} else { |
| 204 |
PyErr_Format(PyExc_TypeError, "vec indices must be integers or slices, not %.100s", |
| 205 |
item->ob_type->tp_name); |
| 206 |
return NULL; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
static int vec_ass_item(PyObject *self, Py_ssize_t i, PyObject *o) { |
| 211 |
VecT v = ((VecTObject *)self)->vec; |
| 212 |
if (!VecT_ItemCheck(v, o, VEC_BUF(v)->item_type)) |
| 213 |
return -1; |
| 214 |
if ((size_t)i < (size_t)v.len) { |
| 215 |
PyObject *old = v.items[i]; |
| 216 |
Py_INCREF(o); |
| 217 |
v.items[i] = o; |
| 218 |
Py_XDECREF(old); |
| 219 |
return 0; |
| 220 |
} else if ((size_t)i + (size_t)v.len < (size_t)v.len) { |
| 221 |
PyObject *old = v.items[i + v.len]; |
| 222 |
Py_INCREF(o); |
| 223 |
v.items[i + v.len] = o; |
| 224 |
Py_XDECREF(old); |
| 225 |
return 0; |
| 226 |
} else { |
| 227 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 228 |
return -1; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
static int vec_contains(PyObject *self, PyObject *value) { |
| 233 |
VecT v = ((VecTObject *)self)->vec; |
| 234 |
for (Py_ssize_t i = 0; i < v.len; i++) { |
| 235 |
PyObject *item = v.items[i]; |
| 236 |
if (item == value) { |
| 237 |
return 1; |
| 238 |
} |
| 239 |
Py_INCREF(item); |
| 240 |
int cmp = PyObject_RichCompareBool(item, value, Py_EQ); |
| 241 |
Py_DECREF(item); |
| 242 |
if (cmp != 0) |
| 243 |
return cmp; // 1 if equal, -1 on error |
| 244 |
} |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
static PyObject *vec_richcompare(PyObject *self, PyObject *other, int op) { |
| 249 |
PyObject *res; |
| 250 |
if (op == Py_EQ || op == Py_NE) { |
| 251 |
if (other->ob_type != &VecTType) { |
| 252 |
res = op == Py_EQ ? Py_False : Py_True; |
| 253 |
} else { |
| 254 |
VecT x = ((VecTObject *)self)->vec; |
| 255 |
VecT y = ((VecTObject *)other)->vec; |
| 256 |
if (VEC_BUF(x)->item_type != VEC_BUF(y)->item_type) { |
| 257 |
res = op == Py_EQ ? Py_False : Py_True; |
| 258 |
} else { |
| 259 |
// TODO: why pointers to len? |
| 260 |
return Vec_GenericRichcompare(&x.len, x.items, &y.len, y.items, op); |
| 261 |
} |
| 262 |
} |
| 263 |
} else |
| 264 |
res = Py_NotImplemented; |
| 265 |
Py_INCREF(res); |
| 266 |
return res; |
| 267 |
} |
| 268 |
|
| 269 |
// Append item to 'vec', stealing 'vec'. Return 'vec' with item appended. |
| 270 |
VecT VecT_Append(VecT vec, PyObject *x, size_t item_type) { |
| 271 |
if (vec.items == NULL) { |
| 272 |
VecT new = vec_alloc(1, item_type); |
| 273 |
if (VEC_IS_ERROR(new)) |
| 274 |
return new; |
| 275 |
Py_INCREF(x); |
| 276 |
new.len = 1; |
| 277 |
new.items[0] = x; |
| 278 |
vec_track_buffer(&new); |
| 279 |
return new; |
| 280 |
} |
| 281 |
Py_ssize_t cap = VEC_CAP(vec); |
| 282 |
Py_INCREF(x); |
| 283 |
if (vec.len < cap) { |
| 284 |
// Slot may have duplicate ref from prior remove/pop |
| 285 |
Py_XSETREF(vec.items[vec.len], x); |
| 286 |
vec.len++; |
| 287 |
return vec; |
| 288 |
} else { |
| 289 |
Py_ssize_t new_size = Vec_GrowCapacity(cap); |
| 290 |
// TODO: Avoid initializing to zero here |
| 291 |
VecT new = vec_alloc(new_size, VEC_BUF(vec)->item_type); |
| 292 |
if (VEC_IS_ERROR(new)) { |
| 293 |
Py_DECREF(x); |
| 294 |
// The input vec is being consumed/stolen by this function, so on error |
| 295 |
// we must decref it to avoid leaking the buffer. |
| 296 |
VEC_DECREF(vec); |
| 297 |
return new; |
| 298 |
} |
| 299 |
// Copy items to new vec. |
| 300 |
memcpy(new.items, vec.items, sizeof(PyObject *) * vec.len); |
| 301 |
memset(new.items + vec.len, 0, sizeof(PyObject *) * (new_size - vec.len)); |
| 302 |
VecTBufObject *old_buf = VEC_BUF(vec); |
| 303 |
if (Py_REFCNT(old_buf) > 1) { |
| 304 |
// Other references to old buffer exist; INCREF items in new buffer |
| 305 |
// so old buffer keeps valid references for aliases. |
| 306 |
for (Py_ssize_t i = 0; i < vec.len; i++) |
| 307 |
Py_XINCREF(new.items[i]); |
| 308 |
} else { |
| 309 |
// No aliases; transfer ownership by clearing old buffer items. |
| 310 |
memset(vec.items, 0, sizeof(PyObject *) * vec.len); |
| 311 |
} |
| 312 |
new.items[vec.len] = x; |
| 313 |
new.len = vec.len + 1; |
| 314 |
vec_track_buffer(&new); |
| 315 |
VEC_DECREF(vec); |
| 316 |
return new; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// Extend 'vec' with items from 'iterable', stealing 'vec'. |
| 321 |
// Return extended 'vec', or error vec on failure. |
| 322 |
VecT VecT_Extend(VecT vec, PyObject *iterable, size_t item_type) { |
| 323 |
if (VecT_Check(iterable)) { |
| 324 |
VecT src = ((VecTObject *)iterable)->vec; |
| 325 |
if (src.items != NULL && VEC_BUF(src)->item_type == item_type) { |
| 326 |
return VecT_ExtendVec(vec, src, item_type); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
PyObject *iter = PyObject_GetIter(iterable); |
| 331 |
if (iter == NULL) { |
| 332 |
VEC_DECREF(vec); |
| 333 |
return vec_error(); |
| 334 |
} |
| 335 |
PyObject *item; |
| 336 |
while ((item = PyIter_Next(iter)) != NULL) { |
| 337 |
if (!VecT_ItemCheck(vec, item, item_type)) { |
| 338 |
Py_DECREF(iter); |
| 339 |
VEC_DECREF(vec); |
| 340 |
Py_DECREF(item); |
| 341 |
return vec_error(); |
| 342 |
} |
| 343 |
vec = VecT_Append(vec, item, item_type); |
| 344 |
Py_DECREF(item); |
| 345 |
if (VEC_IS_ERROR(vec)) { |
| 346 |
Py_DECREF(iter); |
| 347 |
return vec_error(); |
| 348 |
} |
| 349 |
} |
| 350 |
Py_DECREF(iter); |
| 351 |
if (PyErr_Occurred()) { |
| 352 |
VEC_DECREF(vec); |
| 353 |
return vec_error(); |
| 354 |
} |
| 355 |
return vec; |
| 356 |
} |
| 357 |
|
| 358 |
// Extend 'dst' with items from 'src' vec, stealing 'dst', borrowing 'src'. |
| 359 |
// Return extended vec, or error vec on failure. |
| 360 |
VecT VecT_ExtendVec(VecT dst, VecT src, size_t item_type) { |
| 361 |
if (src.len == 0) |
| 362 |
return dst; |
| 363 |
if (src.len > PY_SSIZE_T_MAX - dst.len) { |
| 364 |
PyErr_NoMemory(); |
| 365 |
VEC_DECREF(dst); |
| 366 |
return vec_error(); |
| 367 |
} |
| 368 |
Py_ssize_t new_len = dst.len + src.len; |
| 369 |
if (dst.items == NULL) { |
| 370 |
// dst is empty, allocate new buf |
| 371 |
VecT new = vec_alloc(new_len, item_type); |
| 372 |
if (VEC_IS_ERROR(new)) { |
| 373 |
VEC_DECREF(dst); |
| 374 |
return new; |
| 375 |
} |
| 376 |
for (Py_ssize_t i = 0; i < src.len; i++) { |
| 377 |
Py_INCREF(src.items[i]); |
| 378 |
new.items[i] = src.items[i]; |
| 379 |
} |
| 380 |
memset(new.items + src.len, 0, sizeof(PyObject *) * (new_len - src.len)); |
| 381 |
new.len = new_len; |
| 382 |
vec_track_buffer(&new); |
| 383 |
return new; |
| 384 |
} |
| 385 |
Py_ssize_t cap = VEC_CAP(dst); |
| 386 |
if (new_len <= cap && dst.items != src.items) { |
| 387 |
// Fast path: enough capacity and no aliasing |
| 388 |
for (Py_ssize_t i = 0; i < src.len; i++) { |
| 389 |
Py_INCREF(src.items[i]); |
| 390 |
// Slot may have duplicate ref from prior remove/pop |
| 391 |
Py_XSETREF(dst.items[dst.len + i], src.items[i]); |
| 392 |
} |
| 393 |
dst.len = new_len; |
| 394 |
return dst; |
| 395 |
} |
| 396 |
// Need to reallocate (or dst and src share a buffer) |
| 397 |
Py_ssize_t new_cap = Vec_GrowCapacityTo(cap, new_len); |
| 398 |
int aliased = dst.items == src.items; |
| 399 |
VecT new = vec_alloc(new_cap, VEC_BUF(dst)->item_type); |
| 400 |
if (VEC_IS_ERROR(new)) { |
| 401 |
VEC_DECREF(dst); |
| 402 |
return new; |
| 403 |
} |
| 404 |
if (aliased) { |
| 405 |
// dst and src share a buffer -- incref all items instead of |
| 406 |
// moving refs, to avoid mutating the shared buffer |
| 407 |
for (Py_ssize_t i = 0; i < dst.len; i++) { |
| 408 |
Py_INCREF(dst.items[i]); |
| 409 |
new.items[i] = dst.items[i]; |
| 410 |
} |
| 411 |
} else { |
| 412 |
memcpy(new.items, dst.items, sizeof(PyObject *) * dst.len); |
| 413 |
VecTBufObject *dst_buf = VEC_BUF(dst); |
| 414 |
if (Py_REFCNT(dst_buf) > 1) { |
| 415 |
for (Py_ssize_t i = 0; i < dst.len; i++) |
| 416 |
Py_XINCREF(new.items[i]); |
| 417 |
} else { |
| 418 |
memset(dst.items, 0, sizeof(PyObject *) * dst.len); |
| 419 |
} |
| 420 |
} |
| 421 |
// Copy src items (incref each) |
| 422 |
for (Py_ssize_t i = 0; i < src.len; i++) { |
| 423 |
Py_INCREF(src.items[i]); |
| 424 |
new.items[dst.len + i] = src.items[i]; |
| 425 |
} |
| 426 |
memset(new.items + new_len, 0, sizeof(PyObject *) * (new_cap - new_len)); |
| 427 |
new.len = new_len; |
| 428 |
vec_track_buffer(&new); |
| 429 |
VEC_DECREF(dst); |
| 430 |
return new; |
| 431 |
} |
| 432 |
|
| 433 |
// Convert vec to list, stealing 'v'. |
| 434 |
PyObject *VecT_ToList(VecT v) { |
| 435 |
Py_ssize_t n = v.len; |
| 436 |
PyObject *list = PyList_New(n); |
| 437 |
if (list == NULL) { |
| 438 |
VEC_DECREF(v); |
| 439 |
return NULL; |
| 440 |
} |
| 441 |
if (n > 0 && Py_REFCNT(VEC_BUF(v)) == 1) { |
| 442 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 443 |
PyList_SET_ITEM(list, i, v.items[i]); |
| 444 |
v.items[i] = NULL; |
| 445 |
} |
| 446 |
} else { |
| 447 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 448 |
PyObject *item = v.items[i]; |
| 449 |
Py_INCREF(item); |
| 450 |
PyList_SET_ITEM(list, i, item); |
| 451 |
} |
| 452 |
} |
| 453 |
VEC_DECREF(v); |
| 454 |
return list; |
| 455 |
} |
| 456 |
|
| 457 |
// Convert vec to tuple, stealing 'v'. |
| 458 |
PyObject *VecT_ToTuple(VecT v) { |
| 459 |
Py_ssize_t n = v.len; |
| 460 |
PyObject *tuple = PyTuple_New(n); |
| 461 |
if (tuple == NULL) { |
| 462 |
VEC_DECREF(v); |
| 463 |
return NULL; |
| 464 |
} |
| 465 |
if (n > 0 && Py_REFCNT(VEC_BUF(v)) == 1) { |
| 466 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 467 |
PyTuple_SET_ITEM(tuple, i, v.items[i]); |
| 468 |
v.items[i] = NULL; |
| 469 |
} |
| 470 |
} else { |
| 471 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 472 |
PyObject *item = v.items[i]; |
| 473 |
Py_INCREF(item); |
| 474 |
PyTuple_SET_ITEM(tuple, i, item); |
| 475 |
} |
| 476 |
} |
| 477 |
VEC_DECREF(v); |
| 478 |
return tuple; |
| 479 |
} |
| 480 |
|
| 481 |
// Remove item from 'vec', stealing 'vec'. Return 'vec' with item removed. |
| 482 |
VecT VecT_Remove(VecT v, PyObject *arg) { |
| 483 |
PyObject **items = v.items; |
| 484 |
for (Py_ssize_t i = 0; i < v.len; i++) { |
| 485 |
int match = 0; |
| 486 |
if (items[i] == arg) |
| 487 |
match = 1; |
| 488 |
else { |
| 489 |
int itemcmp = PyObject_RichCompareBool(items[i], arg, Py_EQ); |
| 490 |
if (itemcmp < 0) { |
| 491 |
// The input v is being consumed/stolen by this function, so on error |
| 492 |
// we must decref it to avoid leaking the buffer. |
| 493 |
VEC_DECREF(v); |
| 494 |
return vec_error(); |
| 495 |
} |
| 496 |
match = itemcmp; |
| 497 |
} |
| 498 |
if (match) { |
| 499 |
if (i < v.len - 1) { |
| 500 |
Py_CLEAR(items[i]); |
| 501 |
for (; i < v.len - 1; i++) { |
| 502 |
items[i] = items[i + 1]; |
| 503 |
} |
| 504 |
// Keep a duplicate item, since there could be another reference |
| 505 |
// to the buffer with a longer length, and they expect a valid reference. |
| 506 |
Py_XINCREF(items[v.len - 1]); |
| 507 |
} |
| 508 |
v.len--; |
| 509 |
// Return the stolen reference without INCREF |
| 510 |
return v; |
| 511 |
} |
| 512 |
} |
| 513 |
PyErr_SetString(PyExc_ValueError, "vec.remove(x): x not in vec"); |
| 514 |
// The input v is being consumed/stolen by this function, so on error |
| 515 |
// we must decref it to avoid leaking the buffer. |
| 516 |
VEC_DECREF(v); |
| 517 |
return vec_error(); |
| 518 |
} |
| 519 |
|
| 520 |
// Pop item from 'vec', stealing 'vec'. Return struct with modified 'vec' and the popped item. |
| 521 |
VecTPopResult VecT_Pop(VecT v, Py_ssize_t index) { |
| 522 |
VecTPopResult result; |
| 523 |
|
| 524 |
if (index < 0) |
| 525 |
index += v.len; |
| 526 |
|
| 527 |
if (index < 0 || index >= v.len) { |
| 528 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 529 |
// The input v is being consumed/stolen by this function, so on error |
| 530 |
// we must decref it to avoid leaking the buffer. |
| 531 |
VEC_DECREF(v); |
| 532 |
result.f0 = vec_error(); |
| 533 |
result.f1 = NULL; |
| 534 |
return result; |
| 535 |
} |
| 536 |
|
| 537 |
PyObject **items = v.items; |
| 538 |
result.f1 = items[index]; |
| 539 |
for (Py_ssize_t i = index; i < v.len - 1; i++) |
| 540 |
items[i] = items[i + 1]; |
| 541 |
// Keep duplicate item, since there could be another reference |
| 542 |
// to the buffer with a longer length, and they expect a valid reference. |
| 543 |
Py_XINCREF(items[v.len - 1]); |
| 544 |
v.len--; |
| 545 |
// Return the stolen reference without INCREF |
| 546 |
result.f0 = v; |
| 547 |
return result; |
| 548 |
} |
| 549 |
|
| 550 |
static int |
| 551 |
VecT_traverse(VecTObject *self, visitproc visit, void *arg) |
| 552 |
{ |
| 553 |
if (self->vec.items) |
| 554 |
Py_VISIT(VEC_BUF(self->vec)); |
| 555 |
return 0; |
| 556 |
} |
| 557 |
|
| 558 |
static int |
| 559 |
VecT_clear(VecTObject *self) |
| 560 |
{ |
| 561 |
if (self->vec.items) { |
| 562 |
Py_DECREF(VEC_BUF(self->vec)); |
| 563 |
self->vec.items = NULL; |
| 564 |
} |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
static void |
| 569 |
VecT_dealloc(VecTObject *self) |
| 570 |
{ |
| 571 |
PyObject_GC_UnTrack(self); |
| 572 |
Py_TRASHCAN_BEGIN(self, VecT_dealloc) |
| 573 |
VecT_clear(self); |
| 574 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 575 |
Py_TRASHCAN_END |
| 576 |
} |
| 577 |
|
| 578 |
static int |
| 579 |
VecTBuf_traverse(VecTBufObject *self, visitproc visit, void *arg) |
| 580 |
{ |
| 581 |
Py_VISIT(VEC_BUF_ITEM_TYPE(self)); |
| 582 |
for (Py_ssize_t i = 0; i < VEC_BUF_SIZE(self); i++) { |
| 583 |
Py_VISIT(self->items[i]); |
| 584 |
} |
| 585 |
return 0; |
| 586 |
} |
| 587 |
|
| 588 |
static inline int |
| 589 |
VecTBuf_clear(VecTBufObject *self) |
| 590 |
{ |
| 591 |
if (self->item_type) { |
| 592 |
Py_DECREF(VEC_BUF_ITEM_TYPE(self)); |
| 593 |
self->item_type = 0; |
| 594 |
} |
| 595 |
for (Py_ssize_t i = 0; i < VEC_BUF_SIZE(self); i++) { |
| 596 |
Py_CLEAR(self->items[i]); |
| 597 |
} |
| 598 |
return 0; |
| 599 |
} |
| 600 |
|
| 601 |
static void |
| 602 |
VecTBuf_dealloc(VecTBufObject *self) |
| 603 |
{ |
| 604 |
PyObject_GC_UnTrack(self); |
| 605 |
Py_TRASHCAN_BEGIN(self, VecTBuf_dealloc) |
| 606 |
VecTBuf_clear(self); |
| 607 |
Py_TYPE(self)->tp_free((PyObject *)self); |
| 608 |
Py_TRASHCAN_END |
| 609 |
} |
| 610 |
|
| 611 |
static Py_ssize_t vec_length(PyObject *o) { |
| 612 |
return ((VecTObject *)o)->vec.len; |
| 613 |
} |
| 614 |
|
| 615 |
static PyMappingMethods VecTMapping = { |
| 616 |
.mp_length = vec_length, |
| 617 |
.mp_subscript = vec_subscript, |
| 618 |
}; |
| 619 |
|
| 620 |
static PySequenceMethods VecTSequence = { |
| 621 |
.sq_item = vec_get_item, |
| 622 |
.sq_ass_item = vec_ass_item, |
| 623 |
.sq_contains = vec_contains, |
| 624 |
}; |
| 625 |
|
| 626 |
static PyMethodDef vec_methods[] = { |
| 627 |
{NULL, NULL, 0, NULL}, /* Sentinel */ |
| 628 |
}; |
| 629 |
|
| 630 |
// Iterator type for vec[T] (reference types) |
| 631 |
|
| 632 |
typedef struct { |
| 633 |
PyObject_HEAD |
| 634 |
VecT vec; // Unboxed vec (keeps buffer alive via items reference) |
| 635 |
Py_ssize_t index; // Current iteration index |
| 636 |
} VecTIterObject; |
| 637 |
|
| 638 |
PyTypeObject VecTIterType; |
| 639 |
|
| 640 |
static PyObject *VecT_iter(PyObject *self) { |
| 641 |
VecTIterObject *it = PyObject_GC_New(VecTIterObject, &VecTIterType); |
| 642 |
if (it == NULL) |
| 643 |
return NULL; |
| 644 |
it->vec = ((VecTObject *)self)->vec; |
| 645 |
VEC_INCREF(it->vec); |
| 646 |
it->index = 0; |
| 647 |
PyObject_GC_Track(it); |
| 648 |
return (PyObject *)it; |
| 649 |
} |
| 650 |
|
| 651 |
static int |
| 652 |
VecTIter_traverse(VecTIterObject *self, visitproc visit, void *arg) |
| 653 |
{ |
| 654 |
if (self->vec.items) |
| 655 |
Py_VISIT(VEC_BUF(self->vec)); |
| 656 |
return 0; |
| 657 |
} |
| 658 |
|
| 659 |
static int |
| 660 |
VecTIter_clear(VecTIterObject *self) |
| 661 |
{ |
| 662 |
if (self->vec.items) { |
| 663 |
Py_DECREF(VEC_BUF(self->vec)); |
| 664 |
self->vec.items = NULL; |
| 665 |
} |
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
static void VecTIter_dealloc(VecTIterObject *self) { |
| 670 |
PyObject_GC_UnTrack(self); |
| 671 |
VEC_DECREF(self->vec); |
| 672 |
PyObject_GC_Del(self); |
| 673 |
} |
| 674 |
|
| 675 |
static PyObject *VecTIter_next(VecTIterObject *self) { |
| 676 |
if (self->vec.items == NULL) |
| 677 |
return NULL; |
| 678 |
if (self->index < self->vec.len) { |
| 679 |
PyObject *item = self->vec.items[self->index]; |
| 680 |
self->index++; |
| 681 |
Py_INCREF(item); |
| 682 |
return item; |
| 683 |
} |
| 684 |
VEC_DECREF(self->vec); |
| 685 |
self->vec.items = NULL; |
| 686 |
return NULL; // StopIteration |
| 687 |
} |
| 688 |
|
| 689 |
static PyObject *VecTIter_len(VecTIterObject *self, PyObject *Py_UNUSED(ignored)) { |
| 690 |
if (self->vec.items == NULL) |
| 691 |
return PyLong_FromSsize_t(0); |
| 692 |
Py_ssize_t remaining = self->vec.len - self->index; |
| 693 |
if (remaining < 0) |
| 694 |
remaining = 0; |
| 695 |
return PyLong_FromSsize_t(remaining); |
| 696 |
} |
| 697 |
|
| 698 |
static PyMethodDef VecTIter_methods[] = { |
| 699 |
{"__length_hint__", (PyCFunction)VecTIter_len, METH_NOARGS, NULL}, |
| 700 |
{NULL, NULL, 0, NULL}, |
| 701 |
}; |
| 702 |
|
| 703 |
PyTypeObject VecTIterType = { |
| 704 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 705 |
.tp_name = "vec_iterator", |
| 706 |
.tp_basicsize = sizeof(VecTIterObject), |
| 707 |
.tp_itemsize = 0, |
| 708 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 709 |
.tp_traverse = (traverseproc)VecTIter_traverse, |
| 710 |
.tp_clear = (inquiry)VecTIter_clear, |
| 711 |
.tp_dealloc = (destructor)VecTIter_dealloc, |
| 712 |
.tp_iter = PyObject_SelfIter, |
| 713 |
.tp_iternext = (iternextfunc)VecTIter_next, |
| 714 |
.tp_methods = VecTIter_methods, |
| 715 |
}; |
| 716 |
|
| 717 |
PyTypeObject VecTBufType = { |
| 718 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 719 |
.tp_name = "vecbuf", |
| 720 |
.tp_doc = "Internal data buffer used by vec objects", |
| 721 |
.tp_basicsize = sizeof(VecTBufObject) - sizeof(PyObject *), |
| 722 |
.tp_itemsize = sizeof(PyObject *), |
| 723 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 724 |
.tp_traverse = (traverseproc)VecTBuf_traverse, |
| 725 |
//.tp_new = vecbuf_i64_new, //?? |
| 726 |
.tp_free = PyObject_GC_Del, |
| 727 |
.tp_clear = (inquiry)VecTBuf_clear, |
| 728 |
.tp_dealloc = (destructor)VecTBuf_dealloc, |
| 729 |
}; |
| 730 |
|
| 731 |
PyTypeObject VecTType = { |
| 732 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 733 |
.tp_name = "vec", |
| 734 |
.tp_doc = "Mutable sequence-like container optimized for compilation with mypyc", |
| 735 |
.tp_basicsize = sizeof(VecTObject), |
| 736 |
.tp_itemsize = 0, |
| 737 |
.tp_base = &VecType, // Inherit from base vec type for isinstance() support |
| 738 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 739 |
.tp_traverse = (traverseproc)VecT_traverse, |
| 740 |
.tp_clear = (inquiry)VecT_clear, |
| 741 |
.tp_dealloc = (destructor)VecT_dealloc, |
| 742 |
//.tp_free = PyObject_GC_Del, |
| 743 |
.tp_repr = (reprfunc)vec_repr, |
| 744 |
.tp_iter = VecT_iter, |
| 745 |
.tp_as_sequence = &VecTSequence, |
| 746 |
.tp_as_mapping = &VecTMapping, |
| 747 |
.tp_richcompare = vec_richcompare, |
| 748 |
.tp_methods = vec_methods, |
| 749 |
// TODO: free |
| 750 |
}; |
| 751 |
|
| 752 |
static inline VecT vec_from_sequence( |
| 753 |
size_t item_type, PyObject *seq, int64_t cap, const int is_list) { |
| 754 |
Py_ssize_t n = is_list ? PyList_GET_SIZE(seq) : PyTuple_GET_SIZE(seq); |
| 755 |
Py_ssize_t alloc_size = n > cap ? n : cap; |
| 756 |
VecT v = vec_alloc(alloc_size, item_type); |
| 757 |
if (VEC_IS_ERROR(v)) |
| 758 |
return vec_error(); |
| 759 |
#ifdef Py_GIL_DISABLED |
| 760 |
if (is_list) { |
| 761 |
// Other threads could be mutating the list concurrently, so use strong references |
| 762 |
Py_ssize_t actual = 0; |
| 763 |
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(seq) && i < n; i++) { |
| 764 |
PyObject *item = PyList_GetItemRef(seq, i); |
| 765 |
if (unlikely(item == NULL)) { |
| 766 |
// Race condition: list shrank between size read and get item |
| 767 |
for (Py_ssize_t j = actual; j < alloc_size; j++) |
| 768 |
v.items[j] = NULL; |
| 769 |
VEC_DECREF(v); |
| 770 |
return vec_error(); |
| 771 |
} |
| 772 |
if (!VecT_ItemCheck(v, item, item_type)) { |
| 773 |
Py_DECREF(item); |
| 774 |
for (Py_ssize_t j = actual; j < alloc_size; j++) |
| 775 |
v.items[j] = NULL; |
| 776 |
VEC_DECREF(v); |
| 777 |
return vec_error(); |
| 778 |
} |
| 779 |
v.items[actual++] = item; |
| 780 |
} |
| 781 |
for (Py_ssize_t j = actual; j < alloc_size; j++) |
| 782 |
v.items[j] = NULL; |
| 783 |
vec_track_buffer(&v); |
| 784 |
v.len = actual; |
| 785 |
return v; |
| 786 |
} |
| 787 |
// Tuples are immutable, so fall through to the shared loop below |
| 788 |
#endif |
| 789 |
for (Py_ssize_t i = 0; i < n; i++) { |
| 790 |
PyObject *item = is_list ? PyList_GET_ITEM(seq, i) : PyTuple_GET_ITEM(seq, i); |
| 791 |
if (!VecT_ItemCheck(v, item, item_type)) { |
| 792 |
for (Py_ssize_t j = i; j < alloc_size; j++) |
| 793 |
v.items[j] = NULL; |
| 794 |
VEC_DECREF(v); |
| 795 |
return vec_error(); |
| 796 |
} |
| 797 |
Py_INCREF(item); |
| 798 |
v.items[i] = item; |
| 799 |
} |
| 800 |
for (Py_ssize_t j = n; j < alloc_size; j++) |
| 801 |
v.items[j] = NULL; |
| 802 |
vec_track_buffer(&v); |
| 803 |
v.len = n; |
| 804 |
return v; |
| 805 |
} |
| 806 |
|
| 807 |
VecT VecT_FromIterable(size_t item_type, PyObject *iterable, int64_t cap) { |
| 808 |
if (PyList_CheckExact(iterable)) { |
| 809 |
return vec_from_sequence(item_type, iterable, cap, 1); |
| 810 |
} else if (PyTuple_CheckExact(iterable)) { |
| 811 |
return vec_from_sequence(item_type, iterable, cap, 0); |
| 812 |
} |
| 813 |
|
| 814 |
VecT v = vec_alloc(cap, item_type); |
| 815 |
if (VEC_IS_ERROR(v)) |
| 816 |
return vec_error(); |
| 817 |
if (cap > 0) { |
| 818 |
for (int64_t i = 0; i < cap; i++) |
| 819 |
v.items[i] = NULL; |
| 820 |
} |
| 821 |
v.len = 0; |
| 822 |
vec_track_buffer(&v); |
| 823 |
|
| 824 |
PyObject *iter = PyObject_GetIter(iterable); |
| 825 |
if (iter == NULL) { |
| 826 |
VEC_DECREF(v); |
| 827 |
return vec_error(); |
| 828 |
} |
| 829 |
PyObject *item; |
| 830 |
while ((item = PyIter_Next(iter)) != NULL) { |
| 831 |
if (!VecT_ItemCheck(v, item, item_type)) { |
| 832 |
Py_DECREF(iter); |
| 833 |
VEC_DECREF(v); |
| 834 |
Py_DECREF(item); |
| 835 |
return vec_error(); |
| 836 |
} |
| 837 |
v = VecT_Append(v, item, item_type); |
| 838 |
Py_DECREF(item); |
| 839 |
if (VEC_IS_ERROR(v)) { |
| 840 |
Py_DECREF(iter); |
| 841 |
VEC_DECREF(v); |
| 842 |
return vec_error(); |
| 843 |
} |
| 844 |
} |
| 845 |
Py_DECREF(iter); |
| 846 |
if (PyErr_Occurred()) { |
| 847 |
VEC_DECREF(v); |
| 848 |
return vec_error(); |
| 849 |
} |
| 850 |
return v; |
| 851 |
} |
| 852 |
|
| 853 |
VecTAPI Vec_TAPI = { |
| 854 |
&VecTType, |
| 855 |
&VecTBufType, |
| 856 |
VecT_New, |
| 857 |
VecT_Box, |
| 858 |
VecT_Unbox, |
| 859 |
VecT_ConvertFromNested, |
| 860 |
VecT_Append, |
| 861 |
VecT_Pop, |
| 862 |
VecT_Remove, |
| 863 |
VecT_Slice, |
| 864 |
VecT_FromIterable, |
| 865 |
VecT_Extend, |
| 866 |
VecT_ExtendVec, |
| 867 |
VecT_ToList, |
| 868 |
VecT_ToTuple, |
| 869 |
}; |