| 1 |
// Definition of the mypyc.vecs module, which implements 'vec' and related functionality. |
| 2 |
// |
| 3 |
// NOTE: This is still experimental and work in progress. |
| 4 |
// |
| 5 |
// vec[t] is a sequence type that is optimized for use in mypyc-compiled code. |
| 6 |
// Performance in interpreted code is a secondary concern. It's primarily optimized |
| 7 |
// to provide fast item get/set operations and fast len(), especially for primitive |
| 8 |
// value item types, such as 'i32', 'float' or 'bool'. |
| 9 |
// |
| 10 |
// Example: |
| 11 |
// |
| 12 |
// from mypy_extensions import i64 |
| 13 |
// |
| 14 |
// v = vec[i64]() |
| 15 |
// v = append(v, 123) |
| 16 |
// print(v[0]) |
| 17 |
// v[-1] = 234 |
| 18 |
// |
| 19 |
// Key properties: |
| 20 |
// |
| 21 |
// * In native code, a vec instance is an immutable value type with two fields |
| 22 |
// (length, item buffer). |
| 23 |
// * The item buffer can only store types of a specific type (enforced at runtime). |
| 24 |
// * There are specialized item buffer types for certain primitive value item types, |
| 25 |
// such as 'i32', 'float' and 'bool'. These use a packed value encoding. |
| 26 |
// * Since vec values are immutable (only the buffer is mutable), any operation |
| 27 |
// that changes the length (such as append) must return the updated vec value. |
| 28 |
// * The buffer often a has higher capacity than the length of a vec. Append |
| 29 |
// operations only allocate a new buffer if the capacity is exhausted, for O(1) |
| 30 |
// amortized time complexity for appends. |
| 31 |
// * Vec values can be boxed to PyObject *; this happens transparently and uses |
| 32 |
// a wrapper object. |
| 33 |
// * The item type can be a class type, or 't | None' if t is a class type. However, |
| 34 |
// optional value item types such as 'float | None' are not supported, and 'int' |
| 35 |
// is not valid as item type -- a native integer type such as `i64` or `u8` must |
| 36 |
// be used instead. |
| 37 |
// * Nested vecs are also supported (e.g. vec[vec[i32]]). |
| 38 |
// * All features are also available in interpreted code, but often at some |
| 39 |
// performance cost compared to using plain lists. |
| 40 |
// |
| 41 |
// Implementation summary: |
| 42 |
// |
| 43 |
// * In a non-native context, instances are constructed via a temporary VecGenericAlias |
| 44 |
// object, which is the result of 'vec[<t>]' (indexing the 'vec' type). |
| 45 |
// * The module exports a C API via a capsule that mypyc compiled code uses for most |
| 46 |
// operations. |
| 47 |
// * There are multiple C structs that represent vec value objects with different item |
| 48 |
// types, such as VecI64 and VecT. The latter is for arbitrary reference objects |
| 49 |
// ('PyObject *' items internally). |
| 50 |
// * There are also multiple buffer types, such as VecI64Buf and VecTBuf. |
| 51 |
// * Similarly, there are multiple Python wrapper object types for boxed vecs, such as |
| 52 |
// VecI64Type and VecTType. |
| 53 |
// * An empty vec in compiled code can use a NULL pointer to represent the buffer, |
| 54 |
// saving an object allocation in a common use case. |
| 55 |
// |
| 56 |
// Motivation: |
| 57 |
// * A vec with value type items, such as vec[i64] or vec[vec[i32]], is very efficient |
| 58 |
// compared to a list object, since values are not boxed, there is no need for runtime |
| 59 |
// type checks on read, and index overflow checks are very fast due to the immutable |
| 60 |
// value encoding. |
| 61 |
// * A vec with a reference item type, such as vec[str], is often faster than a list, |
| 62 |
// since there is no need for a cast on item read and the index checks are faster |
| 63 |
// (among other things), but here the benefit is much smaller compared to using a |
| 64 |
// value item type. |
| 65 |
// |
| 66 |
// Summary of files: |
| 67 |
// * vec_t.c implements vec[t] and vec[t | None] for reference types |
| 68 |
// * vec_nested.c implements nested vecs, such as vec[vec[str]] |
| 69 |
// * vec_template.c is #included in multiples files (e.g. vec_i64.c) to produce |
| 70 |
// specialized code for vecs with value item types (e.g. vec[i64]) |
| 71 |
// * This file defines the C extension and has some shared code |
| 72 |
|
| 73 |
#define PY_SSIZE_T_CLEAN |
| 74 |
#include <Python.h> |
| 75 |
#include "librt_vecs.h" |
| 76 |
|
| 77 |
PyTypeObject *LibRTVecs_I64TypeObj; |
| 78 |
PyTypeObject *LibRTVecs_I32TypeObj; |
| 79 |
PyTypeObject *LibRTVecs_I16TypeObj; |
| 80 |
PyTypeObject *LibRTVecs_U8TypeObj; |
| 81 |
|
| 82 |
|
| 83 |
// vec generic alias |
| 84 |
// |
| 85 |
// Used for the result of vec[t] (indexing) in interpreted context that must preserve |
| 86 |
// knowledge of 't'. These aren't real types. This only supports constructing instances. |
| 87 |
typedef struct { |
| 88 |
PyObject_HEAD |
| 89 |
// Tagged pointer to PyTypeObject *, lowest bit set for optional item type |
| 90 |
// Can also be one of magic VEC_ITEM_TYPE_* constants |
| 91 |
size_t item_type; |
| 92 |
size_t depth; // Number of nested VecNested or VecT types |
| 93 |
} VecGenericAlias; |
| 94 |
|
| 95 |
static PyObject *vec_generic_alias_call(PyObject *self, PyObject *args, PyObject *kw) |
| 96 |
{ |
| 97 |
static char *kwlist[] = {"", "capacity", NULL}; |
| 98 |
PyObject *init = NULL; |
| 99 |
int64_t cap = 0; |
| 100 |
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OL:vec", kwlist, &init, &cap)) { |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
if (cap < 0) { |
| 104 |
PyErr_SetString(PyExc_ValueError, "capacity must not be negative"); |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
VecGenericAlias *p = (VecGenericAlias *)self; |
| 108 |
if (p->depth == 0) { |
| 109 |
if (init == NULL) { |
| 110 |
VecT vec = VecT_New(0, cap, p->item_type); |
| 111 |
if (VEC_IS_ERROR(vec)) |
| 112 |
return NULL; |
| 113 |
return VecT_Box(vec, p->item_type); |
| 114 |
} else { |
| 115 |
VecT vec = VecT_FromIterable(p->item_type, init, cap); |
| 116 |
if (VEC_IS_ERROR(vec)) |
| 117 |
return NULL; |
| 118 |
return VecT_Box(vec, p->item_type); |
| 119 |
} |
| 120 |
} else { |
| 121 |
if (init == NULL) { |
| 122 |
VecNested vec = VecNested_New(0, cap, p->item_type, p->depth); |
| 123 |
if (VEC_IS_ERROR(vec)) |
| 124 |
return NULL; |
| 125 |
return VecNested_Box(vec); |
| 126 |
} else { |
| 127 |
return VecNested_FromIterable(p->item_type, p->depth, init, cap); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
static int |
| 133 |
VecGenericAlias_traverse(VecGenericAlias *self, visitproc visit, void *arg) |
| 134 |
{ |
| 135 |
if (!Vec_IsMagicItemType(self->item_type)) |
| 136 |
Py_VISIT((PyObject *)(self->item_type & ~1)); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
static void |
| 141 |
VecGenericAlias_dealloc(VecGenericAlias *self) |
| 142 |
{ |
| 143 |
PyObject_GC_UnTrack(self); |
| 144 |
if (self->item_type && !Vec_IsMagicItemType(self->item_type)) { |
| 145 |
Py_DECREF((PyObject *)(self->item_type & ~1)); |
| 146 |
self->item_type = 0; |
| 147 |
} |
| 148 |
PyObject_GC_Del(self); |
| 149 |
} |
| 150 |
|
| 151 |
PyObject *Vec_TypeToStr(size_t item_type, size_t depth) { |
| 152 |
PyObject *item = NULL; |
| 153 |
PyObject *result = NULL; |
| 154 |
|
| 155 |
if (depth == 0) { |
| 156 |
if ((item_type & ~1) == VEC_ITEM_TYPE_I64) { |
| 157 |
item = PyUnicode_FromFormat("i64"); |
| 158 |
} else if ((item_type & ~1) == VEC_ITEM_TYPE_U8) { |
| 159 |
item = PyUnicode_FromFormat("u8"); |
| 160 |
} else if ((item_type & ~1) == VEC_ITEM_TYPE_FLOAT) { |
| 161 |
item = PyUnicode_FromFormat("float"); |
| 162 |
} else if ((item_type & ~1) == VEC_ITEM_TYPE_I32) { |
| 163 |
item = PyUnicode_FromFormat("i32"); |
| 164 |
} else if ((item_type & ~1) == VEC_ITEM_TYPE_I16) { |
| 165 |
item = PyUnicode_FromFormat("i16"); |
| 166 |
} else if ((item_type & ~1) == VEC_ITEM_TYPE_BOOL) { |
| 167 |
item = PyUnicode_FromFormat("bool"); |
| 168 |
} else { |
| 169 |
item = PyObject_GetAttrString((PyObject *)(item_type & ~1), "__name__"); |
| 170 |
if (item == NULL) { |
| 171 |
return NULL; |
| 172 |
} |
| 173 |
if (item_type & 1) { |
| 174 |
PyObject *optional_item = PyUnicode_FromFormat("%U | None", item); |
| 175 |
Py_DECREF(item); |
| 176 |
if (optional_item == NULL) { |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
item = optional_item; |
| 180 |
} |
| 181 |
} |
| 182 |
} else { |
| 183 |
item = Vec_TypeToStr(item_type, depth - 1); |
| 184 |
} |
| 185 |
|
| 186 |
if (item == NULL) { |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
result = PyUnicode_FromFormat("vec[%U]", item); |
| 191 |
Py_DECREF(item); |
| 192 |
return result; |
| 193 |
} |
| 194 |
|
| 195 |
PyObject *VecGenericAlias_repr(PyObject *self) { |
| 196 |
PyObject *l = NULL; |
| 197 |
PyObject *prefix = NULL; |
| 198 |
PyObject *suffix = NULL; |
| 199 |
PyObject *sep = NULL; |
| 200 |
PyObject *type_str = NULL; |
| 201 |
PyObject *result = NULL; |
| 202 |
|
| 203 |
l = PyList_New(0); |
| 204 |
if (l == NULL) goto error; |
| 205 |
|
| 206 |
prefix = PyUnicode_FromString("<class_proxy '"); |
| 207 |
if (prefix == NULL) goto error; |
| 208 |
|
| 209 |
suffix = PyUnicode_FromString("'>"); |
| 210 |
if (suffix == NULL) goto error; |
| 211 |
|
| 212 |
sep = PyUnicode_FromString(""); |
| 213 |
if (sep == NULL) goto error; |
| 214 |
|
| 215 |
if (PyList_Append(l, prefix) < 0) goto error; |
| 216 |
|
| 217 |
VecGenericAlias *v = (VecGenericAlias *)self; |
| 218 |
type_str = Vec_TypeToStr(v->item_type, v->depth); |
| 219 |
if (type_str == NULL) goto error; |
| 220 |
|
| 221 |
if (PyList_Append(l, type_str) < 0) goto error; |
| 222 |
if (PyList_Append(l, suffix) < 0) goto error; |
| 223 |
|
| 224 |
result = PyUnicode_Join(sep, l); |
| 225 |
|
| 226 |
error: |
| 227 |
Py_XDECREF(l); |
| 228 |
Py_XDECREF(prefix); |
| 229 |
Py_XDECREF(suffix); |
| 230 |
Py_XDECREF(sep); |
| 231 |
Py_XDECREF(type_str); |
| 232 |
return result; |
| 233 |
} |
| 234 |
|
| 235 |
PyTypeObject VecGenericAliasType = { |
| 236 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 237 |
.tp_name = "vec_generic_alias", |
| 238 |
.tp_basicsize = sizeof(VecGenericAlias), |
| 239 |
.tp_itemsize = 0, |
| 240 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, |
| 241 |
.tp_call = vec_generic_alias_call, |
| 242 |
.tp_traverse = (traverseproc)VecGenericAlias_traverse, |
| 243 |
.tp_dealloc = (destructor)VecGenericAlias_dealloc, |
| 244 |
.tp_repr = (reprfunc)VecGenericAlias_repr, |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
// The 'vec' type |
| 249 |
// |
| 250 |
// This cannot be instantiated, and it's only used for isinstance and indexing: vec[T]. |
| 251 |
// All specialized vec types (VecI64Type, VecI32Type, etc.) inherit from this base type, |
| 252 |
// so isinstance(v, vec) returns True for any specialized vec instance. |
| 253 |
|
| 254 |
static PyObject *extract_optional_item(PyObject *item) { |
| 255 |
PyObject *args = PyObject_GetAttrString(item, "__args__"); |
| 256 |
if (args == NULL) { |
| 257 |
PyErr_Clear(); |
| 258 |
return NULL; |
| 259 |
} |
| 260 |
if (!PyTuple_CheckExact(args)) |
| 261 |
goto error; |
| 262 |
if (PyTuple_GET_SIZE(args) != 2) |
| 263 |
goto error; |
| 264 |
PyObject *item0 = PyTuple_GET_ITEM(args, 0); |
| 265 |
PyObject *item1 = PyTuple_GET_ITEM(args, 1); |
| 266 |
if (item0 == (PyObject *)Py_None->ob_type) { |
| 267 |
Py_DECREF(args); |
| 268 |
return item1; |
| 269 |
} else if (item1 == (PyObject *)Py_None->ob_type) { |
| 270 |
Py_DECREF(args); |
| 271 |
return item0; |
| 272 |
} |
| 273 |
error: |
| 274 |
Py_DECREF(args); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
|
| 278 |
// Evaluation vec[<type>] in interpreted code and produce a generic alias object. |
| 279 |
static PyObject *vec_class_getitem(PyObject *type, PyObject *item) |
| 280 |
{ |
| 281 |
if (item == (PyObject *)LibRTVecs_I64TypeObj) { |
| 282 |
Py_INCREF(&VecI64Type); |
| 283 |
return (PyObject *)&VecI64Type; |
| 284 |
} else if (item == (PyObject *)LibRTVecs_U8TypeObj) { |
| 285 |
Py_INCREF(&VecU8Type); |
| 286 |
return (PyObject *)&VecU8Type; |
| 287 |
} else if (item == (PyObject *)&PyFloat_Type) { |
| 288 |
Py_INCREF(&VecFloatType); |
| 289 |
return (PyObject *)&VecFloatType; |
| 290 |
} else if (item == (PyObject *)LibRTVecs_I32TypeObj) { |
| 291 |
Py_INCREF(&VecI32Type); |
| 292 |
return (PyObject *)&VecI32Type; |
| 293 |
} else if (item == (PyObject *)LibRTVecs_I16TypeObj) { |
| 294 |
Py_INCREF(&VecI16Type); |
| 295 |
return (PyObject *)&VecI16Type; |
| 296 |
} else if (item == (PyObject *)&PyBool_Type) { |
| 297 |
Py_INCREF(&VecBoolType); |
| 298 |
return (PyObject *)&VecBoolType; |
| 299 |
} else { |
| 300 |
size_t depth = 0; |
| 301 |
size_t item_type; |
| 302 |
int optional = 0; |
| 303 |
if (!PyObject_TypeCheck(item, &PyType_Type)) { |
| 304 |
PyObject *it = extract_optional_item(item); |
| 305 |
if (it != NULL) { |
| 306 |
optional = 1; |
| 307 |
item = it; |
| 308 |
// Check if this is a specialized value type that doesn't support None |
| 309 |
if (item == (PyObject *)LibRTVecs_I64TypeObj || |
| 310 |
item == (PyObject *)LibRTVecs_U8TypeObj || |
| 311 |
item == (PyObject *)&PyFloat_Type || |
| 312 |
item == (PyObject *)LibRTVecs_I32TypeObj || |
| 313 |
item == (PyObject *)LibRTVecs_I16TypeObj || |
| 314 |
item == (PyObject *)&PyBool_Type) { |
| 315 |
PyErr_SetString(PyExc_TypeError, |
| 316 |
"vec does not support optional value types (use vec[object] instead)"); |
| 317 |
return NULL; |
| 318 |
} |
| 319 |
} |
| 320 |
if (item->ob_type == &VecGenericAliasType) { |
| 321 |
if (optional) { |
| 322 |
PyErr_SetString(PyExc_TypeError, "optional type not expected in vec[...]"); |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
VecGenericAlias *p = (VecGenericAlias *)item; |
| 326 |
item_type = p->item_type; |
| 327 |
depth = p->depth + 1; |
| 328 |
} else if (!PyObject_TypeCheck(item, &PyType_Type)) { |
| 329 |
PyErr_SetString(PyExc_TypeError, "type object expected in vec[...]"); |
| 330 |
return NULL; |
| 331 |
} else { |
| 332 |
item_type = (size_t)item | optional; |
| 333 |
} |
| 334 |
} else { |
| 335 |
if (item == (PyObject *)&VecI64Type) { |
| 336 |
item_type = VEC_ITEM_TYPE_I64; |
| 337 |
depth = 1; |
| 338 |
} else if (item == (PyObject *)&VecU8Type) { |
| 339 |
item_type = VEC_ITEM_TYPE_U8; |
| 340 |
depth = 1; |
| 341 |
} else if (item == (PyObject *)&VecFloatType) { |
| 342 |
item_type = VEC_ITEM_TYPE_FLOAT; |
| 343 |
depth = 1; |
| 344 |
} else if (item == (PyObject *)&VecI32Type) { |
| 345 |
item_type = VEC_ITEM_TYPE_I32; |
| 346 |
depth = 1; |
| 347 |
} else if (item == (PyObject *)&VecI16Type) { |
| 348 |
item_type = VEC_ITEM_TYPE_I16; |
| 349 |
depth = 1; |
| 350 |
} else if (item == (PyObject *)&VecBoolType) { |
| 351 |
item_type = VEC_ITEM_TYPE_BOOL; |
| 352 |
depth = 1; |
| 353 |
} else { |
| 354 |
item_type = (size_t)item; |
| 355 |
} |
| 356 |
} |
| 357 |
if (item == (PyObject *)&PyLong_Type) { |
| 358 |
PyErr_Format(PyExc_ValueError, "unsupported type in vec[%s] (use i64, i32, i16, or u8)", |
| 359 |
((PyTypeObject *)item)->tp_name); |
| 360 |
return NULL; |
| 361 |
} |
| 362 |
VecGenericAlias *p; |
| 363 |
p = PyObject_GC_New(VecGenericAlias, &VecGenericAliasType); |
| 364 |
if (p == NULL) |
| 365 |
return NULL; |
| 366 |
Py_INCREF(item); |
| 367 |
p->item_type = item_type; |
| 368 |
p->depth = depth; |
| 369 |
PyObject_GC_Track(p); |
| 370 |
return (PyObject *)p; |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
static PyMethodDef vec_methods[] = { |
| 375 |
{"__class_getitem__", vec_class_getitem, METH_O|METH_CLASS, NULL}, |
| 376 |
{NULL, NULL, 0, NULL}, /* Sentinel */ |
| 377 |
}; |
| 378 |
|
| 379 |
PyTypeObject VecType = { |
| 380 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 381 |
.tp_name = "vec", |
| 382 |
.tp_basicsize = sizeof(VecBaseObject), |
| 383 |
.tp_itemsize = 0, |
| 384 |
.tp_flags = Py_TPFLAGS_DEFAULT, // No Py_TPFLAGS_BASETYPE - prevent Python subclassing |
| 385 |
.tp_methods = vec_methods, |
| 386 |
}; |
| 387 |
|
| 388 |
// Common helpers |
| 389 |
|
| 390 |
PyObject *Vec_GenericRepr(PyObject *vec, size_t item_type, size_t depth, int verbose) { |
| 391 |
PyObject *l = NULL; |
| 392 |
PyObject *prefix = NULL; |
| 393 |
PyObject *mid = NULL; |
| 394 |
PyObject *suffix = NULL; |
| 395 |
PyObject *sep = NULL; |
| 396 |
PyObject *comma = NULL; |
| 397 |
PyObject *result = NULL; |
| 398 |
|
| 399 |
l = PyList_New(0); |
| 400 |
if (l == NULL) goto error; |
| 401 |
|
| 402 |
sep = PyUnicode_FromString(""); |
| 403 |
if (sep == NULL) goto error; |
| 404 |
|
| 405 |
comma = PyUnicode_FromString(", "); |
| 406 |
if (comma == NULL) goto error; |
| 407 |
|
| 408 |
if (verbose) { |
| 409 |
prefix = Vec_TypeToStr(item_type, depth); |
| 410 |
if (prefix == NULL) goto error; |
| 411 |
|
| 412 |
mid = PyUnicode_FromString("(["); |
| 413 |
if (mid == NULL) goto error; |
| 414 |
|
| 415 |
suffix = PyUnicode_FromString("])"); |
| 416 |
if (suffix == NULL) goto error; |
| 417 |
} else { |
| 418 |
prefix = PyUnicode_FromString(""); |
| 419 |
if (prefix == NULL) goto error; |
| 420 |
|
| 421 |
mid = PyUnicode_FromString("["); |
| 422 |
if (mid == NULL) goto error; |
| 423 |
|
| 424 |
suffix = PyUnicode_FromString("]"); |
| 425 |
if (suffix == NULL) goto error; |
| 426 |
} |
| 427 |
|
| 428 |
if (PyList_Append(l, prefix) < 0) goto error; |
| 429 |
if (PyList_Append(l, mid) < 0) goto error; |
| 430 |
|
| 431 |
Py_ssize_t len = PyObject_Length(vec); |
| 432 |
if (len < 0) goto error; |
| 433 |
|
| 434 |
for (Py_ssize_t i = 0; i < len; i++) { |
| 435 |
PyObject *it = PySequence_GetItem(vec, i); |
| 436 |
if (it == NULL) goto error; |
| 437 |
|
| 438 |
PyObject *r; |
| 439 |
if (depth == 0 || it == Py_None) { |
| 440 |
r = PyObject_Repr(it); |
| 441 |
} else { |
| 442 |
r = Vec_GenericRepr(it, item_type, depth - 1, 0); |
| 443 |
} |
| 444 |
Py_DECREF(it); |
| 445 |
|
| 446 |
if (r == NULL) goto error; |
| 447 |
|
| 448 |
if (PyList_Append(l, r) < 0) { |
| 449 |
Py_DECREF(r); |
| 450 |
goto error; |
| 451 |
} |
| 452 |
Py_DECREF(r); |
| 453 |
|
| 454 |
if (i + 1 < len) { |
| 455 |
if (PyList_Append(l, comma) < 0) goto error; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
if (PyList_Append(l, suffix) < 0) goto error; |
| 460 |
|
| 461 |
result = PyUnicode_Join(sep, l); |
| 462 |
|
| 463 |
error: |
| 464 |
Py_XDECREF(l); |
| 465 |
Py_XDECREF(prefix); |
| 466 |
Py_XDECREF(mid); |
| 467 |
Py_XDECREF(suffix); |
| 468 |
Py_XDECREF(sep); |
| 469 |
Py_XDECREF(comma); |
| 470 |
return result; |
| 471 |
} |
| 472 |
|
| 473 |
// Generic comparison implementation for vecs with PyObject * items. |
| 474 |
PyObject *Vec_GenericRichcompare(Py_ssize_t *len, PyObject **items, |
| 475 |
Py_ssize_t *other_len, PyObject **other_items, |
| 476 |
int op) { |
| 477 |
int cmp = 1; |
| 478 |
PyObject *res; |
| 479 |
if (op == Py_EQ || op == Py_NE) { |
| 480 |
if (*len != *other_len) { |
| 481 |
cmp = 0; |
| 482 |
} else { |
| 483 |
for (Py_ssize_t i = 0; i < *len && i < *other_len; i++) { |
| 484 |
int itemcmp = PyObject_RichCompareBool(items[i], other_items[i], Py_EQ); |
| 485 |
if (itemcmp < 0) |
| 486 |
return NULL; |
| 487 |
if (!itemcmp) { |
| 488 |
cmp = 0; |
| 489 |
break; |
| 490 |
} |
| 491 |
} |
| 492 |
} |
| 493 |
if (op == Py_NE) |
| 494 |
cmp = cmp ^ 1; |
| 495 |
res = cmp ? Py_True : Py_False; |
| 496 |
} else |
| 497 |
res = Py_NotImplemented; |
| 498 |
Py_INCREF(res); |
| 499 |
return res; |
| 500 |
} |
| 501 |
|
| 502 |
int Vec_GenericRemove(Py_ssize_t *len, PyObject **items, PyObject *item) { |
| 503 |
for (Py_ssize_t i = 0; i < *len; i++) { |
| 504 |
int match = 0; |
| 505 |
if (items[i] == item) |
| 506 |
match = 1; |
| 507 |
else { |
| 508 |
int itemcmp = PyObject_RichCompareBool(items[i], item, Py_EQ); |
| 509 |
if (itemcmp < 0) |
| 510 |
return 0; |
| 511 |
match = itemcmp; |
| 512 |
} |
| 513 |
if (match) { |
| 514 |
Py_CLEAR(items[i]); |
| 515 |
for (; i < *len - 1; i++) { |
| 516 |
items[i] = items[i + 1]; |
| 517 |
} |
| 518 |
(*len)--; |
| 519 |
return 1; |
| 520 |
} |
| 521 |
} |
| 522 |
PyErr_SetString(PyExc_ValueError, "vec.remove(x): x not in vec"); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
PyObject *Vec_GenericPop(Py_ssize_t *len, PyObject **items, Py_ssize_t index) { |
| 527 |
if (index < 0) |
| 528 |
index += *len; |
| 529 |
|
| 530 |
if (index < 0 || index >= *len) { |
| 531 |
PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 532 |
return NULL; |
| 533 |
} |
| 534 |
|
| 535 |
PyObject *item = items[index]; |
| 536 |
for (Py_ssize_t i = index; i < *len - 1; i++) |
| 537 |
items[i] = items[i + 1]; |
| 538 |
|
| 539 |
(*len)--; |
| 540 |
return item; |
| 541 |
} |
| 542 |
|
| 543 |
PyObject *Vec_GenericPopWrapper(Py_ssize_t *len, PyObject **items, PyObject *args) { |
| 544 |
Py_ssize_t index = -1; |
| 545 |
if (!PyArg_ParseTuple(args, "|n:pop", &index)) |
| 546 |
return NULL; |
| 547 |
|
| 548 |
return Vec_GenericPop(len, items, index); |
| 549 |
} |
| 550 |
|
| 551 |
// Module-level functions |
| 552 |
|
| 553 |
static PyObject *vec_append(PyObject *self, PyObject *args) |
| 554 |
{ |
| 555 |
PyObject *vec; |
| 556 |
PyObject *item; |
| 557 |
|
| 558 |
if (!PyArg_ParseTuple(args, "OO", &vec, &item)) |
| 559 |
return NULL; |
| 560 |
|
| 561 |
if (VecI64_Check(vec)) { |
| 562 |
int64_t x = VecI64_UnboxItem(item); |
| 563 |
if (VecI64_IsUnboxError(x)) { |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
VecI64 v = ((VecI64Object *)vec)->vec; |
| 567 |
VEC_I64_INCREF(v); |
| 568 |
v = VecI64_Append(v, x); |
| 569 |
if (VEC_IS_ERROR(v)) |
| 570 |
return NULL; |
| 571 |
return VecI64_Box(v); |
| 572 |
} else if (VecU8_Check(vec)) { |
| 573 |
uint8_t x = VecU8_UnboxItem(item); |
| 574 |
if (VecU8_IsUnboxError(x)) { |
| 575 |
return NULL; |
| 576 |
} |
| 577 |
VecU8 v = ((VecU8Object *)vec)->vec; |
| 578 |
VEC_U8_INCREF(v); |
| 579 |
v = VecU8_Append(v, x); |
| 580 |
if (VEC_IS_ERROR(v)) |
| 581 |
return NULL; |
| 582 |
return VecU8_Box(v); |
| 583 |
} else if (VecFloat_Check(vec)) { |
| 584 |
double x = VecFloat_UnboxItem(item); |
| 585 |
if (VecFloat_IsUnboxError(x)) { |
| 586 |
return NULL; |
| 587 |
} |
| 588 |
VecFloat v = ((VecFloatObject *)vec)->vec; |
| 589 |
VEC_FLOAT_INCREF(v); |
| 590 |
v = VecFloat_Append(v, x); |
| 591 |
if (VEC_IS_ERROR(v)) |
| 592 |
return NULL; |
| 593 |
return VecFloat_Box(v); |
| 594 |
} else if (VecI32_Check(vec)) { |
| 595 |
int32_t x = VecI32_UnboxItem(item); |
| 596 |
if (VecI32_IsUnboxError(x)) { |
| 597 |
return NULL; |
| 598 |
} |
| 599 |
VecI32 v = ((VecI32Object *)vec)->vec; |
| 600 |
VEC_I32_INCREF(v); |
| 601 |
v = VecI32_Append(v, x); |
| 602 |
if (VEC_IS_ERROR(v)) |
| 603 |
return NULL; |
| 604 |
return VecI32_Box(v); |
| 605 |
} else if (VecI16_Check(vec)) { |
| 606 |
int16_t x = VecI16_UnboxItem(item); |
| 607 |
if (VecI16_IsUnboxError(x)) { |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
VecI16 v = ((VecI16Object *)vec)->vec; |
| 611 |
VEC_I16_INCREF(v); |
| 612 |
v = VecI16_Append(v, x); |
| 613 |
if (VEC_IS_ERROR(v)) |
| 614 |
return NULL; |
| 615 |
return VecI16_Box(v); |
| 616 |
} else if (VecBool_Check(vec)) { |
| 617 |
double x = VecBool_UnboxItem(item); |
| 618 |
if (VecBool_IsUnboxError(x)) { |
| 619 |
return NULL; |
| 620 |
} |
| 621 |
VecBool v = ((VecBoolObject *)vec)->vec; |
| 622 |
VEC_BOOL_INCREF(v); |
| 623 |
v = VecBool_Append(v, x); |
| 624 |
if (VEC_IS_ERROR(v)) |
| 625 |
return NULL; |
| 626 |
return VecBool_Box(v); |
| 627 |
} else if (VecT_Check(vec)) { |
| 628 |
VecT v = ((VecTObject *)vec)->vec; |
| 629 |
if (!VecT_ItemCheck(v, item, VEC_T_BUF(v)->item_type)) { |
| 630 |
return NULL; |
| 631 |
} |
| 632 |
VEC_T_INCREF(v); |
| 633 |
v = VecT_Append(v, item, VEC_T_BUF(v)->item_type); |
| 634 |
if (VEC_IS_ERROR(v)) |
| 635 |
return NULL; |
| 636 |
return VecT_Box(v, VEC_T_BUF(v)->item_type); |
| 637 |
} else if (VecNested_Check(vec)) { |
| 638 |
VecNested v = ((VecNestedObject *)vec)->vec; |
| 639 |
VecNestedBufItem vecitem; |
| 640 |
if (VecNested_UnboxItem(v, item, &vecitem) < 0) |
| 641 |
return NULL; |
| 642 |
VEC_NESTED_INCREF(v); |
| 643 |
v = VecNested_Append(v, vecitem); |
| 644 |
if (VEC_IS_ERROR(v)) |
| 645 |
return NULL; |
| 646 |
return VecNested_Box(v); |
| 647 |
} else { |
| 648 |
PyErr_Format(PyExc_TypeError, "vec argument expected, got %.100s", |
| 649 |
Py_TYPE(vec)->tp_name); |
| 650 |
return NULL; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
static PyObject *vec_remove(PyObject *self, PyObject *args) |
| 655 |
{ |
| 656 |
PyObject *vec; |
| 657 |
PyObject *item; |
| 658 |
|
| 659 |
if (!PyArg_ParseTuple(args, "OO", &vec, &item)) |
| 660 |
return NULL; |
| 661 |
|
| 662 |
if (VecI64_Check(vec)) { |
| 663 |
int64_t x = VecI64_UnboxItem(item); |
| 664 |
if (VecI64_IsUnboxError(x)) { |
| 665 |
return NULL; |
| 666 |
} |
| 667 |
VecI64 v = ((VecI64Object *)vec)->vec; |
| 668 |
VEC_I64_INCREF(v); |
| 669 |
v = VecI64_Remove(v, x); |
| 670 |
if (VEC_IS_ERROR(v)) |
| 671 |
return NULL; |
| 672 |
return VecI64_Box(v); |
| 673 |
} else if (VecU8_Check(vec)) { |
| 674 |
uint8_t x = VecU8_UnboxItem(item); |
| 675 |
if (VecU8_IsUnboxError(x)) { |
| 676 |
return NULL; |
| 677 |
} |
| 678 |
VecU8 v = ((VecU8Object *)vec)->vec; |
| 679 |
VEC_U8_INCREF(v); |
| 680 |
v = VecU8_Remove(v, x); |
| 681 |
if (VEC_IS_ERROR(v)) |
| 682 |
return NULL; |
| 683 |
return VecU8_Box(v); |
| 684 |
} else if (VecFloat_Check(vec)) { |
| 685 |
double x = VecFloat_UnboxItem(item); |
| 686 |
if (VecFloat_IsUnboxError(x)) { |
| 687 |
return NULL; |
| 688 |
} |
| 689 |
VecFloat v = ((VecFloatObject *)vec)->vec; |
| 690 |
VEC_FLOAT_INCREF(v); |
| 691 |
v = VecFloat_Remove(v, x); |
| 692 |
if (VEC_IS_ERROR(v)) |
| 693 |
return NULL; |
| 694 |
return VecFloat_Box(v); |
| 695 |
} else if (VecI32_Check(vec)) { |
| 696 |
int32_t x = VecI32_UnboxItem(item); |
| 697 |
if (VecI32_IsUnboxError(x)) { |
| 698 |
return NULL; |
| 699 |
} |
| 700 |
VecI32 v = ((VecI32Object *)vec)->vec; |
| 701 |
VEC_I32_INCREF(v); |
| 702 |
v = VecI32_Remove(v, x); |
| 703 |
if (VEC_IS_ERROR(v)) |
| 704 |
return NULL; |
| 705 |
return VecI32_Box(v); |
| 706 |
} else if (VecI16_Check(vec)) { |
| 707 |
int16_t x = VecI16_UnboxItem(item); |
| 708 |
if (VecI16_IsUnboxError(x)) { |
| 709 |
return NULL; |
| 710 |
} |
| 711 |
VecI16 v = ((VecI16Object *)vec)->vec; |
| 712 |
VEC_I16_INCREF(v); |
| 713 |
v = VecI16_Remove(v, x); |
| 714 |
if (VEC_IS_ERROR(v)) |
| 715 |
return NULL; |
| 716 |
return VecI16_Box(v); |
| 717 |
} else if (VecBool_Check(vec)) { |
| 718 |
char x = VecBool_UnboxItem(item); |
| 719 |
if (VecBool_IsUnboxError(x)) { |
| 720 |
return NULL; |
| 721 |
} |
| 722 |
VecBool v = ((VecBoolObject *)vec)->vec; |
| 723 |
VEC_BOOL_INCREF(v); |
| 724 |
v = VecBool_Remove(v, x); |
| 725 |
if (VEC_IS_ERROR(v)) |
| 726 |
return NULL; |
| 727 |
return VecBool_Box(v); |
| 728 |
} else if (VecT_Check(vec)) { |
| 729 |
VecT v = ((VecTObject *)vec)->vec; |
| 730 |
if (!VecT_ItemCheck(v, item, VEC_T_BUF(v)->item_type)) { |
| 731 |
return NULL; |
| 732 |
} |
| 733 |
VEC_T_INCREF(v); |
| 734 |
v = VecT_Remove(v, item); |
| 735 |
if (VEC_IS_ERROR(v)) |
| 736 |
return NULL; |
| 737 |
return VecT_Box(v, VEC_T_BUF(v)->item_type); |
| 738 |
} else if (VecNested_Check(vec)) { |
| 739 |
VecNested v = ((VecNestedObject *)vec)->vec; |
| 740 |
VecNestedBufItem vecitem; |
| 741 |
if (VecNested_UnboxItem(v, item, &vecitem) < 0) |
| 742 |
return NULL; |
| 743 |
VEC_NESTED_INCREF(v); |
| 744 |
v = VecNested_Remove(v, vecitem); |
| 745 |
if (VEC_IS_ERROR(v)) |
| 746 |
return NULL; |
| 747 |
return VecNested_Box(v); |
| 748 |
} else { |
| 749 |
PyErr_Format(PyExc_TypeError, "vec argument expected, got %.100s", |
| 750 |
Py_TYPE(vec)->tp_name); |
| 751 |
return NULL; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
static PyObject *vec_pop(PyObject *self, PyObject *args) |
| 756 |
{ |
| 757 |
PyObject *vec; |
| 758 |
Py_ssize_t index = -1; |
| 759 |
|
| 760 |
if (!PyArg_ParseTuple(args, "O|n:pop", &vec, &index)) |
| 761 |
return NULL; |
| 762 |
|
| 763 |
PyObject *result_item0; |
| 764 |
PyObject *result_item1; |
| 765 |
|
| 766 |
if (VecI64_Check(vec)) { |
| 767 |
VecI64 v = ((VecI64Object *)vec)->vec; |
| 768 |
VEC_I64_INCREF(v); |
| 769 |
VecI64PopResult r; |
| 770 |
r = VecI64_Pop(v, index); |
| 771 |
if (VEC_IS_ERROR(r.f0)) |
| 772 |
return NULL; |
| 773 |
if ((result_item0 = VecI64_Box(r.f0)) == NULL) |
| 774 |
return NULL; |
| 775 |
result_item1 = VecI64_BoxItem(r.f1); |
| 776 |
} else if (VecU8_Check(vec)) { |
| 777 |
VecU8 v = ((VecU8Object *)vec)->vec; |
| 778 |
VEC_U8_INCREF(v); |
| 779 |
VecU8PopResult r; |
| 780 |
r = VecU8_Pop(v, index); |
| 781 |
if (VEC_IS_ERROR(r.f0)) |
| 782 |
return NULL; |
| 783 |
if ((result_item0 = VecU8_Box(r.f0)) == NULL) |
| 784 |
return NULL; |
| 785 |
result_item1 = VecU8_BoxItem(r.f1); |
| 786 |
} else if (VecFloat_Check(vec)) { |
| 787 |
VecFloat v = ((VecFloatObject *)vec)->vec; |
| 788 |
VEC_FLOAT_INCREF(v); |
| 789 |
VecFloatPopResult r; |
| 790 |
r = VecFloat_Pop(v, index); |
| 791 |
if (VEC_IS_ERROR(r.f0)) |
| 792 |
return NULL; |
| 793 |
if ((result_item0 = VecFloat_Box(r.f0)) == NULL) |
| 794 |
return NULL; |
| 795 |
result_item1 = VecFloat_BoxItem(r.f1); |
| 796 |
} else if (VecI32_Check(vec)) { |
| 797 |
VecI32 v = ((VecI32Object *)vec)->vec; |
| 798 |
VEC_I32_INCREF(v); |
| 799 |
VecI32PopResult r; |
| 800 |
r = VecI32_Pop(v, index); |
| 801 |
if (VEC_IS_ERROR(r.f0)) |
| 802 |
return NULL; |
| 803 |
if ((result_item0 = VecI32_Box(r.f0)) == NULL) |
| 804 |
return NULL; |
| 805 |
result_item1 = VecI32_BoxItem(r.f1); |
| 806 |
} else if (VecI16_Check(vec)) { |
| 807 |
VecI16 v = ((VecI16Object *)vec)->vec; |
| 808 |
VEC_I16_INCREF(v); |
| 809 |
VecI16PopResult r; |
| 810 |
r = VecI16_Pop(v, index); |
| 811 |
if (VEC_IS_ERROR(r.f0)) |
| 812 |
return NULL; |
| 813 |
if ((result_item0 = VecI16_Box(r.f0)) == NULL) |
| 814 |
return NULL; |
| 815 |
result_item1 = VecI16_BoxItem(r.f1); |
| 816 |
} else if (VecBool_Check(vec)) { |
| 817 |
VecBool v = ((VecBoolObject *)vec)->vec; |
| 818 |
VEC_BOOL_INCREF(v); |
| 819 |
VecBoolPopResult r; |
| 820 |
r = VecBool_Pop(v, index); |
| 821 |
if (VEC_IS_ERROR(r.f0)) |
| 822 |
return NULL; |
| 823 |
if ((result_item0 = VecBool_Box(r.f0)) == NULL) |
| 824 |
return NULL; |
| 825 |
result_item1 = VecBool_BoxItem(r.f1); |
| 826 |
} else if (VecT_Check(vec)) { |
| 827 |
VecT v = ((VecTObject *)vec)->vec; |
| 828 |
VEC_T_INCREF(v); |
| 829 |
VecTPopResult r; |
| 830 |
r = VecT_Pop(v, index); |
| 831 |
if (VEC_IS_ERROR(r.f0)) |
| 832 |
return NULL; |
| 833 |
result_item0 = VecT_Box(r.f0, VEC_T_BUF(v)->item_type); |
| 834 |
if (result_item0 == NULL) { |
| 835 |
Py_DECREF(r.f1); |
| 836 |
return NULL; |
| 837 |
} |
| 838 |
result_item1 = r.f1; |
| 839 |
} else if (VecNested_Check(vec)) { |
| 840 |
VecNested v = ((VecNestedObject *)vec)->vec; |
| 841 |
VEC_NESTED_INCREF(v); |
| 842 |
VecNestedPopResult r; |
| 843 |
r = VecNested_Pop(v, index); |
| 844 |
if (VEC_IS_ERROR(r.f0)) |
| 845 |
return NULL; |
| 846 |
PyObject *popped_buf = VecNested_ItemBuf(VEC_NESTED_BUF(r.f0), r.f1); |
| 847 |
result_item0 = VecNested_Box(r.f0); |
| 848 |
if (result_item0 == NULL) { |
| 849 |
Py_XDECREF(popped_buf); |
| 850 |
return NULL; |
| 851 |
} |
| 852 |
result_item1 = VecNested_BoxItem(r.f0, r.f1); |
| 853 |
if (result_item1 == NULL) { |
| 854 |
Py_DECREF(result_item0); |
| 855 |
Py_XDECREF(popped_buf); |
| 856 |
return NULL; |
| 857 |
} |
| 858 |
Py_XDECREF(popped_buf); |
| 859 |
} else { |
| 860 |
PyErr_Format(PyExc_TypeError, "vec argument expected, got %.100s", |
| 861 |
Py_TYPE(vec)->tp_name); |
| 862 |
return NULL; |
| 863 |
} |
| 864 |
|
| 865 |
if (result_item1 == NULL) { |
| 866 |
Py_DECREF(result_item0); |
| 867 |
return NULL; |
| 868 |
} |
| 869 |
|
| 870 |
PyObject *res = PyTuple_New(2); |
| 871 |
if (res == NULL) { |
| 872 |
Py_DECREF(result_item0); |
| 873 |
Py_DECREF(result_item1); |
| 874 |
return NULL; |
| 875 |
} |
| 876 |
|
| 877 |
PyTuple_SET_ITEM(res, 0, result_item0); |
| 878 |
PyTuple_SET_ITEM(res, 1, result_item1); |
| 879 |
return res; |
| 880 |
} |
| 881 |
|
| 882 |
static PyObject *vec_extend(PyObject *self, PyObject *args) |
| 883 |
{ |
| 884 |
PyObject *vec; |
| 885 |
PyObject *iterable; |
| 886 |
|
| 887 |
if (!PyArg_ParseTuple(args, "OO:extend", &vec, &iterable)) |
| 888 |
return NULL; |
| 889 |
|
| 890 |
if (VecI64_Check(vec)) { |
| 891 |
VecI64 v = ((VecI64Object *)vec)->vec; |
| 892 |
VEC_I64_INCREF(v); |
| 893 |
v = VecI64_Extend(v, iterable); |
| 894 |
if (VEC_IS_ERROR(v)) |
| 895 |
return NULL; |
| 896 |
return VecI64_Box(v); |
| 897 |
} else if (VecU8_Check(vec)) { |
| 898 |
VecU8 v = ((VecU8Object *)vec)->vec; |
| 899 |
VEC_U8_INCREF(v); |
| 900 |
v = VecU8_Extend(v, iterable); |
| 901 |
if (VEC_IS_ERROR(v)) |
| 902 |
return NULL; |
| 903 |
return VecU8_Box(v); |
| 904 |
} else if (VecFloat_Check(vec)) { |
| 905 |
VecFloat v = ((VecFloatObject *)vec)->vec; |
| 906 |
VEC_FLOAT_INCREF(v); |
| 907 |
v = VecFloat_Extend(v, iterable); |
| 908 |
if (VEC_IS_ERROR(v)) |
| 909 |
return NULL; |
| 910 |
return VecFloat_Box(v); |
| 911 |
} else if (VecI32_Check(vec)) { |
| 912 |
VecI32 v = ((VecI32Object *)vec)->vec; |
| 913 |
VEC_I32_INCREF(v); |
| 914 |
v = VecI32_Extend(v, iterable); |
| 915 |
if (VEC_IS_ERROR(v)) |
| 916 |
return NULL; |
| 917 |
return VecI32_Box(v); |
| 918 |
} else if (VecI16_Check(vec)) { |
| 919 |
VecI16 v = ((VecI16Object *)vec)->vec; |
| 920 |
VEC_I16_INCREF(v); |
| 921 |
v = VecI16_Extend(v, iterable); |
| 922 |
if (VEC_IS_ERROR(v)) |
| 923 |
return NULL; |
| 924 |
return VecI16_Box(v); |
| 925 |
} else if (VecBool_Check(vec)) { |
| 926 |
VecBool v = ((VecBoolObject *)vec)->vec; |
| 927 |
VEC_BOOL_INCREF(v); |
| 928 |
v = VecBool_Extend(v, iterable); |
| 929 |
if (VEC_IS_ERROR(v)) |
| 930 |
return NULL; |
| 931 |
return VecBool_Box(v); |
| 932 |
} else if (VecT_Check(vec)) { |
| 933 |
VecT v = ((VecTObject *)vec)->vec; |
| 934 |
size_t item_type = VEC_T_BUF(v)->item_type; |
| 935 |
VEC_T_INCREF(v); |
| 936 |
v = VecT_Extend(v, iterable, item_type); |
| 937 |
if (VEC_IS_ERROR(v)) |
| 938 |
return NULL; |
| 939 |
return VecT_Box(v, item_type); |
| 940 |
} else if (VecNested_Check(vec)) { |
| 941 |
VecNested v = ((VecNestedObject *)vec)->vec; |
| 942 |
VEC_NESTED_INCREF(v); |
| 943 |
v = VecNested_Extend(v, iterable); |
| 944 |
if (VEC_IS_ERROR(v)) |
| 945 |
return NULL; |
| 946 |
return VecNested_Box(v); |
| 947 |
} else { |
| 948 |
PyErr_Format(PyExc_TypeError, "vec argument expected, got %.100s", |
| 949 |
Py_TYPE(vec)->tp_name); |
| 950 |
return NULL; |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
// Return the base VecType for isinstance checks |
| 955 |
static PyTypeObject *get_vec_type(void) { |
| 956 |
return &VecType; |
| 957 |
} |
| 958 |
|
| 959 |
static int |
| 960 |
vecs_abi_version(void) { |
| 961 |
return LIBRT_VECS_ABI_VERSION; |
| 962 |
} |
| 963 |
|
| 964 |
static int |
| 965 |
vecs_api_version(void) { |
| 966 |
return LIBRT_VECS_API_VERSION; |
| 967 |
} |
| 968 |
|
| 969 |
static VecCapsule Capsule = { |
| 970 |
vecs_abi_version, |
| 971 |
vecs_api_version, |
| 972 |
&Vec_TAPI, |
| 973 |
&Vec_NestedAPI, |
| 974 |
&Vec_I64API, |
| 975 |
&Vec_I32API, |
| 976 |
&Vec_I16API, |
| 977 |
&Vec_U8API, |
| 978 |
&Vec_FloatAPI, |
| 979 |
&Vec_BoolAPI, |
| 980 |
get_vec_type, |
| 981 |
}; |
| 982 |
|
| 983 |
static PyMethodDef VecsMethods[] = { |
| 984 |
{"append", vec_append, METH_VARARGS, "Append a value to the end of a vec"}, |
| 985 |
{"remove", vec_remove, METH_VARARGS, "Remove first occurrence of value from a vec"}, |
| 986 |
{"pop", vec_pop, METH_VARARGS, "Remove and return vec item at index (default last)"}, |
| 987 |
{"extend", vec_extend, METH_VARARGS, "Extend a vec with items from an iterable"}, |
| 988 |
{NULL, NULL, 0, NULL} /* Sentinel */ |
| 989 |
}; |
| 990 |
|
| 991 |
static int |
| 992 |
librt_vecs_module_exec(PyObject *m) |
| 993 |
{ |
| 994 |
PyObject *ext = PyImport_ImportModule("mypy_extensions"); |
| 995 |
if (ext == NULL) { |
| 996 |
return -1; |
| 997 |
} |
| 998 |
|
| 999 |
LibRTVecs_I64TypeObj = (PyTypeObject *)PyObject_GetAttrString(ext, "i64"); |
| 1000 |
if (LibRTVecs_I64TypeObj == NULL) { |
| 1001 |
return -1; |
| 1002 |
} |
| 1003 |
if (!PyType_Check(LibRTVecs_I64TypeObj)) { |
| 1004 |
PyErr_SetString(PyExc_TypeError, "mypy_extensions.i64 is not a type"); |
| 1005 |
return -1; |
| 1006 |
} |
| 1007 |
LibRTVecs_I32TypeObj = (PyTypeObject *)PyObject_GetAttrString(ext, "i32"); |
| 1008 |
if (LibRTVecs_I32TypeObj == NULL) { |
| 1009 |
return -1; |
| 1010 |
} |
| 1011 |
if (!PyType_Check(LibRTVecs_I32TypeObj)) { |
| 1012 |
PyErr_SetString(PyExc_TypeError, "mypy_extensions.i32 is not a type"); |
| 1013 |
return -1; |
| 1014 |
} |
| 1015 |
LibRTVecs_I16TypeObj = (PyTypeObject *)PyObject_GetAttrString(ext, "i16"); |
| 1016 |
if (LibRTVecs_I16TypeObj == NULL) { |
| 1017 |
return -1; |
| 1018 |
} |
| 1019 |
if (!PyType_Check(LibRTVecs_I16TypeObj)) { |
| 1020 |
PyErr_SetString(PyExc_TypeError, "mypy_extensions.i16 is not a type"); |
| 1021 |
return -1; |
| 1022 |
} |
| 1023 |
LibRTVecs_U8TypeObj = (PyTypeObject *)PyObject_GetAttrString(ext, "u8"); |
| 1024 |
if (LibRTVecs_U8TypeObj == NULL) { |
| 1025 |
return -1; |
| 1026 |
} |
| 1027 |
if (!PyType_Check(LibRTVecs_U8TypeObj)) { |
| 1028 |
PyErr_SetString(PyExc_TypeError, "mypy_extensions.u8 is not a type"); |
| 1029 |
return -1; |
| 1030 |
} |
| 1031 |
|
| 1032 |
if (PyType_Ready(&VecType) < 0) |
| 1033 |
return -1; |
| 1034 |
if (PyType_Ready(&VecGenericAliasType) < 0) |
| 1035 |
return -1; |
| 1036 |
|
| 1037 |
if (PyType_Ready(&VecTType) < 0) |
| 1038 |
return -1; |
| 1039 |
if (PyType_Ready(&VecTBufType) < 0) |
| 1040 |
return -1; |
| 1041 |
if (PyType_Ready(&VecTIterType) < 0) |
| 1042 |
return -1; |
| 1043 |
|
| 1044 |
if (PyType_Ready(&VecNestedType) < 0) |
| 1045 |
return -1; |
| 1046 |
if (PyType_Ready(&VecNestedBufType) < 0) |
| 1047 |
return -1; |
| 1048 |
if (PyType_Ready(&VecNestedIterType) < 0) |
| 1049 |
return -1; |
| 1050 |
|
| 1051 |
if (PyType_Ready(&VecI64Type) < 0) |
| 1052 |
return -1; |
| 1053 |
if (PyType_Ready(&VecI64BufType) < 0) |
| 1054 |
return -1; |
| 1055 |
if (PyType_Ready(&VecI64IterType) < 0) |
| 1056 |
return -1; |
| 1057 |
if (PyType_Ready(&VecI32Type) < 0) |
| 1058 |
return -1; |
| 1059 |
if (PyType_Ready(&VecI32BufType) < 0) |
| 1060 |
return -1; |
| 1061 |
if (PyType_Ready(&VecI32IterType) < 0) |
| 1062 |
return -1; |
| 1063 |
if (PyType_Ready(&VecI16Type) < 0) |
| 1064 |
return -1; |
| 1065 |
if (PyType_Ready(&VecI16BufType) < 0) |
| 1066 |
return -1; |
| 1067 |
if (PyType_Ready(&VecI16IterType) < 0) |
| 1068 |
return -1; |
| 1069 |
if (PyType_Ready(&VecU8Type) < 0) |
| 1070 |
return -1; |
| 1071 |
if (PyType_Ready(&VecU8BufType) < 0) |
| 1072 |
return -1; |
| 1073 |
if (PyType_Ready(&VecU8IterType) < 0) |
| 1074 |
return -1; |
| 1075 |
if (PyType_Ready(&VecFloatType) < 0) |
| 1076 |
return -1; |
| 1077 |
if (PyType_Ready(&VecFloatBufType) < 0) |
| 1078 |
return -1; |
| 1079 |
if (PyType_Ready(&VecFloatIterType) < 0) |
| 1080 |
return -1; |
| 1081 |
if (PyType_Ready(&VecBoolType) < 0) |
| 1082 |
return -1; |
| 1083 |
if (PyType_Ready(&VecBoolBufType) < 0) |
| 1084 |
return -1; |
| 1085 |
if (PyType_Ready(&VecBoolIterType) < 0) |
| 1086 |
return -1; |
| 1087 |
|
| 1088 |
Py_INCREF(&VecType); |
| 1089 |
if (PyModule_AddObject(m, "vec", (PyObject *)&VecType) < 0) { |
| 1090 |
Py_DECREF(&VecType); |
| 1091 |
return -1; |
| 1092 |
} |
| 1093 |
|
| 1094 |
PyObject *c_api = PyCapsule_New(&Capsule, "librt.vecs._C_API", NULL); |
| 1095 |
if (c_api == NULL) |
| 1096 |
return -1; |
| 1097 |
|
| 1098 |
if (PyModule_AddObject(m, "_C_API", c_api) < 0) { |
| 1099 |
Py_XDECREF(c_api); |
| 1100 |
Py_DECREF(&VecType); |
| 1101 |
return -1; |
| 1102 |
} |
| 1103 |
|
| 1104 |
Py_DECREF(ext); |
| 1105 |
return 0; |
| 1106 |
} |
| 1107 |
|
| 1108 |
static PyModuleDef_Slot librt_vecs_module_slots[] = { |
| 1109 |
{Py_mod_exec, librt_vecs_module_exec}, |
| 1110 |
#ifdef Py_MOD_GIL_NOT_USED |
| 1111 |
{Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
| 1112 |
#endif |
| 1113 |
{0, NULL} |
| 1114 |
}; |
| 1115 |
|
| 1116 |
static PyModuleDef vecsmodule = { |
| 1117 |
.m_base = PyModuleDef_HEAD_INIT, |
| 1118 |
.m_name = "vecs", |
| 1119 |
.m_doc = "This module implements the vec type and related functionality.", |
| 1120 |
.m_size = 0, |
| 1121 |
.m_methods = VecsMethods, |
| 1122 |
.m_slots = librt_vecs_module_slots, |
| 1123 |
}; |
| 1124 |
|
| 1125 |
PyMODINIT_FUNC |
| 1126 |
PyInit_vecs(void) |
| 1127 |
{ |
| 1128 |
return PyModuleDef_Init(&vecsmodule); |
| 1129 |
} |