Skip to content

Commit 7775757

Browse files
committed
Merge pull request adafruit#105 from chipaca/listsort
A more python-style list.sort. And keyword arguments.
2 parents 1e40840 + 3391e19 commit 7775757

6 files changed

Lines changed: 97 additions & 30 deletions

File tree

py/obj.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
4242

4343
#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
4444

45-
#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, n_args_min, n_args_max, (void *)fun_name}
46-
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 0, 0, (mp_fun_0_t)fun_name)
47-
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 1, 1, (mp_fun_1_t)fun_name)
48-
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 2, 2, (mp_fun_2_t)fun_name)
49-
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, 3, 3, (mp_fun_3_t)fun_name)
50-
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
51-
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
45+
#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, is_kw, n_args_min, n_args_max, (void *)fun_name}
46+
#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 0, 0, (mp_fun_0_t)fun_name)
47+
#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 1, 1, (mp_fun_1_t)fun_name)
48+
#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 2, 2, (mp_fun_2_t)fun_name)
49+
#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name)
50+
#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
51+
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
52+
#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, 0, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
53+
54+
// Need to declare this here so we are not dependent on map.h
55+
struct _mp_map_t;
5256

5357
// Type definitions for methods
5458

@@ -58,10 +62,12 @@ typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
5862
typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
5963
typedef mp_obj_t (*mp_fun_t)(void);
6064
typedef mp_obj_t (*mp_fun_var_t)(int n, const mp_obj_t *);
65+
typedef mp_obj_t (*mp_fun_kw_t)(mp_obj_t*, struct _mp_map_t*);
6166

6267
typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o);
6368
typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, int n_args, const mp_obj_t *args); // args are in reverse order in the array
6469
typedef mp_obj_t (*mp_call_n_fun_t)(mp_obj_t fun, int n_args, const mp_obj_t *args); // args are in reverse order in the array
70+
typedef mp_obj_t (*mp_call_n_kw_fun_t)(mp_obj_t fun, int n_args, int n_kw, const mp_obj_t *args); // args are in reverse order in the array
6571
typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
6672
typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
6773

@@ -77,6 +83,7 @@ struct _mp_obj_type_t {
7783
mp_make_new_fun_t make_new; // to make an instance of the type
7884

7985
mp_call_n_fun_t call_n;
86+
mp_call_n_kw_fun_t call_n_kw;
8087
mp_unary_op_fun_t unary_op; // can return NULL if op not supported
8188
mp_binary_op_fun_t binary_op; // can return NULL if op not supported
8289

@@ -120,10 +127,6 @@ extern const mp_obj_t mp_const_empty_tuple;
120127
extern const mp_obj_t mp_const_ellipsis;
121128
extern const mp_obj_t mp_const_stop_iteration; // special object indicating end of iteration (not StopIteration exception!)
122129

123-
// Need to declare this here so we are not dependent on map.h
124-
125-
struct _mp_map_t;
126-
127130
// General API for objects
128131

129132
mp_obj_t mp_obj_new_none(void);
@@ -146,8 +149,8 @@ mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun);
146149
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun);
147150
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args);
148151
mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple);
149-
mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items);
150-
mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items);
152+
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
153+
mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items);
151154
mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
152155
mp_obj_t mp_obj_new_list_reverse(uint n, mp_obj_t *items);
153156
mp_obj_t mp_obj_new_dict(int n_args);
@@ -236,7 +239,8 @@ void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *sto
236239
// functions
237240
typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)
238241
mp_obj_base_t base;
239-
machine_uint_t n_args_min; // inclusive
242+
bool is_kw : 1;
243+
machine_uint_t n_args_min : (sizeof(machine_uint_t) - 1); // inclusive
240244
machine_uint_t n_args_max; // inclusive
241245
void *fun;
242246
// TODO add mp_map_t *globals

py/objfun.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717

1818
// mp_obj_fun_native_t defined in obj.h
1919

