map.c 6.54 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 "obj.h"
8
9
#include "runtime0.h"
#include "map.h"
10
11

// 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
12
13
// 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};
14
15
16
17
18
19
20
21
22
23
24
25

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;
}

26
27
28
/******************************************************************************/
/* map                                                                        */

29
void mp_map_init(mp_map_t *map, int n) {
30
    map->alloc = get_doubling_prime_greater_or_equal_to(n + 1);
31
32
    map->used = 0;
    map->all_keys_are_qstrs = 1;
33
    map->table = m_new0(mp_map_elem_t, map->alloc);
34
35
}

36
mp_map_t *mp_map_new(int n) {
37
    mp_map_t *map = m_new(mp_map_t, 1);
38
    mp_map_init(map, n);
39
40
41
    return map;
}

John R. Lenton's avatar
John R. Lenton committed
42
43
void mp_map_clear(mp_map_t *map) {
    map->used = 0;
44
    map->all_keys_are_qstrs = 1;
John R. Lenton's avatar
John R. Lenton committed
45
46
47
48
49
50
51
52
53
    machine_uint_t a = map->alloc;
    map->alloc = 0;
    map->table = m_renew(mp_map_elem_t, map->table, a, map->alloc);
    mp_map_elem_t nul = {NULL, NULL};
    for (uint i=0; i<map->alloc; i++) {
        map->table[i] = nul;
    }
}

54
static void mp_map_rehash(mp_map_t *map) {
John R. Lenton's avatar
John R. Lenton committed
55
56
57
58
    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;
59
    map->all_keys_are_qstrs = 1;
John R. Lenton's avatar
John R. Lenton committed
60
61
62
    map->table = m_new0(mp_map_elem_t, map->alloc);
    for (int i = 0; i < old_alloc; i++) {
        if (old_table[i].key != NULL) {
63
            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
64
65
66
67
68
        }
    }
    m_del(mp_map_elem_t, old_table, old_alloc);
}

69
mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
70
    machine_uint_t hash;
71
    hash = mp_obj_hash(index);
John R. Lenton's avatar
John R. Lenton committed
72
    if (map->alloc == 0) {
73
        if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton's avatar
John R. Lenton committed
74
75
76
77
78
            mp_map_rehash(map);
        } else {
            return NULL;
        }
    }
79
80
    uint pos = hash % map->alloc;
    for (;;) {
81
        mp_map_elem_t *elem = &map->table[pos];
82
83
        if (elem->key == NULL) {
            // not in table
84
            if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
85
86
                if (map->used + 1 >= map->alloc) {
                    // not enough room in table, rehash it
John R. Lenton's avatar
John R. Lenton committed
87
                    mp_map_rehash(map);
88
89
90
91
92
                    // restart the search for the new element
                    pos = hash % map->alloc;
                } else {
                    map->used += 1;
                    elem->key = index;
93
94
95
                    if (!MP_OBJ_IS_QSTR(index)) {
                        map->all_keys_are_qstrs = 0;
                    }
96
97
98
99
100
                    return elem;
                }
            } else {
                return NULL;
            }
101
        } else if (elem->key == index || (!map->all_keys_are_qstrs && mp_obj_equal(elem->key, index))) {
102
103
104
105
106
107
            // 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;
            }
            */
108
            if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
John R. Lenton's avatar
John R. Lenton committed
109
                map->used--;
110
                // this leaks this memory (but see dict_get_helper)
John R. Lenton's avatar
John R. Lenton committed
111
112
113
114
                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
115
                elem->value = NULL;
John R. Lenton's avatar
John R. Lenton committed
116
117
                return retval;
            }
118
119
120
121
122
123
124
125
            return elem;
        } else {
            // not yet found, keep searching in this table
            pos = (pos + 1) % map->alloc;
        }
    }
}

126
127
128
129
130
131
132
/******************************************************************************/
/* 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);
133
134
}

John R. Lenton's avatar
John R. Lenton committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
149
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind) {
150
    int hash = mp_obj_hash(index);
John R. Lenton's avatar
John R. Lenton committed
151
    if (set->alloc == 0) {
John R. Lenton's avatar
John R. Lenton committed
152
        if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
John R. Lenton's avatar
John R. Lenton committed
153
154
155
156
157
            mp_set_rehash(set);
        } else {
            return NULL;
        }
    }
158
    int pos = hash % set->alloc;
159
    for (;;) {
160
161
        mp_obj_t elem = set->table[pos];
        if (elem == MP_OBJ_NULL) {
162
            // not in table
John R. Lenton's avatar
John R. Lenton committed
163
            if (lookup_kind == MP_MAP_LOOKUP_ADD_IF_NOT_FOUND) {
164
                if (set->used + 1 >= set->alloc) {
165
                    // not enough room in table, rehash it
John R. Lenton's avatar
John R. Lenton committed
166
                    mp_set_rehash(set);
167
                    // restart the search for the new element
168
                    pos = hash % set->alloc;
169
                } else {
170
171
                    set->used += 1;
                    set->table[pos] = index;
172
173
174
                    return index;
                }
            } else {
175
                return MP_OBJ_NULL;
176
            }
177
        } else if (mp_obj_equal(elem, index)) {
178
            // found it
John R. Lenton's avatar
John R. Lenton committed
179
180
181
182
183
            if (lookup_kind == MP_MAP_LOOKUP_REMOVE_IF_FOUND) {
                set->used--;
                set->table[pos] = NULL;
                return elem;
            }
184
185
186
            return elem;
        } else {
            // not yet found, keep searching in this table
187
            pos = (pos + 1) % set->alloc;
188
189
190
        }
    }
}
John R. Lenton's avatar
John R. Lenton committed
191
192
193
194
195
196
197
198
199
200

void mp_set_clear(mp_set_t *set) {
    set->used = 0;
    machine_uint_t a = set->alloc;
    set->alloc = 0;
    set->table = m_renew(mp_obj_t, set->table, a, set->alloc);
    for (uint i=0; i<set->alloc; i++) {
        set->table[i] = NULL;
    }
}