From 0ef01d0a75b8b2f48a72f0041e048a390b9e75b6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 18 Mar 2015 01:25:04 +0200 Subject: py: Implement core of OrderedDict type. Given that there's already support for "fixed table" maps, which are essentially ordered maps, the implementation of OrderedDict just extends "fixed table" maps by adding an "is ordered" flag and add/remove operations, and reuses 95% of objdict code, just making methods tolerant to both dict and OrderedDict. Some things are missing so far, like CPython-compatible repr and comparison. OrderedDict is Disabled by default; enabled on unix and stmhal ports. --- py/obj.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'py/obj.h') diff --git a/py/obj.h b/py/obj.h index c58b72f08a..3e2cd2a16f 100644 --- a/py/obj.h +++ b/py/obj.h @@ -109,7 +109,8 @@ typedef struct _mp_obj_base_t mp_obj_base_t; #define MP_DEFINE_CONST_MAP(map_name, table_name) \ const mp_map_t map_name = { \ .all_keys_are_qstrs = 1, \ - .table_is_fixed_array = 1, \ + .is_fixed = 1, \ + .is_ordered = 1, \ .used = MP_ARRAY_SIZE(table_name), \ .alloc = MP_ARRAY_SIZE(table_name), \ .table = (mp_map_elem_t*)table_name, \ @@ -120,7 +121,8 @@ typedef struct _mp_obj_base_t mp_obj_base_t; .base = {&mp_type_dict}, \ .map = { \ .all_keys_are_qstrs = 1, \ - .table_is_fixed_array = 1, \ + .is_fixed = 1, \ + .is_ordered = 1, \ .used = MP_ARRAY_SIZE(table_name), \ .alloc = MP_ARRAY_SIZE(table_name), \ .table = (mp_map_elem_t*)table_name, \ @@ -150,8 +152,9 @@ typedef struct _mp_map_elem_t { typedef struct _mp_map_t { mp_uint_t all_keys_are_qstrs : 1; - mp_uint_t table_is_fixed_array : 1; - mp_uint_t used : (8 * sizeof(mp_uint_t) - 2); + mp_uint_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered + mp_uint_t is_ordered : 1; // an ordered array + mp_uint_t used : (8 * sizeof(mp_uint_t) - 3); mp_uint_t alloc; mp_map_elem_t *table; } mp_map_t; @@ -327,6 +330,7 @@ extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict extern const mp_obj_type_t mp_type_enumerate; extern const mp_obj_type_t mp_type_filter; extern const mp_obj_type_t mp_type_dict; +extern const mp_obj_type_t mp_type_ordereddict; extern const mp_obj_type_t mp_type_range; extern const mp_obj_type_t mp_type_set; extern const mp_obj_type_t mp_type_frozenset; -- cgit v1.2.3