objset.c 19.9 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(const mp_obj_type_t *type, size_t n_args, size_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
            set->base.type = type;
131
            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 = type;
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(size_t n_args, const mp_obj_t *args, bool update) {
227
    mp_obj_t self;
228
    if (update) {
229
        check_set(args[0]);
230
231
        self = args[0];
    } else {
232
233
        check_set_or_frozenset(args[0]);
        self = set_copy_as_mutable(args[0]);
234
235
236
    }


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

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

254
STATIC mp_obj_t set_diff(size_t n_args, const mp_obj_t *args) {
255
256
    return set_diff_int(n_args, args, false);
}
257
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
258

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

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

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

276
277
    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));
278

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

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

294
    return update ? mp_const_none : MP_OBJ_FROM_PTR(out);
295
296
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

443
STATIC mp_obj_t set_update(size_t n_args, const mp_obj_t *args) {
444
    for (mp_uint_t i = 1; i < n_args; i++) {
445
        set_update_int(MP_OBJ_TO_PTR(args[0]), args[i]);
John R. Lenton's avatar
John R. Lenton committed
446
447
448
449
    }

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

452
STATIC mp_obj_t set_union(mp_obj_t self_in, mp_obj_t other_in) {
453
    check_set_or_frozenset(self_in);
454
455
    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
456
457
    return self;
}
458
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_union_obj, set_union);
John R. Lenton's avatar
John R. Lenton committed
459

460
STATIC mp_obj_t set_unary_op(mp_uint_t op, mp_obj_t self_in) {
461
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
462
    switch (op) {
463
        case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->set.used != 0);
464
        case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->set.used);
465
466
467
468
#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
469
                mp_int_t hash = (mp_int_t)(uintptr_t)&mp_type_frozenset;
470
471
472
473
474
475
476
477
478
479
480
                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
481
        default: return MP_OBJ_NULL; // op not supported
482
483
    }
}
John R. Lenton's avatar
John R. Lenton committed
484

485
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
486
487
    mp_obj_t args[] = {lhs, rhs};
    switch (op) {
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
        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: {
515
            mp_obj_set_t *o = MP_OBJ_TO_PTR(lhs);
516
            mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
517
            return mp_obj_new_bool(elem != MP_OBJ_NULL);
518
519
        }
        default:
520
            return MP_OBJ_NULL; // op not supported
John R. Lenton's avatar
John R. Lenton committed
521
522
    }
}
John R. Lenton's avatar
John R. Lenton committed
523

John R. Lenton's avatar
John R. Lenton committed
524
525
526
527
/******************************************************************************/
/* set constructors & public C API                                            */


528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
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
547
548
};

549
550
STATIC MP_DEFINE_CONST_DICT(set_locals_dict, set_locals_dict_table);

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

562
#if MICROPY_PY_BUILTINS_FROZENSET
563
564
565
566
567
568
569
570
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,
571
    .locals_dict = (mp_obj_dict_t*)&set_locals_dict,
572
573
574
};
#endif

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

void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
586
    mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_set));
587
    mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in);
John R. Lenton's avatar
John R. Lenton committed
588
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
589
}
590
591

#endif // MICROPY_PY_BUILTINS_SET