Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objtuple.c
Go to the documentation of this file.
1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2013, 2014 Damien P. George
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #include <string.h>
28 #include <assert.h>
29 
30 #include "py/objtuple.h"
31 #include "py/runtime.h"
32 
33 /******************************************************************************/
34 /* tuple */
35 
36 void mp_obj_tuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
37  mp_obj_tuple_t *o = MP_OBJ_TO_PTR(o_in);
38  if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
39  mp_print_str(print, "[");
40  } else {
41  mp_print_str(print, "(");
42  kind = PRINT_REPR;
43  }
44  for (size_t i = 0; i < o->len; i++) {
45  if (i > 0) {
46  mp_print_str(print, ", ");
47  }
48  mp_obj_print_helper(print, o->items[i], kind);
49  }
50  if (MICROPY_PY_UJSON && kind == PRINT_JSON) {
51  mp_print_str(print, "]");
52  } else {
53  if (o->len == 1) {
54  mp_print_str(print, ",");
55  }
56  mp_print_str(print, ")");
57  }
58 }
59 
60 STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
61  (void)type_in;
62 
63  mp_arg_check_num(n_args, n_kw, 0, 1, false);
64 
65  switch (n_args) {
66  case 0:
67  // return a empty tuple
68  return mp_const_empty_tuple;
69 
70  case 1:
71  default: {
72  // 1 argument, an iterable from which we make a new tuple
73  if (MP_OBJ_IS_TYPE(args[0], &mp_type_tuple)) {
74  return args[0];
75  }
76 
77  // TODO optimise for cases where we know the length of the iterator
78 
79  size_t alloc = 4;
80  size_t len = 0;
81  mp_obj_t *items = m_new(mp_obj_t, alloc);
82 
83  mp_obj_t iterable = mp_getiter(args[0], NULL);
84  mp_obj_t item;
85  while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
86  if (len >= alloc) {
87  items = m_renew(mp_obj_t, items, alloc, alloc * 2);
88  alloc *= 2;
89  }
90  items[len++] = item;
91  }
92 
93  mp_obj_t tuple = mp_obj_new_tuple(len, items);
94  m_del(mp_obj_t, items, alloc);
95 
96  return tuple;
97  }
98  }
99 }
100 
101 // Don't pass MP_BINARY_OP_NOT_EQUAL here
103  // type check is done on getiter method to allow tuple, namedtuple, attrtuple
104  mp_check_self(mp_obj_get_type(self_in)->getiter == mp_obj_tuple_getiter);
105  mp_obj_type_t *another_type = mp_obj_get_type(another_in);
106  mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
107  if (another_type->getiter != mp_obj_tuple_getiter) {
108  // Slow path for user subclasses
110  if (another_in == MP_OBJ_NULL) {
111  if (op == MP_BINARY_OP_EQUAL) {
112  return mp_const_false;
113  }
114  return MP_OBJ_NULL;
115  }
116  }
117  mp_obj_tuple_t *another = MP_OBJ_TO_PTR(another_in);
118 
119  return mp_obj_new_bool(mp_seq_cmp_objs(op, self->items, self->len, another->items, another->len));
120 }
121 
123  mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
124  switch (op) {
125  case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);
126  case MP_UNARY_OP_HASH: {
127  // start hash with pointer to empty tuple, to make it fairly unique
129  for (size_t i = 0; i < self->len; i++) {
130  hash += MP_OBJ_SMALL_INT_VALUE(mp_unary_op(MP_UNARY_OP_HASH, self->items[i]));
131  }
132  return MP_OBJ_NEW_SMALL_INT(hash);
133  }
134  case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(self->len);
135  default: return MP_OBJ_NULL; // op not supported
136  }
137 }
138 
140  mp_obj_tuple_t *o = MP_OBJ_TO_PTR(lhs);
141  switch (op) {
142  case MP_BINARY_OP_ADD:
145  return MP_OBJ_NULL; // op not supported
146  }
147  mp_obj_tuple_t *p = MP_OBJ_TO_PTR(rhs);
149  mp_seq_cat(s->items, o->items, o->len, p->items, p->len, mp_obj_t);
150  return MP_OBJ_FROM_PTR(s);
151  }
154  mp_int_t n;
155  if (!mp_obj_get_int_maybe(rhs, &n)) {
156  return MP_OBJ_NULL; // op not supported
157  }
158  if (n <= 0) {
159  return mp_const_empty_tuple;
160  }
162  mp_seq_multiply(o->items, sizeof(*o->items), o->len, n, s->items);
163  return MP_OBJ_FROM_PTR(s);
164  }
165  case MP_BINARY_OP_EQUAL:
166  case MP_BINARY_OP_LESS:
168  case MP_BINARY_OP_MORE:
170  return tuple_cmp_helper(op, lhs, rhs);
171 
172  default:
173  return MP_OBJ_NULL; // op not supported
174  }
175 }
176 
178  if (value == MP_OBJ_SENTINEL) {
179  // load
180  mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
181 #if MICROPY_PY_BUILTINS_SLICE
182  if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
183  mp_bound_slice_t slice;
184  if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice)) {
185  mp_raise_NotImplementedError("only slices with step=1 (aka None) are supported");
186  }
188  mp_seq_copy(res->items, self->items + slice.start, res->len, mp_obj_t);
189  return MP_OBJ_FROM_PTR(res);
190  }
191 #endif
192  size_t index_value = mp_get_index(self->base.type, self->len, index, false);
193  return self->items[index_value];
194  } else {
195  return MP_OBJ_NULL; // op not supported
196  }
197 }
198 
201  mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
202  return mp_seq_count_obj(self->items, self->len, value);
203 }
205 
206 STATIC mp_obj_t tuple_index(size_t n_args, const mp_obj_t *args) {
208  mp_obj_tuple_t *self = MP_OBJ_TO_PTR(args[0]);
209  return mp_seq_index_obj(self->items, self->len, n_args, args);
210 }
212 
214  { MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&tuple_count_obj) },
215  { MP_ROM_QSTR(MP_QSTR_index), MP_ROM_PTR(&tuple_index_obj) },
216 };
217 
219 
221  { &mp_type_type },
222  .name = MP_QSTR_tuple,
223  .print = mp_obj_tuple_print,
224  .make_new = mp_obj_tuple_make_new,
225  .unary_op = mp_obj_tuple_unary_op,
226  .binary_op = mp_obj_tuple_binary_op,
227  .subscr = mp_obj_tuple_subscr,
228  .getiter = mp_obj_tuple_getiter,
229  .locals_dict = (mp_obj_dict_t*)&tuple_locals_dict,
230 };
231 
232 // the zero-length tuple
234 
235 mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items) {
236  if (n == 0) {
237  return mp_const_empty_tuple;
238  }
240  o->base.type = &mp_type_tuple;
241  o->len = n;
242  if (items) {
243  for (size_t i = 0; i < n; i++) {
244  o->items[i] = items[i];
245  }
246  }
247  return MP_OBJ_FROM_PTR(o);
248 }
249 
250 void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items) {
251  assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
252  mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
253  *len = self->len;
254  *items = &self->items[0];
255 }
256 
257 void mp_obj_tuple_del(mp_obj_t self_in) {
258  assert(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple));
259  mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in);
260  m_del_var(mp_obj_tuple_t, mp_obj_t, self->len, self);
261 }
262 
263 /******************************************************************************/
264 /* tuple iterator */
265 
266 typedef struct _mp_obj_tuple_it_t {
270  size_t cur;
272 
274  mp_obj_tuple_it_t *self = MP_OBJ_TO_PTR(self_in);
275  if (self->cur < self->tuple->len) {
276  mp_obj_t o_out = self->tuple->items[self->cur];
277  self->cur += 1;
278  return o_out;
279  } else {
280  return MP_OBJ_STOP_ITERATION;
281  }
282 }
283 
285  assert(sizeof(mp_obj_tuple_it_t) <= sizeof(mp_obj_iter_buf_t));
286  mp_obj_tuple_it_t *o = (mp_obj_tuple_it_t*)iter_buf;
287  o->base.type = &mp_type_polymorph_iter;
289  o->tuple = MP_OBJ_TO_PTR(o_in);
290  o->cur = 0;
291  return MP_OBJ_FROM_PTR(o);
292 }
mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type)
Definition: objtype.c:1219
size_t len
Definition: objtuple.h:33
mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items)
Definition: objtuple.c:235
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg)
Definition: runtime.c:216
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
mp_obj_base_t base
Definition: objtuple.h:32
mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value)
Definition: objtuple.c:177
mp_uint_t stop
Definition: obj.h:840
#define assert(e)
Definition: assert.h:9
NORETURN void mp_raise_NotImplementedError(const char *msg)
Definition: runtime.c:1468
void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items)
Definition: objtuple.c:250
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
#define m_del(type, ptr, num)
Definition: misc.h:77
STATIC mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value)
Definition: objtuple.c:199
#define MP_OBJ_SENTINEL
Definition: obj.h:75
mp_obj_type_t * mp_obj_get_type(mp_const_obj_t o_in)
Definition: obj.c:40
mp_obj_t mp_obj_tuple_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
Definition: objtuple.c:139
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
#define MP_ROM_QSTR(q)
Definition: obj.h:241
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
STATIC mp_obj_t tuple_cmp_helper(mp_uint_t op, mp_obj_t self_in, mp_obj_t another_in)
Definition: objtuple.c:102
void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw)
Definition: argcheck.c:32
#define MP_ROM_PTR(p)
Definition: obj.h:242
mp_fun_1_t iternext
Definition: objtuple.c:268
void mp_obj_tuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind)
Definition: objtuple.c:36
STATIC MP_DEFINE_CONST_DICT(tuple_locals_dict, tuple_locals_dict_table)
mp_unary_op_t
Definition: runtime0.h:45
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value)
Definition: sequence.c:266
#define STATIC
Definition: mpconfig.h:1178
#define MP_OBJ_SMALL_INT_VALUE(o)
Definition: obj.h:86
mp_obj_t items[]
Definition: objtuple.h:34
mp_obj_t(* mp_fun_1_t)(mp_obj_t)
Definition: obj.h:404
bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo)
Definition: objtype.c:1143
const mp_obj_tuple_t mp_const_empty_tuple_obj
Definition: objtuple.c:233
STATIC mp_obj_t tuple_it_iternext(mp_obj_t self_in)
Definition: objtuple.c:273
size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice)
Definition: obj.c:376
#define MICROPY_PY_UJSON
Definition: mpconfig.h:1055
mp_print_kind_t
Definition: obj.h:412
#define MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
#define m_new_obj_var(obj_type, var_type, var_num)
Definition: misc.h:62
const mp_obj_type_t mp_type_polymorph_iter
Definition: objpolyiter.c:48
mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf)
Definition: objtuple.c:284
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
#define mp_check_self(pred)
Definition: runtime.h:161
STATIC MP_DEFINE_CONST_FUN_OBJ_2(tuple_count_obj, tuple_count)
bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp_obj_t *items2, size_t len2)
Definition: sequence.c:194
void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind)
Definition: obj.c:59
#define m_del_var(obj_type, var_type, var_num, ptr)
Definition: misc.h:78
mp_binary_op_t
Definition: runtime0.h:67
mp_obj_tuple_t * tuple
Definition: objtuple.c:269
void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest)
Definition: sequence.c:38
args
Definition: i18n.py:175
mp_obj_base_t base
Definition: objtuple.c:267
bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value)
Definition: obj.c:258
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
mp_getiter_fun_t getiter
Definition: obj.h:517
STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args)
Definition: objtuple.c:60
#define m_renew(type, ptr, old_num, new_num)
Definition: misc.h:75
void mp_obj_tuple_del(mp_obj_t self_in)
Definition: objtuple.c:257
#define mp_seq_copy(dest, src, len, item_t)
Definition: obj.h:848
STATIC const mp_rom_map_elem_t tuple_locals_dict_table[]
Definition: objtuple.c:213
STATIC mp_obj_t tuple_index(size_t n_args, const mp_obj_t *args)
Definition: objtuple.c:206
#define mp_const_empty_tuple
Definition: obj.h:618
const mp_obj_type_t mp_type_slice
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf)
Definition: runtime.c:1120
mp_uint_t start
Definition: obj.h:839
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(tuple_index_obj, 2, 4, tuple_index)
struct _mp_obj_tuple_it_t mp_obj_tuple_it_t
qstr name
Definition: obj.h:478
#define MP_OBJ_STOP_ITERATION
Definition: obj.h:74
uint64_t mp_obj_t
Definition: obj.h:39
mp_obj_t mp_iternext(mp_obj_t o_in)
Definition: runtime.c:1186
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args)
Definition: sequence.c:243
mp_obj_t mp_obj_tuple_unary_op(mp_unary_op_t op, mp_obj_t self_in)
Definition: objtuple.c:122
#define m_new(type, num)
Definition: misc.h:57
#define mp_const_false
Definition: obj.h:615
#define mp_seq_cat(dest, src1, len1, src2, len2, item_t)
Definition: obj.h:849
const mp_obj_type_t mp_type_tuple
Definition: objtuple.c:220