blob: 6f715ff79502149f8afbd2dbd9d56f01380bded9 [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 Georged182b982014-08-30 14:19:41 +010041void mp_seq_multiply(const void *items, mp_uint_t item_sz, mp_uint_t len, mp_uint_t times, void *dest) {
Damien George42f3de92014-10-03 17:44:14 +000042 for (mp_uint_t i = 0; i < times; i++) {
Paul Sokolovsky439542f2014-01-21 00:19:19 +020043 uint copy_sz = item_sz * len;
44 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
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030056 if (ostart == mp_const_none) {
57 start = 0;
58 } else {
59 start = MP_OBJ_SMALL_INT_VALUE(ostart);
60 }
61 if (ostop == mp_const_none) {
62 stop = len;
63 } else {
64 stop = MP_OBJ_SMALL_INT_VALUE(ostop);
65 }
66
Paul Sokolovsky7364af22014-02-02 02:38:22 +020067 // Unlike subscription, out-of-bounds slice indexes are never error
68 if (start < 0) {
69 start = len + start;
70 if (start < 0) {
71 start = 0;
72 }
Damien George963a5a32015-01-16 17:47:07 +000073 } else if ((mp_uint_t)start > len) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020074 start = len;
75 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030076 if (stop < 0) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020077 stop = len + stop;
Damien George963a5a32015-01-16 17:47:07 +000078 } else if ((mp_uint_t)stop > len) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020079 stop = len;
80 }
Paul Sokolovsky69d081a2014-05-25 02:29:40 +030081
82 // CPython returns empty sequence in such case, or point for assignment is at start
83 if (start > stop) {
84 stop = start;
85 }
86
Paul Sokolovskyde4b9322014-05-25 21:21:57 +030087 indexes->start = start;
88 indexes->stop = stop;
89
90 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +030091 indexes->step = MP_OBJ_SMALL_INT_VALUE(ostep);
92 return false;
93 }
94 indexes->step = 1;
Paul Sokolovsky7364af22014-02-02 02:38:22 +020095 return true;
96}
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020097
Damien Georgec49ddb92014-06-01 13:49:35 +010098#endif
99
Damien Georged182b982014-08-30 14:19:41 +0100100mp_obj_t mp_seq_extract_slice(mp_uint_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000101 (void)len; // TODO can we remove len from the arg list?
102
Damien George40f3c022014-07-03 13:25:24 +0100103 mp_int_t start = indexes->start, stop = indexes->stop;
104 mp_int_t step = indexes->step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300105
106 mp_obj_t res = mp_obj_new_list(0, NULL);
107
108 if (step < 0) {
109 stop--;
110 while (start <= stop) {
111 mp_obj_list_append(res, seq[stop]);
112 stop += step;
113 }
114 } else {
115 while (start < stop) {
116 mp_obj_list_append(res, seq[start]);
117 start += step;
118 }
119 }
120 return res;
121}
122
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200123// Special-case comparison function for sequences of bytes
Damien Georged17926d2014-03-30 13:35:08 +0100124// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George4d917232014-08-30 14:28:06 +0100125bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, mp_uint_t len1, const byte *data2, mp_uint_t len2) {
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300126 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
127 return false;
128 }
129
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200130 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100131 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200132 SWAP(const byte*, data1, data2);
133 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100134 if (op == MP_BINARY_OP_LESS) {
135 op = MP_BINARY_OP_MORE;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200136 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100137 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200138 }
139 }
140 uint min_len = len1 < len2 ? len1 : len2;
141 int res = memcmp(data1, data2, min_len);
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300142 if (op == MP_BINARY_OP_EQUAL) {
143 // If we are checking for equality, here're the answer
144 return res == 0;
145 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200146 if (res < 0) {
147 return false;
148 }
149 if (res > 0) {
150 return true;
151 }
152
153 // If we had tie in the last element...
154 // ... and we have lists of different lengths...
155 if (len1 != len2) {
156 if (len1 < len2) {
157 // ... then longer list length wins (we deal only with >)
158 return false;
159 }
Damien Georged17926d2014-03-30 13:35:08 +0100160 } else if (op == MP_BINARY_OP_MORE) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200161 // Otherwise, if we have strict relation, equality means failure
162 return false;
163 }
164 return true;
165}
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200166
167// Special-case comparison function for sequences of mp_obj_t
Damien Georged17926d2014-03-30 13:35:08 +0100168// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George4d917232014-08-30 14:28:06 +0100169bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, mp_uint_t len1, const mp_obj_t *items2, mp_uint_t len2) {
Damien Georged17926d2014-03-30 13:35:08 +0100170 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200171 return false;
172 }
173
174 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100175 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200176 SWAP(const mp_obj_t *, items1, items2);
177 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100178 if (op == MP_BINARY_OP_LESS) {
179 op = MP_BINARY_OP_MORE;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200180 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100181 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200182 }
183 }
184
Damien George42f3de92014-10-03 17:44:14 +0000185 mp_uint_t len = len1 < len2 ? len1 : len2;
186 for (mp_uint_t i = 0; i < len; i++) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300187 // If current elements equal, can't decide anything - go on
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300188 if (mp_obj_equal(items1[i], items2[i])) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300189 continue;
190 }
191
192 // Othewise, if they are not equal, we can have final decision based on them
193 if (op == MP_BINARY_OP_EQUAL) {
194 // In particular, if we are checking for equality, here're the answer
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200195 return false;
196 }
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300197
198 // Otherwise, application of relation op gives the answer
199 return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200200 }
201
202 // If we had tie in the last element...
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300203 // ... and we have lists of different lengths...
204 if (len1 != len2) {
205 if (len1 < len2) {
206 // ... then longer list length wins (we deal only with >)
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200207 return false;
208 }
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300209 } else if (op == MP_BINARY_OP_MORE) {
210 // Otherwise, if we have strict relation, sequence equality means failure
211 return false;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200212 }
213
214 return true;
215}
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200216
217// Special-case of index() which searches for mp_obj_t
Damien Georged182b982014-08-30 14:19:41 +0100218mp_obj_t mp_seq_index_obj(const mp_obj_t *items, mp_uint_t len, mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200219 mp_obj_type_t *type = mp_obj_get_type(args[0]);
Damien George999cedb2015-11-27 17:01:44 +0000220 mp_obj_t value = args[1];
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200221 uint start = 0;
222 uint stop = len;
223
224 if (n_args >= 3) {
xbe9e1e8cd2014-03-12 22:57:16 -0700225 start = mp_get_index(type, len, args[2], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200226 if (n_args >= 4) {
xbe9e1e8cd2014-03-12 22:57:16 -0700227 stop = mp_get_index(type, len, args[3], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200228 }
229 }
230
Damien George40f3c022014-07-03 13:25:24 +0100231 for (mp_uint_t i = start; i < stop; i++) {
Paul Sokolovsky624eff62014-02-10 06:42:20 +0200232 if (mp_obj_equal(items[i], value)) {
233 // Common sense says this cannot overflow small int
234 return MP_OBJ_NEW_SMALL_INT(i);
235 }
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200236 }
237
Damien Georgeea13f402014-04-05 18:32:08 +0100238 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "object not in sequence"));
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200239}
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200240
Damien Georged182b982014-08-30 14:19:41 +0100241mp_obj_t mp_seq_count_obj(const mp_obj_t *items, mp_uint_t len, mp_obj_t value) {
Damien George40f3c022014-07-03 13:25:24 +0100242 mp_uint_t count = 0;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200243 for (uint i = 0; i < len; i++) {
244 if (mp_obj_equal(items[i], value)) {
245 count++;
246 }
247 }
248
249 // Common sense says this cannot overflow small int
250 return MP_OBJ_NEW_SMALL_INT(count);
251}