// Copyright (c) 2023, Marvin Borner // SPDX-License-Identifier: MIT #include #include #include #include static struct hashmap *all_terms; static void hashmap_free_term(void *item) { struct term *term = *(struct term **)item; free(term); } struct hashmap *map_all_terms(void) { return all_terms; } struct term *map_get(struct hashmap *map, hash_t hash) { struct term **handle = hashmap_get(map, hash); if (!handle) return 0; return *handle; } void map_set(struct hashmap *map, struct term *term, hash_t hash) { hashmap_set(map, &term, hash); } void map_initialize(void) { all_terms = hashmap_new(sizeof(struct term *), 0, hashmap_free_term); } void map_delete(struct hashmap *map, struct term *term) { hashmap_delete(map, term->hash); } void map_destroy(struct hashmap *map) { hashmap_free(map); } void map_dump(struct hashmap *map) { fprintf(stderr, "\n---\nMap dump:\n"); size_t iter = 0; void *iter_val; while (hashmap_iter(map, &iter, &iter_val)) { struct term *term = *(struct term **)iter_val; fprintf(stderr, "%d ", term->type); term_print(term); fprintf(stderr, " %ld\n", term->refs); } fprintf(stderr, "---\n\n"); } struct pqueue *map_to_pqueue(struct hashmap *map, pqueue_cmp_pri_f cmppri, pqueue_get_pri_f getpri, pqueue_set_pos_f setpos) { size_t size = hashmap_count(map) + 42; struct pqueue *queue = pqueue_init(size, cmppri, getpri, setpos); size_t iter = 0; void *iter_val; while (hashmap_iter(map, &iter, &iter_val)) { struct term *term = *(struct term **)iter_val; if (term->type == APP && term->u.app.lhs->type == ABS) { pqueue_insert(queue, term); } } return queue; }