@@ -227,6 +227,70 @@ static mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
227227}
228228static MP_DEFINE_CONST_FUN_OBJ_2 (set_isdisjoint_obj , set_isdisjoint ) ;
229229
230+ static mp_obj_t set_issubset (mp_obj_t self_in , mp_obj_t other_in ) {
231+ mp_obj_set_t * self ;
232+ bool cleanup_self = false;
233+ if (MP_OBJ_IS_TYPE (self_in , & set_type )) {
234+ self = self_in ;
235+ } else {
236+ self = set_make_new (NULL , 1 , & self_in );
237+ cleanup_self = true;
238+ }
239+
240+ mp_obj_set_t * other ;
241+ bool cleanup_other = false;
242+ if (MP_OBJ_IS_TYPE (other_in , & set_type )) {
243+ other = other_in ;
244+ } else {
245+ other = set_make_new (NULL , 1 , & other_in );
246+ cleanup_other = true;
247+ }
248+ mp_obj_t iter = set_getiter (self );
249+ mp_obj_t next ;
250+ mp_obj_t out = mp_const_true ;
251+ while ((next = set_it_iternext (iter )) != mp_const_stop_iteration ) {
252+ if (!mp_set_lookup (& other -> set , next , MP_MAP_LOOKUP )) {
253+ out = mp_const_false ;
254+ break ;
255+ }
256+ }
257+ if (cleanup_self ) {
258+ set_clear (self );
259+ }
260+ if (cleanup_other ) {
261+ set_clear (other );
262+ }
263+ return out ;
264+ }
265+ static MP_DEFINE_CONST_FUN_OBJ_2 (set_issubset_obj , set_issubset ) ;
266+
267+ static mp_obj_t set_issuperset (mp_obj_t self_in , mp_obj_t other_in ) {
268+ return set_issubset (other_in , self_in );
269+ }
270+ static MP_DEFINE_CONST_FUN_OBJ_2 (set_issuperset_obj , set_issuperset ) ;
271+
272+ static mp_obj_t set_pop (mp_obj_t self_in ) {
273+ assert (MP_OBJ_IS_TYPE (self_in , & set_type ));
274+ mp_obj_set_t * self = self_in ;
275+
276+ if (self -> set .used == 0 ) {
277+ nlr_jump (mp_obj_new_exception_msg (MP_QSTR_KeyError , "pop from an empty set" ));
278+ }
279+ mp_obj_t obj = mp_set_lookup (& self -> set , NULL ,
280+ MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST );
281+ return obj ;
282+ }
283+ static MP_DEFINE_CONST_FUN_OBJ_1 (set_pop_obj , set_pop ) ;
284+
285+ static mp_obj_t set_remove (mp_obj_t self_in , mp_obj_t item ) {
286+ assert (MP_OBJ_IS_TYPE (self_in , & set_type ));
287+ mp_obj_set_t * self = self_in ;
288+ if (mp_set_lookup (& self -> set , item , MP_MAP_LOOKUP_REMOVE_IF_FOUND ) == MP_OBJ_NULL ) {
289+ nlr_jump (mp_obj_new_exception (MP_QSTR_KeyError ));
290+ }
291+ return mp_const_none ;
292+ }
293+ static MP_DEFINE_CONST_FUN_OBJ_2 (set_remove_obj , set_remove ) ;
230294
231295/******************************************************************************/
232296/* set constructors & public C API */
@@ -242,6 +306,10 @@ static const mp_method_t set_type_methods[] = {
242306 { "intersection" , & set_intersect_obj },
243307 { "intersection_update" , & set_intersect_update_obj },
244308 { "isdisjoint" , & set_isdisjoint_obj },
309+ { "issubset" , & set_issubset_obj },
310+ { "issuperset" , & set_issuperset_obj },
311+ { "pop" , & set_pop_obj },
312+ { "remove" , & set_remove_obj },
245313 { NULL , NULL }, // end-of-list sentinel
246314};
247315
0 commit comments