-
Notifications
You must be signed in to change notification settings - Fork 1
/
cubelib.c
87 lines (72 loc) · 1.66 KB
/
cubelib.c
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
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include "cubelib.h"
void initLibrary(Library *l) {
*l = safeMalloc(sizeof(struct librarystruct));
(*l)->tr = initTreenode();
(*l)->insertions = 0;
}
int insertCount(Library lib) { return lib->insertions; }
void freeLibrary(Library lib) {
freeTree(lib->tr);
free(lib);
}
Tree initTreenode() {
Tree tr = safeMalloc(sizeof(struct treenode));
for (int i = 0; i < 4; i++) {
tr->child[i] = NULL;
}
tr->Q = NULL;
tr->heuristic = 0;
tr->timesReevaluate = 0;
return tr;
}
void freeTree(Tree tr) {
if (tr == NULL) {
return;
}
if (tr->Q != NULL) {
free(tr->Q);
}
for (int i = 0; i < 4; i++) {
freeTree(tr->child[i]);
}
free(tr);
}
// Insert node in library, returns 0 if cube was already in library
int getNode(Library lib, Cube c, Tree *trh) {
Tree tr;
int piece, inserted;
tr = lib->tr;
inserted = 0;
for (int i = 0; i < NCORNER; i++) {
// 4th corner can be skipped because it is last in its set and therfore
// forced
if (i % 4 == 3) {
continue;
}
piece = c->corner[i] % 4;
if (tr->child[piece] == NULL) {
tr->child[piece] = initTreenode();
}
tr = tr->child[piece];
}
for (int i = 0; i < NEDGE; i++) {
// 4th edge can be skipped because it is last in its set and therfore forced
if (i % 4 == 3) {
continue;
}
piece = c->edge[i] % 4;
if (tr->child[piece] == NULL) {
//cube is new and a new branch has to be created
tr->child[piece] = initTreenode();
if (i == NEDGE - 2) {
inserted = 1;
lib->insertions += 1;
}
}
tr = tr->child[piece];
}
*trh = tr;
return inserted;
}