aboutsummaryrefslogtreecommitdiff
path: root/src/map.c
blob: 799effbe7b6de6bd051412cacc3da1759968ca33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT

#include <stdlib.h>

#include <lib/hashmap.h>
#include <map.h>
#include <parse.h>
#include <schedule.h>
#include <log.h>

static struct hashmap *all_terms;

static void hashmap_free_term(void *item)
{
	struct term *term = *(struct term **)item;
	term_destroy_head(term, 1);
}

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)
{
	if (map == all_terms && !hashmap_get(map, term->hash))
		schedule_add(term); // also add to schedule
	hashmap_set(map, &term, 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, char all)
{
	fprintf(stderr, "\n---\nMap dump:\n");
	fprintf(stderr, "type\trefs\thash\tterm%*sparents\n", 21, "");
	size_t iter = 0;
	void *iter_val;
	while (hashmap_iter(map, &iter, &iter_val)) {
		struct term *term = *(struct term **)iter_val;
		/* if (!all && !term->is_program) */
		/* 	continue; */

		fprintf(stderr, "%d\t%ld\t%lx\t", term->type, term->refs,
			term->hash & HASH_MASK);
		size_t len = term_print(term);
		fprintf(stderr, "%*s{", 25 - len, "");

		size_t jiter = 0;
		void *jiter_val;
		while (hashmap_iter(term->parents, &jiter, &jiter_val)) {
			struct term *parent = *(struct term **)jiter_val;
			term_print(parent);
			fprintf(stderr, ", ");
		}
		fprintf(stderr, "}\n");
	}
	fprintf(stderr, "---\n\n");
}