sequence.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2014 Paul Sokolovsky
  8. *
  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. #include <string.h>
  28. #include "py/runtime.h"
  29. // Helpers for sequence types
  30. #define SWAP(type, var1, var2) { type t = var2; var2 = var1; var1 = t; }
  31. // Implements backend of sequence * integer operation. Assumes elements are
  32. // memory-adjacent in sequence.
  33. void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest) {
  34. for (size_t i = 0; i < times; i++) {
  35. size_t copy_sz = item_sz * len;
  36. memcpy(dest, items, copy_sz);
  37. dest = (char*)dest + copy_sz;
  38. }
  39. }
  40. #if MICROPY_PY_BUILTINS_SLICE
  41. bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes) {
  42. mp_obj_t ostart, ostop, ostep;
  43. mp_int_t start, stop;
  44. mp_obj_slice_get(slice, &ostart, &ostop, &ostep);
  45. if (ostep != mp_const_none && ostep != MP_OBJ_NEW_SMALL_INT(1)) {
  46. indexes->step = mp_obj_get_int(ostep);
  47. if (indexes->step == 0) {
  48. mp_raise_ValueError("slice step cannot be zero");
  49. }
  50. } else {
  51. indexes->step = 1;
  52. }
  53. if (ostart == mp_const_none) {
  54. if (indexes->step > 0) {
  55. start = 0;
  56. } else {
  57. start = len - 1;
  58. }
  59. } else {
  60. start = mp_obj_get_int(ostart);
  61. }
  62. if (ostop == mp_const_none) {
  63. if (indexes->step > 0) {
  64. stop = len;
  65. } else {
  66. stop = 0;
  67. }
  68. } else {
  69. stop = mp_obj_get_int(ostop);
  70. if (stop >= 0 && indexes->step < 0) {
  71. stop += 1;
  72. }
  73. }
  74. // Unlike subscription, out-of-bounds slice indexes are never error
  75. if (start < 0) {
  76. start = len + start;
  77. if (start < 0) {
  78. if (indexes->step < 0) {
  79. start = -1;
  80. } else {
  81. start = 0;
  82. }
  83. }
  84. } else if (indexes->step > 0 && (mp_uint_t)start > len) {
  85. start = len;
  86. } else if (indexes->step < 0 && (mp_uint_t)start >= len) {
  87. start = len - 1;
  88. }
  89. if (stop < 0) {
  90. stop = len + stop;
  91. if (stop < 0) {
  92. stop = -1;
  93. }
  94. if (indexes->step < 0) {
  95. stop += 1;
  96. }
  97. } else if ((mp_uint_t)stop > len) {
  98. stop = len;
  99. }
  100. // CPython returns empty sequence in such case, or point for assignment is at start
  101. if (indexes->step > 0 && start > stop) {
  102. stop = start;
  103. } else if (indexes->step < 0 && start < stop) {
  104. stop = start + 1;
  105. }
  106. indexes->start = start;
  107. indexes->stop = stop;
  108. return indexes->step == 1;
  109. }
  110. #endif
  111. mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
  112. (void)len; // TODO can we remove len from the arg list?
  113. mp_int_t start = indexes->start, stop = indexes->stop;
  114. mp_int_t step = indexes->step;
  115. mp_obj_t res = mp_obj_new_list(0, NULL);
  116. if (step < 0) {
  117. while (start >= stop) {
  118. mp_obj_list_append(res, seq[start]);
  119. start += step;
  120. }
  121. } else {
  122. while (start < stop) {
  123. mp_obj_list_append(res, seq[start]);
  124. start += step;
  125. }
  126. }
  127. return res;
  128. }
  129. // Special-case comparison function for sequences of bytes
  130. // Don't pass MP_BINARY_OP_NOT_EQUAL here
  131. bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2) {
  132. if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
  133. return false;
  134. }
  135. // Let's deal only with > & >=
  136. if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
  137. SWAP(const byte*, data1, data2);
  138. SWAP(size_t, len1, len2);
  139. if (op == MP_BINARY_OP_LESS) {
  140. op = MP_BINARY_OP_MORE;
  141. } else {
  142. op = MP_BINARY_OP_MORE_EQUAL;
  143. }
  144. }
  145. size_t min_len = len1 < len2 ? len1 : len2;
  146. int res = memcmp(data1, data2, min_len);
  147. if (op == MP_BINARY_OP_EQUAL) {
  148. // If we are checking for equality, here're the answer
  149. return res == 0;
  150. }
  151. if (res < 0) {
  152. return false;
  153. }
  154. if (res > 0) {
  155. return true;
  156. }
  157. // If we had tie in the last element...
  158. // ... and we have lists of different lengths...
  159. if (len1 != len2) {
  160. if (len1 < len2) {
  161. // ... then longer list length wins (we deal only with >)
  162. return false;
  163. }
  164. } else if (op == MP_BINARY_OP_MORE) {
  165. // Otherwise, if we have strict relation, equality means failure
  166. return false;
  167. }
  168. return true;
  169. }
  170. // Special-case comparison function for sequences of mp_obj_t
  171. // Don't pass MP_BINARY_OP_NOT_EQUAL here
  172. 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) {
  173. if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
  174. return false;
  175. }
  176. // Let's deal only with > & >=
  177. if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
  178. SWAP(const mp_obj_t *, items1, items2);
  179. SWAP(size_t, len1, len2);
  180. if (op == MP_BINARY_OP_LESS) {
  181. op = MP_BINARY_OP_MORE;
  182. } else {
  183. op = MP_BINARY_OP_MORE_EQUAL;
  184. }
  185. }
  186. size_t len = len1 < len2 ? len1 : len2;
  187. for (size_t i = 0; i < len; i++) {
  188. // If current elements equal, can't decide anything - go on
  189. if (mp_obj_equal(items1[i], items2[i])) {
  190. continue;
  191. }
  192. // Othewise, if they are not equal, we can have final decision based on them
  193. if (op == MP_BINARY_OP_EQUAL) {
  194. // In particular, if we are checking for equality, here're the answer
  195. return false;
  196. }
  197. // Otherwise, application of relation op gives the answer
  198. return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
  199. }
  200. // If we had tie in the last element...
  201. // ... and we have lists of different lengths...
  202. if (len1 != len2) {
  203. if (len1 < len2) {
  204. // ... then longer list length wins (we deal only with >)
  205. return false;
  206. }
  207. } else if (op == MP_BINARY_OP_MORE) {
  208. // Otherwise, if we have strict relation, sequence equality means failure
  209. return false;
  210. }
  211. return true;
  212. }
  213. // Special-case of index() which searches for mp_obj_t
  214. mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args) {
  215. mp_obj_type_t *type = mp_obj_get_type(args[0]);
  216. mp_obj_t value = args[1];
  217. size_t start = 0;
  218. size_t stop = len;
  219. if (n_args >= 3) {
  220. start = mp_get_index(type, len, args[2], true);
  221. if (n_args >= 4) {
  222. stop = mp_get_index(type, len, args[3], true);
  223. }
  224. }
  225. for (size_t i = start; i < stop; i++) {
  226. if (mp_obj_equal(items[i], value)) {
  227. // Common sense says this cannot overflow small int
  228. return MP_OBJ_NEW_SMALL_INT(i);
  229. }
  230. }
  231. mp_raise_ValueError("object not in sequence");
  232. }
  233. mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value) {
  234. size_t count = 0;
  235. for (size_t i = 0; i < len; i++) {
  236. if (mp_obj_equal(items[i], value)) {
  237. count++;
  238. }
  239. }
  240. // Common sense says this cannot overflow small int
  241. return MP_OBJ_NEW_SMALL_INT(count);
  242. }