Commit 760da3f8 authored by Robert Bradshaw's avatar Robert Bradshaw

Merge branch 'larsmans-libcpp'

parents cdcb14ed 40cb5671
...@@ -4738,6 +4738,9 @@ class SimpleCallNode(CallNode): ...@@ -4738,6 +4738,9 @@ class SimpleCallNode(CallNode):
self.is_temp = 1 self.is_temp = 1
# func_type.exception_check = True # func_type.exception_check = True
if self.is_temp and self.type.is_reference:
self.type = PyrexTypes.CFakeReferenceType(self.type.ref_base_type)
# Called in 'nogil' context? # Called in 'nogil' context?
self.nogil = env.nogil self.nogil = env.nogil
if (self.nogil and if (self.nogil and
...@@ -5698,6 +5701,8 @@ class AttributeNode(ExprNode): ...@@ -5698,6 +5701,8 @@ class AttributeNode(ExprNode):
self.op = "->" self.op = "->"
elif obj_type.is_extension_type or obj_type.is_builtin_type: elif obj_type.is_extension_type or obj_type.is_builtin_type:
self.op = "->" self.op = "->"
elif obj_type.is_reference and obj_type.is_fake_reference:
self.op = "->"
else: else:
self.op = "." self.op = "."
if obj_type.has_attributes: if obj_type.has_attributes:
......
...@@ -2422,6 +2422,7 @@ class CNullPtrType(CPtrType): ...@@ -2422,6 +2422,7 @@ class CNullPtrType(CPtrType):
class CReferenceType(BaseType): class CReferenceType(BaseType):
is_reference = 1 is_reference = 1
is_fake_reference = 0
def __init__(self, base_type): def __init__(self, base_type):
self.ref_base_type = base_type self.ref_base_type = base_type
...@@ -2444,7 +2445,7 @@ class CReferenceType(BaseType): ...@@ -2444,7 +2445,7 @@ class CReferenceType(BaseType):
if base_type == self.ref_base_type: if base_type == self.ref_base_type:
return self return self
else: else:
return CReferenceType(base_type) return type(self)(base_type)
def deduce_template_params(self, actual): def deduce_template_params(self, actual):
return self.ref_base_type.deduce_template_params(actual) return self.ref_base_type.deduce_template_params(actual)
...@@ -2453,6 +2454,22 @@ class CReferenceType(BaseType): ...@@ -2453,6 +2454,22 @@ class CReferenceType(BaseType):
return getattr(self.ref_base_type, name) return getattr(self.ref_base_type, name)
class CFakeReferenceType(CReferenceType):
is_fake_reference = 1
def __repr__(self):
return "<CFakeReferenceType %s>" % repr(self.ref_base_type)
def __str__(self):
return "%s [&]" % self.ref_base_type
def declaration_code(self, entity_code,
for_display = 0, dll_linkage = None, pyrex = 0):
#print "CReferenceType.declaration_code: pointer to", self.base_type ###
return "__Pyx_FakeReference<%s> %s" % (self.ref_base_type.empty_declaration_code(), entity_code)
class CFuncType(CType): class CFuncType(CType):
# return_type CType # return_type CType
# args [CFuncTypeArg] # args [CFuncTypeArg]
......
from libcpp cimport bool
cdef extern from "<algorithm>" namespace "std" nogil: cdef extern from "<algorithm>" namespace "std" nogil:
# Sorting and searching
bool binary_search[Iter, T](Iter first, Iter last, const T& value)
bool binary_search[Iter, T, Compare](Iter first, Iter last, const T& value,
Compare comp)
void partial_sort[Iter](Iter first, Iter middle, Iter last)
void partial_sort[Iter, Compare](Iter first, Iter middle, Iter last,
Compare comp)
void sort[Iter](Iter first, Iter last)
void sort[Iter, Compare](Iter first, Iter last, Compare comp)
# Binary heaps (priority queues)
void make_heap[Iter](Iter first, Iter last) void make_heap[Iter](Iter first, Iter last)
void make_heap[Iter, Compare](Iter first, Iter last, Compare comp) void make_heap[Iter, Compare](Iter first, Iter last, Compare comp)
......
...@@ -33,27 +33,27 @@ cdef extern from "<map>" namespace "std" nogil: ...@@ -33,27 +33,27 @@ cdef extern from "<map>" namespace "std" nogil:
bint operator>(map&, map&) bint operator>(map&, map&)
bint operator<=(map&, map&) bint operator<=(map&, map&)
bint operator>=(map&, map&) bint operator>=(map&, map&)
U& at(T&) U& at(const T&) except +
iterator begin() iterator begin()
const_iterator const_begin "begin" () const_iterator const_begin "begin" ()
void clear() void clear()
size_t count(T&) size_t count(const T&)
bint empty() bint empty()
iterator end() iterator end()
const_iterator const_end "end" () const_iterator const_end "end" ()
pair[iterator, iterator] equal_range(T&) pair[iterator, iterator] equal_range(const T&)
#pair[const_iterator, const_iterator] equal_range(key_type&) #pair[const_iterator, const_iterator] equal_range(key_type&)
void erase(iterator) void erase(iterator)
void erase(iterator, iterator) void erase(iterator, iterator)
size_t erase(T&) size_t erase(const T&)
iterator find(T&) iterator find(const T&)
const_iterator const_find "find" (T&) const_iterator const_find "find" (const T&)
pair[iterator, bint] insert(pair[T, U]) # XXX pair[T,U]& pair[iterator, bint] insert(pair[T, U]) except + # XXX pair[T,U]&
iterator insert(iterator, pair[T, U]) # XXX pair[T,U]& iterator insert(iterator, pair[T, U]) except + # XXX pair[T,U]&
#void insert(input_iterator, input_iterator) #void insert(input_iterator, input_iterator)
#key_compare key_comp() #key_compare key_comp()
iterator lower_bound(T&) iterator lower_bound(const T&)
#const_iterator lower_bound(key_type&) #const_iterator lower_bound(const key_type&)
size_t max_size() size_t max_size()
reverse_iterator rbegin() reverse_iterator rbegin()
#const_reverse_iterator rbegin() #const_reverse_iterator rbegin()
...@@ -61,6 +61,6 @@ cdef extern from "<map>" namespace "std" nogil: ...@@ -61,6 +61,6 @@ cdef extern from "<map>" namespace "std" nogil:
#const_reverse_iterator rend() #const_reverse_iterator rend()
size_t size() size_t size()
void swap(map&) void swap(map&)
iterator upper_bound(T&) iterator upper_bound(const T&)
#const_iterator upper_bound(key_type&) #const_iterator upper_bound(const key_type&)
#value_compare value_comp() #value_compare value_comp()
...@@ -31,19 +31,19 @@ cdef extern from "<set>" namespace "std" nogil: ...@@ -31,19 +31,19 @@ cdef extern from "<set>" namespace "std" nogil:
iterator begin() iterator begin()
#const_iterator begin() #const_iterator begin()
void clear() void clear()
size_t count(T&) size_t count(const T&)
bint empty() bint empty()
iterator end() iterator end()
#const_iterator end() #const_iterator end()
pair[iterator, iterator] equal_range(T&) pair[iterator, iterator] equal_range(const T&)
#pair[const_iterator, const_iterator] equal_range(T&) #pair[const_iterator, const_iterator] equal_range(T&)
void erase(iterator) void erase(iterator)
void erase(iterator, iterator) void erase(iterator, iterator)
size_t erase(T&) size_t erase(T&)
iterator find(T&) iterator find(T&)
#const_iterator find(T&) #const_iterator find(T&)
pair[iterator, bint] insert(T&) pair[iterator, bint] insert(const T&) except +
iterator insert(iterator, T&) iterator insert(iterator, const T&) except +
#void insert(input_iterator, input_iterator) #void insert(input_iterator, input_iterator)
#key_compare key_comp() #key_compare key_comp()
iterator lower_bound(T&) iterator lower_bound(T&)
...@@ -55,6 +55,6 @@ cdef extern from "<set>" namespace "std" nogil: ...@@ -55,6 +55,6 @@ cdef extern from "<set>" namespace "std" nogil:
#const_reverse_iterator rend() #const_reverse_iterator rend()
size_t size() size_t size()
void swap(set&) void swap(set&)
iterator upper_bound(T&) iterator upper_bound(const T&)
#const_iterator upper_bound(T&) #const_iterator upper_bound(const T&)
#value_compare value_comp() #value_compare value_comp()
...@@ -41,9 +41,9 @@ cdef extern from "<vector>" namespace "std" nogil: ...@@ -41,9 +41,9 @@ cdef extern from "<vector>" namespace "std" nogil:
bint operator>(vector&, vector&) bint operator>(vector&, vector&)
bint operator<=(vector&, vector&) bint operator<=(vector&, vector&)
bint operator>=(vector&, vector&) bint operator>=(vector&, vector&)
void assign(size_t, T&) void assign(size_t, const T&)
void assign[input_iterator](input_iterator, input_iterator) void assign[input_iterator](input_iterator, input_iterator) except +
T& at(size_t) T& at(size_t) except +
T& back() T& back()
iterator begin() iterator begin()
#const_iterator begin() #const_iterator begin()
...@@ -55,22 +55,22 @@ cdef extern from "<vector>" namespace "std" nogil: ...@@ -55,22 +55,22 @@ cdef extern from "<vector>" namespace "std" nogil:
iterator erase(iterator) iterator erase(iterator)
iterator erase(iterator, iterator) iterator erase(iterator, iterator)
T& front() T& front()
iterator insert(iterator, T&) iterator insert(iterator, const T&) except +
void insert(iterator, size_t, T&) void insert(iterator, size_t, const T&) except +
void insert(iterator, iterator, iterator) void insert[Iter](iterator, Iter, Iter) except +
size_t max_size() size_t max_size()
void pop_back() void pop_back()
void push_back(T&) void push_back(T&) except +
reverse_iterator rbegin() reverse_iterator rbegin()
#const_reverse_iterator rbegin() #const_reverse_iterator rbegin()
reverse_iterator rend() reverse_iterator rend()
#const_reverse_iterator rend() #const_reverse_iterator rend()
void reserve(size_t) void reserve(size_t)
void resize(size_t) void resize(size_t) except +
void resize(size_t, T&) void resize(size_t, T&) except +
size_t size() size_t size()
void swap(vector&) void swap(vector&)
#C++0x methods # C++11 methods
T* data() T* data()
void shrink_to_fit() void shrink_to_fit()
...@@ -217,12 +217,24 @@ static CYTHON_INLINE float __PYX_NAN() { ...@@ -217,12 +217,24 @@ static CYTHON_INLINE float __PYX_NAN() {
#define __Pyx_void_to_None(void_result) (void_result, Py_INCREF(Py_None), Py_None) #define __Pyx_void_to_None(void_result) (void_result, Py_INCREF(Py_None), Py_None)
// Work around clang bug http://stackoverflow.com/questions/21847816/c-invoke-nested-template-class-destructor
#ifdef __cplusplus #ifdef __cplusplus
// Work around clang bug http://stackoverflow.com/questions/21847816/c-invoke-nested-template-class-destructor
template<typename T> template<typename T>
void __Pyx_call_destructor(T* x) { void __Pyx_call_destructor(T* x) {
x->~T(); x->~T();
} }
// Used for temporary variables of "reference" type.
template<typename T>
class __Pyx_FakeReference {
public:
__Pyx_FakeReference() : ptr(NULL) { }
__Pyx_FakeReference(T& ref) : ptr(&ref) { }
T *operator->() { return ptr; }
operator T&() { return *ptr; }
private:
T *ptr;
};
#endif #endif
/////////////// UtilityFunctionPredeclarations.proto /////////////// /////////////// UtilityFunctionPredeclarations.proto ///////////////
......
...@@ -5,8 +5,9 @@ cdef extern from "cpp_template_ref_args.h": ...@@ -5,8 +5,9 @@ cdef extern from "cpp_template_ref_args.h":
cdef cppclass Bar[T]: cdef cppclass Bar[T]:
Bar() Bar()
Bar[T] & ref() # bug: Bar[T] created before class fully defined
T value T value
Bar[T] & ref() except +
cdef cppclass Foo[T]: cdef cppclass Foo[T]:
Foo() Foo()
...@@ -28,3 +29,12 @@ def test_template_ref_arg(int x): ...@@ -28,3 +29,12 @@ def test_template_ref_arg(int x):
bar.value = x bar.value = x
return foo.bar_value(bar.ref()) return foo.bar_value(bar.ref())
def test_template_ref_attr(int x):
"""
>>> test_template_ref_attr(4)
4
"""
cdef Bar[int] bar
bar.value = x
return bar.ref().value
# tag: cpp # tag: cpp
from libcpp cimport bool from libcpp cimport bool
from libcpp.algorithm cimport make_heap, sort_heap from libcpp.algorithm cimport make_heap, sort_heap, sort, partial_sort
from libcpp.vector cimport vector from libcpp.vector cimport vector
...@@ -27,3 +27,33 @@ def heapsort(l, bool reverse=False): ...@@ -27,3 +27,33 @@ def heapsort(l, bool reverse=False):
sort_heap(v.begin(), v.end()) sort_heap(v.begin(), v.end())
return v return v
def partialsort(l, int k, reverse=False):
"""
>>> partialsort([4, 2, 3, 1, 5], k=2)[:2]
[1, 2]
>>> partialsort([4, 2, 3, 1, 5], k=2, reverse=True)[:2]
[5, 4]
"""
cdef vector[int] v = l
if reverse:
partial_sort(v.begin(), v.begin() + k, v.end(), greater)
else:
partial_sort(v.begin(), v.begin() + k, v.end())
return v
def stdsort(l, reverse=False):
"""
>>> stdsort([3, 2, 1, 4, 5])
[1, 2, 3, 4, 5]
>>> stdsort([3, 2, 1, 4, 5], reverse=True)
[5, 4, 3, 2, 1]
"""
cdef vector[int] v = l
if reverse:
sort(v.begin(), v.end(), greater)
else:
sort(v.begin(), v.end())
return v
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment