# C++ vectors with Python indexing
### Tested against Windows 10 / Python 3.11 / Anaconda / C++ 20 - MSVC
### pip install cppvec
### Cython and a C++ compiler must be installed!
```PY
from cppvec import (
CppVectorChar,
CppVectorDouble,
CppVectorFloat,
CppVectorInt,
CppVectorLong,
CppVectorLongLong,
CppVectorUnsignedInt,
CppVectorUnsignedLong,
CppVectorUnsignedLongLong,
CppVectorUnsignedChar,
CppVectorShort,
CppVectorUnsignedShort,
CppVectorByte,
CppVectorUnsignedByte,
CppVectorLongDouble,
CppVectorSize_t,
)
import numpy as np
import ctypes
array_char = np.arange(100).astype(ctypes.c_byte)
array_int = np.arange(100).astype(ctypes.c_int)
array_long = np.arange(100).astype(ctypes.c_long)
array_long_long = np.arange(100).astype(ctypes.c_longlong)
array_float = np.arange(100).astype(ctypes.c_float)
array_double = np.arange(100).astype(ctypes.c_double)
array_uchar = np.arange(100).astype(ctypes.c_ubyte)
array_uint = np.arange(100).astype(ctypes.c_uint)
array_ulong = np.arange(100).astype(ctypes.c_ulong)
array_ulong_long = np.arange(100).astype(ctypes.c_ulonglong)
array_short = np.arange(100).astype(ctypes.c_short)
array_ushort = np.arange(100).astype(ctypes.c_ushort)
array_byte = np.arange(100).astype(ctypes.c_byte)
array_ubyte = np.arange(100).astype(ctypes.c_ubyte)
array_long_double = np.arange(100).astype(ctypes.c_longdouble)
array_size_t = np.arange(100).astype(ctypes.c_size_t)
vec_char = CppVectorChar(array_char)
vec_int = CppVectorInt(array_int)
vec_long = CppVectorLong(array_long)
vec_long_long = CppVectorLongLong(array_long_long)
vec_float = CppVectorFloat(array_float)
vec_double = CppVectorDouble(array_double)
vec_uchar = CppVectorUnsignedChar(array_uchar)
vec_uint = CppVectorUnsignedInt(array_uint)
vec_ulong = CppVectorUnsignedLong(array_ulong)
vec_ulong_long = CppVectorUnsignedLongLong(array_ulong_long)
vec_short = CppVectorShort(array_short)
vec_ushort = CppVectorUnsignedShort(array_ushort)
vec_byte = CppVectorByte(array_byte)
vec_ubyte = CppVectorUnsignedByte(array_ubyte)
vec_long_double = CppVectorLongDouble(array_long_double)
vec_size_t = CppVectorSize_t(array_size_t)
my_vectors = {
"vec_char": vec_char,
"vec_int": vec_int,
"vec_long": vec_long,
"vec_long_long": vec_long_long,
"vec_float": vec_float,
"vec_double": vec_double,
"vec_uchar": vec_uchar,
"vec_uint": vec_uint,
"vec_ulong": vec_ulong,
"vec_ulong_long": vec_ulong_long,
"vec_short": vec_short,
"vec_ushort": vec_ushort,
"vec_byte": vec_byte,
"vec_ubyte": vec_ubyte,
"vec_long_double": vec_long_double,
"vec_size_t": vec_size_t,
}
my_results_apply_as_c_function = []
my_results_apply_as_c_pyfunction = []
my_results_apply_as_c_function_nogil = []
def apply_as_c_function(a):
my_results_apply_as_c_function.append(a + 5)
def apply_as_c_pyfunction(a):
my_results_apply_as_c_pyfunction.append(a + 5)
def apply_function(a):
return a + 5
def apply_as_c_function_nogil(a):
my_results_apply_as_c_function_nogil.append(a + 5) # might not release the gil
for k, c in my_vectors.items():
print(f"before: {k=}")
print(f"{c}")
c.append(5)
print(f"{c=}")
print("-----------------------------------------")
added = c + [1, 23, 3, 3]
print(f"{added=}")
print("-----------------------------------------")
del c[5]
print(c)
print("-----------------------------------------")
del c[:30]
print(c)
print("-----------------------------------------")
del c[:10:2]
print(c)
print("-----------------------------------------")
c[[1, 2, 11]] = 126
print(c)
print("-----------------------------------------")
c[0] = 125
print(c)
print("-----------------------------------------")
c[12:17:2] = 124
print(c)
print("-----------------------------------------")
c.insert(4, 101)
print(c)
print("-----------------------------------------")
c.insert([1, 3, 4, 6], 113)
print(c)
print("-----------------------------------------")
print(c.index_all(113))
print("-----------------------------------------")
print(c.index(113))
print("-----------------------------------------")
print(113 in c)
print("-----------------------------------------")
print(103 in c)
print("-----------------------------------------")
c.remove(113)
print(c)
print("-----------------------------------------")
popped = c.pop(3)
print(popped)
print(c)
print("-----------------------------------------")
c.remove_all(113)
print(c)
print("-----------------------------------------")
print(c.group_items())
print(c.split_at_index([2, 4, 6, 10]))
print(c.split_at_index(3))
print(c.split_at_value(124))
c.extend([77, 1, 2, 3, 4, 5, 99])
print(c)
c.extend_save([7777, 1, 2, 3, 4, 5, 9999, 11.232, "stax", 9999])
print(c)
nparray = np.array([77, 1, 2, 3, 4, 5, 99], dtype=c.nparray.dtype)
c.extend_np(nparray)
print(c)
c.reserve(1000)
c.resize(300)
print(c)
print(c.count(0))
c.remove_all(0)
print(c)
print(c.count(0))
crev = c.reverse()
print(crev)
ccopy = c.copy()
print(ccopy)
ccopy.clear()
print(ccopy)
print(ccopy.empty())
print(c.to_tuple())
print(c.to_list())
print(c.to_set())
try:
print(np.max(c.nparray))
print(np.min(c.nparray))
print(np.where(c.nparray > 10))
except Exception:
print(f"wrong datatype: {c.nparray.dtype}")
c.sort()
print(c)
results1 = c.apply_function(apply_function)
print(results1)
c.apply_as_c_function(apply_as_c_function)
print(my_results_apply_as_c_function)
c.apply_as_c_pyfunction(apply_as_c_pyfunction)
print(my_results_apply_as_c_pyfunction)
c.apply_as_c_function_nogil(apply_as_c_function_nogil)
print(my_results_apply_as_c_function_nogil)
my_results_apply_as_c_pyfunction.clear()
my_results_apply_as_c_function.clear()
my_results_apply_as_c_function_nogil.clear()
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/cppvec",
"name": "cppvec",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "c++, vector",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/0f/02/ed3677cdabee975fdab4c2076dc4f73f7e7fa1f0794f56285a7d055eee84/cppvec-0.10.tar.gz",
"platform": null,
"description": "\r\n# C++ vectors with Python indexing\r\n\r\n### Tested against Windows 10 / Python 3.11 / Anaconda / C++ 20 - MSVC\r\n\r\n### pip install cppvec\r\n\r\n### Cython and a C++ compiler must be installed!\r\n\r\n```PY\r\nfrom cppvec import (\r\n CppVectorChar,\r\n CppVectorDouble,\r\n CppVectorFloat,\r\n CppVectorInt,\r\n CppVectorLong,\r\n CppVectorLongLong,\r\n CppVectorUnsignedInt,\r\n CppVectorUnsignedLong,\r\n CppVectorUnsignedLongLong,\r\n CppVectorUnsignedChar,\r\n CppVectorShort,\r\n CppVectorUnsignedShort,\r\n CppVectorByte,\r\n CppVectorUnsignedByte,\r\n CppVectorLongDouble,\r\n CppVectorSize_t,\r\n)\r\nimport numpy as np\r\nimport ctypes\r\n\r\narray_char = np.arange(100).astype(ctypes.c_byte)\r\narray_int = np.arange(100).astype(ctypes.c_int)\r\narray_long = np.arange(100).astype(ctypes.c_long)\r\narray_long_long = np.arange(100).astype(ctypes.c_longlong)\r\narray_float = np.arange(100).astype(ctypes.c_float)\r\narray_double = np.arange(100).astype(ctypes.c_double)\r\narray_uchar = np.arange(100).astype(ctypes.c_ubyte)\r\narray_uint = np.arange(100).astype(ctypes.c_uint)\r\narray_ulong = np.arange(100).astype(ctypes.c_ulong)\r\narray_ulong_long = np.arange(100).astype(ctypes.c_ulonglong)\r\narray_short = np.arange(100).astype(ctypes.c_short)\r\narray_ushort = np.arange(100).astype(ctypes.c_ushort)\r\narray_byte = np.arange(100).astype(ctypes.c_byte)\r\narray_ubyte = np.arange(100).astype(ctypes.c_ubyte)\r\narray_long_double = np.arange(100).astype(ctypes.c_longdouble)\r\narray_size_t = np.arange(100).astype(ctypes.c_size_t)\r\n\r\n\r\nvec_char = CppVectorChar(array_char)\r\nvec_int = CppVectorInt(array_int)\r\nvec_long = CppVectorLong(array_long)\r\nvec_long_long = CppVectorLongLong(array_long_long)\r\nvec_float = CppVectorFloat(array_float)\r\nvec_double = CppVectorDouble(array_double)\r\nvec_uchar = CppVectorUnsignedChar(array_uchar)\r\nvec_uint = CppVectorUnsignedInt(array_uint)\r\nvec_ulong = CppVectorUnsignedLong(array_ulong)\r\nvec_ulong_long = CppVectorUnsignedLongLong(array_ulong_long)\r\nvec_short = CppVectorShort(array_short)\r\nvec_ushort = CppVectorUnsignedShort(array_ushort)\r\nvec_byte = CppVectorByte(array_byte)\r\nvec_ubyte = CppVectorUnsignedByte(array_ubyte)\r\nvec_long_double = CppVectorLongDouble(array_long_double)\r\nvec_size_t = CppVectorSize_t(array_size_t)\r\n\r\nmy_vectors = {\r\n \"vec_char\": vec_char,\r\n \"vec_int\": vec_int,\r\n \"vec_long\": vec_long,\r\n \"vec_long_long\": vec_long_long,\r\n \"vec_float\": vec_float,\r\n \"vec_double\": vec_double,\r\n \"vec_uchar\": vec_uchar,\r\n \"vec_uint\": vec_uint,\r\n \"vec_ulong\": vec_ulong,\r\n \"vec_ulong_long\": vec_ulong_long,\r\n \"vec_short\": vec_short,\r\n \"vec_ushort\": vec_ushort,\r\n \"vec_byte\": vec_byte,\r\n \"vec_ubyte\": vec_ubyte,\r\n \"vec_long_double\": vec_long_double,\r\n \"vec_size_t\": vec_size_t,\r\n}\r\n\r\nmy_results_apply_as_c_function = []\r\nmy_results_apply_as_c_pyfunction = []\r\nmy_results_apply_as_c_function_nogil = []\r\n\r\n\r\ndef apply_as_c_function(a):\r\n my_results_apply_as_c_function.append(a + 5)\r\n\r\n\r\ndef apply_as_c_pyfunction(a):\r\n my_results_apply_as_c_pyfunction.append(a + 5)\r\n\r\n\r\ndef apply_function(a):\r\n return a + 5\r\n\r\n\r\ndef apply_as_c_function_nogil(a):\r\n my_results_apply_as_c_function_nogil.append(a + 5) # might not release the gil\r\n\r\n\r\nfor k, c in my_vectors.items():\r\n print(f\"before: {k=}\")\r\n print(f\"{c}\")\r\n c.append(5)\r\n print(f\"{c=}\")\r\n print(\"-----------------------------------------\")\r\n added = c + [1, 23, 3, 3]\r\n print(f\"{added=}\")\r\n print(\"-----------------------------------------\")\r\n del c[5]\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n del c[:30]\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n del c[:10:2]\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n c[[1, 2, 11]] = 126\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n\r\n c[0] = 125\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n\r\n c[12:17:2] = 124\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n\r\n c.insert(4, 101)\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n c.insert([1, 3, 4, 6], 113)\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n print(c.index_all(113))\r\n print(\"-----------------------------------------\")\r\n print(c.index(113))\r\n print(\"-----------------------------------------\")\r\n print(113 in c)\r\n print(\"-----------------------------------------\")\r\n print(103 in c)\r\n print(\"-----------------------------------------\")\r\n c.remove(113)\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n popped = c.pop(3)\r\n print(popped)\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n c.remove_all(113)\r\n print(c)\r\n print(\"-----------------------------------------\")\r\n print(c.group_items())\r\n print(c.split_at_index([2, 4, 6, 10]))\r\n print(c.split_at_index(3))\r\n print(c.split_at_value(124))\r\n c.extend([77, 1, 2, 3, 4, 5, 99])\r\n print(c)\r\n c.extend_save([7777, 1, 2, 3, 4, 5, 9999, 11.232, \"stax\", 9999])\r\n print(c)\r\n nparray = np.array([77, 1, 2, 3, 4, 5, 99], dtype=c.nparray.dtype)\r\n c.extend_np(nparray)\r\n print(c)\r\n c.reserve(1000)\r\n c.resize(300)\r\n print(c)\r\n print(c.count(0))\r\n c.remove_all(0)\r\n print(c)\r\n print(c.count(0))\r\n crev = c.reverse()\r\n print(crev)\r\n ccopy = c.copy()\r\n print(ccopy)\r\n ccopy.clear()\r\n print(ccopy)\r\n print(ccopy.empty())\r\n print(c.to_tuple())\r\n print(c.to_list())\r\n print(c.to_set())\r\n try:\r\n print(np.max(c.nparray))\r\n print(np.min(c.nparray))\r\n print(np.where(c.nparray > 10))\r\n except Exception:\r\n print(f\"wrong datatype: {c.nparray.dtype}\")\r\n c.sort()\r\n print(c)\r\n results1 = c.apply_function(apply_function)\r\n print(results1)\r\n c.apply_as_c_function(apply_as_c_function)\r\n print(my_results_apply_as_c_function)\r\n c.apply_as_c_pyfunction(apply_as_c_pyfunction)\r\n print(my_results_apply_as_c_pyfunction)\r\n c.apply_as_c_function_nogil(apply_as_c_function_nogil)\r\n print(my_results_apply_as_c_function_nogil)\r\n my_results_apply_as_c_pyfunction.clear()\r\n my_results_apply_as_c_function.clear()\r\n my_results_apply_as_c_function_nogil.clear()\r\n\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "C++ vectors with Python indexing",
"version": "0.10",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/cppvec"
},
"split_keywords": [
"c++",
" vector"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d0ac70f2f6488c243b279d6577592e49a59c0b2fc3675c6d84af7019c2413447",
"md5": "4ddda4e1b56958843a9a8a4273dcc0d8",
"sha256": "0b9fdd59a14bab41dc18fc7e71b5691197af0a620d836384d5b3324233602a05"
},
"downloads": -1,
"filename": "cppvec-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4ddda4e1b56958843a9a8a4273dcc0d8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 28580,
"upload_time": "2024-09-06T23:14:11",
"upload_time_iso_8601": "2024-09-06T23:14:11.175348Z",
"url": "https://files.pythonhosted.org/packages/d0/ac/70f2f6488c243b279d6577592e49a59c0b2fc3675c6d84af7019c2413447/cppvec-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0f02ed3677cdabee975fdab4c2076dc4f73f7e7fa1f0794f56285a7d055eee84",
"md5": "f4fc9fded87a9b85d22263323648afcd",
"sha256": "fa0b2e9d08442cfabf9a2a81f9a1cb7481fe8a7c9ee2d0106f5503cee99e5c59"
},
"downloads": -1,
"filename": "cppvec-0.10.tar.gz",
"has_sig": false,
"md5_digest": "f4fc9fded87a9b85d22263323648afcd",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 27992,
"upload_time": "2024-09-06T23:14:13",
"upload_time_iso_8601": "2024-09-06T23:14:13.266479Z",
"url": "https://files.pythonhosted.org/packages/0f/02/ed3677cdabee975fdab4c2076dc4f73f7e7fa1f0794f56285a7d055eee84/cppvec-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-06 23:14:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "cppvec",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "cppvec"
}