Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
172f5203
Commit
172f5203
authored
Jan 26, 2015
by
Lars Buitinck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add sort, partial_sort and binary_sort to libcpp.algorithm
parent
b44bdbb3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
1 deletion
+47
-1
Cython/Includes/libcpp/algorithm.pxd
Cython/Includes/libcpp/algorithm.pxd
+16
-0
tests/run/libcpp_algo.pyx
tests/run/libcpp_algo.pyx
+31
-1
No files found.
Cython/Includes/libcpp/algorithm.pxd
View file @
172f5203
from
libcpp
cimport
bool
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
,
Compare
](
Iter
first
,
Iter
last
,
Compare
comp
)
...
...
tests/run/libcpp_algo.pyx
View file @
172f5203
# tag: cpp
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
...
...
@@ -27,3 +27,33 @@ def heapsort(l, bool reverse=False):
sort_heap
(
v
.
begin
(),
v
.
end
())
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment