blob: 144bb6daa857adcc7021f4171ae720c32ed7e460 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
2#include <stdint.h>
John R. Lenton3b0bd872014-01-12 15:56:25 +00003#include <string.h>
Damiend99b0522013-12-21 18:17:45 +00004#include <assert.h>
5
6#include "nlr.h"
7#include "misc.h"
8#include "mpconfig.h"
Damien George71c51812014-01-04 20:21:15 +00009#include "mpqstr.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "obj.h"
Damien George71c51812014-01-04 20:21:15 +000011#include "runtime.h"
Damiend99b0522013-12-21 18:17:45 +000012#include "map.h"
13
14typedef struct _mp_obj_set_t {
15 mp_obj_base_t base;
16 mp_set_t set;
17} mp_obj_set_t;
18
John R. Lenton0ce03b42014-01-12 15:17:42 +000019typedef struct _mp_obj_set_it_t {
20 mp_obj_base_t base;
21 mp_obj_set_t *set;
22 machine_uint_t cur;
23} mp_obj_set_it_t;
24
25static mp_obj_t set_it_iternext(mp_obj_t self_in);
26
Damiend99b0522013-12-21 18:17:45 +000027void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
28 mp_obj_set_t *self = self_in;
29 bool first = true;
30 print(env, "{");
31 for (int i = 0; i < self->set.alloc; i++) {
32 if (self->set.table[i] != MP_OBJ_NULL) {
33 if (!first) {
34 print(env, ", ");
35 }
36 first = false;
37 mp_obj_print_helper(print, env, self->set.table[i]);
38 }
39 }
40 print(env, "}");
41}
42
Damien George71c51812014-01-04 20:21:15 +000043static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) {
44 switch (n_args) {
45 case 0:
46 // return a new, empty set
47 return mp_obj_new_set(0, NULL);
48
49 case 1:
50 {
51 // 1 argument, an iterable from which we make a new set
52 mp_obj_t set = mp_obj_new_set(0, NULL);
53 mp_obj_t iterable = rt_getiter(args[0]);
54 mp_obj_t item;
55 while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
56 mp_obj_set_store(set, item);
57 }
58 return set;
59 }
60
61 default:
62 nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "set takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
63 }
64}
65
John R. Lenton0ce03b42014-01-12 15:17:42 +000066const mp_obj_type_t set_it_type = {
67 { &mp_const_type },
68 "set_iterator",
69 .iternext = set_it_iternext,
70};
71
72static mp_obj_t set_it_iternext(mp_obj_t self_in) {
73 assert(MP_OBJ_IS_TYPE(self_in, &set_it_type));
74 mp_obj_set_it_t *self = self_in;
75 machine_uint_t max = self->set->set.alloc;
76 mp_obj_t *table = self->set->set.table;
77
78 for (machine_uint_t i = self->cur; i < max; i++) {
79 if (table[i] != NULL) {
80 self->cur = i + 1;
81 return table[i];
82 }
83 }
84
85 return mp_const_stop_iteration;
86}
87
88static mp_obj_t set_getiter(mp_obj_t set_in) {
89 mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t);
90 o->base.type = &set_it_type;
91 o->set = (mp_obj_set_t *)set_in;
92 o->cur = 0;
93 return o;
94}
95
John R. Lenton19b14d32014-01-12 15:29:11 +000096
97/******************************************************************************/
98/* set methods */
99
100static mp_obj_t set_add(mp_obj_t self_in, mp_obj_t item) {
101 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
102 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000103 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
John R. Lenton19b14d32014-01-12 15:29:11 +0000104 return mp_const_none;
105}
106static MP_DEFINE_CONST_FUN_OBJ_2(set_add_obj, set_add);
107
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000108static mp_obj_t set_clear(mp_obj_t self_in) {
109 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
110 mp_obj_set_t *self = self_in;
111
112 mp_set_clear(&self->set);
113
114 return mp_const_none;
115}
116static MP_DEFINE_CONST_FUN_OBJ_1(set_clear_obj, set_clear);
117
John R. Lenton3b0bd872014-01-12 15:56:25 +0000118static mp_obj_t set_copy(mp_obj_t self_in) {
119 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
120 mp_obj_set_t *self = self_in;
121
122 mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
123 other->base.type = &set_type;
124 mp_set_init(&other->set, self->set.alloc);
125 other->set.used = self->set.used;
126 memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
127
128 return other;
129}
130static MP_DEFINE_CONST_FUN_OBJ_1(set_copy_obj, set_copy);
131
John R. Lenton2a241722014-01-12 16:39:39 +0000132static mp_obj_t set_discard(mp_obj_t self_in, mp_obj_t item) {
133 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
134 mp_obj_set_t *self = self_in;
135 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_REMOVE_IF_FOUND);
136 return mp_const_none;
137}
138static MP_DEFINE_CONST_FUN_OBJ_2(set_discard_obj, set_discard);
John R. Lenton19b14d32014-01-12 15:29:11 +0000139
John R. Lenton032129f2014-01-12 17:07:17 +0000140static mp_obj_t set_diff_int(int n_args, const mp_obj_t *args, bool update) {
141 assert(n_args > 0);
142 assert(MP_OBJ_IS_TYPE(args[0], &set_type));
143 mp_obj_set_t *self;
144 if (update) {
145 self = args[0];
146 } else {
147 self = set_copy(args[0]);
148 }
149
150
151 for (int i = 1; i < n_args; i++) {
152 mp_obj_t other = args[i];
153 if (self == other) {
154 set_clear(self);
155 } else {
156 mp_obj_t iter = rt_getiter(other);
157 mp_obj_t next;
158 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
159 set_discard(self, next);
160 }
161 }
162 }
163
164 return self;
165}
166
167static mp_obj_t set_diff(int n_args, const mp_obj_t *args) {
168 return set_diff_int(n_args, args, false);
169}
170static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_obj, 1, set_diff);
171
172static mp_obj_t set_diff_update(int n_args, const mp_obj_t *args) {
173 set_diff_int(n_args, args, true);
174 return mp_const_none;
175}
176static MP_DEFINE_CONST_FUN_OBJ_VAR(set_diff_update_obj, 1, set_diff_update);
177
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000178static mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) {
179 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
180 if (self_in == other) {
181 return update ? mp_const_none : set_copy(self_in);
182 }
183
184 mp_obj_set_t *self = self_in;
185 mp_obj_set_t *out = mp_obj_new_set(0, NULL);
186
187 mp_obj_t iter = rt_getiter(other);
188 mp_obj_t next;
189 while ((next = rt_iternext(iter)) != mp_const_stop_iteration) {
190 if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) {
191 set_add(out, next);
192 }
193 }
194
195 if (update) {
196 m_del(mp_obj_t, self->set.table, self->set.alloc);
197 self->set.alloc = out->set.alloc;
198 self->set.used = out->set.used;
199 self->set.table = out->set.table;
200 }
201
202 return update ? mp_const_none : out;
203}
204
205static mp_obj_t set_intersect(mp_obj_t self_in, mp_obj_t other) {
206 return set_intersect_int(self_in, other, false);
207}
208static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_obj, set_intersect);
209
210static mp_obj_t set_intersect_update(mp_obj_t self_in, mp_obj_t other) {
211 return set_intersect_int(self_in, other, true);
212}
213static MP_DEFINE_CONST_FUN_OBJ_2(set_intersect_update_obj, set_intersect_update);
214
John R. Lenton032129f2014-01-12 17:07:17 +0000215
John R. Lenton19b14d32014-01-12 15:29:11 +0000216/******************************************************************************/
217/* set constructors & public C API */
218
219
220static const mp_method_t set_type_methods[] = {
221 { "add", &set_add_obj },
John R. Lenton1d7fb2f2014-01-12 15:44:26 +0000222 { "clear", &set_clear_obj },
John R. Lenton3b0bd872014-01-12 15:56:25 +0000223 { "copy", &set_copy_obj },
John R. Lenton2a241722014-01-12 16:39:39 +0000224 { "discard", &set_discard_obj },
John R. Lenton032129f2014-01-12 17:07:17 +0000225 { "difference", &set_diff_obj },
226 { "difference_update", &set_diff_update_obj },
John R. Lentonf1ae6b42014-01-12 17:54:03 +0000227 { "intersection", &set_intersect_obj },
228 { "intersection_update", &set_intersect_update_obj },
John R. Lenton19b14d32014-01-12 15:29:11 +0000229 { NULL, NULL }, // end-of-list sentinel
230};
231
Damien George71c51812014-01-04 20:21:15 +0000232const mp_obj_type_t set_type = {
Damiend99b0522013-12-21 18:17:45 +0000233 { &mp_const_type },
234 "set",
Damien George97209d32014-01-07 15:58:30 +0000235 .print = set_print,
236 .make_new = set_make_new,
John R. Lenton0ce03b42014-01-12 15:17:42 +0000237 .getiter = set_getiter,
John R. Lenton19b14d32014-01-12 15:29:11 +0000238 .methods = set_type_methods,
Damiend99b0522013-12-21 18:17:45 +0000239};
240
241mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) {
242 mp_obj_set_t *o = m_new_obj(mp_obj_set_t);
243 o->base.type = &set_type;
244 mp_set_init(&o->set, n_args);
245 for (int i = 0; i < n_args; i++) {
John R. Lenton2a241722014-01-12 16:39:39 +0000246 mp_set_lookup(&o->set, items[i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000247 }
248 return o;
249}
250
251void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item) {
252 assert(MP_OBJ_IS_TYPE(self_in, &set_type));
253 mp_obj_set_t *self = self_in;
John R. Lenton2a241722014-01-12 16:39:39 +0000254 mp_set_lookup(&self->set, item, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND);
Damiend99b0522013-12-21 18:17:45 +0000255}