Skip to content

Commit

Permalink
Rewrote amped queriers compressions and made a ton of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
12345ieee committed Feb 13, 2020
1 parent b00d7ea commit a9327e4
Show file tree
Hide file tree
Showing 42 changed files with 570 additions and 688 deletions.
12 changes: 12 additions & 0 deletions include/build_utils_1D.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ inline int fill_pool_1D(gem** pool, int* pool_length, int i)
return comb_tot;
}

template<class gem>
inline void compression_1D(gem* best_p, const gem* pool, int pool_length)
{
const gem* curr_best = pool;
for (int j = 1; j < pool_length; ++j) {
if (gem_more_powerful(pool[j], *curr_best)) {
curr_best = pool + j;
}
}
*best_p = *curr_best;
}

#endif // _BUILD_UTILS_1D_H
49 changes: 29 additions & 20 deletions include/build_utils_2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,49 @@
#include <algorithm>
#include <numeric>


template<unsigned int ACC, class gem>
static inline void merge_subpool(gem** subpool_p, int* subpool_length_p, gem* temp_pool, int temp_length)
inline void compression_2D(gem** pool_out_p, int* pool_length_out_p, gem* temp_pool, int pool_length_in)
{
int full_length = temp_length + *subpool_length_p;
gem* full_array = (gem*)malloc(full_length * sizeof(gem));

gem* temp_end = std::copy_n(temp_pool, temp_length, full_array);
std::copy_n(*subpool_p, *subpool_length_p, temp_end);

free(*subpool_p); // free old pool
gem_sort(temp_pool, pool_length_in, AS_LAMBDA(gem_12_less<ACC>));

gem_sort(full_array, full_length, AS_LAMBDA(gem_12_less<ACC>)); // work starts
int broken = 0;
decltype(get_second(gem{})) lim_second = -1;
for (int l = full_length - 1; l >= 0; --l) {
decltype(get_second(gem {})) lim_second = -1; /* also remove gems with second = 0 */
/* Look at gems from high X to low, keep track of the
* best Y seen till now, discard gems that don't beat that Y
*/
for (int l = pool_length_in - 1; l >= 0; --l) {
bool is_broken;
if constexpr (ACC == 0) {
is_broken = get_second(full_array[l]) <= lim_second;
is_broken = get_second(temp_pool[l]) <= lim_second;
}
else {
is_broken = (int)(ACC * get_second(full_array[l])) <= (int)(ACC * lim_second);
is_broken = (int)(ACC * get_second(temp_pool[l])) <= (int)(ACC * lim_second);
}
if (is_broken) {
full_array[l].grade = 0;
temp_pool[l].grade = 0;
broken++;
}
else
lim_second = get_second(full_array[l]);
} // all unnecessary gems destroyed
lim_second = get_second(temp_pool[l]);
} // all unnecessary gems destroyed

*pool_length_out_p = pool_length_in - broken;
*pool_out_p = (gem*)malloc(*pool_length_out_p * sizeof(gem));
std::copy_if(temp_pool, temp_pool + pool_length_in, *pool_out_p, [](gem g) {return g.grade != 0;});
}

template<unsigned int ACC, class gem>
static inline void merge_subpool(gem** subpool_p, int* subpool_length_p, const gem* temp_pool, int temp_length)
{
int full_length = temp_length + *subpool_length_p;
gem* full_array = (gem*)malloc(full_length * sizeof(gem));

gem* temp_end = std::copy_n(temp_pool, temp_length, full_array);
std::copy_n(*subpool_p, *subpool_length_p, temp_end);

free(*subpool_p); // free old pool

*subpool_length_p = full_length - broken;
*subpool_p = (gem*)malloc(*subpool_length_p * sizeof(gem)); // pool init via broken
std::copy_if(full_array, full_array + full_length, *subpool_p, [](gem g) {return g.grade != 0;});
compression_2D<ACC>(subpool_p, subpool_length_p, full_array, full_length);

free(full_array); // free
}
Expand Down
2 changes: 1 addition & 1 deletion include/build_utils_3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "interval_tree.h"

