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
4ff9b619
Commit
4ff9b619
authored
May 24, 2014
by
Krzysztof Klinikowski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement list.index()
parent
bed955e7
Changes
2
Hide 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 @
4ff9b619
...
...
@@ -366,6 +366,21 @@ Box* listCount(BoxedList* self, Box* elt) {
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
)
{
assert
(
self
->
cls
==
list_cls
);
...
...
@@ -473,6 +488,7 @@ void setupList() {
list_cls
->
giveAttr
(
"__new__"
,
new
BoxedFunction
(
new_
));
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
(
"reverse"
,
new
BoxedFunction
(
boxRTFunction
((
void
*
)
listReverse
,
NONE
,
1
,
false
)));
list_cls
->
freeze
();
...
...
test/tests/list.py
View file @
4ff9b619
...
...
@@ -49,6 +49,13 @@ for i in xrange(5):
l
.
reverse
()
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:
l
=
[]
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