from libcpp.algorithm cimport sort as stdsort
from libcpp.algorithm cimport unique
from libcpp.vector cimport vector
# from libcpp cimport bool
cimport cython
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
cdef class Vector:
cdef vector[cython.int] wrapped_vector
# the easiest thing to do is add short wrappers for the methods you need
def push_back(self, int num):
self.wrapped_vector.push_back(num)
def sort(self):
stdsort(self.wrapped_vector.begin(), self.wrapped_vector.end())
def unique(self):
self.wrapped_vector.erase(unique(self.wrapped_vector.begin(), self.wrapped_vector.end()), self.wrapped_vector.end())
def __str__(self):
return "[" + ", ".join([str(i) for i in self.wrapped_vector]) + "]"
def __repr__(self):
return str(self)
def __len__(self):
return self.wrapped_vector.size()
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def __setitem__(self, int key, int item):
self.wrapped_vector[key] = item
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def __getitem__(self, int key):
return self.wrapped_vector[key]
I have tried to wrap vectors so that I can use them in Python dicts.
This seems to create crazy amounts of overhead. See line 72 and 75 for example. They just add an integer to the number already in the vector:
Is it possible to remove this overhead or is this the price I pay to wrap vectors?