Paul Sokolovsky | 439542f | 2014-01-21 00:19:19 +0200 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "nlr.h" |
| 7 | #include "misc.h" |
| 8 | #include "mpconfig.h" |
Damien George | 12eacca | 2014-01-21 21:54:15 +0000 | [diff] [blame] | 9 | #include "qstr.h" |
Paul Sokolovsky | 439542f | 2014-01-21 00:19:19 +0200 | [diff] [blame] | 10 | #include "obj.h" |
| 11 | #include "map.h" |
| 12 | #include "runtime0.h" |
| 13 | #include "runtime.h" |
| 14 | |
| 15 | // Helpers for sequence types |
| 16 | |
Paul Sokolovsky | 87e85b7 | 2014-02-02 08:24:07 +0200 | [diff] [blame] | 17 | #define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; } |
| 18 | |
Paul Sokolovsky | 439542f | 2014-01-21 00:19:19 +0200 | [diff] [blame] | 19 | // Implements backend of sequence * integer operation. Assumes elements are |
| 20 | // memory-adjacent in sequence. |
| 21 | void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest) { |
| 22 | for (int i = 0; i < times; i++) { |
| 23 | uint copy_sz = item_sz * len; |
| 24 | memcpy(dest, items, copy_sz); |
| 25 | dest = (char*)dest + copy_sz; |
| 26 | } |
| 27 | } |
Paul Sokolovsky | 7364af2 | 2014-02-02 02:38:22 +0200 | [diff] [blame] | 28 | |
| 29 | bool m_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end) { |
| 30 | machine_int_t start, stop, step; |
| 31 | mp_obj_slice_get(slice, &start, &stop, &step); |
| 32 | if (step != 1) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | // Unlike subscription, out-of-bounds slice indexes are never error |
| 37 | if (start < 0) { |
| 38 | start = len + start; |
| 39 | if (start < 0) { |
| 40 | start = 0; |
| 41 | } |
| 42 | } else if (start > len) { |
| 43 | start = len; |
| 44 | } |
| 45 | if (stop <= 0) { |
| 46 | stop = len + stop; |
| 47 | // CPython returns empty sequence in such case |
| 48 | if (stop < 0) { |
| 49 | stop = start; |
| 50 | } |
| 51 | } else if (stop > len) { |
| 52 | stop = len; |
| 53 | } |
| 54 | *begin = start; |
| 55 | *end = stop; |
| 56 | return true; |
| 57 | } |
Paul Sokolovsky | 87e85b7 | 2014-02-02 08:24:07 +0200 | [diff] [blame] | 58 | |
| 59 | // Special-case comparison function for sequences of bytes |
| 60 | // Don't pass RT_BINARY_OP_NOT_EQUAL here |
| 61 | bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2) { |
| 62 | // Let's deal only with > & >= |
| 63 | if (op == RT_BINARY_OP_LESS || op == RT_BINARY_OP_LESS_EQUAL) { |
| 64 | SWAP(const byte*, data1, data2); |
| 65 | SWAP(uint, len1, len2); |
| 66 | if (op == RT_BINARY_OP_LESS) { |
| 67 | op = RT_BINARY_OP_MORE; |
| 68 | } else { |
| 69 | op = RT_BINARY_OP_MORE_EQUAL; |
| 70 | } |
| 71 | } |
| 72 | uint min_len = len1 < len2 ? len1 : len2; |
| 73 | int res = memcmp(data1, data2, min_len); |
| 74 | if (res < 0) { |
| 75 | return false; |
| 76 | } |
| 77 | if (res > 0) { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // If we had tie in the last element... |
| 82 | // ... and we have lists of different lengths... |
| 83 | if (len1 != len2) { |
| 84 | if (len1 < len2) { |
| 85 | // ... then longer list length wins (we deal only with >) |
| 86 | return false; |
| 87 | } |
| 88 | } else if (op == RT_BINARY_OP_MORE) { |
| 89 | // Otherwise, if we have strict relation, equality means failure |
| 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
Paul Sokolovsky | 1a996c4 | 2014-02-08 22:49:46 +0200 | [diff] [blame] | 94 | |
| 95 | // Special-case comparison function for sequences of mp_obj_t |
| 96 | // Don't pass RT_BINARY_OP_NOT_EQUAL here |
| 97 | bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2) { |
| 98 | if (op == RT_BINARY_OP_EQUAL && len1 != len2) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // Let's deal only with > & >= |
| 103 | if (op == RT_BINARY_OP_LESS || op == RT_BINARY_OP_LESS_EQUAL) { |
| 104 | SWAP(const mp_obj_t *, items1, items2); |
| 105 | SWAP(uint, len1, len2); |
| 106 | if (op == RT_BINARY_OP_LESS) { |
| 107 | op = RT_BINARY_OP_MORE; |
| 108 | } else { |
| 109 | op = RT_BINARY_OP_MORE_EQUAL; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | int len = len1 < len2 ? len1 : len2; |
| 114 | bool eq_status = true; // empty lists are equal |
| 115 | bool rel_status; |
| 116 | for (int i = 0; i < len; i++) { |
| 117 | eq_status = mp_obj_equal(items1[i], items2[i]); |
| 118 | if (op == RT_BINARY_OP_EQUAL && !eq_status) { |
| 119 | return false; |
| 120 | } |
| 121 | rel_status = (rt_binary_op(op, items1[i], items2[i]) == mp_const_true); |
| 122 | if (!eq_status && !rel_status) { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // If we had tie in the last element... |
| 128 | if (eq_status) { |
| 129 | // ... and we have lists of different lengths... |
| 130 | if (len1 != len2) { |
| 131 | if (len1 < len2) { |
| 132 | // ... then longer list length wins (we deal only with >) |
| 133 | return false; |
| 134 | } |
| 135 | } else if (op == RT_BINARY_OP_MORE) { |
| 136 | // Otherwise, if we have strict relation, equality means failure |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return true; |
| 142 | } |
Paul Sokolovsky | 0cd1dc0 | 2014-02-10 06:37:11 +0200 | [diff] [blame^] | 143 | |
| 144 | // Special-case of index() which searches for mp_obj_t |
| 145 | mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args) { |
| 146 | mp_obj_type_t *type = mp_obj_get_type(args[0]); |
| 147 | mp_obj_t *value = args[1]; |
| 148 | uint start = 0; |
| 149 | uint stop = len; |
| 150 | |
| 151 | if (n_args >= 3) { |
| 152 | start = mp_get_index(type, len, args[2]); |
| 153 | if (n_args >= 4) { |
| 154 | stop = mp_get_index(type, len, args[3]); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | for (uint i = start; i < stop; i++) { |
| 159 | if (mp_obj_equal(items[i], value)) { |
| 160 | return mp_obj_new_int_from_uint(i); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | nlr_jump(mp_obj_new_exception_msg(MP_QSTR_ValueError, "object not in sequence")); |
| 165 | } |