GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Dobatymo/cpp-containers-python

BranchesTags
Open more actions menu

Latest commit

 

History

14 Commits
14 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
cppcontainers
cppcontainers
 
 
include
include
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.pre-commit-config.yaml
.pre-commit-config.yaml
 
 
LICENSE
LICENSE
 
 
MANIFEST.in
MANIFEST.in
 
 
README.md
README.md
 
 
development.md
development.md
 
 
pyproject.toml
pyproject.toml
 
 
setup.py
setup.py
 
 
uv.lock
uv.lock
 
 

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

Readme
ISC license
Activity

Stars

1 star

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.