objset.c 20 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

xbe's avatar
xbe committed
27
#include <stdbool.h>
John R. Lenton's avatar
John R. Lenton committed
28
#include <string.h>
29
30
#include <assert.h>

31
32
33
34
#include "py/nlr.h"
#include "py/runtime.h"
#include "py/runtime0.h"
#include "py/builtin.h"
35

36
37
#if MICROPY_PY_BUILTINS_SET

38
39
40
41
42
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
43
44
typedef struct _mp_obj_set_it_t {
    mp_obj_base_t base;
45
    mp_fun_1_t iternext;
John R. Lenton's avatar
John R. Lenton committed
46
    mp_obj_set_t *set;
47
    mp_uint_t cur;
John R. Lenton's avatar
John R. Lenton committed
48
49
} mp_obj_set_it_t;

50
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
John R. Lenton's avatar
John R. Lenton committed
51

52
STATIC bool is_set_or_frozenset(mp_obj_t o) {
53
    return MP_OBJ_IS_TYPE(o, &mp_type_set)
54
#if MICROPY_PY_BUILTINS_FROZENSET
55
56
57
        || MP_OBJ_IS_TYPE(o, &mp_type_frozenset)
#endif
    ;
58
59
}

60
#if MICROPY_PY_BUILTINS_FROZENSET
61
62
63
64
65
STATIC void check_set_or_frozenset(mp_obj_t o) {
    if (!is_set_or_frozenset(o)) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
    }
}
66
67
68
#else
#define check_set_or_frozenset(o) check_set(o)
#endif
69
70
71
72
73

STATIC void check_set(mp_obj_t o) {
    if (!MP_OBJ_IS_TYPE(o, &mp_type_set)) {
        // Emulate CPython behavior
        // AttributeError: 'frozenset' object has no attribute 'add'
74
        #if MICROPY_PY_BUILTINS_FROZENSET
75
76
77
        if (MP_OBJ_IS_TYPE(o, &mp_type_frozenset)) {
            nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError, "'frozenset' has no such attribute"));
        }
78
        #endif
79
80
81
82
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'set' object required"));
    }
}

83
STATIC void set_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
84
    (void)kind;
85
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
86
    #if MICROPY_PY_BUILTINS_FROZENSET
87
    bool is_frozen = MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset);
88
    #endif
89
    if (self->set.used == 0) {
90
        #if MICROPY_PY_BUILTINS_FROZENSET
91
        if (is_frozen) {
92
            mp_print_str(print, "frozen");
93
        }
94
        #endif
95
        mp_print_str(print, "set()");
96
97
        return;
    }
98
    bool first = true;
99
    #if MICROPY_PY_BUILTINS_FROZENSET
100
    if (is_frozen) {
101
        mp_print_str(print, "frozenset(");
102
    }
103
    #endif
104
    mp_print_str(print, "{");
105
    for (mp_uint_t i = 0; i < self->set.alloc; i++) {
106
        if (MP_SET_SLOT_IS_FILLED(&self->set, i)) {
107
            if (!first) {
108
                mp_print_str(print, ", ");
109
110
            }
            first = false;
111
            mp_obj_print_helper(print, self->set.table[i], PRINT_REPR);
112
113
        }
    }
114
    mp_print_str(print, "}");
115
    #if MICROPY_PY_BUILTINS_FROZENSET
116
    if (is_frozen) {
117
        mp_print_str(print, ")");
118
    }
119
    #endif
120
121
}

122
STATIC mp_obj_t set_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
123
    mp_arg_check_num(n_args, n_kw, 0, 1, false);
124

125
    switch (n_args) {
126
127
        case 0: {
            // create a new, empty set
128
            mp_obj_set_t *set = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
129
            // set actual set/frozenset type
130
131
            set->base.type = MP_OBJ_TO_PTR(type_in);
            return MP_OBJ_FROM_PTR(set);
132
        }
133
134

        case 1:
135
        default: { // can only be 0 or 1 arg
136
            // 1 argument, an iterable from which we make a new set
137
            mp_obj_t set = mp_obj_new_set(0, NULL);
Damien George's avatar
Damien George committed
138
            mp_obj_t iterable = mp_getiter(args[0]);
139
            mp_obj_t item;
140
            while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
141
142
                mp_obj_set_store(set, item);
            }
143
            // Set actual set/frozenset type
144
            ((mp_obj_set_t*)MP_OBJ_TO_PTR(set))->base.type = MP_OBJ_TO_PTR(type_in);
145
146
147
148
149
            return set;
        }
    }
}

150
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
151
    mp_obj_set_it_t *self = MP_OBJ_TO_PTR(self_in);
152
    mp_uint_t max = self->set->set.alloc;
153
    mp_set_t *set = &self->set->set;
John R. Lenton's avatar
John R. Lenton committed
154

155
    for (mp_uint_t i = self->cur; i < max; i++) {
156
        if (MP_SET_SLOT_IS_FILLED(set, i)) {
John R. Lenton's avatar
John R. Lenton committed
157
            self->cur = i + 1;
158
            return set->table[i];
John R. Lenton's avatar
John R. Lenton committed
159
160
161
        }
    }

162
    return MP_OBJ_STOP_ITERATION;
John R. Lenton's avatar
John R. Lenton committed
163
164
}

165
STATIC mp_obj_t set_getiter(mp_obj_t set_in) {
John R. Lenton's avatar
John R. Lenton committed
166
    mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
167
168
    o->base.type = &mp_type_polymorph_iter;
    o->iternext = set_it_iternext;
169
    o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in);
John R. Lenton's avatar
John R. Lenton committed
170
    o->cur = 0;
171
    return MP_OBJ_FROM_PTR(o);
John R. Lenton's avatar
John R. Lenton committed
172
173
}

John R. Lenton's avatar
John R. Lenton committed
174
175
176
177

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

178
STATIC mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
179
    check_set(self_in);
180
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
181
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton's avatar
John R. Lenton committed
182
183
    return mp_const_none;
}
184
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
John R. Lenton's avatar
John R. Lenton committed
185

186
STATIC mp_obj_t set_clear(mp_obj_t self_in) {
187
    check_set(self_in);
188
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
189
190
191
192
193

    mp_set_clear(&self->set);

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

196
STATIC mp_obj_t set_copy_as_mutable(mp_obj_t self_in) {
197
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
198
199

    mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
200
    other->base.type = &mp_type_set;
201
    mp_set_init(&other->set, self->set.alloc);
John R. Lenton's avatar
John R. Lenton committed
202
203
204
    other->set.used = self->set.used;
    memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));

205
    return MP_OBJ_FROM_PTR(other);
John R. Lenton's avatar
John R. Lenton committed
206
}
207
208
209
210

STATIC mp_obj_t set_copy(mp_obj_t self_in) {
    check_set_or_frozenset(self_in);

211
212
    mp_obj_t other = set_copy_as_mutable(self_in);
    ((mp_obj_base_t*)MP_OBJ_TO_PTR(other))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_in))->type;
213
214
215

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

218
STATIC mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
219
    check_set(self_in);
220
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
221
222
223
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
    return mp_const_none;
}
224
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton's avatar
John R. Lenton committed
225

226
STATIC mp_obj_t set_diff_int(mp_uint_t n_args, const mp_obj_t *args, bool update) {
227
    assert(n_args > 0);
228

229
    mp_obj_t self;
230
    if (update) {
231
        check_set(args[0]);
232
233
        self = args[0];
    } else {
234
235
        check_set_or_frozenset(args[0]);
        self = set_copy_as_mutable(args[0]);
236
237
238
    }


239
    for (mp_uint_t i = 1; i < n_args; i++) {
240
241
242
243
        mp_obj_t other = args[i];
        if (self == other) {
            set_clear(self);
        } else {
Damien George's avatar
Damien George committed
244
            mp_obj_t iter = mp_getiter(other);
245
            mp_obj_t next;
246
            while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
247
248
249
250
251
                set_discard(self, next);
            }
        }
    }

252
    ((mp_obj_base_t*)MP_OBJ_TO_PTR(self))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]))->type;
253
254
255
    return self;
}

256
STATIC mp_obj_t set_diff(mp_uint_t n_args, const mp_obj_t *args) {
257
258
    return set_diff_int(n_args, args, false);
}
259
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
260

261
STATIC mp_obj_t set_diff_update(mp_uint_t n_args, const mp_obj_t *args) {
262
263
264
    set_diff_int(n_args, args, true);
    return mp_const_none;
}
265
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
266

267
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
268
269
270
271
272
273
    if (update) {
        check_set(self_in);
    } else {
        check_set_or_frozenset(self_in);
    }

274
275
276
277
    if (self_in == other) {
        return update ? mp_const_none : set_copy(self_in);
    }

278
279
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
    mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL));
280

Damien George's avatar
Damien George committed
281
    mp_obj_t iter = mp_getiter(other);
282
    mp_obj_t next;
283
    while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
284
        if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
285
            set_add(MP_OBJ_FROM_PTR(out), next);
286
287
288
289
290
291
292
293
294
295
        }
    }

    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;
    }

296
    return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
297
298
}

299
STATIC mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
300
301
    return set_intersect_int(self_in, other, false);
}
302
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
303

304
STATIC mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
305
306
    return set_intersect_int(self_in, other, true);
}
307
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
308

309
STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
310
    check_set_or_frozenset(self_in);
311
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
312

Damien George's avatar
Damien George committed
313
    mp_obj_t iter = mp_getiter(other);
John R. Lenton's avatar
John R. Lenton committed
314
    mp_obj_t next;
315
    while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton's avatar
John R. Lenton committed
316
317
318
319
320
321
        if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
            return mp_const_false;
        }
    }
    return mp_const_true;
}
322
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton's avatar
John R. Lenton committed
323

324
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
325
326
    mp_obj_set_t *self;
    bool cleanup_self = false;
327
    if (is_set_or_frozenset(self_in)) {
328
        self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
329
    } else {
330
        self = MP_OBJ_TO_PTR(set_make_new(MP_OBJ_FROM_PTR(&mp_type_set), 1, 0, &self_in));
John R. Lenton's avatar
John R. Lenton committed
331
332
333
334
335
        cleanup_self = true;
    }

    mp_obj_set_t *other;
    bool cleanup_other = false;
336
    if (is_set_or_frozenset(other_in)) {
337
        other = MP_OBJ_TO_PTR(other_in);
John R. Lenton's avatar
John R. Lenton committed
338
    } else {
339
        other = MP_OBJ_TO_PTR(set_make_new(MP_OBJ_FROM_PTR(&mp_type_set), 1, 0, &other_in));
John R. Lenton's avatar
John R. Lenton committed
340
341
        cleanup_other = true;
    }
John R. Lenton's avatar
John R. Lenton committed
342
343
344
345
    bool out = true;
    if (proper && self->set.used == other->set.used) {
        out = false;
    } else {
346
        mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self));
John R. Lenton's avatar
John R. Lenton committed
347
        mp_obj_t next;
348
        while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton's avatar
John R. Lenton committed
349
350
351
352
            if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) {
                out = false;
                break;
            }
John R. Lenton's avatar
John R. Lenton committed
353
354
        }
    }
355
    // TODO: Should free objects altogether
John R. Lenton's avatar
John R. Lenton committed
356
    if (cleanup_self) {
357
        set_clear(MP_OBJ_FROM_PTR(self));
John R. Lenton's avatar
John R. Lenton committed
358
359
    }
    if (cleanup_other) {
360
        set_clear(MP_OBJ_FROM_PTR(other));
John R. Lenton's avatar
John R. Lenton committed
361
    }
362
    return mp_obj_new_bool(out);
John R. Lenton's avatar
John R. Lenton committed
363
}
364
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
365
    return set_issubset_internal(self_in, other_in, false);
John R. Lenton's avatar
John R. Lenton committed
366
}
367
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issubset_obj, set_issubset);
John R. Lenton's avatar
John R. Lenton committed
368

369
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
370
371
372
    return set_issubset_internal(self_in, other_in, true);
}

373
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
374
    return set_issubset_internal(other_in, self_in, false);
John R. Lenton's avatar
John R. Lenton committed
375
}
376
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lenton's avatar
John R. Lenton committed
377

378
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
379
380
381
    return set_issubset_internal(other_in, self_in, true);
}

382
STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
383
    check_set_or_frozenset(self_in);
384
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
385
    if (!is_set_or_frozenset(other_in)) {
John R. Lenton's avatar
John R. Lenton committed
386
387
        return mp_const_false;
    }
388
    mp_obj_set_t *other = MP_OBJ_TO_PTR(other_in);
John R. Lenton's avatar
John R. Lenton committed
389
390
391
392
393
394
    if (self->set.used != other->set.used) {
        return mp_const_false;
    }
    return set_issubset(self_in, other_in);
}

395
STATIC mp_obj_t set_pop(mp_obj_t self_in) {
396
    check_set(self_in);
397
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
398
399
    mp_obj_t obj = mp_set_remove_first(&self->set);
    if (obj == MP_OBJ_NULL) {
400
        nlr_raise(mp_obj_new_exception_msg(&mp_type_KeyError, "pop from an empty set"));
John R. Lenton's avatar
John R. Lenton committed
401
402
403
    }
    return obj;
}
404
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lenton's avatar
John R. Lenton committed
405

406
STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
407
    check_set(self_in);
408
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
409
    if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
410
        nlr_raise(mp_obj_new_exception(&mp_type_KeyError));
John R. Lenton's avatar
John R. Lenton committed
411
412
413
    }
    return mp_const_none;
}
414
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
415

416
STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
417
    check_set(self_in);
418
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
Damien George's avatar
Damien George committed
419
    mp_obj_t iter = mp_getiter(other_in);
John R. Lenton's avatar
John R. Lenton committed
420
    mp_obj_t next;
421
    while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
422
        mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND);
John R. Lenton's avatar
John R. Lenton committed
423
424
425
    }
    return mp_const_none;
}
426
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
427

428
STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) {
429
    check_set_or_frozenset(self_in);
430
    mp_obj_t self_out = set_copy_as_mutable(self_in);
431
    set_symmetric_difference_update(self_out, other_in);
432
    ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_out))->type = ((mp_obj_base_t*)MP_OBJ_TO_PTR(self_in))->type;
433
    return self_out;
John R. Lenton's avatar
John R. Lenton committed
434
}
435
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference);
John R. Lenton's avatar
John R. Lenton committed
436

437
STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
Damien George's avatar
Damien George committed
438
    mp_obj_t iter = mp_getiter(other_in);
John R. Lenton's avatar
John R. Lenton committed
439
    mp_obj_t next;
440
    while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
John R. Lenton's avatar
John R. Lenton committed
441
442
443
444
        mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
    }
}

445
STATIC mp_obj_t set_update(mp_uint_t n_args, const mp_obj_t *args) {
John R. Lenton's avatar
John R. Lenton committed
446
447
    assert(n_args > 0);

448
    for (mp_uint_t i = 1; i < n_args; i++) {
449
        set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
John R. Lenton's avatar
John R. Lenton committed
450
451
452
453
    }

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

456
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
457
    check_set_or_frozenset(self_in);
458
459
    mp_obj_t self = set_copy(self_in);
    set_update_int(MP_OBJ_TO_PTR(self), other_in);
John R. Lenton's avatar
John R. Lenton committed
460
461
    return self;
}
462
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton's avatar
John R. Lenton committed
463

464
STATIC mp_obj_t set_unary_op(mp_uint_t op, mp_obj_t self_in) {
465
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
466
    switch (op) {
467
        case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->set.used != 0);
468
        case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->set.used);
469
470
471
472
#if MICROPY_PY_BUILTINS_FROZENSET
        case MP_UNARY_OP_HASH:
            if (MP_OBJ_IS_TYPE(self_in, &mp_type_frozenset)) {
                // start hash with unique value
473
                mp_int_t hash = (mp_int_t)(uintptr_t)&mp_type_frozenset;
474
475
476
477
478
479
480
481
482
483
484
                mp_uint_t max = self->set.alloc;
                mp_set_t *set = &self->set;

                for (mp_uint_t i = 0; i < max; i++) {
                    if (MP_SET_SLOT_IS_FILLED(set, i)) {
                        hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, set->table[i]));
                    }
                }
                return MP_OBJ_NEW_SMALL_INT(hash);
            }
#endif
485
        default: return MP_OBJ_NULL; // op not supported
486
487
    }
}
John R. Lenton's avatar
John R. Lenton committed
488

489
STATIC mp_obj_t set_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) {
John R. Lenton's avatar
John R. Lenton committed
490
491
    mp_obj_t args[] = {lhs, rhs};
    switch (op) {
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
        case MP_BINARY_OP_OR:
            return set_union(lhs, rhs);
        case MP_BINARY_OP_XOR:
            return set_symmetric_difference(lhs, rhs);
        case MP_BINARY_OP_AND:
            return set_intersect(lhs, rhs);
        case MP_BINARY_OP_SUBTRACT:
            return set_diff(2, args);
        case MP_BINARY_OP_INPLACE_OR:
            return set_union(lhs, rhs);
        case MP_BINARY_OP_INPLACE_XOR:
            return set_symmetric_difference(lhs, rhs);
        case MP_BINARY_OP_INPLACE_AND:
            return set_intersect(lhs, rhs);
        case MP_BINARY_OP_INPLACE_SUBTRACT:
            return set_diff(2, args);
        case MP_BINARY_OP_LESS:
            return set_issubset_proper(lhs, rhs);
        case MP_BINARY_OP_MORE:
            return set_issuperset_proper(lhs, rhs);
        case MP_BINARY_OP_EQUAL:
            return set_equal(lhs, rhs);
        case MP_BINARY_OP_LESS_EQUAL:
            return set_issubset(lhs, rhs);
        case MP_BINARY_OP_MORE_EQUAL:
            return set_issuperset(lhs, rhs);
        case MP_BINARY_OP_IN: {
519
            mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
520
            mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
521
            return mp_obj_new_bool(elem != MP_OBJ_NULL);
522
523
        }
        default:
524
            return MP_OBJ_NULL; // op not supported
John R. Lenton's avatar
John R. Lenton committed
525
526
    }
}
John R. Lenton's avatar
John R. Lenton committed
527

John R. Lenton's avatar
John R. Lenton committed
528
529
530
531
/******************************************************************************/
/* set constructors & public C API                                            */


532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
STATIC const mp_rom_map_elem_t set_locals_dict_table[] = {
    { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&set_add_obj) },
    { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&set_clear_obj) },
    { MP_ROM_QSTR(MP_QSTR_copy), MP_ROM_PTR(&set_copy_obj) },
    { MP_ROM_QSTR(MP_QSTR_discard), MP_ROM_PTR(&set_discard_obj) },
    { MP_ROM_QSTR(MP_QSTR_difference), MP_ROM_PTR(&set_diff_obj) },
    { MP_ROM_QSTR(MP_QSTR_difference_update), MP_ROM_PTR(&set_diff_update_obj) },
    { MP_ROM_QSTR(MP_QSTR_intersection), MP_ROM_PTR(&set_intersect_obj) },
    { MP_ROM_QSTR(MP_QSTR_intersection_update), MP_ROM_PTR(&set_intersect_update_obj) },
    { MP_ROM_QSTR(MP_QSTR_isdisjoint), MP_ROM_PTR(&set_isdisjoint_obj) },
    { MP_ROM_QSTR(MP_QSTR_issubset), MP_ROM_PTR(&set_issubset_obj) },
    { MP_ROM_QSTR(MP_QSTR_issuperset), MP_ROM_PTR(&set_issuperset_obj) },
    { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&set_pop_obj) },
    { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&set_remove_obj) },
    { MP_ROM_QSTR(MP_QSTR_symmetric_difference), MP_ROM_PTR(&set_symmetric_difference_obj) },
    { MP_ROM_QSTR(MP_QSTR_symmetric_difference_update), MP_ROM_PTR(&set_symmetric_difference_update_obj) },
    { MP_ROM_QSTR(MP_QSTR_union), MP_ROM_PTR(&set_union_obj) },
    { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&set_update_obj) },
    { MP_ROM_QSTR(MP_QSTR___contains__), MP_ROM_PTR(&mp_op_contains_obj) },
John R. Lenton's avatar
John R. Lenton committed
551
552
};

553
554
STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);

555
const mp_obj_type_t mp_type_set = {
556
    { &mp_type_type },
557
    .name = MP_QSTR_set,
558
559
    .print = set_print,
    .make_new = set_make_new,
560
    .unary_op = set_unary_op,
John R. Lenton's avatar
John R. Lenton committed
561
    .binary_op = set_binary_op,
John R. Lenton's avatar
John R. Lenton committed
562
    .getiter = set_getiter,
563
    .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
564
565
};

566
#if MICROPY_PY_BUILTINS_FROZENSET
567
568
569
570
571
572
573
574
const mp_obj_type_t mp_type_frozenset = {
    { &mp_type_type },
    .name = MP_QSTR_frozenset,
    .print = set_print,
    .make_new = set_make_new,
    .unary_op = set_unary_op,
    .binary_op = set_binary_op,
    .getiter = set_getiter,
575
    .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
576
577
578
};
#endif

579
mp_obj_t mp_obj_new_set(mp_uint_t n_args, mp_obj_t *items) {
580
    mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
581
    o->base.type = &mp_type_set;
582
    mp_set_init(&o->set, n_args);
583
    for (mp_uint_t i = 0; i < n_args; i++) {
John R. Lenton's avatar
John R. Lenton committed
584
        mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
585
    }
586
    return MP_OBJ_FROM_PTR(o);
587
588
589
}

void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
590
    assert(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
591
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
592
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
593
}
594
595

#endif // MICROPY_PY_BUILTINS_SET