blob: 11761f65180327ae765f43c4e42316505516420a [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) {
91 start = 0;
92 }
Fabio Utzig8908e502016-10-30 14:25:04 -020093 } else if (indexes->step > 0 && (mp_uint_t)start > len) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020094 start = len;
Fabio Utzig8908e502016-10-30 14:25:04 -020095 } else if (indexes->step < 0 && (mp_uint_t)start > len - 1) {
96 start = len - 1;
Paul Sokolovsky7364af22014-02-02 02:38:22 +020097 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030098 if (stop < 0) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020099 stop = len + stop;
Fabio Utzig8908e502016-10-30 14:25:04 -0200100 if (indexes->step < 0) {
101 stop += 1;
102 }
Damien George963a5a32015-01-16 17:47:07 +0000103 } else if ((mp_uint_t)stop > len) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200104 stop = len;
105 }
Paul Sokolovsky69d081a2014-05-25 02:29:40 +0300106
107 // CPython returns empty sequence in such case, or point for assignment is at start
Fabio Utzig8908e502016-10-30 14:25:04 -0200108 if (indexes->step > 0 && start > stop) {
Paul Sokolovsky69d081a2014-05-25 02:29:40 +0300109 stop = start;
Fabio Utzig8908e502016-10-30 14:25:04 -0200110 } else if (indexes->step < 0 && start < stop) {
111 stop = start + 1;
Paul Sokolovsky69d081a2014-05-25 02:29:40 +0300112 }
113
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300114 indexes->start = start;
115 indexes->stop = stop;
116
Fabio Utzig8908e502016-10-30 14:25:04 -0200117 return indexes->step == 1;
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200118}
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200119
Damien Georgec49ddb92014-06-01 13:49:35 +0100120#endif
121
Damien George507119f2017-03-23 16:23:20 +1100122mp_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 +0000123 (void)len; // TODO can we remove len from the arg list?
124
Damien George40f3c022014-07-03 13:25:24 +0100125 mp_int_t start = indexes->start, stop = indexes->stop;
126 mp_int_t step = indexes->step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300127
128 mp_obj_t res = mp_obj_new_list(0, NULL);
129
130 if (step < 0) {
Fabio Utzig8908e502016-10-30 14:25:04 -0200131 while (start >= stop) {
132 mp_obj_list_append(res, seq[start]);
133 start += step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300134 }
135 } else {
136 while (start < stop) {
137 mp_obj_list_append(res, seq[start]);
138 start += step;
139 }
140 }
141 return res;
142}
143
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200144// Special-case comparison function for sequences of bytes
Damien Georged17926d2014-03-30 13:35:08 +0100145// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George507119f2017-03-23 16:23:20 +1100146bool 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 +0300147 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
148 return false;
149 }
150
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200151 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100152 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200153 SWAP(const byte*, data1, data2);
Damien George507119f2017-03-23 16:23:20 +1100154 SWAP(size_t, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100155 if (op == MP_BINARY_OP_LESS) {
156 op = MP_BINARY_OP_MORE;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200157 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100158 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200159 }
160 }
Damien George507119f2017-03-23 16:23:20 +1100161 size_t min_len = len1 < len2 ? len1 : len2;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200162 int res = memcmp(data1, data2, min_len);
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300163 if (op == MP_BINARY_OP_EQUAL) {
164 // If we are checking for equality, here're the answer
165 return res == 0;
166 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200167 if (res < 0) {
168 return false;
169 }
170 if (res > 0) {
171 return true;
172 }
173
174 // If we had tie in the last element...
175 // ... and we have lists of different lengths...
176 if (len1 != len2) {
177 if (len1 < len2) {
178 // ... then longer list length wins (we deal only with >)
179 return false;
180 }
Damien Georged17926d2014-03-30 13:35:08 +0100181 } else if (op == MP_BINARY_OP_MORE) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200182 // Otherwise, if we have strict relation, equality means failure
183 return false;
184 }
185 return true;
186}
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200187
188// Special-case comparison function for sequences of mp_obj_t
Damien Georged17926d2014-03-30 13:35:08 +0100189// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George507119f2017-03-23 16:23:20 +1100190bool 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 +0100191 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200192 return false;
193 }
194
195 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100196 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200197 SWAP(const mp_obj_t *, items1, items2);
Damien George507119f2017-03-23 16:23:20 +1100198 SWAP(size_t, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100199 if (op == MP_BINARY_OP_LESS) {
200 op = MP_BINARY_OP_MORE;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200201 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100202 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200203 }
204 }
205
Damien George507119f2017-03-23 16:23:20 +1100206 size_t len = len1 < len2 ? len1 : len2;
207 for (size_t i = 0; i < len; i++) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300208 // If current elements equal, can't decide anything - go on
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300209 if (mp_obj_equal(items1[i], items2[i])) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300210 continue;
211 }
212
213 // Othewise, if they are not equal, we can have final decision based on them
214 if (op == MP_BINARY_OP_EQUAL) {
215 // In particular, if we are checking for equality, here're the answer
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200216 return false;
217 }
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300218
219 // Otherwise, application of relation op gives the answer
220 return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200221 }
222
223 // If we had tie in the last element...
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300224 // ... and we have lists of different lengths...
225 if (len1 != len2) {
226 if (len1 < len2) {
227 // ... then longer list length wins (we deal only with >)
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200228 return false;
229 }
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300230 } else if (op == MP_BINARY_OP_MORE) {
231 // Otherwise, if we have strict relation, sequence equality means failure
232 return false;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200233 }
234
235 return true;
236}
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200237
238// Special-case of index() which searches for mp_obj_t
Damien George507119f2017-03-23 16:23:20 +1100239mp_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 +0200240 mp_obj_type_t *type = mp_obj_get_type(args[0]);
Damien George999cedb2015-11-27 17:01:44 +0000241 mp_obj_t value = args[1];
Damien Georgec88cfe12017-03-23 16:17:40 +1100242 size_t start = 0;
243 size_t stop = len;
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200244
245 if (n_args >= 3) {
xbe9e1e8cd2014-03-12 22:57:16 -0700246 start = mp_get_index(type, len, args[2], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200247 if (n_args >= 4) {
xbe9e1e8cd2014-03-12 22:57:16 -0700248 stop = mp_get_index(type, len, args[3], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200249 }
250 }
251
Damien Georgec88cfe12017-03-23 16:17:40 +1100252 for (size_t i = start; i < stop; i++) {
Paul Sokolovsky624eff62014-02-10 06:42:20 +0200253 if (mp_obj_equal(items[i], value)) {
254 // Common sense says this cannot overflow small int
255 return MP_OBJ_NEW_SMALL_INT(i);
256 }
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200257 }
258
Damien George7d0d7212016-10-17 12:17:37 +1100259 mp_raise_msg(&mp_type_ValueError, "object not in sequence");
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200260}
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200261
Damien George507119f2017-03-23 16:23:20 +1100262mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value) {
263 size_t count = 0;
264 for (size_t i = 0; i < len; i++) {
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200265 if (mp_obj_equal(items[i], value)) {
266 count++;
267 }
268 }
269
270 // Common sense says this cannot overflow small int
271 return MP_OBJ_NEW_SMALL_INT(count);
272}