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
d1dd80fd
Commit
d1dd80fd
authored
Sep 07, 2008
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enable the switch transform also for long 'or' expressions in a single 'if' statement
parent
117049de
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
54 additions
and
2 deletions
+54
-2
Cython/Compiler/Optimize.py
Cython/Compiler/Optimize.py
+4
-2
tests/run/switch.pyx
tests/run/switch.pyx
+50
-0
No files found.
Cython/Compiler/Optimize.py
View file @
d1dd80fd
...
@@ -56,9 +56,8 @@ class SwitchTransform(Visitor.VisitorTransform):
...
@@ -56,9 +56,8 @@ class SwitchTransform(Visitor.VisitorTransform):
def
visit_IfStatNode
(
self
,
node
):
def
visit_IfStatNode
(
self
,
node
):
self
.
visitchildren
(
node
)
self
.
visitchildren
(
node
)
if
len
(
node
.
if_clauses
)
<
3
:
return
node
common_var
=
None
common_var
=
None
case_count
=
0
cases
=
[]
cases
=
[]
for
if_clause
in
node
.
if_clauses
:
for
if_clause
in
node
.
if_clauses
:
var
,
conditions
=
self
.
extract_conditions
(
if_clause
.
condition
)
var
,
conditions
=
self
.
extract_conditions
(
if_clause
.
condition
)
...
@@ -70,9 +69,12 @@ class SwitchTransform(Visitor.VisitorTransform):
...
@@ -70,9 +69,12 @@ class SwitchTransform(Visitor.VisitorTransform):
return
node
return
node
else
:
else
:
common_var
=
var
common_var
=
var
case_count
+=
len
(
conditions
)
cases
.
append
(
Nodes
.
SwitchCaseNode
(
pos
=
if_clause
.
pos
,
cases
.
append
(
Nodes
.
SwitchCaseNode
(
pos
=
if_clause
.
pos
,
conditions
=
conditions
,
conditions
=
conditions
,
body
=
if_clause
.
body
))
body
=
if_clause
.
body
))
if
case_count
<
2
:
return
node
common_var
=
unwrap_node
(
common_var
)
common_var
=
unwrap_node
(
common_var
)
return
Nodes
.
SwitchStatNode
(
pos
=
node
.
pos
,
return
Nodes
.
SwitchStatNode
(
pos
=
node
.
pos
,
...
...
tests/run/switch.pyx
View file @
d1dd80fd
...
@@ -62,6 +62,33 @@ __doc__ = u"""
...
@@ -62,6 +62,33 @@ __doc__ = u"""
12
12
>>> switch_c(13)
>>> switch_c(13)
0
0
>>> switch_or(0)
0
>>> switch_or(1)
1
>>> switch_or(2)
1
>>> switch_or(3)
1
>>> switch_or(4)
0
>>> switch_short(0)
0
>>> switch_short(1)
1
>>> switch_short(2)
2
>>> switch_short(3)
0
>>> switch_off(0)
0
>>> switch_off(1)
1
>>> switch_off(2)
0
"""
"""
def
switch_simple_py
(
x
):
def
switch_simple_py
(
x
):
...
@@ -123,3 +150,26 @@ def switch_c(int x):
...
@@ -123,3 +150,26 @@ def switch_c(int x):
else
:
else
:
return
0
return
0
return
-
1
return
-
1
def
switch_or
(
int
x
):
if
x
==
1
or
x
==
2
or
x
==
3
:
return
1
else
:
return
0
return
-
1
def
switch_short
(
int
x
):
if
x
==
1
:
return
1
elif
2
==
x
:
return
2
else
:
return
0
return
-
1
def
switch_off
(
int
x
):
if
x
==
1
:
return
1
else
:
return
0
return
-
1
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