20+
mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args);
2021
// args are in reverse order in the array
2122
mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
2223
mp_obj_fun_native_t *self = self_in;
24+
if (self->is_kw) {
25+
return fun_native_call_n_kw(self_in, n_args, 0, args);
26+
}
2327
if (self->n_args_min == self->n_args_max) {
2428
// function requires a fixed number of arguments
2529

@@ -69,10 +73,29 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) {
6973
}
7074
}
7175

76+
mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_obj_t *args) {
77+
mp_obj_fun_native_t *self = self_in;
78+
79+
if (!self->is_kw) {
80+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "function does not take keyword arguments"));
81+
}
82+
83+
mp_obj_t *vargs = mp_obj_new_tuple_reverse(n_args, args + 2*n_kw);
84+
mp_map_t *kw_args = mp_map_new(MP_MAP_QSTR, n_kw);
85+
for (int i = 0; i < 2*n_kw; i+=2) {
86+
qstr name = mp_obj_str_get(args[i+1]);
87+
mp_qstr_map_lookup(kw_args, name, true)->value = args[i];
88+
}
89+
mp_obj_t res = ((mp_fun_kw_t)self->fun)(vargs, kw_args);
90+
/* TODO clean up vargs and kw_args */
91+
return res;
92+
}
93+
7294
const mp_obj_type_t fun_native_type = {
7395
{ &mp_const_type },
7496
"function",
7597
.call_n = fun_native_call_n,
98+
.call_n_kw = fun_native_call_n_kw,
7699
};
77100

