A C++ xvector<T> can be wrapped in a Python object, supporting all the usual Python methods such as append(), extend(), insert(), remove(), pop(), index(), count(), sort() and reverse(). Also the functions len(), min(), max() are supported as well as + for concatenation.
In addition L[i] provides read and write access to the ith element, and L[i:j] provides access to the slice from i up to but not including j. It is even possible to assign a slice or use the 'del' operator on a slice.
Consider the following code that returns or takes a C++ xvector<int32>
// C++ code
namespace ns
{
$function+ xvector<int32> Create()
{
return xvector<int32>();
}
$function+ void Print(const xvector<int32>& L)
{
std::cout << L;
}
}
This allows for the following Python code:
# python code
L = ns.Create()
ns.Print(L + [1,2,3])
// C++ code
namespace ns
{
$struct+ X isa IObject :
model
{
xvector<int32> L;
}
{
};
}
# python code
x = ns.X()
print 'x = ' + `x`
print 'Demonstration of various read only functions'
x.L.extend([1,2,3,4,5,6,7,8,9])
print 'x.L = ' + `x.L`
print 'bool(x.L) = ' + `bool(x.L)`
print 'str(x.L) = ' + `str(x.L)`
print 'repr(x.L) = ' + `repr(x.L)`
print 'len(x.L) = ' + `len(x.L)`
print 'min(x.L) = ' + `min(x.L)`
print 'max(x.L) = ' + `max(x.L)`
print 'x.L.index(2) = ' + `x.L.index(2)`
print 'x.L.index(9) = ' + `x.L.index(9)`
print 'x.L.index(15) = ' + `x.L.index(15)`
print 'x.L.count(4) = ' + `x.L.count(4)`
print 'x.L.count(20) = ' + `x.L.count(20)`
print 'x.L*2 = ' + `x.L*2`
print 'x.L[-1] = ' + `x.L[-1]`
print '3 in x.L = ' + `3 in x.L`
print '10 in x.L = ' + `10 in x.L`
print '3 not in x.L = ' + `3 not in x.L`
print '10 not in x.L = ' + `10 not in x.L`
print 'x.L+[100,200] = ' + `x.L+[100,200]`
print 'x.L[3] = ' + `x.L[3]`
print 'x.L[1:4] = ' + `x.L[1:4]`
print '\nDemonstration of various mutative functions'
print 'del x.L[0:len(x.L)]'
del x.L[0:len(x.L)]
print 'x.L = ' + `x.L`
print 'x.L.extend([10,20,30,40,50])'
x.L.extend([10,20,30,40,50])
print 'x.L = ' + `x.L`
print 'x.L.append(100)'
x.L.append(100)
print 'x.L = ' + `x.L`
print 'x.L.insert(0,-1)'
x.L.insert(0,-1)
print 'x.L = ' + `x.L`
print 'x.L[0] = 4'
x.L[0] = 4
print 'x.L = ' + `x.L`
print 'del x.L[1]'
del x.L[1]
print 'x.L = ' + `x.L`
print 'del x.L[1:3]'
del x.L[1:3]
print 'x.L = ' + `x.L`
print 'x.L[1:1] = [1,2,3,4,5]'
x.L[1:1] = [1,2,3,4,5]
print 'x.L = ' + `x.L`
print 'x.L.remove(2)'
x.L.remove(2)
print 'x.L = ' + `x.L`
print 'x.L.pop()'
x.L.pop()
print 'x.L = ' + `x.L`
print 'x.L.pop(1)'
x.L.pop(1)
print 'x.L = ' + `x.L`
#assignment to vector not supported
#x.L = []
#deletion of vector not supported
#del x.L
#sort not supported
#x.L.sort()
#reverse not supported
#x.L.reverse()
print 'x = ' + `x`
which produces the following output:
x = X([])
Demonstration of various read only functions
x.L = [1,2,3,4,5,6,7,8,9]
bool(x.L) = True
str(x.L) = '[1,2,3,4,5,6,7,8,9]'
repr(x.L) = '[1,2,3,4,5,6,7,8,9]'
len(x.L) = 9
min(x.L) = 1
max(x.L) = 9
x.L.index(2) = 1
x.L.index(9) = 8
x.L.index(15) = -1
x.L.count(4) = 1
x.L.count(20) = 0
x.L*2 = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]
x.L[-1] = 9
3 in x.L = True
10 in x.L = False
3 not in x.L = False
10 not in x.L = True
x.L+[100,200] = [1,2,3,4,5,6,7,8,9,100,200]
x.L[3] = 4
x.L[1:4] = [2,3,4]
Demonstration of various mutative functions
del x.L[0:len(x.L)]
x.L = []
x.L.extend([10,20,30,40,50])
x.L = [10,20,30,40,50]
x.L.append(100)
x.L = [10,20,30,40,50,100]
x.L.insert(0,-1)
x.L = [-1,10,20,30,40,50,100]
x.L[0] = 4
x.L = [4,10,20,30,40,50,100]
del x.L[1]
x.L = [4,20,30,40,50,100]
del x.L[1:3]
x.L = [4,40,50,100]
x.L[1:1] = [1,2,3,4,5]
x.L = [4,1,2,3,4,5,40,50,100]
x.L.remove(2)
x.L = [4,1,3,4,5,40,50,100]
x.L.pop()
x.L = [4,1,3,4,5,40,50]
x.L.pop(1)
x.L = [4,3,4,5,40,50]
x = X([4,3,4,5,40,50])