objset.c 15.4 KB
Newer Older
xbe's avatar
xbe committed
1
#include <stdbool.h>
John R. Lenton's avatar
John R. Lenton committed
2
#include <string.h>
3
4
5
6
7
#include <assert.h>

#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
8
#include "qstr.h"
9
#include "obj.h"
10
#include "runtime.h"
11
#include "runtime0.h"
12
13
14
15
16
17

typedef struct _mp_obj_set_t {
    mp_obj_base_t base;
    mp_set_t set;
} mp_obj_set_t;

John R. Lenton's avatar
John R. Lenton committed
18
19
20
21
22
23
typedef struct _mp_obj_set_it_t {
    mp_obj_base_t base;
    mp_obj_set_t *set;
    machine_uint_t cur;
} mp_obj_set_it_t;

24
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
John R. Lenton's avatar
John R. Lenton committed
25

26
STATIC void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
27
    mp_obj_set_t *self = self_in;
28
29
30
31
    if (self->set.used == 0) {
        print(env, "set()");
        return;
    }
32
33
34
    bool first = true;
    print(env, "{");
    for (int i = 0; i < self->set.alloc; i++) {
35
        if (self->set.table[i] != MP_OBJ_NULL && self->set.table[i] != MP_OBJ_SENTINEL) {
36
37
38
39
            if (!first) {
                print(env, ", ");
            }
            first = false;
40
            mp_obj_print_helper(print, env, self->set.table[i], PRINT_REPR);
41
42
43
44
45
        }
    }
    print(env, "}");
}

46

47
STATIC mp_obj_t set_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
48
49
    // TODO check n_kw == 0

50
51
52
53
54
55
56
57
58
    switch (n_args) {
        case 0:
            // return a new, empty set
            return mp_obj_new_set(0, NULL);

        case 1:
        {
            // 1 argument, an iterable from which we make a new set
            mp_obj_t set = mp_obj_new_set(0, NULL);
Damien George's avatar
Damien George committed
59
            mp_obj_t iterable = mp_getiter(args[0]);
60
            mp_obj_t item;
Damien George's avatar
Damien George committed
61
            while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
62
63
64
65
66
67
                mp_obj_set_store(set, item);
            }
            return set;
        }

        default:
68
            nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "set takes at most 1 argument, %d given", n_args));
69
70
71
    }
}

72
const mp_obj_type_t mp_type_set_it = {
73
    { &mp_type_type },
74
    .name = MP_QSTR_iterator,
75
    .getiter = mp_identity,
John R. Lenton's avatar
John R. Lenton committed
76
77
78
    .iternext = set_it_iternext,
};

79
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
80
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set_it));
John R. Lenton's avatar
John R. Lenton committed
81
82
83
84
85
    mp_obj_set_it_t *self = self_in;
    machine_uint_t max = self->set->set.alloc;
    mp_obj_t *table = self->set->set.table;

    for (machine_uint_t i = self->cur; i < max; i++) {
86
        if (table[i] != MP_OBJ_NULL && table[i] != MP_OBJ_SENTINEL) {
John R. Lenton's avatar
John R. Lenton committed
87
88
89
90
91
            self->cur = i + 1;
            return table[i];
        }
    }

92
    return MP_OBJ_NULL;
John R. Lenton's avatar
John R. Lenton committed
93
94
}

95
STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
John R. Lenton's avatar
John R. Lenton committed
96
    mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
97
    o->base.type = &mp_type_set_it;
John R. Lenton's avatar
John R. Lenton committed
98
99
100
101
102
    o->set = (mp_obj_set_t *)set_in;
    o->cur = 0;
    return o;
}

John R. Lenton's avatar
John R. Lenton committed
103
104
105
106

/******************************************************************************/
/* set methods                                                                */

107
STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
108
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
109
    mp_obj_set_t *self = self_in;
John R. Lenton's avatar
John R. Lenton committed
110
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton's avatar
John R. Lenton committed
111
112
    return mp_const_none;
}
113
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton's avatar
John R. Lenton committed
114

115
STATIC mp_obj_t set_clear(mp_obj_t self_in) {
116
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
117
118
119
120
121
122
    mp_obj_set_t *self = self_in;

    mp_set_clear(&self->set);

    return mp_const_none;
}
123
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
John R. Lenton's avatar
John R. Lenton committed
124

125
STATIC mp_obj_t set_copy(mp_obj_t self_in) {
126
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
127
128
129
    mp_obj_set_t *self = self_in;

    mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
130
    other->base.type = &mp_type_set;
131
    mp_set_init(&other->set, self->set.alloc - 1);
John R. Lenton's avatar
John R. Lenton committed
132
133
134
135
136
    other->set.used = self->set.used;
    memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));

    return other;
}
137
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
John R. Lenton's avatar
John R. Lenton committed
138

