Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

cpp-containers-python

Python wrapper for the C++ Standard Library containers. The behavior of the containers might not be as expected, since for sorting and hashing the items, only the pointer to the Python object is used, and not the actual content. That means items in ordered collections will be ordered according to memory layout and not object content. Furthermore, equality checks will work as pairwise is tests, because only the pointers are compared internally. Also certain operations will lead to undefined behavior, such as accessing out of bounds indices in Vector.

This design decision was made for performance reasons and to keep the code as simple and close to C++ as possible. Real equality checks can still be implemented by pairwise comparison of the objects at Python level. This won't be any faster than doing so on normal Python collections.

For more documentation about the C++ containers see https://en.cppreference.com/w/cpp/container.

Requirements

Python 3.8+. Source builds currently target C++14.

Install

pip install cpp-containers

Examples

from cppcontainers import MultiSet
s = MultiSet()
s.insert(1)
s.insert(2)
s.insert(2)
assert sorted(s) == [1, 2, 2]
assert s.size() == 3
assert s.count(2) == 2

The containers store references to Python objects, not copies.

from cppcontainers import Vector
from sys import getrefcount
v = Vector()
a = object()
b = [1, 2, 3]
refcount = getrefcount(a)
v.push_back(a)
v.push_back(b)
assert id(a) == id(v[0])
assert list(v) == [a, b]
b.append(4)
assert b[-1] == v[1][-1]
assert getrefcount(a) == refcount + 1
del v
assert getrefcount(a) == refcount

The following crashes, just like in C++.

from cppcontainers import Vector
Vector()[0]

And this throws an exception like in C++.

from cppcontainers import Vector
Vector().at(0)

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages