objdeque.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2018 Paul Sokolovsky
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <unistd.h> // for ssize_t
  27. #include <string.h>
  28. #include "py/mpconfig.h"
  29. #if MICROPY_PY_COLLECTIONS_DEQUE
  30. #include "py/runtime.h"
  31. typedef struct _mp_obj_deque_t {
  32. mp_obj_base_t base;
  33. size_t alloc;
  34. size_t i_get;
  35. size_t i_put;
  36. mp_obj_t *items;
  37. uint32_t flags;
  38. #define FLAG_CHECK_OVERFLOW 1
  39. } mp_obj_deque_t;
  40. STATIC mp_obj_t deque_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  41. mp_arg_check_num(n_args, n_kw, 2, 3, false);
  42. /* Initialization from existing sequence is not supported, so an empty
  43. tuple must be passed as such. */
  44. if (args[0] != mp_const_empty_tuple) {
  45. mp_raise_ValueError(NULL);
  46. }
  47. // Protect against -1 leading to zero-length allocation and bad array access
  48. mp_int_t maxlen = mp_obj_get_int(args[1]);
  49. if (maxlen < 0) {
  50. mp_raise_ValueError(NULL);
  51. }
  52. mp_obj_deque_t *o = m_new_obj(mp_obj_deque_t);
  53. o->base.type = type;
  54. o->alloc = maxlen + 1;
  55. o->i_get = o->i_put = 0;
  56. o->items = m_new0(mp_obj_t, o->alloc);
  57. if (n_args > 2) {
  58. o->flags = mp_obj_get_int(args[2]);
  59. }
  60. return MP_OBJ_FROM_PTR(o);
  61. }
  62. STATIC mp_obj_t deque_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
  63. mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
  64. switch (op) {
  65. case MP_UNARY_OP_BOOL:
  66. return mp_obj_new_bool(self->i_get != self->i_put);
  67. case MP_UNARY_OP_LEN: {
  68. ssize_t len = self->i_put - self->i_get;
  69. if (len < 0) {
  70. len += self->alloc;
  71. }
  72. return MP_OBJ_NEW_SMALL_INT(len);
  73. }
  74. #if MICROPY_PY_SYS_GETSIZEOF
  75. case MP_UNARY_OP_SIZEOF: {
  76. size_t sz = sizeof(*self) + sizeof(mp_obj_t) * self->alloc;
  77. return MP_OBJ_NEW_SMALL_INT(sz);
  78. }
  79. #endif
  80. default:
  81. return MP_OBJ_NULL; // op not supported
  82. }
  83. }
  84. STATIC mp_obj_t mp_obj_deque_append(mp_obj_t self_in, mp_obj_t arg) {
  85. mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
  86. size_t new_i_put = self->i_put + 1;
  87. if (new_i_put == self->alloc) {
  88. new_i_put = 0;
  89. }
  90. if (self->flags & FLAG_CHECK_OVERFLOW && new_i_put == self->i_get) {
  91. mp_raise_msg(&mp_type_IndexError, "full");
  92. }
  93. self->items[self->i_put] = arg;
  94. self->i_put = new_i_put;
  95. if (self->i_get == new_i_put) {
  96. if (++self->i_get == self->alloc) {
  97. self->i_get = 0;
  98. }
  99. }
  100. return mp_const_none;
  101. }
  102. STATIC MP_DEFINE_CONST_FUN_OBJ_2(deque_append_obj, mp_obj_deque_append);
  103. STATIC mp_obj_t deque_popleft(mp_obj_t self_in) {
  104. mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
  105. if (self->i_get == self->i_put) {
  106. mp_raise_msg(&mp_type_IndexError, "empty");
  107. }
  108. mp_obj_t ret = self->items[self->i_get];
  109. self->items[self->i_get] = MP_OBJ_NULL;
  110. if (++self->i_get == self->alloc) {
  111. self->i_get = 0;
  112. }
  113. return ret;
  114. }
  115. STATIC MP_DEFINE_CONST_FUN_OBJ_1(deque_popleft_obj, deque_popleft);
  116. #if 0
  117. STATIC mp_obj_t deque_clear(mp_obj_t self_in) {
  118. mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
  119. self->i_get = self->i_put = 0;
  120. mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
  121. return mp_const_none;
  122. }
  123. STATIC MP_DEFINE_CONST_FUN_OBJ_1(deque_clear_obj, deque_clear);
  124. #endif
  125. STATIC const mp_rom_map_elem_t deque_locals_dict_table[] = {
  126. { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&deque_append_obj) },
  127. #if 0
  128. { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&deque_clear_obj) },
  129. #endif
  130. { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&deque_popleft_obj) },
  131. };
  132. STATIC MP_DEFINE_CONST_DICT(deque_locals_dict, deque_locals_dict_table);
  133. const mp_obj_type_t mp_type_deque = {
  134. { &mp_type_type },
  135. .name = MP_QSTR_deque,
  136. .make_new = deque_make_new,
  137. .unary_op = deque_unary_op,
  138. .locals_dict = (mp_obj_dict_t*)&deque_locals_dict,
  139. };
  140. #endif // MICROPY_PY_COLLECTIONS_DEQUE