aboutsummaryrefslogtreecommitdiff
path: root/inc/tree.h
blob: 271e5a2e34c8c774c514059399d0532e9ed2c6a3 (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
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT

#ifndef BLOC_TREE_H
#define BLOC_TREE_H

#include <stdint.h>

#include <term.h>
#include <hash.h>

#define VALIDATED_TREE ((hash_t)0x0)
#define INVALIDATED_TREE ((hash_t)0xffffffff)
#define FREEABLE_TREE(t)                                                       \
	((t)->state != VALIDATED_TREE && (t)->state != INVALIDATED_TREE)

struct tree {
	term_type type;
	hash_t hash;
	hash_t state; // zero or index to ref
	size_t size; // blc length
	int duplication_count; // needed count to be considered for deduplication
	union {
		struct {
			struct tree *term;
		} abs;
		struct {
			struct tree *lhs;
			struct tree *rhs;
		} app;
		struct {
			int index;
		} var;
		struct {
			hash_t hash;
			size_t table_index;
		} ref;
	} u;
};

struct list {
	int val; // length or priority
	void *data;
	struct list *next;
};

struct list *list_add(struct list *list, void *data);
struct tree *tree_merge_duplicates(struct term *term, void **all_trees);
void tree_destroy(struct list *table);

#endif