map.c 7.8 KB
Newer Older
1
2
3
4
5
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>

#include "misc.h"
6
#include "mpconfig.h"
7
#include "qstr.h"
8
#include "obj.h"
9
10
#include "runtime0.h"
#include "map.h"
11
12

// approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}]
John R. Lenton's avatar
John R. Lenton committed
13
14
// prefixed with zero for the empty case.
static int doubling_primes[] = {0, 7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607};
15
16
17
18
19
20
21
22
23
24
25
26

int get_doubling_prime_greater_or_equal_to(int x) {
    for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) {
        if (doubling_primes[i] >= x) {
            return doubling_primes[i];
        }
    }
    // ran out of primes in the table!
    // return something sensible, at least make it odd
    return x | 1;
}

27
28
29
/******************************************************************************/
/* map                                                                        */

30
void mp_map_init(mp_map_t *map, int n) {
31
32
33
34
35
36
37
    if (n == 0) {
        map->alloc = 0;
        map->table = NULL;
    } else {
        map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
        map->table = m_new0(mp_map_elem_t, map->alloc);
    }
38
39
    map->used = 0;
    map->all_keys_are_qstrs = 1;
40
41
42
43
44
45
46
47
48
    map->table_is_fixed_array = 0;
}

void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table) {
    map->alloc = n;
    map->used = n;
    map->all_keys_are_qstrs = 1;
    map->table_is_fixed_array = 1;
    map->table = (mp_map_elem_t*)table;
49
50
}

51
mp_map_t *mp_map_new(int n) {
52
    mp_map_t *map = m_new(mp_map_t, 1);
53
    mp_map_init(map, n);
54
55
56
    return map;
}

57
58
// Differentiate from mp_map_clear() - semantics is different
void mp_map_deinit(mp_map_t *map) {
59
60
61
    if (!map->table_is_fixed_array) {
        m_del(mp_map_elem_t, map->table, map->alloc);
    }
62
63
64
65
66
67
68
69
    map->used = map->alloc = 0;
}

void mp_map_free(mp_map_t *map) {
    mp_map_deinit(map);
    m_del_obj(mp_map_t, map);
}

John R. Lenton's avatar
John R. Lenton committed
70
void mp_map_clear(mp_map_t *map) {
71
72
73
74
    if (!map->table_is_fixed_array) {
        m_del(mp_map_elem_t, map->table, map->alloc);
    }
    map->alloc = 0;
John R. Lenton's avatar
John R. Lenton committed
75
    map->used = 0;
76
    map->all_keys_are_qstrs = 1;
77
78
    map->table_is_fixed_array = 0;
    map->table = NULL;
John R. Lenton's avatar
John R. Lenton committed
79
80
}

81
static void mp_map_rehash(mp_map_t *map) {
John R. Lenton's avatar
John R. Lenton committed
82
83
84
85
    int old_alloc = map->alloc;
    mp_map_elem_t *old_table = map->table;
    map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1);
    map->used = 0;
86
    map->all_keys_are_qstrs = 1;
John R. Lenton's avatar
John R. Lenton committed
87
88
89
    map->table = m_new0(mp_map_elem_t, map->alloc);
    for (int i = 0; i < old_alloc; i++) {
        if (old_table[i].key != NULL) {
90
            mp_map_lookup(map, old_table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = old_table[i].value;
John R. Lenton's avatar
John R. Lenton committed
91
92
93
94
95
        }
    }
    m_del(mp_map_elem_t, old_table, old_alloc);
}

96
mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
    // if the map is a fixed array then we must do a brute force linear search
    if (map->table_is_fixed_array) {
        if (lookup_kind != MP_MAP_LOOKUP) {
            return NULL;
        }
        for (mp_map_elem_t *elem = &map->table[0], *top = &map->table[map->used]; elem < top; elem++) {
            if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
                return elem;
            }
        }
        return NULL;
    }

    // map is a hash table (not a fixed array), so do a hash lookup

112
    machine_uint_t hash;
113
    hash = mp_obj_hash(index);
John R. Lenton's avatar
John R. Lenton committed
114
    if (map->alloc == 0) {
115
        if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton's avatar
John R. Lenton committed
116
117
118
119
120
            mp_map_rehash(map);
        } else {
            return NULL;
        }
    }
121

122
123
    uint pos = hash % map->alloc;
    for (;;) {
124
        mp_map_elem_t *elem = &map->table[pos];
125
126
        if (elem->key == NULL) {
            // not in table
127
            if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
128
129
                if (map->used + 1 >= map->alloc) {
                    // not enough room in table, rehash it
John R. Lenton's avatar
John R. Lenton committed
130
                    mp_map_rehash(map);
131
132
133
134
135
                    // restart the search for the new element
                    pos = hash % map->alloc;
                } else {
                    map->used += 1;
                    elem->key = index;
136
137
138
                    if (!MP_OBJ_IS_QSTR(index)) {
                        map->all_keys_are_qstrs = 0;
                    }
139
140
141
142
143
                    return elem;
                }
            } else {
                return NULL;
            }
144
        } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
145
146
147
148
149
150
            // found it
            /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x
            if (add_if_not_found) {
                elem->key = index;
            }
            */
151
            if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton's avatar
John R. Lenton committed
152
                map->used--;
153
                // this leaks this memory (but see dict_get_helper)
John R. Lenton's avatar
John R. Lenton committed
154
155
156
157
                mp_map_elem_t *retval = m_new(mp_map_elem_t, 1);
                retval->key = elem->key;
                retval->value = elem->value;
                elem->key = NULL;
John R. Lenton's avatar
John R. Lenton committed
158
                elem->value = NULL;
John R. Lenton's avatar
John R. Lenton committed
159
160
                return retval;
            }
161
162
163
164
165
166
167
168
            return elem;
        } else {
            // not yet found, keep searching in this table
            pos = (pos + 1) % map->alloc;
        }
    }
}

169
170
171
172
173
174
175
/******************************************************************************/
/* set                                                                        */

void mp_set_init(mp_set_t *set, int n) {
    set->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
    set->used = 0;
    set->table = m_new0(mp_obj_t, set->alloc);
176
177
}

John R. Lenton's avatar
John R. Lenton committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
static void mp_set_rehash(mp_set_t *set) {
    int old_alloc = set->alloc;
    mp_obj_t *old_table = set->table;
    set->alloc = get_doubling_prime_greater_or_equal_to(set->alloc + 1);
    set->used = 0;
    set->table = m_new0(mp_obj_t, set->alloc);
    for (int i = 0; i < old_alloc; i++) {
        if (old_table[i] != NULL) {
            mp_set_lookup(set, old_table[i], true);
        }
    }
    m_del(mp_obj_t, old_table, old_alloc);
}

John R. Lenton's avatar
John R. Lenton committed
192
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
John R. Lenton's avatar
John R. Lenton committed
193
194
    int hash;
    int pos;
John R. Lenton's avatar
John R. Lenton committed
195
    if (set->alloc == 0) {
John R. Lenton's avatar
John R. Lenton committed
196
        if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton's avatar
John R. Lenton committed
197
198
199
200
201
            mp_set_rehash(set);
        } else {
            return NULL;
        }
    }
John R. Lenton's avatar
John R. Lenton committed
202
203
204
205
206
207
208
    if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
        hash = 0;
        pos = 0;
    } else {
        hash = mp_obj_hash(index);;
        pos = hash % set->alloc;
    }
209
    for (;;) {
210
211
        mp_obj_t elem = set->table[pos];
        if (elem == MP_OBJ_NULL) {
212
            // not in table
John R. Lenton's avatar
John R. Lenton committed
213
            if (lookup_kind & MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
214
                if (set->used + 1 >= set->alloc) {
215
                    // not enough room in table, rehash it
John R. Lenton's avatar
John R. Lenton committed
216
                    mp_set_rehash(set);
217
                    // restart the search for the new element
218
                    pos = hash % set->alloc;
219
                } else {
220
221
                    set->used += 1;
                    set->table[pos] = index;
222
223
                    return index;
                }
John R. Lenton's avatar
John R. Lenton committed
224
225
            } else if (lookup_kind & MP_MAP_LOOKUP_FIRST) {
                pos++;
226
            } else {
227
                return MP_OBJ_NULL;
228
            }
229
        } else if ((lookup_kind & MP_MAP_LOOKUP_FIRST) || mp_obj_equal(elem, index)) {
230
            // found it
John R. Lenton's avatar
John R. Lenton committed
231
            if (lookup_kind & MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton's avatar
John R. Lenton committed
232
233
234
                set->used--;
                set->table[pos] = NULL;
            }
235
236
237
            return elem;
        } else {
            // not yet found, keep searching in this table
238
            pos = (pos + 1) % set->alloc;
239
240
241
        }
    }
}
John R. Lenton's avatar
John R. Lenton committed
242
243

void mp_set_clear(mp_set_t *set) {
244
    m_del(mp_obj_t, set->table, set->alloc);
John R. Lenton's avatar
John R. Lenton committed
245
    set->alloc = 0;
246
247
    set->used = 0;
    set->table = NULL;
John R. Lenton's avatar
John R. Lenton committed
248
}