Skip to content

Commit 19b14d3

Browse files
committed
Implemented set.add
1 parent 0ce03b4 commit 19b14d3

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

py/objset.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,35 @@ static mp_obj_t set_getiter(mp_obj_t set_in) {
9292
return o;
9393
}
9494

95+
96+
/******************************************************************************/
97+
/* set methods */
98+
99+
static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
100+
assert(MP_OBJ_IS_TYPE(self_in, &set_type));
101+
mp_obj_set_t *self = self_in;
102+
mp_set_lookup(&self->set, item, true);
103+
return mp_const_none;
104+
}
105+
static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
106+
107+
108+
/******************************************************************************/
109+
/* set constructors & public C API */
110+
111+
112+
static const mp_method_t set_type_methods[] = {
113+
{ "add", &set_add_obj },
114+
{ NULL, NULL }, // end-of-list sentinel
115+
};
116+
95117
const mp_obj_type_t set_type = {
96118
{ &mp_const_type },
97119
"set",
98120
.print = set_print,
99121
.make_new = set_make_new,
100122
.getiter = set_getiter,
123+
.methods = set_type_methods,
101124
};
102125

103126
mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {

tests/basics/tests/set_add.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
s = {1, 2, 3, 4}
2+
print(s.add(5))
3+
l = list(s)
4+
l.sort()
5+
print(l)

0 commit comments

Comments
 (0)