blob: 0e76ccc027fdf7c13911cc6f16546f98751b2ac2 [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 }
73 } else if (start > len) {
74 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;
Paul Sokolovsky7364af22014-02-02 02:38:22 +020078 } else if (stop > len) {
79 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 George40f3c022014-07-03 13:25:24 +0100101 mp_int_t start = indexes->start, stop = indexes->stop;
102 mp_int_t step = indexes->step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300103
104 mp_obj_t res = mp_obj_new_list(0, NULL);
105
106 if (step < 0) {
107 stop--;
108 while (start <= stop) {
109 mp_obj_list_append(res, seq[stop]);
110 stop += step;
111 }
112 } else {
113 while (start < stop) {
114 mp_obj_list_append(res, seq[start]);
115 start += step;
116 }
117 }
118 return res;
119}
120
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200121// Special-case comparison function for sequences of bytes
Damien Georged17926d2014-03-30 13:35:08 +0100122// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George4d917232014-08-30 14:28:06 +0100123bool 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 +0300124 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
125 return false;
126 }
127
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200128 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100129 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200130 SWAP(const byte*, data1, data2);
131 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100132 if (op == MP_BINARY_OP_LESS) {
133 op = MP_BINARY_OP_MORE;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200134 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100135 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200136 }
137 }
138 uint min_len = len1 < len2 ? len1 : len2;
139 int res = memcmp(data1, data2, min_len);
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300140 if (op == MP_BINARY_OP_EQUAL) {
141 // If we are checking for equality, here're the answer
142 return res == 0;
143 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200144 if (res < 0) {
145 return false;
146 }
147 if (res > 0) {
148 return true;
149 }
150
151 // If we had tie in the last element...
152 // ... and we have lists of different lengths...
153 if (len1 != len2) {
154 if (len1 < len2) {
155 // ... then longer list length wins (we deal only with >)
156 return false;
157 }
Damien Georged17926d2014-03-30 13:35:08 +0100158 } else if (op == MP_BINARY_OP_MORE) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200159 // Otherwise, if we have strict relation, equality means failure
160 return false;
161 }
162 return true;
163}
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200164
165// Special-case comparison function for sequences of mp_obj_t
Damien Georged17926d2014-03-30 13:35:08 +0100166// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George4d917232014-08-30 14:28:06 +0100167bool 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 +0100168 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200169 return false;
170 }
171
172 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100173 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200174 SWAP(const mp_obj_t *, items1, items2);
175 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100176 if (op == MP_BINARY_OP_LESS) {
177 op = MP_BINARY_OP_MORE;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200178 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100179 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200180 }
181 }
182
Damien George42f3de92014-10-03 17:44:14 +0000183 mp_uint_t len = len1 < len2 ? len1 : len2;
184 for (mp_uint_t i = 0; i < len; i++) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300185 // If current elements equal, can't decide anything - go on
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300186 if (mp_obj_equal(items1[i], items2[i])) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300187 continue;
188 }
189
190 // Othewise, if they are not equal, we can have final decision based on them
191 if (op == MP_BINARY_OP_EQUAL) {
192 // In particular, if we are checking for equality, here're the answer
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200193 return false;
194 }
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300195
196 // Otherwise, application of relation op gives the answer
197 return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200198 }
199
200 // If we had tie in the last element...
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300201 // ... and we have lists of different lengths...
202 if (len1 != len2) {
203 if (len1 < len2) {
204 // ... then longer list length wins (we deal only with >)
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200205 return false;
206 }
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300207 } else if (op == MP_BINARY_OP_MORE) {
208 // Otherwise, if we have strict relation, sequence equality means failure
209 return false;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200210 }
211
212 return true;
213}
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200214
215// Special-case of index() which searches for mp_obj_t
Damien Georged182b982014-08-30 14:19:41 +0100216mp_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 +0200217 mp_obj_type_t *type = mp_obj_get_type(args[0]);
218 mp_obj_t *value = args[1];
219 uint start = 0;
220 uint stop = len;
221
222 if (n_args >= 3) {
xbe9e1e8cd2014-03-12 22:57:16 -0700223 start = mp_get_index(type, len, args[2], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200224 if (n_args >= 4) {
xbe9e1e8cd2014-03-12 22:57:16 -0700225 stop = mp_get_index(type, len, args[3], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200226 }
227 }
228
Damien George40f3c022014-07-03 13:25:24 +0100229 for (mp_uint_t i = start; i < stop; i++) {
Paul Sokolovsky624eff62014-02-10 06:42:20 +0200230 if (mp_obj_equal(items[i], value)) {
231 // Common sense says this cannot overflow small int
232 return MP_OBJ_NEW_SMALL_INT(i);
233 }
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200234 }
235
Damien Georgeea13f402014-04-05 18:32:08 +0100236 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "object not in sequence"));
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200237}
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200238
Damien Georged182b982014-08-30 14:19:41 +0100239mp_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 +0100240 mp_uint_t count = 0;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200241 for (uint i = 0; i < len; i++) {
242 if (mp_obj_equal(items[i], value)) {
243 count++;
244 }
245 }
246
247 // Common sense says this cannot overflow small int
248 return MP_OBJ_NEW_SMALL_INT(count);
249}