Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
ac0134d4
Commit
ac0134d4
authored
Feb 10, 2014
by
Paul Sokolovsky
Browse files
Options
Downloads
Patches
Plain Diff
Factor out mp_seq_count_obj() and implement tuple.count().
parent
624eff6a
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
py/obj.h
+1
-0
1 addition, 0 deletions
py/obj.h
py/objlist.c
+1
-8
1 addition, 8 deletions
py/objlist.c
py/objtuple.c
+8
-0
8 additions, 0 deletions
py/objtuple.c
py/sequence.c
+12
-0
12 additions, 0 deletions
py/sequence.c
tests/basics/tuple_count.py
+5
-0
5 additions, 0 deletions
tests/basics/tuple_count.py
with
27 additions
and
8 deletions
py/obj.h
+
1
−
0
View file @
ac0134d4
...
...
@@ -403,3 +403,4 @@ bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_ui
bool
mp_seq_cmp_bytes
(
int
op
,
const
byte
*
data1
,
uint
len1
,
const
byte
*
data2
,
uint
len2
);
bool
mp_seq_cmp_objs
(
int
op
,
const
mp_obj_t
*
items1
,
uint
len1
,
const
mp_obj_t
*
items2
,
uint
len2
);
mp_obj_t
mp_seq_index_obj
(
const
mp_obj_t
*
items
,
uint
len
,
uint
n_args
,
const
mp_obj_t
*
args
);
mp_obj_t
mp_seq_count_obj
(
const
mp_obj_t
*
items
,
uint
len
,
mp_obj_t
value
);
This diff is collapsed.
Click to expand it.
py/objlist.c
+
1
−
8
View file @
ac0134d4
...
...
@@ -260,14 +260,7 @@ static mp_obj_t list_copy(mp_obj_t self_in) {
static
mp_obj_t
list_count
(
mp_obj_t
self_in
,
mp_obj_t
value
)
{
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
list_type
));
mp_obj_list_t
*
self
=
self_in
;
int
count
=
0
;
for
(
int
i
=
0
;
i
<
self
->
len
;
i
++
)
{
if
(
mp_obj_equal
(
self
->
items
[
i
],
value
))
{
count
++
;
}
}
return
mp_obj_new_int
(
count
);
return
mp_seq_count_obj
(
self
->
items
,
self
->
len
,
value
);
}
static
mp_obj_t
list_index
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
...
...
This diff is collapsed.
Click to expand it.
py/objtuple.c
+
8
−
0
View file @
ac0134d4
...
...
@@ -153,6 +153,13 @@ static mp_obj_t tuple_getiter(mp_obj_t o_in) {
return
mp_obj_new_tuple_iterator
(
o_in
,
0
);
}
static
mp_obj_t
tuple_count
(
mp_obj_t
self_in
,
mp_obj_t
value
)
{
assert
(
MP_OBJ_IS_TYPE
(
self_in
,
&
tuple_type
));
mp_obj_tuple_t
*
self
=
self_in
;
return
mp_seq_count_obj
(
self
->
items
,
self
->
len
,
value
);
}
static
MP_DEFINE_CONST_FUN_OBJ_2
(
tuple_count_obj
,
tuple_count
);
static
mp_obj_t
tuple_index
(
uint
n_args
,
const
mp_obj_t
*
args
)
{
assert
(
MP_OBJ_IS_TYPE
(
args
[
0
],
&
tuple_type
));
mp_obj_tuple_t
*
self
=
args
[
0
];
...
...
@@ -161,6 +168,7 @@ static mp_obj_t tuple_index(uint n_args, const mp_obj_t *args) {
static
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN
(
tuple_index_obj
,
2
,
4
,
tuple_index
);
static
const
mp_method_t
tuple_type_methods
[]
=
{
{
"count"
,
&
tuple_count_obj
},
{
"index"
,
&
tuple_index_obj
},
{
NULL
,
NULL
},
// end-of-list sentinel
};
...
...
This diff is collapsed.
Click to expand it.
py/sequence.c
+
12
−
0
View file @
ac0134d4
...
...
@@ -164,3 +164,15 @@ mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp
nlr_jump
(
mp_obj_new_exception_msg
(
MP_QSTR_ValueError
,
"object not in sequence"
));
}
mp_obj_t
mp_seq_count_obj
(
const
mp_obj_t
*
items
,
uint
len
,
mp_obj_t
value
)
{
uint
count
=
0
;
for
(
uint
i
=
0
;
i
<
len
;
i
++
)
{
if
(
mp_obj_equal
(
items
[
i
],
value
))
{
count
++
;
}
}
// Common sense says this cannot overflow small int
return
MP_OBJ_NEW_SMALL_INT
(
count
);
}
This diff is collapsed.
Click to expand it.
tests/basics/tuple_count.py
0 → 100644
+
5
−
0
View file @
ac0134d4
a
=
(
1
,
2
,
3
)
a
=
a
+
a
+
a
b
=
(
0
,
0
,
a
,
0
,
a
,
0
)
print
(
a
.
count
(
2
))
print
(
b
.
count
(
a
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment