objset.c 14.4 KB
Newer Older
1
2
#include <stdlib.h>
#include <stdint.h>
John R. Lenton's avatar
John R. Lenton committed
3
#include <string.h>
4
5
6
7
8
#include <assert.h>

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

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
20
21
22
23
24
25
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;

26
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in);
John R. Lenton's avatar
John R. Lenton committed
27

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

48

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

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    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);
            mp_obj_t iterable = rt_getiter(args[0]);
            mp_obj_t item;
            while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
                mp_obj_set_store(set, item);
            }
            return set;
        }

        default:
70
            nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "set takes at most 1 argument, %d given", n_args));
71
72
73
    }
}

John R. Lenton's avatar
John R. Lenton committed
74
75
const mp_obj_type_t set_it_type = {
    { &mp_const_type },
76
    .name = MP_QSTR_iterator,
John R. Lenton's avatar
John R. Lenton committed
77
78
79
    .iternext = set_it_iternext,
};

80
STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) {
John R. Lenton's avatar
John R. Lenton committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    assert(MP_OBJ_IS_TYPE(self_in, &set_it_type));
    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++) {
        if (table[i] != NULL) {
            self->cur = i + 1;
            return table[i];
        }
    }

    return mp_const_stop_iteration;
}

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

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

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

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

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

    mp_set_clear(&self->set);

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

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

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

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

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

148
STATIC mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    assert(n_args > 0);
    assert(MP_OBJ_IS_TYPE(args[0], &set_type));
    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 {
            mp_obj_t iter = rt_getiter(other);
            mp_obj_t next;
            while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
                set_discard(self, next);
            }
        }
    }

    return self;
}

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

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

186
STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    assert(MP_OBJ_IS_TYPE(self_in, &set_type));
    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);

    mp_obj_t iter = rt_getiter(other);
    mp_obj_t next;
    while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
        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;
}

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

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

223
STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) {
John R. Lenton's avatar
John R. Lenton committed
224
225
226
227
228
229
230
231
232
233
234
235
    assert(MP_OBJ_IS_TYPE(self_in, &set_type));
    mp_obj_set_t *self = self_in;

    mp_obj_t iter = rt_getiter(other);
    mp_obj_t next;
    while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
        if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
            return mp_const_false;
        }
    }
    return mp_const_true;
}
236
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_isdisjoint_obj, set_isdisjoint);
John R. Lenton's avatar
John R. Lenton committed
237

238
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
239
240
241
242
243
    mp_obj_set_t *self;
    bool cleanup_self = false;
    if (MP_OBJ_IS_TYPE(self_in, &set_type)) {
        self = self_in;
    } else {
244
        self = set_make_new((mp_obj_t)&set_type, 1, 0, &self_in);
John R. Lenton's avatar
John R. Lenton committed
245
246
247
248
249
250
251
252
        cleanup_self = true;
    }

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

282
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
283
284
285
    return set_issubset_internal(self_in, other_in, true);
}

286
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
287
    return set_issubset_internal(other_in, self_in, false);
John R. Lenton's avatar
John R. Lenton committed
288
}
289
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_issuperset_obj, set_issuperset);
John R. Lenton's avatar
John R. Lenton committed
290

291
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
292
293
294
    return set_issubset_internal(other_in, self_in, true);
}

295
STATIC mp_obj_t set_equal(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lenton's avatar
John R. Lenton committed
296
297
298
299
300
301
302
303
304
305
306
307
    assert(MP_OBJ_IS_TYPE(self_in, &set_type));
    mp_obj_set_t *self = self_in;
    if (!MP_OBJ_IS_TYPE(other_in, &set_type)) {
        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);
}

308
STATIC mp_obj_t set_pop(mp_obj_t self_in) {
John R. Lenton's avatar
John R. Lenton committed
309
310
311
312
313
314
315
316
317
318
    assert(MP_OBJ_IS_TYPE(self_in, &set_type));
    mp_obj_set_t *self = self_in;

    if (self->set.used == 0) {
        nlr_jump(mp_obj_new_exception_msg(MP_QSTR_KeyError, "pop from an empty set"));
    }
    mp_obj_t obj = mp_set_lookup(&self->set, NULL,
                         MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_FIRST);
    return obj;
}
319
STATIC MP_DEFINE_CONST_FUN_OBJ_1(set_pop_obj, set_pop);
John R. Lenton's avatar
John R. Lenton committed
320

321
STATIC mp_obj_t set_remove(mp_obj_t self_in, mp_obj_t item) {
John R. Lenton's avatar
John R. Lenton committed
322
323
324
325
326
327
328
    assert(MP_OBJ_IS_TYPE(self_in, &set_type));
    mp_obj_set_t *self = self_in;
    if (mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND) == MP_OBJ_NULL) {
        nlr_jump(mp_obj_new_exception(MP_QSTR_KeyError));
    }
    return mp_const_none;
}
329
STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove);
330

331
STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) {
John R. Lenton's avatar
John R. Lenton committed
332
333
334
335
336
337
338
339
340
    assert(MP_OBJ_IS_TYPE(self_in, &set_type));
    mp_obj_set_t *self = self_in;
    mp_obj_t iter = rt_getiter(other_in);
    mp_obj_t next;
    while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
        mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_REMOVE_IF_FOUND | MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
    }
    return mp_const_none;
}
341
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
342

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

351
STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) {
John R. Lenton's avatar
John R. Lenton committed
352
353
354
355
356
357
358
    mp_obj_t iter = rt_getiter(other_in);
    mp_obj_t next;
    while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
        mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
    }
}

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

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

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

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


380
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
    mp_obj_t args[] = {lhs, rhs};
    switch (op) {
    case RT_BINARY_OP_OR:
        return set_union(lhs, rhs);
    case RT_BINARY_OP_XOR:
        return set_symmetric_difference(lhs, rhs);
    case RT_BINARY_OP_AND:
        return set_intersect(lhs, rhs);
    case RT_BINARY_OP_SUBTRACT:
        return set_diff(2, args);
    case RT_BINARY_OP_INPLACE_OR:
        return set_union(lhs, rhs);
    case RT_BINARY_OP_INPLACE_XOR:
        return set_symmetric_difference(lhs, rhs);
    case RT_BINARY_OP_INPLACE_AND:
        return set_intersect(lhs, rhs);
    case RT_BINARY_OP_INPLACE_SUBTRACT:
        return set_diff(2, args);
399
    case RT_BINARY_OP_LESS:
John R. Lenton's avatar
John R. Lenton committed
400
        return set_issubset_proper(lhs, rhs);
401
    case RT_BINARY_OP_MORE:
John R. Lenton's avatar
John R. Lenton committed
402
        return set_issuperset_proper(lhs, rhs);
403
    case RT_BINARY_OP_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
404
        return set_equal(lhs, rhs);
405
    case RT_BINARY_OP_LESS_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
406
        return set_issubset(lhs, rhs);
407
    case RT_BINARY_OP_MORE_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
408
        return set_issuperset(lhs, rhs);
409
    case RT_BINARY_OP_NOT_EQUAL:
John R. Lenton's avatar
John R. Lenton committed
410
        return MP_BOOL(set_equal(lhs, rhs) == mp_const_false);
411
    case RT_BINARY_OP_IN:
412
413
414
    {
        mp_obj_set_t *o = lhs;
        mp_obj_t elem = mp_set_lookup(&o->set, rhs, MP_MAP_LOOKUP);
415
        return MP_BOOL(elem != NULL);
416
    }
John R. Lenton's avatar
John R. Lenton committed
417
418
419
420
421
    default:
        // op not supported
        return NULL;
    }
}
John R. Lenton's avatar
John R. Lenton committed
422

John R. Lenton's avatar
John R. Lenton committed
423
424
425
426
/******************************************************************************/
/* set constructors & public C API                                            */


427
STATIC const mp_method_t set_type_methods[] = {
John R. Lenton's avatar
John R. Lenton committed
428
    { "add", &set_add_obj },
John R. Lenton's avatar
John R. Lenton committed
429
    { "clear", &set_clear_obj },
John R. Lenton's avatar
John R. Lenton committed
430
    { "copy", &set_copy_obj },
John R. Lenton's avatar
John R. Lenton committed
431
    { "discard", &set_discard_obj },
432
433
    { "difference", &set_diff_obj },
    { "difference_update", &set_diff_update_obj },
434
435
    { "intersection", &set_intersect_obj },
    { "intersection_update", &set_intersect_update_obj },
John R. Lenton's avatar
John R. Lenton committed
436
    { "isdisjoint", &set_isdisjoint_obj },
John R. Lenton's avatar
John R. Lenton committed
437
438
439
440
    { "issubset", &set_issubset_obj },
    { "issuperset", &set_issuperset_obj },
    { "pop", &set_pop_obj },
    { "remove", &set_remove_obj },
John R. Lenton's avatar
John R. Lenton committed
441
442
443
444
    { "symmetric_difference", &set_symmetric_difference_obj },
    { "symmetric_difference_update", &set_symmetric_difference_update_obj },
    { "union", &set_union_obj },
    { "update", &set_update_obj },
John R. Lenton's avatar
John R. Lenton committed
445
446
447
    { NULL, NULL }, // end-of-list sentinel
};

448
const mp_obj_type_t set_type = {
449
    { &mp_const_type },
450
    .name = MP_QSTR_set,
451
452
    .print = set_print,
    .make_new = set_make_new,
John R. Lenton's avatar
John R. Lenton committed
453
    .binary_op = set_binary_op,
John R. Lenton's avatar
John R. Lenton committed
454
    .getiter = set_getiter,
John R. Lenton's avatar
John R. Lenton committed
455
    .methods = set_type_methods,
456
457
458
459
460
461
462
};

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);
    o->base.type = &set_type;
    mp_set_init(&o->set, n_args);
    for (int i = 0; i < n_args; i++) {
John R. Lenton's avatar
John R. Lenton committed
463
        mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
464
465
466
467
468
469
470
    }
    return o;
}

void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
    assert(MP_OBJ_IS_TYPE(self_in, &set_type));
    mp_obj_set_t *self = self_in;
John R. Lenton's avatar
John R. Lenton committed
471
    mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
472
}