blob: 8418f1ef67da50a9008d4a634307e13dda04a87f [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 Sokolovskyd915a522014-05-10 21:36:33 +030054bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end) {
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);
58 if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
Paul Sokolovskyff4b6da2014-05-25 03:02:57 +030059 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
60 "Only slices with step=1 (aka None) are supported"));
Paul Sokolovsky7364af22014-02-02 02:38:22 +020061 return false;
62 }
63
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030064 if (ostart == mp_const_none) {
65 start = 0;
66 } else {
67 start = MP_OBJ_SMALL_INT_VALUE(ostart);
68 }
69 if (ostop == mp_const_none) {
70 stop = len;
71 } else {
72 stop = MP_OBJ_SMALL_INT_VALUE(ostop);
73 }
74
Paul Sokolovsky7364af22014-02-02 02:38:22 +020075 // Unlike subscription, out-of-bounds slice indexes are never error
76 if (start < 0) {
77 start = len + start;
78 if (start < 0) {
79 start = 0;
80 }
81 } else if (start > len) {
82 start = len;
83 }
Paul Sokolovskyafaaf532014-05-25 01:39:27 +030084 if (stop < 0) {
Paul Sokolovsky7364af22014-02-02 02:38:22 +020085 stop = len + stop;
86 // CPython returns empty sequence in such case
87 if (stop < 0) {
88 stop = start;
89 }
90 } else if (stop > len) {
91 stop = len;
92 }
Paul Sokolovsky69d081a2014-05-25 02:29:40 +030093
94 // CPython returns empty sequence in such case, or point for assignment is at start
95 if (start > stop) {
96 stop = start;
97 }
98
Paul Sokolovsky7364af22014-02-02 02:38:22 +020099 *begin = start;
100 *end = stop;
101 return true;
102}
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200103
104// Special-case comparison function for sequences of bytes
Damien Georged17926d2014-03-30 13:35:08 +0100105// Don't pass MP_BINARY_OP_NOT_EQUAL here
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200106bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2) {
Paul Sokolovsky7b0f9a72014-05-10 04:26:10 +0300107 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
108 return false;
109 }
110
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200111 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100112 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200113 SWAP(const byte*, data1, data2);
114 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100115 if (op == MP_BINARY_OP_LESS) {
116 op = MP_BINARY_OP_MORE;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200117 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100118 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200119 }
120 }
121 uint min_len = len1 < len2 ? len1 : len2;
122 int res = memcmp(data1, data2, min_len);
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300123 if (op == MP_BINARY_OP_EQUAL) {
124 // If we are checking for equality, here're the answer
125 return res == 0;
126 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200127 if (res < 0) {
128 return false;
129 }
130 if (res > 0) {
131 return true;
132 }
133
134 // If we had tie in the last element...
135 // ... and we have lists of different lengths...
136 if (len1 != len2) {
137 if (len1 < len2) {
138 // ... then longer list length wins (we deal only with >)
139 return false;
140 }
Damien Georged17926d2014-03-30 13:35:08 +0100141 } else if (op == MP_BINARY_OP_MORE) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200142 // Otherwise, if we have strict relation, equality means failure
143 return false;
144 }
145 return true;
146}
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200147
148// Special-case comparison function for sequences of mp_obj_t
Damien Georged17926d2014-03-30 13:35:08 +0100149// Don't pass MP_BINARY_OP_NOT_EQUAL here
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200150bool 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 +0100151 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200152 return false;
153 }
154
155 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100156 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200157 SWAP(const mp_obj_t *, items1, items2);
158 SWAP(uint, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100159 if (op == MP_BINARY_OP_LESS) {
160 op = MP_BINARY_OP_MORE;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200161 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100162 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200163 }
164 }
165
166 int len = len1 < len2 ? len1 : len2;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200167 for (int i = 0; i < len; i++) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300168 // If current elements equal, can't decide anything - go on
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300169 if (mp_obj_equal(items1[i], items2[i])) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300170 continue;
171 }
172
173 // Othewise, if they are not equal, we can have final decision based on them
174 if (op == MP_BINARY_OP_EQUAL) {
175 // In particular, if we are checking for equality, here're the answer
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200176 return false;
177 }
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300178
179 // Otherwise, application of relation op gives the answer
180 return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200181 }
182
183 // If we had tie in the last element...
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300184 // ... and we have lists of different lengths...
185 if (len1 != len2) {
186 if (len1 < len2) {
187 // ... then longer list length wins (we deal only with >)
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200188 return false;
189 }
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300190 } else if (op == MP_BINARY_OP_MORE) {
191 // Otherwise, if we have strict relation, sequence equality means failure
192 return false;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200193 }
194
195 return true;
196}
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200197
198// Special-case of index() which searches for mp_obj_t
199mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args) {
200 mp_obj_type_t *type = mp_obj_get_type(args[0]);
201 mp_obj_t *value = args[1];
202 uint start = 0;
203 uint stop = len;
204
205 if (n_args >= 3) {
xbe9e1e8cd2014-03-12 22:57:16 -0700206 start = mp_get_index(type, len, args[2], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200207 if (n_args >= 4) {
xbe9e1e8cd2014-03-12 22:57:16 -0700208 stop = mp_get_index(type, len, args[3], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200209 }
210 }
211
Damien Georged46ca252014-02-10 21:46:47 +0000212 for (machine_uint_t i = start; i < stop; i++) {
Paul Sokolovsky624eff62014-02-10 06:42:20 +0200213 if (mp_obj_equal(items[i], value)) {
214 // Common sense says this cannot overflow small int
215 return MP_OBJ_NEW_SMALL_INT(i);
216 }
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200217 }
218
Damien Georgeea13f402014-04-05 18:32:08 +0100219 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "object not in sequence"));
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200220}
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200221
222mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value) {
Damien Georged46ca252014-02-10 21:46:47 +0000223 machine_uint_t count = 0;
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200224 for (uint i = 0; i < len; i++) {
225 if (mp_obj_equal(items[i], value)) {
226 count++;
227 }
228 }
229
230 // Common sense says this cannot overflow small int
231 return MP_OBJ_NEW_SMALL_INT(count);
232}