template<unsigned int ACC, unsigned int ACC_TR, class gem>
static inline void merge_subpool(gem** subpool_p, int* subpool_length_p, gem* temp_pool, int temp_length)
static inline void merge_subpool(gem** subpool_p, int* subpool_length_p, const gem* temp_pool, int temp_length)
{
int full_length = temp_length + *subpool_length_p;
gem* full_array = (gem*)malloc(full_length * sizeof(gem));
Expand Down
4 changes: 2 additions & 2 deletions include/chain_adder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <cstdlib>

template<class gem, class chain_init_class, class cfr_class>
gem* gem_putchain_templ(gem* pool, int pool_length, gem** gem_array, chain_init_class chain_init, cfr_class cfr_expr)
gem* gem_putchain_templ(const gem* pool, int pool_length, gem** gem_array, chain_init_class chain_init, cfr_class cfr_expr)
{
double best_pow = 0;
gem* best_gem = NULL;
Expand All @@ -15,7 +15,7 @@ gem* gem_putchain_templ(gem* pool, int pool_length, gem** gem_array, chain_init_
gem* new_array = (gem*)malloc(depth*sizeof(gem));
chain_init(new_array);

gem** stack = (gem**)malloc(depth*sizeof(gem*));
const gem** stack = (const gem**)malloc(depth*sizeof(gem*));
stack[0] = pool + i;
int stack_length = 1;

Expand Down
14 changes: 7 additions & 7 deletions include/crit_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ struct gem_Y {
#include "gem_utils.h"
#include "gem_stats.h"

inline double gem_power(gem_Y gem1)
inline double gem_power(const gem_Y& gem1)
{
return gem1.damage*gem1.crit;
}

inline void gem_print(gem_Y *p_gem) {
inline void gem_print(const gem_Y *p_gem) {
printf("Grade:\t%d\n"
"Damage:\t%f\n"
"Crit:\t%f\n"
"Power:\t%f\n\n",
p_gem->grade, p_gem->damage, p_gem->crit, p_gem->damage * p_gem->crit);
}

inline char gem_color(gem_Y* p_gem)
inline char gem_color(const gem_Y* p_gem)
{
if (p_gem->crit==0) return COLOR_CHHIT;
else return COLOR_CRIT;
Expand All @@ -57,7 +57,7 @@ inline auto get_second(const gem_Y& gem)
// Combining section
// -----------------

inline void gem_comb_eq(gem_Y *p_gem1, gem_Y *p_gem2, gem_Y *p_gem_combined)
inline void gem_comb_eq(const gem_Y *p_gem1, const gem_Y *p_gem2, gem_Y *p_gem_combined)
{
p_gem_combined->grade = p_gem1->grade+1;
if (p_gem1->damage > p_gem2->damage) p_gem_combined->damage = DAMAGE_EQ_1*p_gem1->damage + DAMAGE_EQ_2*p_gem2->damage;
Expand All @@ -66,7 +66,7 @@ inline void gem_comb_eq(gem_Y *p_gem1, gem_Y *p_gem2, gem_Y *p_gem_combined)
else p_gem_combined->crit = CRIT_EQ_1*p_gem2->crit + CRIT_EQ_2*p_gem1->crit;
}

inline void gem_comb_d1(gem_Y *p_gem1, gem_Y *p_gem2, gem_Y *p_gem_combined) //bigger is always gem1
inline void gem_comb_d1(const gem_Y *p_gem1, const gem_Y *p_gem2, gem_Y *p_gem_combined) //bigger is always gem1
{
p_gem_combined->grade = p_gem1->grade;
if (p_gem1->damage > p_gem2->damage) p_gem_combined->damage = DAMAGE_D1_1*p_gem1->damage + DAMAGE_D1_2*p_gem2->damage;
Expand All @@ -75,7 +75,7 @@ inline void gem_comb_d1(gem_Y *p_gem1, gem_Y *p_gem2, gem_Y *p_gem_combined)
else p_gem_combined->crit = CRIT_D1_1*p_gem2->crit + CRIT_D1_2*p_gem1->crit;
}

inline void gem_comb_gn(gem_Y *p_gem1, gem_Y *p_gem2, gem_Y *p_gem_combined)
inline void gem_comb_gn(const gem_Y *p_gem1, const gem_Y *p_gem2, gem_Y *p_gem_combined)
{
p_gem_combined->grade = std::max(p_gem1->grade, p_gem2->grade);
if (p_gem1->damage > p_gem2->damage) p_gem_combined->damage = DAMAGE_GN_1*p_gem1->damage + DAMAGE_GN_2*p_gem2->damage;
Expand Down Expand Up @@ -122,7 +122,7 @@ inline void gem_init(gem_Y *p_gem, int grd, float damage, float crit)

#include "chain_adder.h"

gem_Y* gem_putchain(gem_Y* pool, int pool_length, gem_Y** gem_array)
gem_Y* gem_putchain(const gem_Y* pool, int pool_length, gem_Y** gem_array)
{
return gem_putchain_templ(pool, pool_length, gem_array,
[](gem_Y* arg) {gem_init(arg, 1, 0, 0);},
Expand Down
6 changes: 3 additions & 3 deletions include/gem_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
#include <algorithm>

template<class gem>
int gem_getvalue(gem* p_gem)
int gem_getvalue(const gem* p_gem)
{
if(p_gem->father==NULL) return 1;
else return gem_getvalue(p_gem->father)+gem_getvalue(p_gem->mother);
}

template<class gem>
int gem_getdepth(gem* p_gem)
int gem_getdepth(const gem* p_gem)
{
if (p_gem->father==NULL)
return 1;
return std::max(gem_getdepth(p_gem->father), gem_getdepth(p_gem->mother)) + 1;
}

template<class gem>
inline bool gem_more_powerful(gem gem1, gem gem2)
inline bool gem_more_powerful(const gem& gem1, const gem& gem2)
{
return gem_power(gem1) > gem_power(gem2);
}
Expand Down
6 changes: 3 additions & 3 deletions include/gfon.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void line_init(FILE* table, int pool_zero)
fprintf(table, "0\n\n");
}

FILE* table_init(char* filename, int pool_zero)
FILE* table_init(const char* filename, int pool_zero)
{
FILE* table = fopen(filename, "rb"); // binary to check size
if (table == NULL) {
Expand All @@ -53,7 +53,7 @@ FILE* table_init(char* filename, int pool_zero)
return table;
}

FILE* file_check(char* filename)
FILE* file_check(const char* filename)
{
FILE* table = fopen(filename, "rb"); // binary to check size
if (table == NULL) {
Expand All @@ -80,7 +80,7 @@ static inline void fprint64(int n, FILE* steam)
}

template<class gem>
void table_write_iteration(gem** pool, int* pool_length, int iteration, FILE* table)
void table_write_iteration(const gem*const* pool, const int* pool_length, int iteration, FILE* table)
{
int i=iteration;
fprintf(table, "%d\n", pool_length[i]);
Expand Down
62 changes: 34 additions & 28 deletions include/interval_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,72 @@
/* place is always 0..N-1 */

template<class T>
void tree_add_element(T* tree, int N, int place, T val)
void tree_add_element(T* tree, unsigned int N, unsigned int place, T val)
{
place+=N;
while (place!=1) {
place += N;
while (place != 1) {
if (val > tree[place]) {
tree[place]=val;
place >>= 1; // place/=2 bitwise, it's a bit faster
tree[place] = val;
place /= 2;
}
else return; // no need to go on, they'll sure be even bigger
else
return; // no need to go on, they'll sure be even bigger
}
if (val > tree[1]) tree[1]=val;
if (val > tree[1])
tree[1] = val;
}

template<class T>
bool tree_check_after(T* tree, int N, int place, T val)
bool tree_check_after(const T* tree, unsigned int N, unsigned int place, T val)
{
place+=N;
if (val <= tree[place]) return false;
while (place!=1) {
if (place%2==0 && val <= tree[place+1]) return false;
place >>= 1; // place/=2 bitwise, it's a bit faster
place += N;
if (val <= tree[place])
return false;
while (place != 1) {
if (place % 2 == 0 && val <= tree[place + 1])
return false;
place /= 2;
}
return true; // if we never found bigger return true
return true; // if we never found bigger return true
}

template<class T>
T tree_read_max(T* tree, int N, int place)
T tree_read_max(const T* tree, unsigned int N, unsigned int place)
{
place+=N;
place += N;
T result = tree[place];
while (place!=1) {
if (place%2==0) result=std::max(result,tree[place+1]);
place >>= 1; // place/=2 bitwise, it's a bit faster
while (place != 1) {
if (place % 2 == 0)
result = std::max(result, tree[place + 1]);
place /= 2;
}
return result;
}

template<class T>
bool tree_check_after_debug(T* tree, int N, int place, T val)
bool tree_check_after_debug(const T* tree, unsigned int N, unsigned int place, T val)
{
printf("\nTree state (baselength %d):\n", N);
for (int i=1; i<2*N; ++i) {
for (int i = 1; i < 2 * N; ++i) {
printf("%+.4f ", tree[i]);
if (!(i & (i+1))) printf("\n");
if (!(i & (i + 1)))
printf("\n");
}
printf("Trying bb %f at place %d+%d\n", val, N, place);
place+=N;
place += N;
if (val <= tree[place]) {
printf("Failed base check\n");
return false;
}
while (place!=1) {
if (place%2==0 && val <= tree[place+1]) {
printf ("Failed check against place %d\n", place);
while (place != 1) {
if (place % 2 == 0 && val <= tree[place + 1]) {
printf("Failed check against place %d\n", place);
return false;
}
place >>= 1; // place/=2 bitwise, it's a bit faster
place /= 2;
}
printf("Success, adding\n");
return true; // if we never found bigger return true
return true; // if we never found bigger return true
}

#endif // _INTERVAL_TREE_H
Loading

0 comments on commit a9327e4

Please sign in to comment.