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
27#include <unistd.h> // for ssize_t
28#include <string.h>
29
30#include "py/mpconfig.h"
31#if MICROPY_PY_COLLECTIONS_DEQUE
32
33#include "py/runtime.h"
34
35typedef struct _mp_obj_deque_t {
36 mp_obj_base_t base;
37 size_t alloc;
38 size_t i_get;
39 size_t i_put;
40 mp_obj_t *items;
41 uint32_t flags;
42 #define FLAG_CHECK_OVERFLOW 1
43} mp_obj_deque_t;
44
45STATIC 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) {
46 mp_arg_check_num(n_args, n_kw, 2, 3, false);
47
48 /* Initialization from existing sequence is not supported, so an empty
49 tuple must be passed as such. */
50 if (args[0] != mp_const_empty_tuple) {
51 mp_raise_ValueError(NULL);
52 }
53
54 // Protect against -1 leading to zero-length allocation and bad array access
55 mp_int_t maxlen = mp_obj_get_int(args[1]);
56 if (maxlen < 0) {
57 mp_raise_ValueError(NULL);
58 }
59
60 mp_obj_deque_t *o = m_new_obj(mp_obj_deque_t);
61 o->base.type = type;
62 o->alloc = maxlen + 1;
63 o->i_get = o->i_put = 0;
64 o->items = m_new0(mp_obj_t, o->alloc);
65
66 if (n_args > 2) {
67 o->flags = mp_obj_get_int(args[2]);
68 }
69
70 return MP_OBJ_FROM_PTR(o);
71}
72
73STATIC mp_obj_t deque_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
74 mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
75 switch (op) {
76 case MP_UNARY_OP_BOOL:
77 return mp_obj_new_bool(self->i_get != self->i_put);
78 case MP_UNARY_OP_LEN: {
79 ssize_t len = self->i_put - self->i_get;
80 if (len < 0) {
81 len += self->alloc;
82 }
83 return MP_OBJ_NEW_SMALL_INT(len);
84 }
85 #if MICROPY_PY_SYS_GETSIZEOF
86 case MP_UNARY_OP_SIZEOF: {
87 size_t sz = sizeof(*self) + sizeof(mp_obj_t) * self->alloc;
88 return MP_OBJ_NEW_SMALL_INT(sz);
89 }
90 #endif
91 default:
92 return MP_OBJ_NULL; // op not supported
93 }
94}
95
96STATIC mp_obj_t mp_obj_deque_append(mp_obj_t self_in, mp_obj_t arg) {
97 mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
98
99 size_t new_i_put = self->i_put + 1;
100 if (new_i_put == self->alloc) {
101 new_i_put = 0;
102 }
103
104 if (self->flags & FLAG_CHECK_OVERFLOW && new_i_put == self->i_get) {
105 mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("full"));
106 }
107
108 self->items[self->i_put] = arg;
109 self->i_put = new_i_put;
110
111 if (self->i_get == new_i_put) {
112 if (++self->i_get == self->alloc) {
113 self->i_get = 0;
114 }
115 }
116
117 return mp_const_none;
118}
119STATIC MP_DEFINE_CONST_FUN_OBJ_2(deque_append_obj, mp_obj_deque_append);
120
121STATIC mp_obj_t deque_popleft(mp_obj_t self_in) {
122 mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
123
124 if (self->i_get == self->i_put) {
125 mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("empty"));
126 }
127
128 mp_obj_t ret = self->items[self->i_get];
129 self->items[self->i_get] = MP_OBJ_NULL;
130
131 if (++self->i_get == self->alloc) {
132 self->i_get = 0;
133 }
134
135 return ret;
136}
137STATIC MP_DEFINE_CONST_FUN_OBJ_1(deque_popleft_obj, deque_popleft);
138
139#if 0
140STATIC mp_obj_t deque_clear(mp_obj_t self_in) {
141 mp_obj_deque_t *self = MP_OBJ_TO_PTR(self_in);
142 self->i_get = self->i_put = 0;
143 mp_seq_clear(self->items, 0, self->alloc, sizeof(*self->items));
144 return mp_const_none;
145}
146STATIC MP_DEFINE_CONST_FUN_OBJ_1(deque_clear_obj, deque_clear);
147#endif
148
149STATIC const mp_rom_map_elem_t deque_locals_dict_table[] = {
150 { MP_ROM_QSTR(MP_QSTR_append), MP_ROM_PTR(&deque_append_obj) },
151 #if 0
152 { MP_ROM_QSTR(MP_QSTR_clear), MP_ROM_PTR(&deque_clear_obj) },
153 #endif
154 { MP_ROM_QSTR(MP_QSTR_popleft), MP_ROM_PTR(&deque_popleft_obj) },
155};
156
157STATIC MP_DEFINE_CONST_DICT(deque_locals_dict, deque_locals_dict_table);
158
159const mp_obj_type_t mp_type_deque = {
160 { &mp_type_type },
161 .name = MP_QSTR_deque,
162 .make_new = deque_make_new,
163 .unary_op = deque_unary_op,
164 .locals_dict = (mp_obj_dict_t *)&deque_locals_dict,
165};
166
167#endif // MICROPY_PY_COLLECTIONS_DEQUE
168