Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objset.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013-2017 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <stdbool.h>
28 #include <string.h>
29 #include <assert.h>
30 
31 #include "py/runtime.h"
32 #include "py/builtin.h"
33 
34 #if MICROPY_PY_BUILTINS_SET
35 
36 typedef struct _mp_obj_set_t {
37  mp_obj_base_t base;
38  mp_set_t set;
39 } mp_obj_set_t;
40 
41 typedef struct _mp_obj_set_it_t {
42  mp_obj_base_t base;
43  mp_fun_1_t iternext;
44  mp_obj_set_t *set;
45  size_t cur;
46 } mp_obj_set_it_t;
47 
48 STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
49 
50 STATIC bool is_set_or_frozenset(mp_obj_t o) {
51  return MP_OBJ_IS_TYPE(o, &mp_type_set)
52 #if MICROPY_PY_BUILTINS_FROZENSET
54 #endif
55  ;
56 }
57 
58 // This macro is shorthand for mp_check_self to verify the argument is a set.
59 #define check_set(o) mp_check_self(MP_OBJ_IS_TYPE(o, &mp_type_set))
60 
61 // This macro is shorthand for mp_check_self to verify the argument is a
62 // set or frozenset for methods that operate on both of these types.
63 #define check_set_or_frozenset(o) mp_check_self(is_set_or_frozenset(o))
64 
65 STATIC void set_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
66  (void)kind;
67  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
68  #if MICROPY_PY_BUILTINS_FROZENSET
69  bool is_frozen = MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset);
70  #endif
71  if (self->set.used == 0) {
72  #if MICROPY_PY_BUILTINS_FROZENSET
73  if (is_frozen) {
74  mp_print_str(print, "frozen");
75  }
76  #endif
77  mp_print_str(print, "set()");
78  return;
79  }
80  bool first = true;
81  #if MICROPY_PY_BUILTINS_FROZENSET
82  if (is_frozen) {
83  mp_print_str(print, "frozenset(");
84  }
85  #endif
86  mp_print_str(print, "{");
87  for (size_t i = 0; i < self->set.alloc; i++) {
88  if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
89  if (!first) {
90  mp_print_str(print, ", ");
91  }
92  first = false;
93  mp_obj_print_helper(print, self->set.table[i], PRINT_REPR);
94  }
95  }
96  mp_print_str(print, "}");
97  #if MICROPY_PY_BUILTINS_FROZENSET
98  if (is_frozen) {
99  mp_print_str(print, ")");
100  }
101  #endif
102 }
103 
104 STATIC mp_obj_t set_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
105  mp_arg_check_num(n_args, n_kw, 0, 1, false);
106 
107  switch (n_args) {
108  case 0: {
109  // create a new, empty set
110  mp_obj_set_t *set = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
111  // set actual set/frozenset type
112  set->base.type = type;
113  return MP_OBJ_FROM_PTR(set);
114  }
115 
116  case 1:
117  default: { // can only be 0 or 1 arg
118  // 1 argument, an iterable from which we make a new set
119  mp_obj_t set = mp_obj_new_set(0, NULL);
120  mp_obj_t iterable = mp_getiter(args[0], NULL);
121  mp_obj_t item;
122  while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
123  mp_obj_set_store(set, item);
124  }
125  // Set actual set/frozenset type
126  ((mp_obj_set_t*)MP_OBJ_TO_PTR(set))->base.type = type;
127  return set;
128  }
129  }
130 }
131 
132 STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
133  mp_obj_set_it_t *self = MP_OBJ_TO_PTR(self_in);
134  size_t max = self->set->set.alloc;
135  mp_set_t *set = &self->set->set;
136 
137  for (size_t i = self->cur; i < max; i++) {
138  if (MP_SET_SLOT_IS_FILLED(set, i)) {
139  self->cur = i + 1;
140  return set->table[i];
141  }
142  }
143 
144  return MP_OBJ_STOP_ITERATION;
145 }
146 
147 STATIC mp_obj_t set_getiter(mp_obj_t set_in, mp_obj_iter_buf_t *iter_buf) {
148  assert(sizeof(mp_obj_set_it_t) <= sizeof(mp_obj_iter_buf_t));
149  mp_obj_set_it_t *o = (mp_obj_set_it_t*)iter_buf;
150  o->base.type = &mp_type_polymorph_iter;
151  o->iternext = set_it_iternext;
152  o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
153  o->cur = 0;
154  return MP_OBJ_FROM_PTR(o);
155 }
156 
157 
158 /******************************************************************************/
159 /* set methods */
160 
161 STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
162  check_set(self_in);
163  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
165  return mp_const_none;
166 }
167 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
168 
169 STATIC mp_obj_t set_clear(mp_obj_t self_in) {
170  check_set(self_in);
171  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
172 
173  mp_set_clear(&self->set);
174 
175  return mp_const_none;
176 }
177 STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
178 
179 STATIC mp_obj_t set_copy(mp_obj_t self_in) {
180  check_set_or_frozenset(self_in);
181  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
182  mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
183  other->base.type = self->base.type;
184  mp_set_init(&other->set, self->set.alloc);
185  other->set.used = self->set.used;
186  memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
187  return MP_OBJ_FROM_PTR(other);
188 }
189 STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
190 
191 STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
192  check_set(self_in);
193  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
194  mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
195  return mp_const_none;
196 }
197 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
198 
199 STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) {
200  mp_obj_t self;
201  if (update) {
202  check_set(args[0]);
203  self = args[0];
204  } else {
205  self = set_copy(args[0]);
206  }
207 
208  for (size_t i = 1; i < n_args; i++) {
209  mp_obj_t other = args[i];
210  if (self == other) {
211  set_clear(self);
212  } else {
213  mp_set_t *self_set = &((mp_obj_set_t*)MP_OBJ_TO_PTR(self))->set;
214  mp_obj_t iter = mp_getiter(other, NULL);
215  mp_obj_t next;
216  while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
218  }
219  }
220  }
221 
222  return self;
223 }
224 
225 STATIC mp_obj_t set_diff(size_t n_args, const mp_obj_t *args) {
226  return set_diff_int(n_args, args, false);
227 }
228 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
229 
230 STATIC mp_obj_t set_diff_update(size_t n_args, const mp_obj_t *args) {
231  set_diff_int(n_args, args, true);
232  return mp_const_none;
233 }
234 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
235 
236 STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
237  if (update) {
238  check_set(self_in);
239  } else {
240  check_set_or_frozenset(self_in);
241  }
242 
243  if (self_in == other) {
244  return update ? mp_const_none : set_copy(self_in);
245  }
246 
247  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
248  mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
249 
250  mp_obj_t iter = mp_getiter(other, NULL);
251  mp_obj_t next;
252  while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
253  if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
254  set_add(MP_OBJ_FROM_PTR(out), next);
255  }
256  }
257 
258  if (update) {
259  m_del(mp_obj_t, self->set.table, self->set.alloc);
260  self->set.alloc = out->set.alloc;
261  self->set.used = out->set.used;
262  self->set.table = out->set.table;
263  }
264 
265  return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
266 }
267 
268 STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
269  return set_intersect_int(self_in, other, false);
270 }
271 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
272 
273 STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
274  return set_intersect_int(self_in, other, true);
275 }
276 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
277 
278 STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
279  check_set_or_frozenset(self_in);
280  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
281 
282  mp_obj_iter_buf_t iter_buf;
283  mp_obj_t iter = mp_getiter(other, &iter_buf);
284  mp_obj_t next;
285  while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
286  if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
287  return mp_const_false;
288  }
289  }
290  return mp_const_true;
291 }
292 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
293 
294 STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) {
295  mp_obj_set_t *self;
296  bool cleanup_self = false;
297  if (is_set_or_frozenset(self_in)) {
298  self = MP_OBJ_TO_PTR(self_in);
299  } else {
300  self = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &self_in));
301  cleanup_self = true;
302  }
303 
304  mp_obj_set_t *other;
305  bool cleanup_other = false;
306  if (is_set_or_frozenset(other_in)) {
307  other = MP_OBJ_TO_PTR(other_in);
308  } else {
309  other = MP_OBJ_TO_PTR(set_make_new(&mp_type_set, 1, 0, &other_in));
310  cleanup_other = true;
311  }
312  mp_obj_t out = mp_const_true;
313  if (proper && self->set.used == other->set.used) {
314  out = mp_const_false;
315  } else {
316  mp_obj_iter_buf_t iter_buf;
317  mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self), &iter_buf);
318  mp_obj_t next;
319  while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
320  if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
321  out = mp_const_false;
322  break;
323  }
324  }
325  }
326  // TODO: Should free objects altogether
327  if (cleanup_self) {
328  set_clear(MP_OBJ_FROM_PTR(self));
329  }
330  if (cleanup_other) {
331  set_clear(MP_OBJ_FROM_PTR(other));
332  }
333  return out;
334 }
335 STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
336  return set_issubset_internal(self_in, other_in, false);
337 }
338 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
339 
340 STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
341  return set_issubset_internal(self_in, other_in, true);
342 }
343 
344 STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
345  return set_issubset_internal(other_in, self_in, false);
346 }
347 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
348 
349 STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
350  return set_issubset_internal(other_in, self_in, true);
351 }
352 
353 STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
354  check_set_or_frozenset(self_in);
355  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
356  if (!is_set_or_frozenset(other_in)) {
357  return mp_const_false;
358  }
359  mp_obj_set_t *other = MP_OBJ_TO_PTR(other_in);
360  if (self->set.used != other->set.used) {
361  return mp_const_false;
362  }
363  return set_issubset(self_in, other_in);
364 }
365 
366 STATIC mp_obj_t set_pop(mp_obj_t self_in) {
367  check_set(self_in);
368  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
369  mp_obj_t obj = mp_set_remove_first(&self->set);
370  if (obj == MP_OBJ_NULL) {
371  mp_raise_msg(&mp_type_KeyError, "pop from an empty set");
372  }
373  return obj;
374 }
375 STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
376 
377 STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
378  check_set(self_in);
379  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
380  if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
382  }
383  return mp_const_none;
384 }
385 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
386 
387 STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
388  check_set_or_frozenset(self_in); // can be frozenset due to call from set_symmetric_difference
389  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
390  mp_obj_t iter = mp_getiter(other_in, NULL);
391  mp_obj_t next;
392  while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
394  }
395  return mp_const_none;
396 }
397 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
398 
399 STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
400  mp_obj_t self_out = set_copy(self_in);
401  set_symmetric_difference_update(self_out, other_in);
402  return self_out;
403 }
404 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
405 
406 STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
407  mp_obj_t iter = mp_getiter(other_in, NULL);
408  mp_obj_t next;
409  while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
411  }
412 }
413 
414 STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
415  check_set(args[0]);
416  for (size_t i = 1; i < n_args; i++) {
417  set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
418  }
419 
420  return mp_const_none;
421 }
422 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
423 
424 STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
425  check_set_or_frozenset(self_in);
426  mp_obj_t self = set_copy(self_in);
427  set_update_int(MP_OBJ_TO_PTR(self), other_in);
428  return self;
429 }
430 STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
431 
432 STATIC mp_obj_t set_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
433  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
434  switch (op) {
435  case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->set.used != 0);
436  case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->set.used);
437 #if MICROPY_PY_BUILTINS_FROZENSET
438  case MP_UNARY_OP_HASH:
439  if (MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset)) {
440  // start hash with unique value
442  size_t max = self->set.alloc;
443  mp_set_t *set = &self->set;
444 
445  for (size_t i = 0; i < max; i++) {
446  if (MP_SET_SLOT_IS_FILLED(set, i)) {
447  hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
448  }
449  }
450  return MP_OBJ_NEW_SMALL_INT(hash);
451  }
452 #endif
453  default: return MP_OBJ_NULL; // op not supported
454  }
455 }
456 
457 STATIC mp_obj_t set_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {
458  mp_obj_t args[] = {lhs, rhs};
459  #if MICROPY_PY_BUILTINS_FROZENSET
460  bool update = MP_OBJ_IS_TYPE(lhs, &mp_type_set);
461  #else
462  bool update = true;
463  #endif
464  if (op != MP_BINARY_OP_IN && !is_set_or_frozenset(rhs)) {
465  // For all ops except containment the RHS must be a set/frozenset
466  return MP_OBJ_NULL;
467  }
468  switch (op) {
469  case MP_BINARY_OP_OR:
470  return set_union(lhs, rhs);
471  case MP_BINARY_OP_XOR:
472  return set_symmetric_difference(lhs, rhs);
473  case MP_BINARY_OP_AND:
474  return set_intersect(lhs, rhs);
476  return set_diff(2, args);
478  if (update) {
479  set_update(2, args);
480  return lhs;
481  } else {
482  return set_union(lhs, rhs);
483  }
485  if (update) {
486  set_symmetric_difference_update(lhs, rhs);
487  return lhs;
488  } else {
489  return set_symmetric_difference(lhs, rhs);
490  }
492  rhs = set_intersect_int(lhs, rhs, update);
493  if (update) {
494  return lhs;
495  } else {
496  return rhs;
497  }
499  return set_diff_int(2, args, update);
500  case MP_BINARY_OP_LESS:
501  return set_issubset_proper(lhs, rhs);
502  case MP_BINARY_OP_MORE:
503  return set_issuperset_proper(lhs, rhs);
504  case MP_BINARY_OP_EQUAL:
505  return set_equal(lhs, rhs);
507  return set_issubset(lhs, rhs);
509  return set_issuperset(lhs, rhs);
510  case MP_BINARY_OP_IN: {
511  mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
512  mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
513  return mp_obj_new_bool(elem != MP_OBJ_NULL);
514  }
515  default:
516  return MP_OBJ_NULL; // op not supported
517  }
518 }
519 
520 /******************************************************************************/
521 /* set constructors & public C API */
522 
523 
524 STATIC const mp_rom_map_elem_t set_locals_dict_table[] = {
525  { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&set_add_obj) },
526  { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&set_clear_obj) },
527  { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
528  { MP_ROM_QSTR(MP_QSTR_discard), MP_ROM_PTR(&set_discard_obj) },
529  { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
530  { MP_ROM_QSTR(MP_QSTR_difference_update), MP_ROM_PTR(&set_diff_update_obj) },
531  { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
532  { MP_ROM_QSTR(MP_QSTR_intersection_update), MP_ROM_PTR(&set_intersect_update_obj) },
533  { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
534  { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
535  { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
536  { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&set_pop_obj) },
537  { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&set_remove_obj) },
538  { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
539  { MP_ROM_QSTR(MP_QSTR_symmetric_difference_update), MP_ROM_PTR(&set_symmetric_difference_update_obj) },
540  { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
541  { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&set_update_obj) },
542  { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
543 };
544 
545 STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);
546 
547 const mp_obj_type_t mp_type_set = {
548  { &mp_type_type },
549  .name = MP_QSTR_set,
550  .print = set_print,
551  .make_new = set_make_new,
552  .unary_op = set_unary_op,
553  .binary_op = set_binary_op,
554  .getiter = set_getiter,
555  .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
556 };
557 
558 #if MICROPY_PY_BUILTINS_FROZENSET
559 STATIC const mp_rom_map_elem_t frozenset_locals_dict_table[] = {
560  { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
561  { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
562  { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
563  { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
564  { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
565  { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
566  { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
567  { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
568  { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
569 };
570 STATIC MP_DEFINE_CONST_DICT(frozenset_locals_dict, frozenset_locals_dict_table);
571 
573  { &mp_type_type },
574  .name = MP_QSTR_frozenset,
575  .print = set_print,
576  .make_new = set_make_new,
577  .unary_op = set_unary_op,
578  .binary_op = set_binary_op,
579  .getiter = set_getiter,
580  .locals_dict = (mp_obj_dict_t*)&frozenset_locals_dict,
581 };
582 #endif
583 
584 mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items) {
585  mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
586  o->base.type = &mp_type_set;
587  mp_set_init(&o->set, n_args);
588  for (size_t i = 0; i < n_args; i++) {
589  mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
590  }
591  return MP_OBJ_FROM_PTR(o);
592 }
593 
594 void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
596  mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
598 }
599 
600 #endif // MICROPY_PY_BUILTINS_SET
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg)
Definition: runtime.c:216
intptr_t mp_int_t
Definition: mpconfigport.h:73
const mp_obj_type_t mp_type_set
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
Definition: runtime.c:1448
mp_fun_1_t iternext
Definition: obj.h:521
#define assert(e)
Definition: assert.h:9
#define mp_const_none
Definition: obj.h:614
#define MP_DEFINE_CONST_DICT(dict_name, table_name)
Definition: obj.h:317
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
#define m_del(type, ptr, num)
Definition: misc.h:77
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
#define MP_ROM_QSTR(q)
Definition: obj.h:241
unsigned int uintptr_t
Definition: stdint.h:14
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw)
Definition: argcheck.c:32
#define MP_ROM_PTR(p)
Definition: obj.h:242
#define mp_const_true
Definition: obj.h:616
mp_unary_op_t
Definition: runtime0.h:45
#define STATIC
Definition: mpconfig.h:1178
#define MP_OBJ_SMALL_INT_VALUE(o)
Definition: obj.h:86
mp_obj_t(* mp_fun_1_t)(mp_obj_t)
Definition: obj.h:404
void mp_set_init(mp_set_t *set, size_t n)
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name)
Definition: obj.h:285
mp_print_kind_t
Definition: obj.h:412
#define MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item)
mp_obj_t mp_set_remove_first(mp_set_t *set)
const mp_obj_type_t mp_type_polymorph_iter
Definition: objpolyiter.c:48
mp_obj_t * table
Definition: obj.h:391
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg)
Definition: objexcept.c:334
#define mp_check_self(pred)
Definition: runtime.h:161
const mp_obj_type_t mp_type_frozenset
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind)
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind)
Definition: obj.c:59
mp_binary_op_t
Definition: runtime0.h:67
mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items)
args
Definition: i18n.py:175
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name)
Definition: obj.h:288
void mp_set_clear(mp_set_t *set)
MP_DEFINE_CONST_FUN_OBJ_VAR(mp_builtin___build_class___obj, 2, mp_builtin___build_class__)
Definition: obj.h:388
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf)
Definition: runtime.c:1120
#define nlr_raise(val)
Definition: nlr.h:89
qstr name
Definition: obj.h:478
#define MP_OBJ_STOP_ITERATION
Definition: obj.h:74
uint64_t mp_obj_t
Definition: obj.h:39
mp_obj_t mp_iternext(mp_obj_t o_in)
Definition: runtime.c:1186
#define m_new_obj(type)
Definition: misc.h:60
const mp_obj_type_t mp_type_KeyError
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3
#define mp_const_false
Definition: obj.h:615