2018-02-05 22:06:42 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Paul Sokolovsky
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-05-11 04:44:50 +01:00
|
|
|
#include <unistd.h> // for ssize_t
|
2018-02-05 22:06:42 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#if MICROPY_PY_COLLECTIONS_DEQUE
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
typedef struct _mp_obj_deque_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
size_t alloc;
|
|
|
|
size_t i_get;
|
|
|
|
size_t i_put;
|
|
|
|
mp_obj_t *items;
|
|
|
|
uint32_t flags;
|
|
|
|
#define FLAG_CHECK_OVERFLOW 1
|
|
|
|
} mp_obj_deque_t;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
mp_arg_check_num(n_args, n_kw, 2, 3, false);
|
|
|
|
|
|
|
|
/* Initialization from existing sequence is not supported, so an empty
|
|
|
|
tuple must be passed as such. */
|
|
|
|
if (args[0] != mp_const_empty_tuple) {
|
|
|
|
mp_raise_ValueError(NULL);
|
|
|
|
}
|
|
|
|
|
2018-02-21 12:34:17 +00:00
|
|
|
// Protect against -1 leading to zero-length allocation and bad array access
|
|
|
|
mp_int_t maxlen = mp_obj_get_int(args[1]);
|
|
|
|
if (maxlen < 0) {
|
|
|
|
mp_raise_ValueError(NULL);
|
|
|
|
}
|
|
|
|
|
2018-02-05 22:06:42 +00:00
|
|
|
mp_obj_deque_t *o = m_new_obj(mp_obj_deque_t);
|
|
|
|
o->base.type = type;
|
2018-02-21 12:34:17 +00:00
|
|
|
o->alloc = maxlen + 1;
|
2018-02-05 22:06:42 +00:00
|
|
|
o->i_get = o->i_put = 0;
|
2018-02-21 12:36:46 +00:00
|
|
|
o->items = m_new0(mp_obj_t, o->alloc);
|
2018-02-05 22:06:42 +00:00
|
|
|
|
|
|
|
if (n_args > 2) {
|
|
|
|
o->flags = mp_obj_get_int(args[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t deque_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|
|
|
mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
switch (op) {
|
|
|
|
case MP_UNARY_OP_BOOL:
|
|
|
|
return mp_obj_new_bool(self->i_get != self->i_put);
|
|
|
|
case MP_UNARY_OP_LEN: {
|
2018-05-11 04:44:50 +01:00
|
|
|
ssize_t len = self->i_put - self->i_get;
|
2018-02-05 22:06:42 +00:00
|
|
|
if (len < 0) {
|
|
|
|
len += self->alloc;
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(len);
|
|
|
|
}
|
|
|
|
#if MICROPY_PY_SYS_GETSIZEOF
|
|
|
|
case MP_UNARY_OP_SIZEOF: {
|
|
|
|
size_t sz = sizeof(*self) + sizeof(mp_obj_t) * self->alloc;
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(sz);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t mp_obj_deque_append(mp_obj_t self_in, mp_obj_t arg) {
|
|
|
|
mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
size_t new_i_put = self->i_put + 1;
|
|
|
|
if (new_i_put == self->alloc) {
|
|
|
|
new_i_put = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->flags & FLAG_CHECK_OVERFLOW && new_i_put == self->i_get) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("full"));
|
2018-02-05 22:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self->items[self->i_put] = arg;
|
|
|
|
self->i_put = new_i_put;
|
|
|
|
|
|
|
|
if (self->i_get == new_i_put) {
|
|
|
|
if (++self->i_get == self->alloc) {
|
|
|
|
self->i_get = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(deque_append_obj, mp_obj_deque_append);
|
|
|
|
|
|
|
|
STATIC mp_obj_t deque_popleft(mp_obj_t self_in) {
|
|
|
|
mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
if (self->i_get == self->i_put) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("empty"));
|
2018-02-05 22:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t ret = self->items[self->i_get];
|
|
|
|
self->items[self->i_get] = MP_OBJ_NULL;
|
|
|
|
|
|
|
|
if (++self->i_get == self->alloc) {
|
|
|
|
self->i_get = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(deque_popleft_obj, deque_popleft);
|
|
|
|
|
2018-02-21 11:52:58 +00:00
|
|
|
#if 0
|
2018-02-05 22:06:42 +00:00
|
|
|
STATIC mp_obj_t deque_clear(mp_obj_t self_in) {
|
|
|
|
mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
self->i_get = self->i_put = 0;
|
|
|
|
mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(deque_clear_obj, deque_clear);
|
2018-02-21 11:52:58 +00:00
|
|
|
#endif
|
2018-02-05 22:06:42 +00:00
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t deque_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&deque_append_obj) },
|
|
|
|
#if 0
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&deque_clear_obj) },
|
|
|
|
#endif
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&deque_popleft_obj) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(deque_locals_dict, deque_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t mp_type_deque = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_deque,
|
|
|
|
.make_new = deque_make_new,
|
|
|
|
.unary_op = deque_unary_op,
|
2020-02-27 04:36:53 +00:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&deque_locals_dict,
|
2018-02-05 22:06:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MICROPY_PY_COLLECTIONS_DEQUE
|