139
STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
140
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
141
142
143
144
    mp_obj_set_t *self = self_in;
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
    return mp_const_none;
}
145
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton's avatar
John R. Lenton committed
146

147
STATIC mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
148
    assert(n_args > 0);
149
    assert(MP_OBJ_IS_TYPE(args[0], &mp_type_set));
150
151
152
153
154
155
156
157
158
159
160
161
162
    mp_obj_set_t *self;
    if (update) {
        self = args[0];
    } else {
        self = set_copy(args[0]);
    }


    for (int i = 1; i < n_args; i++) {
        mp_obj_t other = args[i];
        if (self == other) {
            set_clear(self);
        } else {
Damien George's avatar
Damien George committed
163
            mp_obj_t iter = mp_getiter(other);
164
            mp_obj_t next;
Damien George's avatar
Damien George committed
165
            while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
166
167
168
169
170
171
172
173
                set_discard(self, next);
            }
        }
    }

    return self;
}

174
STATIC mp_obj_t set_diff(uint n_args, const mp_obj_t *args) {
175
176
    return set_diff_int(n_args, args, false);
}
177
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
178

179
STATIC mp_obj_t set_diff_update(uint n_args, const mp_obj_t *args) {
180
181
182
    set_diff_int(n_args, args, true);
    return mp_const_none;
}
183
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
184

185
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
186
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
187
188
189
190
191
192
193
    if (self_in == other) {
        return update ? mp_const_none : set_copy(self_in);
    }

    mp_obj_set_t *self = self_in;
    mp_obj_set_t *out = mp_obj_new_set(0, NULL);

Damien George's avatar
Damien George committed
194
    mp_obj_t iter = mp_getiter(other);
195
    mp_obj_t next;
Damien George's avatar
Damien George committed
196
    while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
        if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
            set_add(out, next);
        }
    }

    if (update) {
        m_del(mp_obj_t, self->set.table, self->set.alloc);
        self->set.alloc = out->set.alloc;
        self->set.used = out->set.used;
        self->set.table = out->set.table;
    }

    return update ? mp_const_none : out;
}

212
STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
213
214
    return set_intersect_int(self_in, other, false);
}
215
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
216

217
STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
218
219
    return set_intersect_int(self_in, other, true);
}
220
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
221

222
STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
223
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
224
225
    mp_obj_set_t *self = self_in;

Damien George's avatar
Damien George committed
226
    mp_obj_t iter = mp_getiter(other);
John R. Lenton's avatar
John R. Lenton committed
227
    mp_obj_t next;
Damien George's avatar
Damien George committed
228
    while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton's avatar
John R. Lenton committed
229
230
231
232
233
234
        if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
            return mp_const_false;
        }
    }
    return mp_const_true;
}
235
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton's avatar
John R. Lenton committed
236

237
STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool proper) {
John R. Lenton's avatar
John R. Lenton committed
238
239
    mp_obj_set_t *self;
    bool cleanup_self = false;
240
    if (MP_OBJ_IS_TYPE(self_in, &mp_type_set)) {
John R. Lenton's avatar
John R. Lenton committed
241
242
        self = self_in;
    } else {
243
        self = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &self_in);
John R. Lenton's avatar
John R. Lenton committed
244
245
246
247
248
        cleanup_self = true;
    }

    mp_obj_set_t *other;
    bool cleanup_other = false;
249
    if (MP_OBJ_IS_TYPE(other_in, &mp_type_set)) {
John R. Lenton's avatar
John R. Lenton committed
250
251
        other = other_in;
    } else {
252
        other = set_make_new((mp_obj_t)&mp_type_set, 1, 0, &other_in);
John R. Lenton's avatar
John R. Lenton committed
253
254
        cleanup_other = true;
    }
John R. Lenton's avatar
John R. Lenton committed
255
256
257
258
259
260
    bool out = true;
    if (proper && self->set.used == other->set.used) {
        out = false;
    } else {
        mp_obj_t iter = set_getiter(self);
        mp_obj_t next;
261
        while ((next = set_it_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton's avatar
John R. Lenton committed
262
263
264
265
            if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
                out = false;
                break;
            }
John R. Lenton's avatar
John R. Lenton committed
266
267
268
269
270
271
272
273
        }
    }
    if (cleanup_self) {
        set_clear(self);
    }
    if (cleanup_other) {
        set_clear(other);
    }
John R. Lenton's avatar
John R. Lenton committed
274
275
    return MP_BOOL(out);
}
276
STATIC mp_obj_t set_issubset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lenton's avatar
John R. Lenton committed
277
    return set_issubset_internal(self_in, other_in, false);
John R. Lenton's avatar
John R. Lenton committed
278
}
279
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lenton's avatar
John R. Lenton committed
280

