blob: c940d9f69fdd9d3973a221fc62c13412377cb498 [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 Sokolovsky83eba5d2014-04-18 21:42:54 +030028#include <assert.h>
xbeefe34222014-03-16 00:14:26 -070029#include <stdbool.h>
Paul Sokolovsky439542f2014-01-21 00:19:19 +020030#include <string.h>
Paul Sokolovsky439542f2014-01-21 00:19:19 +020031
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Paul Sokolovsky439542f2014-01-21 00:19:19 +020033#include "nlr.h"
34#include "misc.h"
Damien George12eacca2014-01-21 21:54:15 +000035#include "qstr.h"
Paul Sokolovsky439542f2014-01-21 00:19:19 +020036#include "obj.h"
Paul Sokolovsky439542f2014-01-21 00:19:19 +020037#include "runtime0.h"
38#include "runtime.h"
39
40// Helpers for sequence types
41
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020042#define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
43
Paul Sokolovsky439542f2014-01-21 00:19:19 +020044// Implements backend of sequence * integer operation. Assumes elements are
45// memory-adjacent in sequence.
46void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest) {
47 for (int i = 0; i < times; i++) {
48 uint copy_sz = item_sz * len;
49 memcpy(dest, items, copy_sz);
50 dest = (char*)dest + copy_sz;
51 }
52}
Paul Sokolovsky7364af22014-02-02 02:38:22 +020053
Paul Sokolovskyde4b9322014-05-25 21:21:57 +030054bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030055 mp_obj_t ostart, ostop, ostep;
56 machine_int_t start, stop;
57 mp_obj_slice_get(slice, &ostart, &ostop, &ostep);
Paul Sokolovsky7364af22014-02-02 02:38:22 +020058
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030059 if (ostart == mp_const_none) {
60 start = 0;
61 } else {
62 start = MP_OBJ_SMALL_INT_VALUE(ostart);
63 }
64 if (ostop == mp_const_none) {
65 stop = len;
66 } else {
67 stop = MP_OBJ_SMALL_INT_VALUE(ostop);
68 }
69
Paul Sokolovsky7364af22014-02-02 02:38:22 +020070 // Unlike subscription, out-of-bounds slice indexes are never error
71 if (start < 0) {
72 start = len + start;
73 if (start < 0) {
74 start = 0;
75 }
76 } else if (start > len) {
77 start = len;
78 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030079 if (stop < 0) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020080 stop = len + stop;
81 // CPython returns empty sequence in such case
82 if (stop < 0) {
83 stop = start;
84 }
85 } else if (stop > len) {
86 stop = len;
87 }
Paul Sokolovsky69d081a2014-05-25 02:29:40 +030088
89 // CPython returns empty sequence in such case, or point for assignment is at start
90 if (start > stop) {
91 stop = start;
92 }
93
Paul Sokolovskyde4b9322014-05-25 21:21:57 +030094 indexes->start = start;
95 indexes->stop = stop;
96
97 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
Paul Sokolovskyde4b9322014-05-25 21:21:57 +030098 indexes->step = MP_OBJ_SMALL_INT_VALUE(ostep);
99 return false;
100 }
101 indexes->step = 1;
Paul Sokolovsky7364af22014-02-02 02:38:22 +0200102 return true;
103}
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200104
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +0300105mp_obj_t mp_seq_extract_slice(uint len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
106 machine_int_t start = indexes->start, stop = indexes->stop;
107 machine_int_t step = indexes->step;
108
109 mp_obj_t res = mp_obj_new_list(0, NULL);
110
111 if (step < 0) {
112 stop--;
113 while (start <= stop) {
114 mp_obj_list_append(res, seq[stop]);
115 stop += step;
116 }
117 } else {
118 while (start < stop) {
119 mp_obj_list_append(res, seq[start]);
120 start += step;
121 }
122 }
123 return res;
124}
125
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200126// Special-case comparison function for sequences of bytes
Damien Georged17926d2014-03-30 13:35:08 +0100127// Don't pass MP_BINARY_OP_NOT_EQUAL here
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200128bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2) {
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300129 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
130 return false;
131 }
132
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200133 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100134 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200135 SWAP(const byte*, data1, data2);
136 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100137 if (op == MP_BINARY_OP_LESS) {
138 op = MP_BINARY_OP_MORE;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200139 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100140 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200141 }
142 }
143 uint min_len = len1 < len2 ? len1 : len2;
144 int res = memcmp(data1, data2, min_len);
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300145 if (op == MP_BINARY_OP_EQUAL) {
146 // If we are checking for equality, here're the answer
147 return res == 0;
148 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200149 if (res < 0) {
150 return false;
151 }
152 if (res > 0) {
153 return true;
154 }
155
156 // If we had tie in the last element...
157 // ... and we have lists of different lengths...
158 if (len1 != len2) {
159 if (len1 < len2) {
160 // ... then longer list length wins (we deal only with >)
161 return false;
162 }
Damien Georged17926d2014-03-30 13:35:08 +0100163 } else if (op == MP_BINARY_OP_MORE) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200164 // Otherwise, if we have strict relation, equality means failure
165 return false;
166 }
167 return true;
168}
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200169
170// Special-case comparison function for sequences of mp_obj_t
Damien Georged17926d2014-03-30 13:35:08 +0100171// Don't pass MP_BINARY_OP_NOT_EQUAL here
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200172bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2) {
Damien Georged17926d2014-03-30 13:35:08 +0100173 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200174 return false;
175 }
176
177 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100178 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200179 SWAP(const mp_obj_t *, items1, items2);
180 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100181 if (op == MP_BINARY_OP_LESS) {
182 op = MP_BINARY_OP_MORE;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200183 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100184 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200185 }
186 }
187
188 int len = len1 < len2 ? len1 : len2;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200189 for (int i = 0; i < len; i++) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300190 // If current elements equal, can't decide anything - go on
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300191 if (mp_obj_equal(items1[i], items2[i])) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300192 continue;
193 }
194
195 // Othewise, if they are not equal, we can have final decision based on them
196 if (op == MP_BINARY_OP_EQUAL) {
197 // In particular, if we are checking for equality, here're the answer
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200198 return false;
199 }
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300200
201 // Otherwise, application of relation op gives the answer
202 return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200203 }
204
205 // If we had tie in the last element...
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300206 // ... and we have lists of different lengths...
207 if (len1 != len2) {
208 if (len1 < len2) {
209 // ... then longer list length wins (we deal only with >)
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200210 return false;
211 }
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300212 } else if (op == MP_BINARY_OP_MORE) {
213 // Otherwise, if we have strict relation, sequence equality means failure
214 return false;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200215 }
216
217 return true;
218}
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200219
220// Special-case of index() which searches for mp_obj_t
221mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args) {
222 mp_obj_type_t *type = mp_obj_get_type(args[0]);
223 mp_obj_t *value = args[1];
224 uint start = 0;
225 uint stop = len;
226
227 if (n_args >= 3) {
xbe9e1e8cd2014-03-12 22:57:16 -0700228 start = mp_get_index(type, len, args[2], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200229 if (n_args >= 4) {
xbe9e1e8cd2014-03-12 22:57:16 -0700230 stop = mp_get_index(type, len, args[3], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200231 }
232 }
233
Damien Georged46ca252014-02-10 21:46:47 +0000234 for (machine_uint_t i = start; i < stop; i++) {
Paul Sokolovsky624eff62014-02-10 06:42:20 +0200235 if (mp_obj_equal(items[i], value)) {
236 // Common sense says this cannot overflow small int
237 return MP_OBJ_NEW_SMALL_INT(i);
238 }
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200239 }
240
Damien Georgeea13f402014-04-05 18:32:08 +0100241 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "object not in sequence"));
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200242}
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200243
244mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value) {
Damien Georged46ca252014-02-10 21:46:47 +0000245 machine_uint_t count = 0;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200246 for (uint i = 0; i < len; i++) {
247 if (mp_obj_equal(items[i], value)) {
248 count++;
249 }
250 }
251
252 // Common sense says this cannot overflow small int
253 return MP_OBJ_NEW_SMALL_INT(count);
254}