2014-04-18 14:42:54 -04:00
|
|
|
#include <assert.h>
|
2014-03-16 03:14:26 -04:00
|
|
|
#include <stdbool.h>
|
2014-01-20 17:19:19 -05:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-21 16:54:15 -05:00
|
|
|
#include "qstr.h"
|
2014-01-20 17:19:19 -05:00
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime0.h"
|
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
// Helpers for sequence types
|
|
|
|
|
2014-02-02 01:24:07 -05:00
|
|
|
#define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
|
|
|
|
|
2014-01-20 17:19:19 -05:00
|
|
|
// Implements backend of sequence * integer operation. Assumes elements are
|
|
|
|
// memory-adjacent in sequence.
|
|
|
|
void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest) {
|
|
|
|
for (int i = 0; i < times; i++) {
|
|
|
|
uint copy_sz = item_sz * len;
|
|
|
|
memcpy(dest, items, copy_sz);
|
|
|
|
dest = (char*)dest + copy_sz;
|
|
|
|
}
|
|
|
|
}
|
2014-02-01 19:38:22 -05:00
|
|
|
|
|
|
|
bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end) {
|
|
|
|
machine_int_t start, stop, step;
|
|
|
|
mp_obj_slice_get(slice, &start, &stop, &step);
|
|
|
|
if (step != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlike subscription, out-of-bounds slice indexes are never error
|
|
|
|
if (start < 0) {
|
|
|
|
start = len + start;
|
|
|
|
if (start < 0) {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
} else if (start > len) {
|
|
|
|
start = len;
|
|
|
|
}
|
|
|
|
if (stop <= 0) {
|
|
|
|
stop = len + stop;
|
|
|
|
// CPython returns empty sequence in such case
|
|
|
|
if (stop < 0) {
|
|
|
|
stop = start;
|
|
|
|
}
|
|
|
|
} else if (stop > len) {
|
|
|
|
stop = len;
|
|
|
|
}
|
|
|
|
*begin = start;
|
|
|
|
*end = stop;
|
|
|
|
return true;
|
|
|
|
}
|
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
|
2014-02-02 01:24:07 -05:00
|
|
|
bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2) {
|
|
|
|
// 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-02 01:24:07 -05:00
|
|
|
SWAP(const byte*, data1, data2);
|
|
|
|
SWAP(uint, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
uint min_len = len1 < len2 ? len1 : len2;
|
|
|
|
int res = memcmp(data1, data2, min_len);
|
|
|
|
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
|
2014-02-08 15:49:46 -05:00
|
|
|
bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint 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);
|
|
|
|
SWAP(uint, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = len1 < len2 ? len1 : len2;
|
|
|
|
bool eq_status = true; // empty lists are equal
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
eq_status = mp_obj_equal(items1[i], items2[i]);
|
2014-04-18 14:42:54 -04:00
|
|
|
// If current elements equal, can't decide anything - go on
|
|
|
|
if (eq_status) {
|
|
|
|
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
|
|
|
|
return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
|
2014-02-08 15:49:46 -05:00
|
|
|
}
|
|
|
|
|
2014-04-18 14:42:54 -04:00
|
|
|
assert(eq_status);
|
2014-02-08 15:49:46 -05:00
|
|
|
// If we had tie in the last element...
|
|
|
|
if (eq_status) {
|
|
|
|
// ... 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-08 15:49:46 -05:00
|
|
|
// Otherwise, if we have strict relation, equality means failure
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-09 23:37:11 -05:00
|
|
|
|
|
|
|
// Special-case of index() which searches for mp_obj_t
|
|
|
|
mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_type_t *type = mp_obj_get_type(args[0]);
|
|
|
|
mp_obj_t *value = args[1];
|
|
|
|
uint start = 0;
|
|
|
|
uint stop = len;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 16:46:47 -05:00
|
|
|
for (machine_uint_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
|
|
|
}
|
|
|
|
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "object not in sequence"));
|
2014-02-09 23:37:11 -05:00
|
|
|
}
|
2014-02-10 00:10:55 -05:00
|
|
|
|
|
|
|
mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value) {
|
2014-02-10 16:46:47 -05:00
|
|
|
machine_uint_t count = 0;
|
2014-02-10 00:10:55 -05:00
|
|
|
for (uint i = 0; i < len; i++) {
|
|
|
|
if (mp_obj_equal(items[i], value)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Common sense says this cannot overflow small int
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(count);
|
|
|
|
}
|