blob: 94a9bb9d5f4af8c45390a33dcc3866fa3cab5b8d [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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/runtime.h"
Paul Sokolovsky439542f2014-01-21 00:19:19 +020031
32// Helpers for sequence types
33
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020034#define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
35
Paul Sokolovsky439542f2014-01-21 00:19:19 +020036// Implements backend of sequence * integer operation. Assumes elements are
37// memory-adjacent in sequence.
Damien George507119f2017-03-23 16:23:20 +110038void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest) {
39 for (size_t i = 0; i < times; i++) {
40 size_t copy_sz = item_sz * len;
Paul Sokolovsky439542f2014-01-21 00:19:19 +020041 memcpy(dest, items, copy_sz);
42 dest = (char*)dest + copy_sz;
43 }
44}
Paul Sokolovsky7364af22014-02-02 02:38:22 +020045
Damien Georgec49ddb92014-06-01 13:49:35 +010046#if MICROPY_PY_BUILTINS_SLICE
47
Damien George40f3c022014-07-03 13:25:24 +010048bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
Nicko van Someren4c939552019-11-16 17:07:11 -070049 mp_obj_slice_indices(slice, len, indexes);
Paul Sokolovsky7364af22014-02-02 02:38:22 +020050
Nicko van Someren4c939552019-11-16 17:07:11 -070051 // If the index is negative then stop points to the last item, not after it
52 if (indexes->step < 0) {
53 indexes->stop++;
Paul Sokolovsky7364af22014-02-02 02:38:22 +020054 }
Paul Sokolovsky69d081a2014-05-25 02:29:40 +030055
56 // CPython returns empty sequence in such case, or point for assignment is at start
Nicko van Someren4c939552019-11-16 17:07:11 -070057 if (indexes->step > 0 && indexes->start > indexes->stop) {
58 indexes->stop = indexes->start;
59 } else if (indexes->step < 0 && indexes->start < indexes->stop) {
60 indexes->stop = indexes->start + 1;
Paul Sokolovsky69d081a2014-05-25 02:29:40 +030061 }
62
Fabio Utzig8908e502016-10-30 14:25:04 -020063 return indexes->step == 1;
Paul Sokolovsky7364af22014-02-02 02:38:22 +020064}
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020065
Damien Georgec49ddb92014-06-01 13:49:35 +010066#endif
67
Damien George507119f2017-03-23 16:23:20 +110068mp_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 +000069 (void)len; // TODO can we remove len from the arg list?
70
Damien George40f3c022014-07-03 13:25:24 +010071 mp_int_t start = indexes->start, stop = indexes->stop;
72 mp_int_t step = indexes->step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +030073
74 mp_obj_t res = mp_obj_new_list(0, NULL);
75
76 if (step < 0) {
Fabio Utzig8908e502016-10-30 14:25:04 -020077 while (start >= stop) {
78 mp_obj_list_append(res, seq[start]);
79 start += step;
Paul Sokolovsky5fd5af92014-05-25 22:12:56 +030080 }
81 } else {
82 while (start < stop) {
83 mp_obj_list_append(res, seq[start]);
84 start += step;
85 }
86 }
87 return res;
88}
89
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020090// Special-case comparison function for sequences of bytes
Damien Georged17926d2014-03-30 13:35:08 +010091// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George507119f2017-03-23 16:23:20 +110092bool 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 +030093 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
94 return false;
95 }
96
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020097 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +010098 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +020099 SWAP(const byte*, data1, data2);
Damien George507119f2017-03-23 16:23:20 +1100100 SWAP(size_t, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100101 if (op == MP_BINARY_OP_LESS) {
102 op = MP_BINARY_OP_MORE;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200103 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100104 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200105 }
106 }
Damien George507119f2017-03-23 16:23:20 +1100107 size_t min_len = len1 < len2 ? len1 : len2;
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200108 int res = memcmp(data1, data2, min_len);
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300109 if (op == MP_BINARY_OP_EQUAL) {
Yonatan Goldschmidte9593d52019-07-21 23:23:11 +0300110 // If we are checking for equality, here's the answer
Paul Sokolovskyad3baec2014-05-15 19:09:06 +0300111 return res == 0;
112 }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200113 if (res < 0) {
114 return false;
115 }
116 if (res > 0) {
117 return true;
118 }
119
120 // If we had tie in the last element...
121 // ... and we have lists of different lengths...
122 if (len1 != len2) {
123 if (len1 < len2) {
124 // ... then longer list length wins (we deal only with >)
125 return false;
126 }
Damien Georged17926d2014-03-30 13:35:08 +0100127 } else if (op == MP_BINARY_OP_MORE) {
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200128 // Otherwise, if we have strict relation, equality means failure
129 return false;
130 }
131 return true;
132}
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200133
134// Special-case comparison function for sequences of mp_obj_t
Damien Georged17926d2014-03-30 13:35:08 +0100135// Don't pass MP_BINARY_OP_NOT_EQUAL here
Damien George507119f2017-03-23 16:23:20 +1100136bool 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 +0100137 if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200138 return false;
139 }
140
141 // Let's deal only with > & >=
Damien Georged17926d2014-03-30 13:35:08 +0100142 if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200143 SWAP(const mp_obj_t *, items1, items2);
Damien George507119f2017-03-23 16:23:20 +1100144 SWAP(size_t, len1, len2);
Damien Georged17926d2014-03-30 13:35:08 +0100145 if (op == MP_BINARY_OP_LESS) {
146 op = MP_BINARY_OP_MORE;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200147 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100148 op = MP_BINARY_OP_MORE_EQUAL;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200149 }
150 }
151
Damien George507119f2017-03-23 16:23:20 +1100152 size_t len = len1 < len2 ? len1 : len2;
153 for (size_t i = 0; i < len; i++) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300154 // If current elements equal, can't decide anything - go on
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300155 if (mp_obj_equal(items1[i], items2[i])) {
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300156 continue;
157 }
158
159 // Othewise, if they are not equal, we can have final decision based on them
160 if (op == MP_BINARY_OP_EQUAL) {
161 // In particular, if we are checking for equality, here're the answer
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200162 return false;
163 }
Paul Sokolovsky83eba5d2014-04-18 21:42:54 +0300164
165 // Otherwise, application of relation op gives the answer
166 return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200167 }
168
169 // If we had tie in the last element...
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300170 // ... and we have lists of different lengths...
171 if (len1 != len2) {
172 if (len1 < len2) {
173 // ... then longer list length wins (we deal only with >)
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200174 return false;
175 }
Paul Sokolovsky0fc47752014-04-18 21:47:58 +0300176 } else if (op == MP_BINARY_OP_MORE) {
177 // Otherwise, if we have strict relation, sequence equality means failure
178 return false;
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200179 }
180
181 return true;
182}
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200183
184// Special-case of index() which searches for mp_obj_t
Damien George507119f2017-03-23 16:23:20 +1100185mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args) {
Damien Georgebfbd9442020-01-09 11:01:14 +1100186 const mp_obj_type_t *type = mp_obj_get_type(args[0]);
Damien George999cedb2015-11-27 17:01:44 +0000187 mp_obj_t value = args[1];
Damien Georgec88cfe12017-03-23 16:17:40 +1100188 size_t start = 0;
189 size_t stop = len;
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200190
191 if (n_args >= 3) {
xbe9e1e8cd2014-03-12 22:57:16 -0700192 start = mp_get_index(type, len, args[2], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200193 if (n_args >= 4) {
xbe9e1e8cd2014-03-12 22:57:16 -0700194 stop = mp_get_index(type, len, args[3], true);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200195 }
196 }
197
Damien Georgec88cfe12017-03-23 16:17:40 +1100198 for (size_t i = start; i < stop; i++) {
Paul Sokolovsky624eff62014-02-10 06:42:20 +0200199 if (mp_obj_equal(items[i], value)) {
200 // Common sense says this cannot overflow small int
201 return MP_OBJ_NEW_SMALL_INT(i);
202 }
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200203 }
204
Damien George94c41bb2017-03-28 22:37:26 +1100205 mp_raise_ValueError("object not in sequence");
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200206}
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200207
Damien George507119f2017-03-23 16:23:20 +1100208mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value) {
209 size_t count = 0;
210 for (size_t i = 0; i < len; i++) {
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200211 if (mp_obj_equal(items[i], value)) {
212 count++;
213 }
214 }
215
216 // Common sense says this cannot overflow small int
217 return MP_OBJ_NEW_SMALL_INT(count);
218}