Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
694413da
Commit
694413da
authored
May 28, 2014
by
Kevin Modzelewski
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #63 from kkszysiu/list_index_impl
Implement list.index()
parents
bed955e7
4ff9b619
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
0 deletions
+23
-0
src/runtime/list.cpp
src/runtime/list.cpp
+16
-0
test/tests/list.py
test/tests/list.py
+7
-0
No files found.
src/runtime/list.cpp
View file @
694413da
...
@@ -366,6 +366,21 @@ Box* listCount(BoxedList* self, Box* elt) {
...
@@ -366,6 +366,21 @@ Box* listCount(BoxedList* self, Box* elt) {
return
new
BoxedInt
(
count
);
return
new
BoxedInt
(
count
);
}
}
Box
*
listIndex
(
BoxedList
*
self
,
Box
*
elt
)
{
int
size
=
self
->
size
;
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
Box
*
e
=
self
->
elts
->
elts
[
i
];
Box
*
cmp
=
compareInternal
(
e
,
elt
,
AST_TYPE
::
Eq
,
NULL
);
bool
b
=
nonzero
(
cmp
);
if
(
b
)
return
new
BoxedInt
(
i
);
}
BoxedString
*
tostr
=
static_cast
<
BoxedString
*>
(
repr
(
elt
));
raiseExcHelper
(
ValueError
,
"%s is not in list"
,
tostr
->
s
.
c_str
());
}
Box
*
listRemove
(
BoxedList
*
self
,
Box
*
elt
)
{
Box
*
listRemove
(
BoxedList
*
self
,
Box
*
elt
)
{
assert
(
self
->
cls
==
list_cls
);
assert
(
self
->
cls
==
list_cls
);
...
@@ -473,6 +488,7 @@ void setupList() {
...
@@ -473,6 +488,7 @@ void setupList() {
list_cls
->
giveAttr
(
"__new__"
,
new
BoxedFunction
(
new_
));
list_cls
->
giveAttr
(
"__new__"
,
new
BoxedFunction
(
new_
));
list_cls
->
giveAttr
(
"count"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listCount
,
BOXED_INT
,
2
,
false
)));
list_cls
->
giveAttr
(
"count"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listCount
,
BOXED_INT
,
2
,
false
)));
list_cls
->
giveAttr
(
"index"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listIndex
,
NULL
,
2
,
false
)));
list_cls
->
giveAttr
(
"remove"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listRemove
,
NONE
,
2
,
false
)));
list_cls
->
giveAttr
(
"remove"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listRemove
,
NONE
,
2
,
false
)));
list_cls
->
giveAttr
(
"reverse"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listReverse
,
NONE
,
1
,
false
)));
list_cls
->
giveAttr
(
"reverse"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listReverse
,
NONE
,
1
,
false
)));
list_cls
->
freeze
();
list_cls
->
freeze
();
...
...
test/tests/list.py
View file @
694413da
...
@@ -49,6 +49,13 @@ for i in xrange(5):
...
@@ -49,6 +49,13 @@ for i in xrange(5):
l
.
reverse
()
l
.
reverse
()
print
l
print
l
# list index
list_index
=
[
1
,
2
,
3
,
4
,
5
]
for
i
in
xrange
(
1
,
6
):
assert
list_index
.
index
(
i
)
==
i
-
1
assert
list_index
.
index
(
3
)
==
2
assert
[
1
,
'2'
].
index
(
'2'
)
==
1
# growing and shrinking a list:
# growing and shrinking a list:
l
=
[]
l
=
[]
for
i
in
xrange
(
100
):
for
i
in
xrange
(
100
):
...
...
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