sequence.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_slice_indices(slice, len, indexes);
  43. // If the index is negative then stop points to the last item, not after it
  44. if (indexes->step < 0) {
  45. indexes->stop++;
  46. }
  47. // CPython returns empty sequence in such case, or point for assignment is at start
  48. if (indexes->step > 0 && indexes->start > indexes->stop) {
  49. indexes->stop = indexes->start;
  50. } else if (indexes->step < 0 && indexes->start < indexes->stop) {
  51. indexes->stop = indexes->start + 1;
  52. }
  53. return indexes->step == 1;
  54. }
  55. #endif
  56. mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes) {
  57. (void)len; // TODO can we remove len from the arg list?
  58. mp_int_t start = indexes->start, stop = indexes->stop;
  59. mp_int_t step = indexes->step;
  60. mp_obj_t res = mp_obj_new_list(0, NULL);
  61. if (step < 0) {
  62. while (start >= stop) {
  63. mp_obj_list_append(res, seq[start]);
  64. start += step;
  65. }
  66. } else {
  67. while (start < stop) {
  68. mp_obj_list_append(res, seq[start]);
  69. start += step;
  70. }
  71. }
  72. return res;
  73. }
  74. // Special-case comparison function for sequences of bytes
  75. // Don't pass MP_BINARY_OP_NOT_EQUAL here
  76. bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2) {
  77. if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
  78. return false;
  79. }
  80. // Let's deal only with > & >=
  81. if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
  82. SWAP(const byte *, data1, data2);
  83. SWAP(size_t, len1, len2);
  84. if (op == MP_BINARY_OP_LESS) {
  85. op = MP_BINARY_OP_MORE;
  86. } else {
  87. op = MP_BINARY_OP_MORE_EQUAL;
  88. }
  89. }
  90. size_t min_len = len1 < len2 ? len1 : len2;
  91. int res = memcmp(data1, data2, min_len);
  92. if (op == MP_BINARY_OP_EQUAL) {
  93. // If we are checking for equality, here's the answer
  94. return res == 0;
  95. }
  96. if (res < 0) {
  97. return false;
  98. }
  99. if (res > 0) {
  100. return true;
  101. }
  102. // If we had tie in the last element...
  103. // ... and we have lists of different lengths...
  104. if (len1 != len2) {
  105. if (len1 < len2) {
  106. // ... then longer list length wins (we deal only with >)
  107. return false;
  108. }
  109. } else if (op == MP_BINARY_OP_MORE) {
  110. // Otherwise, if we have strict relation, equality means failure
  111. return false;
  112. }
  113. return true;
  114. }
  115. // Special-case comparison function for sequences of mp_obj_t
  116. // Don't pass MP_BINARY_OP_NOT_EQUAL here
  117. 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) {
  118. if (op == MP_BINARY_OP_EQUAL && len1 != len2) {
  119. return false;
  120. }
  121. // Let's deal only with > & >=
  122. if (op == MP_BINARY_OP_LESS || op == MP_BINARY_OP_LESS_EQUAL) {
  123. SWAP(const mp_obj_t *, items1, items2);
  124. SWAP(size_t, len1, len2);
  125. if (op == MP_BINARY_OP_LESS) {
  126. op = MP_BINARY_OP_MORE;
  127. } else {
  128. op = MP_BINARY_OP_MORE_EQUAL;
  129. }
  130. }
  131. size_t len = len1 < len2 ? len1 : len2;
  132. for (size_t i = 0; i < len; i++) {
  133. // If current elements equal, can't decide anything - go on
  134. if (mp_obj_equal(items1[i], items2[i])) {
  135. continue;
  136. }
  137. // Otherwise, if they are not equal, we can have final decision based on them
  138. if (op == MP_BINARY_OP_EQUAL) {
  139. // In particular, if we are checking for equality, here're the answer
  140. return false;
  141. }
  142. // Otherwise, application of relation op gives the answer
  143. return mp_binary_op(op, items1[i], items2[i]) == mp_const_true;
  144. }
  145. // If we had tie in the last element...
  146. // ... and we have lists of different lengths...
  147. if (len1 != len2) {
  148. if (len1 < len2) {
  149. // ... then longer list length wins (we deal only with >)
  150. return false;
  151. }
  152. } else if (op == MP_BINARY_OP_MORE) {
  153. // Otherwise, if we have strict relation, sequence equality means failure
  154. return false;
  155. }
  156. return true;
  157. }
  158. // Special-case of index() which searches for mp_obj_t
  159. mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args) {
  160. const mp_obj_type_t *type = mp_obj_get_type(args[0]);
  161. mp_obj_t value = args[1];
  162. size_t start = 0;
  163. size_t stop = len;
  164. if (n_args >= 3) {
  165. start = mp_get_index(type, len, args[2], true);
  166. if (n_args >= 4) {
  167. stop = mp_get_index(type, len, args[3], true);
  168. }
  169. }
  170. for (size_t i = start; i < stop; i++) {
  171. if (mp_obj_equal(items[i], value)) {
  172. // Common sense says this cannot overflow small int
  173. return MP_OBJ_NEW_SMALL_INT(i);
  174. }
  175. }
  176. mp_raise_ValueError(MP_ERROR_TEXT("object not in sequence"));
  177. }
  178. mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value) {
  179. size_t count = 0;
  180. for (size_t i = 0; i < len; i++) {
  181. if (mp_obj_equal(items[i], value)) {
  182. count++;
  183. }
  184. }
  185. // Common sense says this cannot overflow small int
  186. return MP_OBJ_NEW_SMALL_INT(count);
  187. }