2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2014-05-13 01:44:45 -04:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-20 17:19:19 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/runtime.h"
|
2018-08-08 21:24:49 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2014-01-20 17:19:19 -05:00
|
|
|
|
|
|
|
// Helpers for sequence types
|
|
|
|
|
2014-02-02 01:24:07 -05:00
|
|
|
#define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
|
|
|
|
|
2020-01-25 15:58:14 -05:00
|
|
|
#if __GNUC__ < 5
|
|
|
|
// n.b. does not actually detect overflow!
|
|
|
|
#define __builtin_mul_overflow(a, b, x) (*(x) = (a) * (b), false)
|
|
|
|
#endif
|
|
|
|
|
2019-02-04 18:33:36 -05:00
|
|
|
// Detect when a multiply causes an overflow.
|
|
|
|
size_t mp_seq_multiply_len(size_t item_sz, size_t len) {
|
|
|
|
size_t new_len;
|
2019-02-04 19:11:16 -05:00
|
|
|
if (__builtin_mul_overflow(item_sz, len, &new_len)) {
|
2019-02-04 18:33:36 -05:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, translate("small int overflow"));
|
|
|
|
}
|
|
|
|
return new_len;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:19:19 -05:00
|
|
|
// Implements backend of sequence * integer operation. Assumes elements are
|
|
|
|
// memory-adjacent in sequence.
|
2017-03-23 01:23:20 -04:00
|
|
|
void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest) {
|
|
|
|
for (size_t i = 0; i < times; i++) {
|
|
|
|
size_t copy_sz = item_sz * len;
|
2014-01-20 17:19:19 -05:00
|
|
|
memcpy(dest, items, copy_sz);
|
2021-03-15 09:57:36 -04:00
|
|
|
dest = (char *)dest + copy_sz;
|
2014-01-20 17:19:19 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-01 19:38:22 -05:00
|
|
|
|
2014-06-01 08:49:35 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_SLICE
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
|
2019-11-16 19:07:11 -05:00
|
|
|
mp_obj_slice_indices(slice, len, indexes);
|
2014-05-24 18:39:27 -04:00
|
|
|
|
2019-11-16 19:07:11 -05:00
|
|
|
// If the index is negative then stop points to the last item, not after it
|
|
|
|
if (indexes->step < 0) {
|
|
|
|
indexes->stop++;
|
2014-02-01 19:38:22 -05:00
|
|
|
}
|
2014-05-24 19:29:40 -04:00
|
|
|
|
|
|
|
// CPython returns empty sequence in such case, or point for assignment is at start
|
2019-11-16 19:07:11 -05:00
|
|
|
if (indexes->step > 0 && indexes->start > indexes->stop) {
|
|
|
|
indexes->stop = indexes->start;
|
|
|
|
} else if (indexes->step < 0 && indexes->start < indexes->stop) {
|
|
|
|
indexes->stop = indexes->start + 1;
|
2014-05-24 19:29:40 -04:00
|
|
|
}
|
|
|
|
|
2016-10-30 12:25:04 -04:00
|
|
|
return indexes->step == 1;
|
2014-02-01 19:38:22 -05:00
|
|
|
}
|
2014-02-02 01:24:07 -05:00
|
|
|
|
2014-06-01 08:49:35 -04:00
|
|
|
#endif
|
|
|
|
|
2017-03-23 01:23:20 -04:00
|
|
|
mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)len; // TODO can we remove len from the arg list?
|
|
|
|
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t start = indexes->start, stop = indexes->stop;
|
|
|
|
mp_int_t step = indexes->step;
|
2014-05-25 15:12:56 -04:00
|
|
|
|
|
|
|
mp_obj_t res = mp_obj_new_list(0, NULL);
|
|
|
|
|
|
|
|
if (step < 0) {
|
2016-10-30 12:25:04 -04:00
|
|
|
while (start >= stop) {
|
|
|
|
mp_obj_list_append(res, seq[start]);
|
|
|
|
start += step;
|
2014-05-25 15:12:56 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (start < stop) {
|
|
|
|
mp_obj_list_append(res, seq[start]);
|
|
|
|
start += step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-02-02 01:24:07 -05:00
|
|
|
// Special-case comparison function for sequences of bytes
|
2014-03-30 08:35:08 -04:00
|
|
|
// Don't pass MP_BINARY_OP_NOT_EQUAL here
|
2017-03-23 01:23:20 -04:00
|
|
|
bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2) {
|
2014-05-09 21:26:10 -04:00
|
|
|
if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-02 01:24:07 -05:00
|
|
|
// Let's deal only with > & >=
|
2014-03-30 08:35:08 -04:00
|
|
|
if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
|
2021-03-15 09:57:36 -04:00
|
|
|
SWAP(const byte *, data1, data2);
|
2017-03-23 01:23:20 -04:00
|
|
|
SWAP(size_t, len1, len2);
|
2014-03-30 08:35:08 -04:00
|
|
|
if (op == MP_BINARY_OP_LESS) {
|
|
|
|
op = MP_BINARY_OP_MORE;
|
2014-02-02 01:24:07 -05:00
|
|
|
} else {
|
2014-03-30 08:35:08 -04:00
|
|
|
op = MP_BINARY_OP_MORE_EQUAL;
|
2014-02-02 01:24:07 -05:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 01:23:20 -04:00
|
|
|
size_t min_len = len1 < len2 ? len1 : len2;
|
2014-02-02 01:24:07 -05:00
|
|
|
int res = memcmp(data1, data2, min_len);
|
2014-05-15 12:09:06 -04:00
|
|
|
if (op == MP_BINARY_OP_EQUAL) {
|
2021-04-23 15:26:42 -04:00
|
|
|
// If we are checking for equality, here's the answer
|
2014-05-15 12:09:06 -04:00
|
|
|
return res == 0;
|
|
|
|
}
|
2014-02-02 01:24:07 -05:00
|
|
|
if (res < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (res > 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we had tie in the last element...
|
|
|
|
// ... and we have lists of different lengths...
|
|
|
|
if (len1 != len2) {
|
|
|
|
if (len1 < len2) {
|
|
|
|
// ... then longer list length wins (we deal only with >)
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-30 08:35:08 -04:00
|
|
|
} else if (op == MP_BINARY_OP_MORE) {
|
2014-02-02 01:24:07 -05:00
|
|
|
// Otherwise, if we have strict relation, equality means failure
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-08 15:49:46 -05:00
|
|
|
|
|
|
|
// Special-case comparison function for sequences of mp_obj_t
|
2014-03-30 08:35:08 -04:00
|
|
|
// Don't pass MP_BINARY_OP_NOT_EQUAL here
|
2017-03-23 01:23:20 -04:00
|
|
|
bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp_obj_t *items2, size_t len2) {
|
2014-03-30 08:35:08 -04:00
|
|
|
if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
|
2014-02-08 15:49:46 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's deal only with > & >=
|
2014-03-30 08:35:08 -04:00
|
|
|
if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
|
2014-02-08 15:49:46 -05:00
|
|
|
SWAP(const mp_obj_t *, items1, items2);
|
2017-03-23 01:23:20 -04:00
|
|
|
SWAP(size_t, len1, len2);
|
2014-03-30 08:35:08 -04:00
|
|
|
if (op == MP_BINARY_OP_LESS) {
|
|
|
|
op = MP_BINARY_OP_MORE;
|
2014-02-08 15:49:46 -05:00
|
|
|
} else {
|
2014-03-30 08:35:08 -04:00
|
|
|
op = MP_BINARY_OP_MORE_EQUAL;
|
2014-02-08 15:49:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 01:23:20 -04:00
|
|
|
size_t len = len1 < len2 ? len1 : len2;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2014-04-18 14:42:54 -04:00
|
|
|
// If current elements equal, can't decide anything - go on
|
2014-04-18 14:47:58 -04:00
|
|
|
if (mp_obj_equal(items1[i], items2[i])) {
|
2014-04-18 14:42:54 -04:00
|
|
|
continue;
|
2014-02-08 15:49:46 -05:00
|
|
|
}
|
2014-04-18 14:42:54 -04:00
|
|
|
|
|
|
|
// Othewise, if they are not equal, we can have final decision based on them
|
|
|
|
if (op == MP_BINARY_OP_EQUAL) {
|
|
|
|
// In particular, if we are checking for equality, here're the answer
|
2014-02-08 15:49:46 -05:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-18 14:42:54 -04:00
|
|
|
|
|
|
|
// Otherwise, application of relation op gives the answer
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_binary_op(op, items1[i], items2[i]) == mp_const_true;
|
2014-02-08 15:49:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we had tie in the last element...
|
2014-04-18 14:47:58 -04:00
|
|
|
// ... and we have lists of different lengths...
|
|
|
|
if (len1 != len2) {
|
|
|
|
if (len1 < len2) {
|
|
|
|
// ... then longer list length wins (we deal only with >)
|
2014-02-08 15:49:46 -05:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-18 14:47:58 -04:00
|
|
|
} else if (op == MP_BINARY_OP_MORE) {
|
|
|
|
// Otherwise, if we have strict relation, sequence equality means failure
|
|
|
|
return false;
|
2014-02-08 15:49:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-09 23:37:11 -05:00
|
|
|
|
|
|
|
// Special-case of index() which searches for mp_obj_t
|
2017-03-23 01:23:20 -04:00
|
|
|
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args) {
|
2020-01-08 19:01:14 -05:00
|
|
|
const mp_obj_type_t *type = mp_obj_get_type(args[0]);
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_t value = args[1];
|
2017-03-23 01:17:40 -04:00
|
|
|
size_t start = 0;
|
|
|
|
size_t stop = len;
|
2014-02-09 23:37:11 -05:00
|
|
|
|
|
|
|
if (n_args >= 3) {
|
2014-03-13 01:57:16 -04:00
|
|
|
start = mp_get_index(type, len, args[2], true);
|
2014-02-09 23:37:11 -05:00
|
|
|
if (n_args >= 4) {
|
2014-03-13 01:57:16 -04:00
|
|
|
stop = mp_get_index(type, len, args[3], true);
|
2014-02-09 23:37:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 01:17:40 -04:00
|
|
|
for (size_t i = start; i < stop; i++) {
|
2014-02-09 23:42:20 -05:00
|
|
|
if (mp_obj_equal(items[i], value)) {
|
|
|
|
// Common sense says this cannot overflow small int
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(i);
|
|
|
|
}
|
2014-02-09 23:37:11 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("object not in sequence"));
|
2014-02-09 23:37:11 -05:00
|
|
|
}
|
2014-02-10 00:10:55 -05:00
|
|
|
|
2017-03-23 01:23:20 -04:00
|
|
|
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value) {
|
|
|
|
size_t count = 0;
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2021-03-15 09:57:36 -04:00
|
|
|
if (mp_obj_equal(items[i], value)) {
|
|
|
|
count++;
|
|
|
|
}
|
2014-02-10 00:10:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Common sense says this cannot overflow small int
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(count);
|
|
|
|
}
|