78101
mp_obj_t rt_make_function_0(mp_fun_0_t fun) {

py/objlist.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "mpconfig.h"
99
#include "mpqstr.h"
1010
#include "obj.h"
11+
#include "map.h"
1112
#include "runtime0.h"
1213
#include "runtime.h"
1314

@@ -120,14 +121,15 @@ static mp_obj_t list_pop(int n_args, const mp_obj_t *args) {
120121
}
121122

122123
// TODO make this conform to CPython's definition of sort
123-
static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
124+
static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn, bool reversed) {
125+
int op = reversed ? RT_COMPARE_OP_MORE : RT_COMPARE_OP_LESS;
124126
while (head < tail) {
125127
mp_obj_t *h = head - 1;
126128
mp_obj_t *t = tail;
127-
mp_obj_t v = rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
129+
mp_obj_t v = key_fn == NULL ? tail[0] : rt_call_function_1(key_fn, tail[0]); // get pivot using key_fn
128130
for (;;) {
129-
do ++h; while (rt_compare_op(RT_COMPARE_OP_LESS, rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
130-
do --t; while (h < t && rt_compare_op(RT_COMPARE_OP_LESS, v, rt_call_function_1(key_fn, t[0])) == mp_const_true);
131+
do ++h; while (rt_compare_op(op, key_fn == NULL ? h[0] : rt_call_function_1(key_fn, h[0]), v) == mp_const_true);
132+
do --t; while (h < t && rt_compare_op(op, v, key_fn == NULL ? t[0] : rt_call_function_1(key_fn, t[0])) == mp_const_true);
131133
if (h >= t) break;
132134
mp_obj_t x = h[0];
133135
h[0] = t[0];
@@ -136,16 +138,31 @@ static void mp_quicksort(mp_obj_t *head, mp_obj_t *tail, mp_obj_t key_fn) {
136138
mp_obj_t x = h[0];
137139
h[0] = tail[0];
138140
tail[0] = x;
139-
mp_quicksort(head, t, key_fn);
141+
mp_quicksort(head, t, key_fn, reversed);
140142
head = h + 1;
141143
}
142144
}
143145

144-
static mp_obj_t list_sort(mp_obj_t self_in, mp_obj_t key_fn) {
145-
assert(MP_OBJ_IS_TYPE(self_in, &list_type));
146-
mp_obj_list_t *self = self_in;
146+
static mp_obj_t list_sort(mp_obj_t *args, mp_map_t *kwargs) {
147+
mp_obj_t *args_items = NULL;
148+
machine_uint_t args_len = 0;
149+
qstr key_idx = qstr_from_str_static("key");
150+
qstr reverse_idx = qstr_from_str_static("reverse");
151+
152+
assert(MP_OBJ_IS_TYPE(args, &tuple_type));
153+
mp_obj_tuple_get(args, &args_len, &args_items);
154+
assert(args_len >= 1);
155+
if (args_len > 1) {
156+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
157+
"list.sort takes no positional arguments"));
158+
}
159+
mp_obj_list_t *self = args_items[0];
147160
if (self->len > 1) {
148-
mp_quicksort(self->items, self->items + self->len - 1, key_fn);
161+
mp_map_elem_t *keyfun = mp_qstr_map_lookup(kwargs, key_idx, false);
162+
mp_map_elem_t *reverse = mp_qstr_map_lookup(kwargs, reverse_idx, false);
163+
mp_quicksort(self->items, self->items + self->len - 1,
164+
keyfun ? keyfun->value : NULL,
165+
reverse && reverse->value ? rt_is_true(reverse->value) : false);
149166
}
150167
return mp_const_none; // return None, as per CPython
151168
}
@@ -259,7 +276,7 @@ static MP_DEFINE_CONST_FUN_OBJ_3(list_insert_obj, list_insert);
259276
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(list_pop_obj, 1, 2, list_pop);
260277
static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
261278
static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
262-
static MP_DEFINE_CONST_FUN_OBJ_2(list_sort_obj, list_sort);
279+
static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort);
263280

264281
static const mp_method_t list_type_methods[] = {
265282
{ "append", &list_append_obj },

py/objtuple.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ const mp_obj_type_t tuple_type = {
109109
static const mp_obj_tuple_t empty_tuple_obj = {{&tuple_type}, 0};
110110
const mp_obj_t mp_const_empty_tuple = (mp_obj_t)&empty_tuple_obj;
111111

112-
mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items) {
112+
mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items) {
113113
if (n == 0) {
114114
return mp_const_empty_tuple;
115115
}
@@ -122,7 +122,7 @@ mp_obj_t mp_obj_new_tuple(uint n, mp_obj_t *items) {
122122
return o;
123123
}
124124

125-
mp_obj_t mp_obj_new_tuple_reverse(uint n, mp_obj_t *items) {
125+
mp_obj_t mp_obj_new_tuple_reverse(uint n, const mp_obj_t *items) {
126126
if (n == 0) {
127127
return mp_const_empty_tuple;
128128
}

py/runtime.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,20 @@ mp_obj_t rt_call_function_n(mp_obj_t fun_in, int n_args, const mp_obj_t *args) {
689689

690690
// args are in reverse order in the array; keyword arguments come first, value then key
691691
// eg: (value1, key1, value0, key0, arg1, arg0)
692-
mp_obj_t rt_call_function_n_kw(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args) {
693-
// TODO
694-
assert(0);
695-
return mp_const_none;
692+
mp_obj_t rt_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
693+
// TODO merge this and _n into a single, smarter thing
694+
DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
695+
696+
if (MP_OBJ_IS_SMALL_INT(fun_in)) {
697+
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "'int' object is not callable"));
698+
} else {
699+
mp_obj_base_t *fun = fun_in;
700+
if (fun->type->call_n_kw != NULL) {
701+
return fun->type->call_n_kw(fun_in, n_args, n_kw, args);
702+
} else {
703+
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "'%s' object is not callable", fun->type->name));
704+
}
705+
}
696706
}
697707

698708
// args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun

tests/basics/tests/list_sort.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
l = [1, 3, 2, 5]
2+
print(l)
3+
l.sort()
4+
print(l)
5+
l.sort(key=lambda x: -x)
6+
print(l)
7+
l.sort(key=lambda x: -x, reverse=True)
8+
print(l)
9+
l.sort(reverse=True)
10+
print(l)
11+
l.sort(reverse=False)
12+
print(l)
13+

0 commit comments

Comments
 (0)