281
STATIC mp_obj_t set_issubset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lenton's avatar
John R. Lenton committed
282
283
284
    return set_issubset_internal(self_in, other_in, true);
}

285
STATIC mp_obj_t set_issuperset(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lenton's avatar
John R. Lenton committed
286
    return set_issubset_internal(other_in, self_in, false);
John R. Lenton's avatar
John R. Lenton committed
287
}
288
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lenton's avatar
John R. Lenton committed
289

290
STATIC mp_obj_t set_issuperset_proper(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lenton's avatar
John R. Lenton committed
291
292
293
    return set_issubset_internal(other_in, self_in, true);
}

294
STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
295
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
296
    mp_obj_set_t *self = self_in;
297
    if (!MP_OBJ_IS_TYPE(other_in, &mp_type_set)) {
John R. Lenton's avatar
John R. Lenton committed
298
299
300
301
302
303
304
305
306
        return mp_const_false;
    }
    mp_obj_set_t *other = other_in;
    if (self->set.used != other->set.used) {
        return mp_const_false;
    }
    return set_issubset(self_in, other_in);
}

307
STATIC mp_obj_t set_pop(mp_obj_t self_in) {
308
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
309
    mp_obj_set_t *self = self_in;
310
311
    mp_obj_t obj = mp_set_remove_first(&self->set);
    if (obj == MP_OBJ_NULL) {
312
        nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "pop from an empty set"));
John R. Lenton's avatar
John R. Lenton committed
313
314
315
    }
    return obj;
}
316
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lenton's avatar
John R. Lenton committed
317

318
STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
319
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
320
321
    mp_obj_set_t *self = self_in;
    if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
322
        nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lenton's avatar
John R. Lenton committed
323
324
325
    }
    return mp_const_none;
}
326
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
327

328
STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
329
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
330
    mp_obj_set_t *self = self_in;
Damien George's avatar
Damien George committed
331
    mp_obj_t iter = mp_getiter(other_in);
John R. Lenton's avatar
John R. Lenton committed
332
    mp_obj_t next;
Damien George's avatar
Damien George committed
333
    while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton's avatar
John R. Lenton committed
334
335
336
337
        mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
    }
    return mp_const_none;
}
338
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_update_obj, set_symmetric_difference_update);
John R. Lenton's avatar
John R. Lenton committed
339

340
STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
341
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
342
343
344
345
    self_in = set_copy(self_in);
    set_symmetric_difference_update(self_in, other_in);
    return self_in;
}
346
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton's avatar
John R. Lenton committed
347

348
STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien George's avatar
Damien George committed
349
    mp_obj_t iter = mp_getiter(other_in);
John R. Lenton's avatar
John R. Lenton committed
350
    mp_obj_t next;
Damien George's avatar
Damien George committed
351
    while ((next = mp_iternext(iter)) != MP_OBJ_NULL) {
John R. Lenton's avatar
John R. Lenton committed
352
353
354
355
        mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
    }
}

356
STATIC mp_obj_t set_update(uint n_args, const mp_obj_t *args) {
John R. Lenton's avatar
John R. Lenton committed
357
    assert(n_args > 0);
358
    assert(MP_OBJ_IS_TYPE(args[0], &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
359
360
361
362
363
364
365

    for (int i = 1; i < n_args; i++) {
        set_update_int(args[0], args[i]);
    }

    return mp_const_none;
}
366
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_update_obj, 1, set_update);
John R. Lenton's avatar
John R. Lenton committed
367

368
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
369
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
John R. Lenton's avatar
John R. Lenton committed
370
371
372
373
    mp_obj_set_t *self = set_copy(self_in);
    set_update_int(self, other_in);
    return self;
}
374
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton's avatar
John R. Lenton committed
375

376
377
378
379
380
381
382
383
STATIC mp_obj_t set_unary_op(int op, mp_obj_t self_in) {
    mp_obj_set_t *self = self_in;
    switch (op) {
        case MP_UNARY_OP_BOOL: return MP_BOOL(self->set.used != 0);
        case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT((machine_int_t)self->set.used);
        default: return MP_OBJ_NULL; // op not supported for None
    }
}
John R. Lenton's avatar
John R. Lenton committed
384

