Numworks Epsilon  1.4.1
Graphing Calculator Operating System
sequence.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  * Copyright (c) 2014 Paul Sokolovsky
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 
28 #include <string.h>
29 
30 #include "py/runtime.h"
31 
32 // Helpers for sequence types
33 
34 #define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
35 
36 // Implements backend of sequence * integer operation. Assumes elements are
37 // memory-adjacent in sequence.
38 void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest) {
39  for (size_t i = 0; i < times; i++) {
40  size_t copy_sz = item_sz * len;
41  memcpy(dest, items, copy_sz);
42  dest = (char*)dest + copy_sz;
43  }
44 }
45 
46 #if MICROPY_PY_BUILTINS_SLICE
47 
48 bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
49  mp_obj_t ostart, ostop, ostep;
50  mp_int_t start, stop;
51  mp_obj_slice_get(slice, &ostart, &ostop, &ostep);
52 
53  if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
54  indexes->step = mp_obj_get_int(ostep);
55  if (indexes->step == 0) {
56  mp_raise_ValueError("slice step cannot be zero");
57  }
58  } else {
59  indexes->step = 1;
60  }
61 
62  if (ostart == mp_const_none) {
63  if (indexes->step > 0) {
64  start = 0;
65  } else {
66  start = len - 1;
67  }
68  } else {
69  start = mp_obj_get_int(ostart);
70  }
71  if (ostop == mp_const_none) {
72  if (indexes->step > 0) {
73  stop = len;
74  } else {
75  stop = 0;
76  }
77  } else {
78  stop = mp_obj_get_int(ostop);
79  if (stop >= 0 && indexes->step < 0) {
80  stop += 1;
81  }
82  }
83 
84  // Unlike subscription, out-of-bounds slice indexes are never error
85  if (start < 0) {
86  start = len + start;
87  if (start < 0) {
88  if (indexes->step < 0) {
89  start = -1;
90  } else {
91  start = 0;
92  }
93  }
94  } else if (indexes->step > 0 && (mp_uint_t)start > len) {
95  start = len;
96  } else if (indexes->step < 0 && (mp_uint_t)start >= len) {
97  start = len - 1;
98  }
99  if (stop < 0) {
100  stop = len + stop;
101  if (stop < 0) {
102  stop = -1;
103  }
104  if (indexes->step < 0) {
105  stop += 1;
106  }
107  } else if ((mp_uint_t)stop > len) {
108  stop = len;
109  }
110 
111  // CPython returns empty sequence in such case, or point for assignment is at start
112  if (indexes->step > 0 && start > stop) {
113  stop = start;
114  } else if (indexes->step < 0 && start < stop) {
115  stop = start + 1;
116  }
117 
118  indexes->start = start;
119  indexes->stop = stop;
120 
121  return indexes->step == 1;
122 }
123 
124 #endif
125 
126 mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
127  (void)len; // TODO can we remove len from the arg list?
128 
129  mp_int_t start = indexes->start, stop = indexes->stop;
130  mp_int_t step = indexes->step;
131 
132  mp_obj_t res = mp_obj_new_list(0, NULL);
133 
134  if (step < 0) {
135  while (start >= stop) {
136  mp_obj_list_append(res, seq[start]);
137  start += step;
138  }
139  } else {
140  while (start < stop) {
141  mp_obj_list_append(res, seq[start]);
142  start += step;
143  }
144  }
145  return res;
146 }
147 
148 // Special-case comparison function for sequences of bytes
149 // Don't pass MP_BINARY_OP_NOT_EQUAL here
150 bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2) {
151  if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
152  return false;
153  }
154 
155  // Let's deal only with > & >=
156  if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
157  SWAP(const byte*, data1, data2);
158  SWAP(size_t, len1, len2);
159  if (op == MP_BINARY_OP_LESS) {
160  op = MP_BINARY_OP_MORE;
161  } else {
163  }
164  }
165  size_t min_len = len1 < len2 ? len1 : len2;
166  int res = memcmp(data1, data2, min_len);
167  if (op == MP_BINARY_OP_EQUAL) {
168  // If we are checking for equality, here're the answer
169  return res == 0;
170  }
171  if (res < 0) {
172  return false;
173  }
174  if (res > 0) {
175  return true;
176  }
177 
178  // If we had tie in the last element...
179  // ... and we have lists of different lengths...
180  if (len1 != len2) {
181  if (len1 < len2) {
182  // ... then longer list length wins (we deal only with >)
183  return false;
184  }
185  } else if (op == MP_BINARY_OP_MORE) {
186  // Otherwise, if we have strict relation, equality means failure
187  return false;
188  }
189  return true;
190 }
191 
192 // Special-case comparison function for sequences of mp_obj_t
193 // Don't pass MP_BINARY_OP_NOT_EQUAL here
194 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) {
195  if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
196  return false;
197  }
198 
199  // Let's deal only with > & >=
200  if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
201  SWAP(const mp_obj_t *, items1, items2);
202  SWAP(size_t, len1, len2);
203  if (op == MP_BINARY_OP_LESS) {
204  op = MP_BINARY_OP_MORE;
205  } else {
207  }
208  }
209 
210  size_t len = len1 < len2 ? len1 : len2;
211  for (size_t i = 0; i < len; i++) {
212  // If current elements equal, can't decide anything - go on
213  if (mp_obj_equal(items1[i], items2[i])) {
214  continue;
215  }
216 
217  // Othewise, if they are not equal, we can have final decision based on them
218  if (op == MP_BINARY_OP_EQUAL) {
219  // In particular, if we are checking for equality, here're the answer
220  return false;
221  }
222 
223  // Otherwise, application of relation op gives the answer
224  return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
225  }
226 
227  // If we had tie in the last element...
228  // ... and we have lists of different lengths...
229  if (len1 != len2) {
230  if (len1 < len2) {
231  // ... then longer list length wins (we deal only with >)
232  return false;
233  }
234  } else if (op == MP_BINARY_OP_MORE) {
235  // Otherwise, if we have strict relation, sequence equality means failure
236  return false;
237  }
238 
239  return true;
240 }
241 
242 // Special-case of index() which searches for mp_obj_t
243 mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args) {
244  mp_obj_type_t *type = mp_obj_get_type(args[0]);
245  mp_obj_t value = args[1];
246  size_t start = 0;
247  size_t stop = len;
248 
249  if (n_args >= 3) {
250  start = mp_get_index(type, len, args[2], true);
251  if (n_args >= 4) {
252  stop = mp_get_index(type, len, args[3], true);
253  }
254  }
255 
256  for (size_t i = start; i < stop; i++) {
257  if (mp_obj_equal(items[i], value)) {
258  // Common sense says this cannot overflow small int
259  return MP_OBJ_NEW_SMALL_INT(i);
260  }
261  }
262 
263  mp_raise_ValueError("object not in sequence");
264 }
265 
266 mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value) {
267  size_t count = 0;
268  for (size_t i = 0; i < len; i++) {
269  if (mp_obj_equal(items[i], value)) {
270  count++;
271  }
272  }
273 
274  // Common sense says this cannot overflow small int
275  return MP_OBJ_NEW_SMALL_INT(count);
276 }
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
mp_uint_t stop
Definition: obj.h:840
#define mp_const_none
Definition: obj.h:614
mp_obj_type_t * mp_obj_get_type(mp_const_obj_t o_in)
Definition: obj.c:40
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2)
Definition: obj.c:162
#define mp_const_true
Definition: obj.h:616
mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items)
Definition: objlist.c:470
mp_int_t mp_obj_get_int(mp_const_obj_t arg)
Definition: obj.c:225
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg)
Definition: objlist.c:234
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 MP_OBJ_NEW_SMALL_INT(small_int)
Definition: obj.h:87
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
mp_int_t step
Definition: obj.h:841
#define NULL
Definition: stddef.h:4
#define SWAP(type, var1, var2)
Definition: sequence.c:34
mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
Definition: runtime.c:277
mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes)
Definition: sequence.c:126
args
Definition: i18n.py:175
void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest)
Definition: sequence.c:38
bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2)
Definition: sequence.c:150
unsigned char byte
Definition: misc.h:37
void start()
Definition: rt0.cpp:31
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value)
Definition: sequence.c:266
NORETURN void mp_raise_ValueError(const char *msg)
Definition: runtime.c:1456
mp_uint_t start
Definition: obj.h:839
LIBA_BEGIN_DECLS int memcmp(const void *s1, const void *s2, size_t n)
Definition: memcmp.c:3
void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step)
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
uint64_t mp_obj_t
Definition: obj.h:39
void * memcpy(void *dst, const void *src, size_t n)
Definition: memcpy.c:3