Skip to content

Commit 7cf057a

Browse files
committed
objdict: Implement equality operator.
Sure, it's O(n^2).
1 parent 5fedd0c commit 7cf057a

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

py/objdict.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,28 @@ STATIC mp_obj_t dict_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
8686
mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
8787
return MP_BOOL(elem != NULL);
8888
}
89+
case MP_BINARY_OP_EQUAL: {
90+
// TODO: Support equality to other object types
91+
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
92+
mp_obj_dict_t *rhs = rhs_in;
93+
if (o->map.used != rhs->map.used) {
94+
return mp_const_false;
95+
}
96+
97+
machine_uint_t size = o->map.alloc;
98+
mp_map_t *map = &o->map;
99+
100+
for (machine_uint_t i = 0; i < size; i++) {
101+
if (MP_MAP_SLOT_IS_FILLED(map, i)) {
102+
mp_map_elem_t *elem = mp_map_lookup(&rhs->map, map->table[i].key, MP_MAP_LOOKUP);
103+
if (elem == NULL || !mp_obj_equal(map->table[i].value, elem->value)) {
104+
return mp_const_false;
105+
}
106+
}
107+
}
108+
return mp_const_true;
109+
}
110+
}
89111
default:
90112
// op not supported
91113
return NULL;

0 commit comments

Comments
 (0)