Exemplo n.º 1
0
Arquivo: elf.c Projeto: rui314/8cc-old
/*
 * Symbols whose attribute is LOCAL must come before other symbols in symbol
 * table.  This function is called twice.  In the first pass, localonly is false
 * so this outputs local symbols.  In the second pass, this outputs non-local
 * ones.
 */
static void write_sym_to_buf(Elf *elf, int *index, String *symtab, String *strtab, bool localonly) {
    DictIter *iter = make_dict_iter(elf->syms);
    void **p;
    for (p = dict_iter_next(iter); p; p = dict_iter_next(iter)) {
        Symbol *sym = p[1];
        if (localonly && sym->bind != STB_LOCAL)
            continue;
        if (!localonly && sym->bind == STB_LOCAL)
            continue;
        write_one_symbol(sym, index, symtab, strtab);
    }
}
Exemplo n.º 2
0
STATIC void dict_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
    mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
    bool first = true;
    if (!(MICROPY_PY_UJSON && kind == PRINT_JSON)) {
        kind = PRINT_REPR;
    }
    if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
        mp_printf(print, "%q(", self->base.type->name);
    }
    mp_print_str(print, "{");
    size_t cur = 0;
    mp_map_elem_t *next = NULL;
    while ((next = dict_iter_next(self, &cur)) != NULL) {
        if (!first) {
            mp_print_str(print, ", ");
        }
        first = false;
        mp_obj_print_helper(print, next->key, kind);
        mp_print_str(print, ": ");
        mp_obj_print_helper(print, next->value, kind);
    }
    mp_print_str(print, "}");
    if (MICROPY_PY_COLLECTIONS_ORDEREDDICT && self->base.type != &mp_type_dict) {
        mp_print_str(print, ")");
    }
}
Exemplo n.º 3
0
STATIC mp_obj_t dict_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    mp_obj_dict_t *o = MP_OBJ_TO_PTR(lhs_in);
    switch (op) {
        case MP_BINARY_OP_CONTAINS: {
            mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
            return mp_obj_new_bool(elem != NULL);
        }
        case MP_BINARY_OP_EQUAL: {
            #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
            if (MP_UNLIKELY(mp_obj_is_type(lhs_in, &mp_type_ordereddict) && mp_obj_is_type(rhs_in, &mp_type_ordereddict))) {
                // Iterate through both dictionaries simultaneously and compare keys and values.
                mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
                size_t c1 = 0, c2 = 0;
                mp_map_elem_t *e1 = dict_iter_next(o, &c1), *e2 = dict_iter_next(rhs, &c2);
                for (; e1 != NULL && e2 != NULL; e1 = dict_iter_next(o, &c1), e2 = dict_iter_next(rhs, &c2)) {
                    if (!mp_obj_equal(e1->key, e2->key) || !mp_obj_equal(e1->value, e2->value)) {
                        return mp_const_false;
                    }
                }
                return e1 == NULL && e2 == NULL ? mp_const_true : mp_const_false;
            } else
            #endif
            if (mp_obj_is_type(rhs_in, &mp_type_dict)) {
                mp_obj_dict_t *rhs = MP_OBJ_TO_PTR(rhs_in);
                if (o->map.used != rhs->map.used) {
                    return mp_const_false;
                }

                size_t cur = 0;
                mp_map_elem_t *next = NULL;
                while ((next = dict_iter_next(o, &cur)) != NULL) {
                    mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
                    if (elem == NULL || !mp_obj_equal(next->value, elem->value)) {
                        return mp_const_false;
                    }
                }
                return mp_const_true;
            } else {
                // dict is not equal to instance of any other type
                return mp_const_false;
            }
        }
        default:
            // op not supported
            return MP_OBJ_NULL;
    }
}
Exemplo n.º 4
0
void
case_dict_iter()
{
    struct dict *dict = dict();
    char *key1 = "key1"; size_t len1 = 4;
    char *key2 = "key2"; size_t len2 = 4;
    char *key3 = "key3"; size_t len3 = 4;
    char *key4 = "key4"; size_t len4 = 4;
    char *key5 = "key5"; size_t len5 = 4;
    char *key6 = "key6"; size_t len6 = 4;
    assert(dict_set(dict, key1, len1, "val1") == DICT_OK);
    assert(dict_set(dict, key2, len2, "val2") == DICT_OK);
    assert(dict_set(dict, key3, len3, "val3") == DICT_OK);
    assert(dict_set(dict, key4, len4, "val4") == DICT_OK);
    assert(dict_set(dict, key5, len5, "val5") == DICT_OK);
    assert(dict_set(dict, key6, len6, "val6") == DICT_OK);

    struct dict_iter *iter = dict_iter(dict);

    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) == NULL);
    dict_iter_free(iter);
    dict_free(dict);
}
Exemplo n.º 5
0
STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) {
    mp_obj_dict_it_t *self = MP_OBJ_TO_PTR(self_in);
    mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);

    if (next == NULL) {
        return MP_OBJ_STOP_ITERATION;
    } else {
        return next->key;
    }
}
Exemplo n.º 6
0
struct fts_iterator_t *fts_search(fts_t *fts, robj *query, unsigned long *size) {
    dict_t * scores = search_with_bm25_score(fts, query);
    *size = dict_length(scores);
    struct fts_iterator_t *it = create_fts_iterator(*size);
    dict_iterator_t *dict_it = dict_iter_create(scores);
    while (dict_iter_hasnext(dict_it)) {
        dict_kv_t score = dict_iter_next(dict_it);
        minheap_push(it->docs, score.value);
    }
    dict_free(scores);
    return it;
}
Exemplo n.º 7
0
STATIC mp_obj_t dict_update(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
    assert(MP_OBJ_IS_DICT_TYPE(args[0]));
    mp_obj_dict_t *self = MP_OBJ_CAST(args[0]);

    mp_arg_check_num(n_args, kwargs->used, 1, 2, true);

    if (n_args == 2) {
        // given a positional argument

        if (MP_OBJ_IS_DICT_TYPE(args[1])) {
            // update from other dictionary (make sure other is not self)
            if (args[1] != args[0]) {
                mp_uint_t cur = 0;
                mp_map_elem_t *elem = NULL;
                while ((elem = dict_iter_next((mp_obj_dict_t*)args[1], &cur)) != NULL) {
                    mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
                }
            }
        } else {
            // update from a generic iterable of pairs
            mp_obj_t iter = mp_getiter(args[1]);
            mp_obj_t next = NULL;
            while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
                mp_obj_t inneriter = mp_getiter(next);
                mp_obj_t key = mp_iternext(inneriter);
                mp_obj_t value = mp_iternext(inneriter);
                mp_obj_t stop = mp_iternext(inneriter);
                if (key == MP_OBJ_STOP_ITERATION
                    || value == MP_OBJ_STOP_ITERATION
                    || stop != MP_OBJ_STOP_ITERATION) {
                    nlr_raise(mp_obj_new_exception_msg(
                                 &mp_type_ValueError,
                                 "dictionary update sequence has the wrong length"));
                } else {
                    mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
                }
            }
        }
    }

    // update the dict with any keyword args
    for (mp_uint_t i = 0; i < kwargs->alloc; i++) {
        if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
            mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
        }
    }

    return mp_const_none;
}
Exemplo n.º 8
0
STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
    mp_check_self(mp_obj_is_dict_type(args[0]));
    mp_obj_dict_t *self = MP_OBJ_TO_PTR(args[0]);
    mp_ensure_not_fixed(self);

    mp_arg_check_num(n_args, kwargs->used, 1, 2, true);

    if (n_args == 2) {
        // given a positional argument

        if (mp_obj_is_dict_type(args[1])) {
            // update from other dictionary (make sure other is not self)
            if (args[1] != args[0]) {
                size_t cur = 0;
                mp_map_elem_t *elem = NULL;
                while ((elem = dict_iter_next((mp_obj_dict_t*)MP_OBJ_TO_PTR(args[1]), &cur)) != NULL) {
                    mp_map_lookup(&self->map, elem->key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = elem->value;
                }
            }
        } else {
            // update from a generic iterable of pairs
            mp_obj_t iter = mp_getiter(args[1], NULL);
            mp_obj_t next = MP_OBJ_NULL;
            while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
                mp_obj_t inneriter = mp_getiter(next, NULL);
                mp_obj_t key = mp_iternext(inneriter);
                mp_obj_t value = mp_iternext(inneriter);
                mp_obj_t stop = mp_iternext(inneriter);
                if (key == MP_OBJ_STOP_ITERATION
                    || value == MP_OBJ_STOP_ITERATION
                    || stop != MP_OBJ_STOP_ITERATION) {
                    mp_raise_ValueError("dict update sequence has wrong length");
                } else {
                    mp_map_lookup(&self->map, key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = value;
                }
            }
        }
    }

    // update the dict with any keyword args
    for (size_t i = 0; i < kwargs->alloc; i++) {
        if (mp_map_slot_is_filled(kwargs, i)) {
            mp_map_lookup(&self->map, kwargs->table[i].key, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = kwargs->table[i].value;
        }
    }

    return mp_const_none;
}
Exemplo n.º 9
0
STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
    mp_check_self(MP_OBJ_IS_DICT_TYPE(self_in));
    mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
    size_t cur = 0;
    mp_map_elem_t *next = dict_iter_next(self, &cur);
    if (next == NULL) {
        mp_raise_msg(&mp_type_KeyError, "popitem(): dictionary is empty");
    }
    self->map.used--;
    mp_obj_t items[] = {next->key, next->value};
    next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted
    next->value = MP_OBJ_NULL;
    mp_obj_t tuple = mp_obj_new_tuple(2, items);

    return tuple;
}
Exemplo n.º 10
0
static void fts_cat_index(fts_t *fts) {
    dict_iterator_t *iter;

    iter = dict_iter_create(fts->index);
    while (dict_iter_hasnext(iter)) {
        dict_kv_t kv = dict_iter_next(iter);
        rr_debug("key: %s", kv.key);
        list *l = kv.value;
        listIter *liter = listGetIterator(l, AL_START_HEAD);
        listNode *node;
        
        while((node = listNext(liter)) != NULL) {
            index_item_t *item = node->value;
            rr_debug("doc title: %s, tf: %d",
                (char *) item->doc->title->ptr, item->tf);
        }
        listReleaseIterator(liter);
    }
    dict_iter_free(iter);
}
Exemplo n.º 11
0
STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) {
    mp_check_self(mp_obj_is_type(self_in, &dict_view_it_type));
    mp_obj_dict_view_it_t *self = MP_OBJ_TO_PTR(self_in);
    mp_map_elem_t *next = dict_iter_next(MP_OBJ_TO_PTR(self->dict), &self->cur);

    if (next == NULL) {
        return MP_OBJ_STOP_ITERATION;
    } else {
        switch (self->kind) {
            case MP_DICT_VIEW_ITEMS:
            default: {
                mp_obj_t items[] = {next->key, next->value};
                return mp_obj_new_tuple(2, items);
            }
            case MP_DICT_VIEW_KEYS:
                return next->key;
            case MP_DICT_VIEW_VALUES:
                return next->value;
        }
    }
}
Exemplo n.º 12
0
STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
    mp_obj_dict_t *o = MP_OBJ_CAST(lhs_in);
    switch (op) {
        case MP_BINARY_OP_IN: {
            mp_map_elem_t *elem = mp_map_lookup(&o->map, rhs_in, MP_MAP_LOOKUP);
            return MP_BOOL(elem != NULL);
        }
        case MP_BINARY_OP_EQUAL: {
            #if MICROPY_PY_COLLECTIONS_ORDEREDDICT
            if (MP_UNLIKELY(MP_OBJ_IS_TYPE(lhs_in, &mp_type_ordereddict) && MP_OBJ_IS_TYPE(rhs_in, &mp_type_ordereddict))) {
                //TODO: implement
                return MP_OBJ_NULL;
            } else
            #endif
            if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_dict)) {
                mp_obj_dict_t *rhs = MP_OBJ_CAST(rhs_in);
                if (o->map.used != rhs->map.used) {
                    return mp_const_false;
                }

                mp_uint_t cur = 0;
                mp_map_elem_t *next = NULL;
                while ((next = dict_iter_next(o, &cur)) != NULL) {
                    mp_map_elem_t *elem = mp_map_lookup(&rhs->map, next->key, MP_MAP_LOOKUP);
                    if (elem == NULL || !mp_obj_equal(next->value, elem->value)) {
                        return mp_const_false;
                    }
                }
                return mp_const_true;
            } else {
                // dict is not equal to instance of any other type
                return mp_const_false;
            }
        }
        default:
            // op not supported
            return MP_OBJ_NULL;
    }
}