fixes, additions and tests for libcpp.vector
authorLisandro Dalcin <dalcinl@gmail.com>
Tue, 5 Oct 2010 17:35:12 +0000 (14:35 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Tue, 5 Oct 2010 17:35:12 +0000 (14:35 -0300)
Cython/Includes/libcpp/vector.pxd
tests/run/cpp_stl_vector.pyx

index 0a6ba1f078fb29be9262cbfedb82d18348b1e2de..61c6aefbc8a2f474506fba443c61b55f020d1ede 100644 (file)
@@ -6,12 +6,20 @@ cdef extern from "<vector>" namespace "std":
             iterator operator--()
             bint operator==(iterator)
             bint operator!=(iterator)
+            bint operator< (iterator)
+            bint operator> (iterator)
+            bint operator<=(iterator)
+            bint operator>=(iterator)
         cppclass reverse_iterator:
             T& operator*()
             iterator operator++()
             iterator operator--()
-            bint operator==(iterator)
-            bint operator!=(iterator)
+            bint operator==(reverse_iterator)
+            bint operator!=(reverse_iterator)
+            bint operator< (reverse_iterator)
+            bint operator> (reverse_iterator)
+            bint operator<=(reverse_iterator)
+            bint operator>=(reverse_iterator)
         #cppclass const_iterator(iterator):
         #    pass
         #cppclass const_reverse_iterator(reverse_iterator):
index eee6ef893b2e11f8a3db175e12eb7a1b6703564a..4e03706bf2171e4e901734119389387861ad5f6d 100644 (file)
@@ -1,11 +1,7 @@
 from cython.operator cimport dereference as d
+from cython.operator cimport preincrement as incr
 
-cdef extern from "<vector>" namespace "std":
-
-    cdef cppclass vector[T]:
-        void push_back(T)
-        size_t size()
-        T& operator[](size_t)
+from libcpp.vector cimport vector
 
 def simple_test(double x):
     """
@@ -71,3 +67,43 @@ def index_set_test(L):
         return d(v)[0], d(v)[v.size()-1]
     finally:
         del v
+
+def iteration_test(L):
+    """
+    >>> iteration_test([1,2,4,8])
+    1
+    2
+    4
+    8
+    """
+    try:
+        v = new vector[int]()
+        for a in L:
+            v.push_back(a)
+        it = v.begin()
+        while it != v.end():
+            a = d(it)
+            incr(it)
+            print(a)
+    finally:
+        del v
+
+def reverse_iteration_test(L):
+    """
+    >>> reverse_iteration_test([1,2,4,8])
+    8
+    4
+    2
+    1
+    """
+    try:
+        v = new vector[int]()
+        for a in L:
+            v.push_back(a)
+        it = v.rbegin()
+        while it != v.rend():
+            a = d(it)
+            incr(it)
+            print(a)
+    finally:
+        del v