-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathprint_utils.h
129 lines (113 loc) · 3.29 KB
/
print_utils.h
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef _PRINT_UTILS_H
#define _PRINT_UTILS_H
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include "gem_utils.h"
#include "container_utils.h"
template<class gem>
void print_parens(const gem* gemf)
{
if (gemf->father==NULL) printf("%c", gem_color(*gemf));
else {
printf("(");
print_parens(gemf->mother);
printf("+");
print_parens(gemf->father);
printf(")");
}
return;
}
template<class gem>
bool monocolor_ancestors(const gem* gemf)
{
if (gemf->father==NULL) return true;
else if (gem_color(gemf->father)!=gem_color(gemf->mother)) return false;
else return monocolor_ancestors(gemf->mother) && monocolor_ancestors(gemf->father);
}
template<class gem>
void print_parens_compressed(const gem* gemf)
{
if (gemf->father==NULL) printf("%c", gem_color(gemf));
else if (monocolor_ancestors(gemf) // if gem is uniform combination (g1 are already done)
&& 1 << (gemf->grade-1) == gem_getvalue(gemf)) { // and is standard combine
printf("%d%c",gemf->grade,gem_color(gemf));
}
else {
printf("(");
print_parens_compressed(gemf->mother);
printf("+");
print_parens_compressed(gemf->father);
printf(")");
}
}
template<class gem>
void fill_uniques_array(gem* gemf, pool_t<gem*>& p_gems, int* uniques)
{
for (int i=0; i<*uniques; ++i)
if (gemf==p_gems[i]) return;
if (gemf->father != NULL) {
fill_uniques_array(gemf->father, p_gems, uniques);
fill_uniques_array(gemf->mother, p_gems, uniques);
}
p_gems[*uniques]=gemf;
(*uniques)++;
}
template<class gem>
void print_equations(gem* gemf)
{
// fill
int value = gem_getvalue(gemf);
int len = 2 * value - 1;
pool_t<gem*> p_gems = make_uninitialized_pool<gem*>(len); // stores all the gem pointers
int uniques = 0;
fill_uniques_array(gemf, p_gems, &uniques); // this array contains uniques only and is long `uniques`
// mark
int orig_grades[uniques]; // stores all the original gem grades
for (int i = 0; i < uniques; i++) {
gem* p_gem = p_gems[i];
orig_grades[i] = p_gem->grade;
p_gem->grade = i;
}
// print
for (int i = 0; i < uniques; i++) {
gem* p_gem = p_gems[i];
if (p_gem->father == NULL)
printf("(val = 1)\t%2d = g1 %c\n", p_gem->grade, gem_color(p_gem));
else
printf("(val = %d)\t%2d = %2d + %2d\n", gem_getvalue(p_gem), p_gem->grade, p_gem->mother->grade, p_gem->father->grade);
}
// clean
for (int i = 0; i < uniques; i++) {
p_gems[i]->grade = orig_grades[i];
}
}
template<class gem>
void print_tree(const gem* gemf, const char* prefix)
{
if (gemf->father==NULL) {
printf("─ g1 %c\n",gem_color(gemf));
}
else {
printf("─%d\n",gem_getvalue(gemf));
printf("%s ├",prefix);
char string1[strlen(prefix)+5]; // 1 space, 1 unicode bar and and the null term are 5 extra chars
sprintf(string1, "%s │", prefix);
print_tree(gemf->mother, string1);
printf("%s └",prefix);
char string2[strlen(prefix)+3]; // 2 spaces and the null term are 3 extra chars
sprintf(string2, "%s ", prefix);
print_tree(gemf->father, string2);
}
}
template<class gem>
void print_table(const vector<gem>& gems)
{
printf("# Gems\tPower\t\tGrowth\n");
printf("1\t%f\t%f\n", gem_power(gems[0]), log(gem_power(gems[0])));
for (size_t i = 1; i < gems.size(); i++)
printf("%zu\t%f\t%f\n", i + 1, gem_power(gems[i]), log(gem_power(gems[i])) / log(i + 1));
printf("\n");
}
#endif // _PRINT_UTILS_H