Numworks Epsilon  1.4.1
Graphing Calculator Operating System
objrange.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 <stdlib.h>
28 
29 #include "py/runtime.h"
30 
31 /******************************************************************************/
32 /* range iterator */
33 
34 typedef struct _mp_obj_range_it_t {
36  // TODO make these values generic objects or something
41 
44  if ((o->step > 0 && o->cur < o->stop) || (o->step < 0 && o->cur > o->stop)) {
45  mp_obj_t o_out = MP_OBJ_NEW_SMALL_INT(o->cur);
46  o->cur += o->step;
47  return o_out;
48  } else {
49  return MP_OBJ_STOP_ITERATION;
50  }
51 }
52 
54  { &mp_type_type },
55  .name = MP_QSTR_iterator,
56  .getiter = mp_identity_getiter,
57  .iternext = range_it_iternext,
58 };
59 
61  assert(sizeof(mp_obj_range_it_t) <= sizeof(mp_obj_iter_buf_t));
62  mp_obj_range_it_t *o = (mp_obj_range_it_t*)iter_buf;
63  o->base.type = &range_it_type;
64  o->cur = cur;
65  o->stop = stop;
66  o->step = step;
67  return MP_OBJ_FROM_PTR(o);
68 }
69 
70 /******************************************************************************/
71 /* range */
72 
73 typedef struct _mp_obj_range_t {
75  // TODO make these values generic objects or something
80 
81 STATIC void range_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
82  (void)kind;
83  mp_obj_range_t *self = MP_OBJ_TO_PTR(self_in);
84  mp_printf(print, "range(" INT_FMT ", " INT_FMT "", self->start, self->stop);
85  if (self->step == 1) {
86  mp_print_str(print, ")");
87  } else {
88  mp_printf(print, ", " INT_FMT ")", self->step);
89  }
90 }
91 
92 STATIC mp_obj_t range_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
93  mp_arg_check_num(n_args, n_kw, 1, 3, false);
94 
96  o->base.type = type;
97  o->start = 0;
98  o->step = 1;
99 
100  if (n_args == 1) {
101  o->stop = mp_obj_get_int(args[0]);
102  } else {
103  o->start = mp_obj_get_int(args[0]);
104  o->stop = mp_obj_get_int(args[1]);
105  if (n_args == 3) {
106  o->step = mp_obj_get_int(args[2]);
107  if (o->step == 0) {
108  mp_raise_ValueError("zero step");
109  }
110  }
111  }
112 
113  return MP_OBJ_FROM_PTR(o);
114 }
115 
117  // When computing length, need to take into account step!=1 and step<0.
118  mp_int_t len = self->stop - self->start + self->step;
119  if (self->step > 0) {
120  len -= 1;
121  } else {
122  len += 1;
123  }
124  len = len / self->step;
125  if (len < 0) {
126  len = 0;
127  }
128  return len;
129 }
130 
132  mp_obj_range_t *self = MP_OBJ_TO_PTR(self_in);
133  mp_int_t len = range_len(self);
134  switch (op) {
135  case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len > 0);
136  case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
137  default: return MP_OBJ_NULL; // op not supported
138  }
139 }
140 
142  if (value == MP_OBJ_SENTINEL) {
143  // load
144  mp_obj_range_t *self = MP_OBJ_TO_PTR(self_in);
145  mp_int_t len = range_len(self);
146 #if MICROPY_PY_BUILTINS_SLICE
147  if (MP_OBJ_IS_TYPE(index, &mp_type_slice)) {
148  mp_bound_slice_t slice;
149  mp_seq_get_fast_slice_indexes(len, index, &slice);
151  o->base.type = &mp_type_range;
152  o->start = self->start + slice.start * self->step;
153  o->stop = self->start + slice.stop * self->step;
154  o->step = slice.step * self->step;
155  if (slice.step < 0) {
156  // Negative slice steps have inclusive stop, so adjust for exclusive
157  o->stop -= self->step;
158  }
159  return MP_OBJ_FROM_PTR(o);
160  }
161 #endif
162  size_t index_val = mp_get_index(self->base.type, len, index, false);
163  return MP_OBJ_NEW_SMALL_INT(self->start + index_val * self->step);
164  } else {
165  return MP_OBJ_NULL; // op not supported
166  }
167 }
168 
170  mp_obj_range_t *o = MP_OBJ_TO_PTR(o_in);
171  return mp_obj_new_range_iterator(o->start, o->stop, o->step, iter_buf);
172 }
173 
174 
175 #if MICROPY_PY_BUILTINS_RANGE_ATTRS
176 STATIC void range_attr(mp_obj_t o_in, qstr attr, mp_obj_t *dest) {
177  if (dest[0] != MP_OBJ_NULL) {
178  // not load attribute
179  return;
180  }
181  mp_obj_range_t *o = MP_OBJ_TO_PTR(o_in);
182  if (attr == MP_QSTR_start) {
183  dest[0] = mp_obj_new_int(o->start);
184  } else if (attr == MP_QSTR_stop) {
185  dest[0] = mp_obj_new_int(o->stop);
186  } else if (attr == MP_QSTR_step) {
187  dest[0] = mp_obj_new_int(o->step);
188  }
189 }
190 #endif
191 
193  { &mp_type_type },
194  .name = MP_QSTR_range,
195  .print = range_print,
196  .make_new = range_make_new,
197  .unary_op = range_unary_op,
198  .subscr = range_subscr,
199  .getiter = range_getiter,
200 #if MICROPY_PY_BUILTINS_RANGE_ATTRS
201  .attr = range_attr,
202 #endif
203 };
intptr_t mp_int_t
Definition: mpconfigport.h:73
STATIC mp_obj_t range_it_iternext(mp_obj_t o_in)
Definition: objrange.c:42
#define INT_FMT
Definition: mpconfigport.h:72
mp_uint_t stop
Definition: obj.h:840
#define assert(e)
Definition: assert.h:9
STATIC mp_obj_t range_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf)
Definition: objrange.c:169
mp_int_t cur
Definition: objrange.c:37
mp_obj_base_t base
Definition: objrange.c:74
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
STATIC const uint8_t attr[]
Definition: unicode.c:51
STATIC const mp_obj_type_t range_it_type
Definition: objrange.c:53
#define MP_OBJ_SENTINEL
Definition: obj.h:75
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
#define MP_OBJ_FROM_PTR(p)
Definition: obj.h:233
mp_int_t step
Definition: objrange.c:39
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
mp_unary_op_t
Definition: runtime0.h:45
mp_int_t mp_obj_get_int(mp_const_obj_t arg)
Definition: obj.c:225
#define STATIC
Definition: mpconfig.h:1178
STATIC mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value)
Definition: objrange.c:141
STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step, mp_obj_iter_buf_t *iter_buf)
Definition: objrange.c:60
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
mp_print_kind_t
Definition: obj.h:412
#define MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
STATIC mp_obj_t range_unary_op(mp_unary_op_t op, mp_obj_t self_in)
Definition: objrange.c:131
mp_int_t step
Definition: obj.h:841
#define MP_OBJ_NULL
Definition: obj.h:73
mp_obj_base_t base
Definition: objrange.c:35
size_t qstr
Definition: qstr.h:48
args
Definition: i18n.py:175
mp_obj_t mp_obj_new_int(mp_int_t value)
Definition: objint.c:353
struct _mp_obj_range_it_t mp_obj_range_it_t
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
const mp_obj_type_t mp_type_range
Definition: objrange.c:192
NORETURN void mp_raise_ValueError(const char *msg)
Definition: runtime.c:1456
mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf)
Definition: obj.c:507
mp_int_t step
Definition: objrange.c:78
const mp_obj_type_t mp_type_slice
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
struct _mp_obj_range_t mp_obj_range_t
mp_uint_t start
Definition: obj.h:839
STATIC void range_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
Definition: objrange.c:81
mp_int_t stop
Definition: objrange.c:38
mp_int_t start
Definition: objrange.c:76
mp_int_t stop
Definition: objrange.c:77
qstr name
Definition: obj.h:478
STATIC mp_int_t range_len(mp_obj_range_t *self)
Definition: objrange.c:116
#define MP_OBJ_STOP_ITERATION
Definition: obj.h:74
uint64_t mp_obj_t
Definition: obj.h:39
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
STATIC mp_obj_t range_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
Definition: objrange.c:92
#define m_new_obj(type)
Definition: misc.h:60