blob: 32db640dc1222720a9fa187b74ef8f04dc831586 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
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
Paul Sokolovsky439542f2014-01-21 00:19:19 +020028#include <string.h>
Paul Sokolovsky439542f2014-01-21 00:19:19 +020029
Damien George51dfcb42015-01-01 20:27:54 +000030#include "py/nlr.h"
31#include "py/obj.h"
32#include "py/runtime0.h"
33#include "py/runtime.h"
Paul Sokolovsky439542f2014-01-21 00:19:19 +020034
35// Helpers for sequence types
36
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020037#define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
38
Paul Sokolovsky439542f2014-01-21 00:19:19 +020039// Implements backend of sequence * integer operation. Assumes elements are
40// memory-adjacent in sequence.
Damien George507119f2017-03-23 16:23:20 +110041void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest) {
42 for (size_t i = 0; i < times; i++) {
43 size_t copy_sz = item_sz * len;
Paul Sokolovsky439542f2014-01-21 00:19:19 +020044 memcpy(dest, items, copy_sz);
45 dest = (char*)dest + copy_sz;
46 }
47}
Paul Sokolovsky7364af22014-02-02 02:38:22 +020048
Damien Georgec49ddb92014-06-01 13:49:35 +010049#if MICROPY_PY_BUILTINS_SLICE
50
Damien George40f3c022014-07-03 13:25:24 +010051bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030052 mp_obj_t ostart, ostop, ostep;
Damien George40f3c022014-07-03 13:25:24 +010053 mp_int_t start, stop;
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030054 mp_obj_slice_get(slice, &ostart, &ostop, &ostep);
Paul Sokolovsky7364af22014-02-02 02:38:22 +020055
Fabio Utzig8908e502016-10-30 14:25:04 -020056 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
57 indexes->step = mp_obj_get_int(ostep);
58 if (indexes->step == 0) {
59 mp_raise_ValueError("slice step cannot be zero");
60 }
61 } else {
62 indexes->step = 1;
63 }
64
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030065 if (ostart == mp_const_none) {
Fabio Utzig8908e502016-10-30 14:25:04 -020066 if (indexes->step > 0) {
67 start = 0;
68 } else {
69 start = len - 1;
70 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030071 } else {
Damien George095e43a2016-08-15 23:26:34 +100072 start = mp_obj_get_int(ostart);
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030073 }
74 if (ostop == mp_const_none) {
Fabio Utzig8908e502016-10-30 14:25:04 -020075 if (indexes->step > 0) {
76 stop = len;
77 } else {
78 stop = 0;
79 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030080 } else {
Damien George095e43a2016-08-15 23:26:34 +100081 stop = mp_obj_get_int(ostop);
Fabio Utzig8908e502016-10-30 14:25:04 -020082 if (stop >= 0 && indexes->step < 0) {
83 stop += 1;
84 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030085 }
86
Paul Sokolovsky7364af22014-02-02 02:38:22 +020087 // Unlike subscription, out-of-bounds slice indexes are never error
88 if (start < 0) {
89 start = len + start;
90 if (start < 0) {
Damien Georgeeb4c37f2017-05-17 16:33:07 +100091 if (indexes->step < 0) {
92 start = -1;
93 } else {
94 start = 0;
95 }
Paul Sokolovsky7364af22014-02-02 02:38:22 +020096 }
Fabio Utzig8908e502016-10-30 14:25:04 -020097 } else if (indexes->step > 0 && (mp_uint_t)start > len) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020098 start = len;
Damien Georgeeb4c37f2017-05-17 16:33:07 +100099 } else if (indexes->step < 0 && (mp_uint_t)start >= len) {
Fabio Utzig8908e502016-10-30 14:25:04 -0200100 start = len - 1;
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200101 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +0300102 if (stop < 0) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200103 stop = len + stop;
Damien Georgeeb4c37f2017-05-17 16:33:07 +1000104 if (stop < 0) {
105 stop = -1;
106 }
Fabio Utzig8908e502016-10-30 14:25:04 -0200107 if (indexes->step < 0) {
108 stop += 1;
109 }
Damien George963a5a32015-01-16 17:47:07 +0000110 } else if ((mp_uint_t)stop > len) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200111 stop = len;
112 }
Paul Sokolovsky69d081a2014-05-25 02:29:40 +0300113
114 // CPython returns empty sequence in such case, or point for assignment is at start
Fabio Utzig8908e502016-10-30 14:25:04 -0200115 if (indexes->step > 0 && start > stop) {
Paul Sokolovsky69d081a2014-05-25 02:29:40 +0300116 stop = start;
Fabio Utzig8908e502016-10-30 14:25:04 -0200117 } else if (indexes->step < 0 && start < stop) {
118 stop = start + 1;
Paul Sokolovsky69d081a2014-05-25 02:29:40 +0300119 }
120
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300121 indexes->start = start;
122 indexes->stop = stop;
123
Fabio Utzig8908e502016-10-30 14:25:04 -0200124 return indexes->step == 1;
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200125}
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200126
Damien Georgec49ddb92014-06-01 13:49:35 +0100127#endif
128
Damien George507119f2017-03-23 16:23:20 +1100129mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000130 (void)len; // TODO can we remove len from the arg list?
131
Damien George40f3c022014-07-03 13:25:24 +0100132 mp_int_t start = indexes->start, stop = indexes->stop;
133 mp_int_t step = indexes->step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300134
135 mp_obj_t res = mp_obj_new_list(0, NULL);
136
137 if (step < 0) {
Fabio Utzig8908e502016-10-30 14:25:04 -0200138 while (start >= stop) {
139 mp_obj_list_append(res, seq[start]);
140 start += step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300141 }
142 } else {
143 while (start < stop) {
144 mp_obj_list_append(res, seq[start]);
145 start += step;
146 }
147 }
148 return res;
149}
150
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200151// Special-case comparison function for sequences of bytes
Damien Georged17926d2014-03-30 13:35:08 +0100152// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George507119f2017-03-23 16:23:20 +1100153bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2) {
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300154 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
155 return false;
156 }
157
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200158 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100159 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200160 SWAP(const byte*, data1, data2);
Damien George507119f2017-03-23 16:23:20 +1100161 SWAP(size_t, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100162 if (op == MP_BINARY_OP_LESS) {
163 op = MP_BINARY_OP_MORE;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200164 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100165 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200166 }
167 }
Damien George507119f2017-03-23 16:23:20 +1100168 size_t min_len = len1 < len2 ? len1 : len2;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200169 int res = memcmp(data1, data2, min_len);
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300170 if (op == MP_BINARY_OP_EQUAL) {
171 // If we are checking for equality, here're the answer
172 return res == 0;
173 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200174 if (res < 0) {
175 return false;
176 }
177 if (res > 0) {
178 return true;
179 }
180
181 // If we had tie in the last element...
182 // ... and we have lists of different lengths...
183 if (len1 != len2) {
184 if (len1 < len2) {
185 // ... then longer list length wins (we deal only with >)
186 return false;
187 }
Damien Georged17926d2014-03-30 13:35:08 +0100188 } else if (op == MP_BINARY_OP_MORE) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200189 // Otherwise, if we have strict relation, equality means failure
190 return false;
191 }
192 return true;
193}
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200194
195// Special-case comparison function for sequences of mp_obj_t
Damien Georged17926d2014-03-30 13:35:08 +0100196// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George507119f2017-03-23 16:23:20 +1100197bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp_obj_t *items2, size_t len2) {
Damien Georged17926d2014-03-30 13:35:08 +0100198 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200199 return false;
200 }
201
202 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100203 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200204 SWAP(const mp_obj_t *, items1, items2);
Damien George507119f2017-03-23 16:23:20 +1100205 SWAP(size_t, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100206 if (op == MP_BINARY_OP_LESS) {
207 op = MP_BINARY_OP_MORE;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200208 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100209 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200210 }
211 }
212
Damien George507119f2017-03-23 16:23:20 +1100213 size_t len = len1 < len2 ? len1 : len2;
214 for (size_t i = 0; i < len; i++) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300215 // If current elements equal, can't decide anything - go on
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300216 if (mp_obj_equal(items1[i], items2[i])) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300217 continue;
218 }
219
220 // Othewise, if they are not equal, we can have final decision based on them
221 if (op == MP_BINARY_OP_EQUAL) {
222 // In particular, if we are checking for equality, here're the answer
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200223 return false;
224 }
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300225
226 // Otherwise, application of relation op gives the answer
227 return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200228 }
229
230 // If we had tie in the last element...
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300231 // ... and we have lists of different lengths...
232 if (len1 != len2) {
233 if (len1 < len2) {
234 // ... then longer list length wins (we deal only with >)
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200235 return false;
236 }
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300237 } else if (op == MP_BINARY_OP_MORE) {
238 // Otherwise, if we have strict relation, sequence equality means failure
239 return false;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200240 }
241
242 return true;
243}
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200244
245// Special-case of index() which searches for mp_obj_t
Damien George507119f2017-03-23 16:23:20 +1100246mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args) {
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200247 mp_obj_type_t *type = mp_obj_get_type(args[0]);
Damien George999cedb2015-11-27 17:01:44 +0000248 mp_obj_t value = args[1];
Damien Georgec88cfe12017-03-23 16:17:40 +1100249 size_t start = 0;
250 size_t stop = len;
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200251
252 if (n_args >= 3) {
xbe9e1e8cd2014-03-12 22:57:16 -0700253 start = mp_get_index(type, len, args[2], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200254 if (n_args >= 4) {
xbe9e1e8cd2014-03-12 22:57:16 -0700255 stop = mp_get_index(type, len, args[3], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200256 }
257 }
258
Damien Georgec88cfe12017-03-23 16:17:40 +1100259 for (size_t i = start; i < stop; i++) {
Paul Sokolovsky624eff62014-02-10 06:42:20 +0200260 if (mp_obj_equal(items[i], value)) {
261 // Common sense says this cannot overflow small int
262 return MP_OBJ_NEW_SMALL_INT(i);
263 }
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200264 }
265
Damien George94c41bb2017-03-28 22:37:26 +1100266 mp_raise_ValueError("object not in sequence");
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200267}
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200268
Damien George507119f2017-03-23 16:23:20 +1100269mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value) {
270 size_t count = 0;
271 for (size_t i = 0; i < len; i++) {
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200272 if (mp_obj_equal(items[i], value)) {
273 count++;
274 }
275 }
276
277 // Common sense says this cannot overflow small int
278 return MP_OBJ_NEW_SMALL_INT(count);
279}