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
2d146971
Commit
2d146971
authored
Dec 29, 2014
by
root
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Functioning with Nodes which do not have a constant value that is a long or int.
parent
db4001fa
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
14 deletions
+85
-14
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+57
-13
tests/run/reversed_iteration.pyx
tests/run/reversed_iteration.pyx
+28
-1
No files found.
Cython/Compiler/Optimize.py
View file @
2d146971
...
@@ -663,20 +663,64 @@ class IterationTransform(Visitor.EnvTransform):
...
@@ -663,20 +663,64 @@ class IterationTransform(Visitor.EnvTransform):
if
abs_step
!=
1
:
if
abs_step
!=
1
:
if
not
(
isinstance
(
bound1
.
constant_result
,
(
int
,
long
))
and
if
not
(
isinstance
(
bound1
.
constant_result
,
(
int
,
long
))
and
isinstance
(
bound2
.
constant_result
,
(
int
,
long
))):
isinstance
(
bound2
.
constant_result
,
(
int
,
long
))):
# FIXME: calculate final bounds at runtime
spanning_type
=
PyrexTypes
.
spanning_type
(
bound1
.
type
,
bound2
.
type
)
return
node
spanning_step_type
=
PyrexTypes
.
spanning_type
(
spanning_type
,
step
.
type
)
if
step_value
<
0
:
begin_value
=
bound2
.
constant_result
if
step_value
<
0
:
end_value
=
bound1
.
constant_result
begin_value
=
bound2
bound1_value
=
begin_value
-
abs_step
*
((
begin_value
-
end_value
-
1
)
//
abs_step
)
-
1
end_value
=
bound1
final_op
=
'-'
final_node
=
ExprNodes
.
SubNode
else
:
begin_value
=
bound1
end_value
=
bound2
final_op
=
'+'
final_node
=
ExprNodes
.
AddNode
beg_sub_end
=
ExprNodes
.
SubNode
(
bound1
.
pos
,
operand1
=
begin_value
,
operator
=
'-'
,
operand2
=
end_value
,
type
=
spanning_type
)
one_node
=
ExprNodes
.
IntNode
(
bound1
.
pos
,
value
=
'1'
,
constant_result
=
1
)
beg_sub_end_sub_1
=
ExprNodes
.
SubNode
(
bound1
.
pos
,
operand1
=
beg_sub_end
,
operator
=
'-'
,
operand2
=
one_node
,
type
=
spanning_step_type
)
abs_step_node
=
ExprNodes
.
IntNode
(
bound1
.
pos
,
value
=
str
(
abs_step
),
constant_value
=
abs_step
)
div_val_node
=
ExprNodes
.
DivNode
(
bound1
.
pos
,
operand1
=
beg_sub_end_sub_1
,
operator
=
'//'
,
operand2
=
abs_step_node
,
type
=
spanning_step_type
)
delta_node
=
ExprNodes
.
MulNode
(
bound1
.
pos
,
operand1
=
abs_step_node
,
operator
=
'*'
,
operand2
=
div_val_node
,
type
=
spanning_step_type
)
beg_func_delta
=
final_node
(
bound1
.
pos
,
operand1
=
bound2
,
operator
=
final_op
,
operand2
=
delta_node
,
type
=
spanning_step_type
)
bound1
=
final_node
(
bound1
.
pos
,
operand1
=
beg_func_delta
,
operator
=
final_op
,
operand2
=
one_node
,
type
=
spanning_type
)
else
:
else
:
begin_value
=
bound1
.
constant_result
if
step_value
<
0
:
end_value
=
bound2
.
constant_result
begin_value
=
bound2
.
constant_result
bound1_value
=
end_value
+
abs_step
*
((
begin_value
-
end_value
-
1
)
//
abs_step
)
+
1
end_value
=
bound1
.
constant_result
bound1_value
=
begin_value
-
abs_step
*
((
begin_value
-
end_value
-
1
)
//
abs_step
)
-
1
bound1
=
ExprNodes
.
IntNode
(
else
:
bound1
.
pos
,
value
=
str
(
bound1_value
),
constant_result
=
bound1_value
,
begin_value
=
bound1
.
constant_result
type
=
PyrexTypes
.
spanning_type
(
bound1
.
type
,
bound2
.
type
))
end_value
=
bound2
.
constant_result
bound1_value
=
end_value
+
abs_step
*
((
begin_value
-
end_value
-
1
)
//
abs_step
)
+
1
bound1
=
ExprNodes
.
IntNode
(
bound1
.
pos
,
value
=
str
(
bound1_value
),
constant_result
=
bound1_value
,
type
=
PyrexTypes
.
spanning_type
(
bound1
.
type
,
bound2
.
type
))
if
step_value
<
0
:
if
step_value
<
0
:
step_value
=
-
step_value
step_value
=
-
step_value
...
...
tests/run/reversed_iteration.pyx
View file @
2d146971
...
@@ -134,7 +134,8 @@ def reversed_range_step_neg(int a, int b):
...
@@ -134,7 +134,8 @@ def reversed_range_step_neg(int a, int b):
return
result
,
i
return
result
,
i
#cython.test_assert_path_exists('//ForFromStatNode')
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
@
cython
.
test_fail_if_path_exists
(
'//ForInStatNode'
)
def
reversed_range_step3
(
int
a
,
int
b
):
def
reversed_range_step3
(
int
a
,
int
b
):
"""
"""
>>> [ i for i in _reversed(range(-5, 0, 3)) ]
>>> [ i for i in _reversed(range(-5, 0, 3)) ]
...
@@ -159,6 +160,32 @@ def reversed_range_step3(int a, int b):
...
@@ -159,6 +160,32 @@ def reversed_range_step3(int a, int b):
return
result
,
i
return
result
,
i
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
@
cython
.
test_fail_if_path_exists
(
'//ForInStatNode'
)
def
reversed_range_step3_neg
(
int
a
,
int
b
):
"""
>>> [ i for i in _reversed(range(0, -5, -3)) ]
[-3, 0]
>>> reversed_range_step3_neg(0, -5)
([-3, 0], 0)
>>> [ i for i in _reversed(range(5, 0, -3)) ]
[2, 5]
>>> reversed_range_step3_neg(5, 0)
([2, 5], 5)
>>> [ i for i in _reversed(range(0, 5, -3)) ]
[]
>>> reversed_range_step3_neg(0, 5)
([], 99)
"""
cdef
int
i
=
99
result
=
[]
for
i
in
reversed
(
range
(
a
,
b
,
-
3
)):
result
.
append
(
i
)
return
result
,
i
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
@
cython
.
test_assert_path_exists
(
'//ForFromStatNode'
)
@
cython
.
test_fail_if_path_exists
(
'//ForInStatNode'
)
@
cython
.
test_fail_if_path_exists
(
'//ForInStatNode'
)
def
reversed_range_constant
():
def
reversed_range_constant
():
...
...
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