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
09b4c92a
Commit
09b4c92a
authored
May 14, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement for-in-reversed(list/tuple)
parent
fabff41b
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
93 additions
and
8 deletions
+93
-8
Cython/Compiler/ExprNodes.py
Cython/Compiler/ExprNodes.py
+26
-6
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+1
-1
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+9
-1
tests/run/reversed_iteration.pyx
tests/run/reversed_iteration.pyx
+57
-0
No files found.
Cython/Compiler/ExprNodes.py
View file @
09b4c92a
...
...
@@ -1788,6 +1788,7 @@ class IteratorNode(ExprNode):
type
=
py_object_type
iter_func_ptr
=
None
counter_cname
=
None
reversed
=
False
# currently only used for list/tuple types (see Optimize.py)
subexprs
=
[
'sequence'
]
...
...
@@ -1817,6 +1818,9 @@ class IteratorNode(ExprNode):
raise
InternalError
(
"for in carray slice not transformed"
)
is_builtin_sequence
=
sequence_type
is
list_type
or
\
sequence_type
is
tuple_type
if
not
is_builtin_sequence
:
# reversed() not currently optimised (see Optimize.py)
assert
not
self
.
reversed
,
"internal error: reversed() only implemented for list/tuple objects"
self
.
may_be_a_sequence
=
not
sequence_type
.
is_builtin_type
if
self
.
may_be_a_sequence
:
code
.
putln
(
...
...
@@ -1826,12 +1830,21 @@ class IteratorNode(ExprNode):
if
is_builtin_sequence
or
self
.
may_be_a_sequence
:
self
.
counter_cname
=
code
.
funcstate
.
allocate_temp
(
PyrexTypes
.
c_py_ssize_t_type
,
manage_ref
=
False
)
if
self
.
reversed
:
if
sequence_type
is
list_type
:
init_value
=
'PyList_GET_SIZE(%s) - 1'
%
self
.
result
()
else
:
init_value
=
'PyTuple_GET_SIZE(%s) - 1'
%
self
.
result
()
else
:
init_value
=
'0'
code
.
putln
(
"%s = 0; %s = %s; __Pyx_INCREF(%s);"
%
(
self
.
counter_cname
,
"%s = %s; __Pyx_INCREF(%s); %s = %s;"
%
(
self
.
result
(),
self
.
sequence
.
py_result
(),
self
.
result
()))
self
.
result
(),
self
.
counter_cname
,
init_value
))
if
not
is_builtin_sequence
:
self
.
iter_func_ptr
=
code
.
funcstate
.
allocate_temp
(
self
.
_func_iternext_type
,
manage_ref
=
False
)
if
self
.
may_be_a_sequence
:
...
...
@@ -1854,17 +1867,24 @@ class IteratorNode(ExprNode):
self
.
counter_cname
,
test_name
,
self
.
py_result
()))
if
self
.
reversed
:
inc_dec
=
'--'
else
:
inc_dec
=
'++'
code
.
putln
(
"%s = Py%s_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s
++
;"
%
(
"%s = Py%s_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s
%s
;"
%
(
result_name
,
test_name
,
self
.
py_result
(),
self
.
counter_cname
,
result_name
,
self
.
counter_cname
))
self
.
counter_cname
,
inc_dec
))
def
generate_iter_next_result_code
(
self
,
result_name
,
code
):
sequence_type
=
self
.
sequence
.
type
if
self
.
reversed
:
code
.
putln
(
"if (%s < 0) break;"
%
self
.
counter_cname
)
if
sequence_type
is
list_type
:
self
.
generate_next_sequence_item
(
'List'
,
result_name
,
code
)
return
...
...
@@ -1913,7 +1933,7 @@ class NextNode(AtomicExprNode):
type
=
py_object_type
def
__init__
(
self
,
iterator
,
env
):
def
__init__
(
self
,
iterator
):
self
.
pos
=
iterator
.
pos
self
.
iterator
=
iterator
if
iterator
.
type
.
is_ptr
or
iterator
.
type
.
is_array
:
...
...
Cython/Compiler/Nodes.py
View file @
09b4c92a
...
...
@@ -4631,7 +4631,7 @@ class ForInStatNode(LoopNode, StatNode):
import
ExprNodes
self
.
target
.
analyse_target_types
(
env
)
self
.
iterator
.
analyse_expressions
(
env
)
self
.
item
=
ExprNodes
.
NextNode
(
self
.
iterator
,
env
)
self
.
item
=
ExprNodes
.
NextNode
(
self
.
iterator
)
if
(
self
.
iterator
.
type
.
is_ptr
or
self
.
iterator
.
type
.
is_array
)
and
\
self
.
target
.
type
.
assignable_from
(
self
.
iterator
.
type
):
# C array slice optimization.
...
...
Cython/Compiler/Optimize.py
View file @
09b4c92a
...
...
@@ -218,7 +218,15 @@ class IterationTransform(Visitor.VisitorTransform):
error
(
reversed_function
.
pos
,
"reversed() takes exactly 1 argument"
)
return
node
return
self
.
_optimise_for_loop
(
node
,
args
[
0
],
reversed
=
True
)
arg
=
args
[
0
]
# reversed(list/tuple) ?
if
arg
.
type
in
(
Builtin
.
tuple_type
,
Builtin
.
list_type
):
node
.
iterator
.
sequence
=
arg
.
as_none_safe_node
(
"'NoneType' object is not iterable"
)
node
.
iterator
.
reversed
=
True
return
node
return
self
.
_optimise_for_loop
(
node
,
arg
,
reversed
=
True
)
PyUnicode_AS_UNICODE_func_type
=
PyrexTypes
.
CFuncType
(
PyrexTypes
.
c_py_unicode_ptr_type
,
[
...
...
tests/run/reversed_iteration.pyx
View file @
09b4c92a
# mode: run
# tag: forin, builtins, reversed, enumerate
cimport
cython
...
...
@@ -7,18 +9,73 @@ IS_PY3 = sys.version_info[0] >= 3
def
_reversed
(
it
):
return
list
(
it
)[::
-
1
]
@
cython
.
test_assert_path_exists
(
'//ForInStatNode'
,
'//ForInStatNode/IteratorNode'
,
'//ForInStatNode/IteratorNode[@reversed = True]'
,
)
@
cython
.
test_fail_if_path_exists
(
'//ForInStatNode/IteratorNode//SimpleCallNode'
)
def
reversed_list
(
list
l
):
"""
>>> [ i for i in _reversed([1,2,3,4]) ]
[4, 3, 2, 1]
>>> reversed_list([1,2,3,4])
[4, 3, 2, 1]
>>> reversed_list([])
[]
>>> reversed_list(None)
Traceback (most recent call last):
TypeError: 'NoneType' object is not iterable
"""
result
=
[]
for
item
in
reversed
(
l
):
result
.
append
(
item
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForInStatNode'
,
'//ForInStatNode/IteratorNode'
,
'//ForInStatNode/IteratorNode[@reversed = True]'
,
)
@
cython
.
test_fail_if_path_exists
(
'//ForInStatNode/IteratorNode//SimpleCallNode'
)
def
reversed_tuple
(
tuple
t
):
"""
>>> [ i for i in _reversed((1,2,3,4)) ]
[4, 3, 2, 1]
>>> reversed_tuple((1,2,3,4))
[4, 3, 2, 1]
>>> reversed_tuple(())
[]
>>> reversed_tuple(None)
Traceback (most recent call last):
TypeError: 'NoneType' object is not iterable
"""
result
=
[]
for
item
in
reversed
(
t
):
result
.
append
(
item
)
return
result
@
cython
.
test_assert_path_exists
(
'//ForInStatNode'
,
'//ForInStatNode/IteratorNode'
,
'//ForInStatNode/IteratorNode[@reversed = True]'
,
)
@
cython
.
test_fail_if_path_exists
(
'//ForInStatNode/IteratorNode//SimpleCallNode'
)
def
enumerate_reversed_list
(
list
l
):
"""
>>> list(enumerate(_reversed([1,2,3])))
[(0, 3), (1, 2), (2, 1)]
>>> enumerate_reversed_list([1,2,3])
[(0, 3), (1, 2), (2, 1)]
>>> enumerate_reversed_list([])
[]
>>> enumerate_reversed_list(None)
Traceback (most recent call last):
TypeError: 'NoneType' object is not iterable
"""
result
=
[]
cdef
Py_ssize_t
i
for
i
,
item
in
enumerate
(
reversed
(
l
)):
result
.
append
((
i
,
item
))
return
result
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
def
reversed_range
(
int
N
):
"""
...
...
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