385
STATIC mp_obj_t set_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
John R. Lenton's avatar
John R. Lenton committed
386
387
    mp_obj_t args[] = {lhs, rhs};
    switch (op) {
Damien George's avatar
Damien George committed
388
    case MP_BINARY_OP_OR:
John R. Lenton's avatar
John R. Lenton committed
389
        return set_union(lhs, rhs);
Damien George's avatar
Damien George committed
390
    case MP_BINARY_OP_XOR:
John R. Lenton's avatar
John R. Lenton committed
391
        return set_symmetric_difference(lhs, rhs);
Damien George's avatar
Damien George committed
392
    case MP_BINARY_OP_AND:
John R. Lenton's avatar
John R. Lenton committed
393
        return set_intersect(lhs, rhs);
Damien George's avatar
Damien George committed
394
    case MP_BINARY_OP_SUBTRACT:
John R. Lenton's avatar
John R. Lenton committed
395
        return set_diff(2, args);
Damien George's avatar
Damien George committed
396
    case MP_BINARY_OP_INPLACE_OR:
John R. Lenton's avatar
John R. Lenton committed
397
        return set_union(lhs, rhs);
Damien George's avatar
Damien George committed
398
    case MP_BINARY_OP_INPLACE_XOR:
John R. Lenton's avatar
John R. Lenton committed
399
        return set_symmetric_difference(lhs, rhs);
Damien George's avatar
Damien George committed
400
    case MP_BINARY_OP_INPLACE_AND:
John R. Lenton's avatar
John R. Lenton committed
401
        return set_intersect(lhs, rhs);
Damien George's avatar
Damien George committed
402
    case MP_BINARY_OP_INPLACE_SUBTRACT:
John R. Lenton's avatar
John R. Lenton committed
403
        return set_diff(2, args);
Damien George's avatar
Damien George committed
404
    case MP_BINARY_OP_LESS:
John R. Lenton's avatar
John R. Lenton committed
405
        return set_issubset_proper(lhs, rhs);
Damien George's avatar
Damien George committed
406
    case MP_BINARY_OP_MORE:
John R. Lenton's avatar
John R. Lenton committed
407
        return set_issuperset_proper(lhs, rhs);
Damien George's avatar
Damien George committed
408
    case MP_BINARY_OP_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
409
        return set_equal(lhs, rhs);
Damien George's avatar
Damien George committed
410
    case MP_BINARY_OP_LESS_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
411
        return set_issubset(lhs, rhs);
Damien George's avatar
Damien George committed
412
    case MP_BINARY_OP_MORE_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
413
        return set_issuperset(lhs, rhs);
Damien George's avatar
Damien George committed
414
    case MP_BINARY_OP_NOT_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
415
        return MP_BOOL(set_equal(lhs, rhs) == mp_const_false);
Damien George's avatar
Damien George committed
416
    case MP_BINARY_OP_IN:
417
418
419
    {
        mp_obj_set_t *o = lhs;
        mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
420
        return MP_BOOL(elem != NULL);
421
    }
John R. Lenton's avatar
John R. Lenton committed
422
423
424
425
426
    default:
        // op not supported
        return NULL;
    }
}
John R. Lenton's avatar
John R. Lenton committed
427

John R. Lenton's avatar
John R. Lenton committed
428
429
430
431
/******************************************************************************/
/* set constructors & public C API                                            */


432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
STATIC const mp_map_elem_t set_locals_dict_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR_add), (mp_obj_t)&set_add_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_clear), (mp_obj_t)&set_clear_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_copy), (mp_obj_t)&set_copy_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_discard), (mp_obj_t)&set_discard_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_difference), (mp_obj_t)&set_diff_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_difference_update), (mp_obj_t)&set_diff_update_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_intersection), (mp_obj_t)&set_intersect_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_intersection_update), (mp_obj_t)&set_intersect_update_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_isdisjoint), (mp_obj_t)&set_isdisjoint_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_issubset), (mp_obj_t)&set_issubset_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_issuperset), (mp_obj_t)&set_issuperset_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_pop), (mp_obj_t)&set_pop_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&set_remove_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference), (mp_obj_t)&set_symmetric_difference_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_symmetric_difference_update), (mp_obj_t)&set_symmetric_difference_update_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_union), (mp_obj_t)&set_union_obj },
    { MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&set_update_obj },
John R. Lenton's avatar
John R. Lenton committed
450
451
};

452
453
STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);

454
const mp_obj_type_t mp_type_set = {
455
    { &mp_type_type },
456
    .name = MP_QSTR_set,
457
458
    .print = set_print,
    .make_new = set_make_new,
459
    .unary_op = set_unary_op,
John R. Lenton's avatar
John R. Lenton committed
460
    .binary_op = set_binary_op,
John R. Lenton's avatar
John R. Lenton committed
461
    .getiter = set_getiter,
462
    .locals_dict = (mp_obj_t)&set_locals_dict,
463
464
465
466
};

mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
    mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
467
    o->base.type = &mp_type_set;
468
469
    mp_set_init(&o->set, n_args);
    for (int i = 0; i < n_args; i++) {
John R. Lenton's avatar
John R. Lenton committed
470
        mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
471
472
473
474
475
    }
    return o;
}

void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
476
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
477
    mp_obj_set_t *self = self_in;
John R. Lenton's avatar
John R. Lenton committed
478
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
479
}