Skip to content

Commit

Permalink
Move to C++ vectors and unique_ptr, refactor EVERYTHING
Browse files Browse the repository at this point in the history
  • Loading branch information
12345ieee committed May 29, 2022
1 parent d13ce48 commit 8258bc2
Show file tree
Hide file tree
Showing 61 changed files with 1,681 additions and 3,133 deletions.
20 changes: 11 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ CXX:=g++
WINCXX:=x86_64-w64-mingw32-$(CXX)

# Flags
CSTD:=-std=gnu11
CXXSTD:=-std=gnu++17
CSTD:=-std=gnu17
CXXSTD:=-std=gnu++20
WFLAGS:=-Wall -Wextra
OFLAGS:=-O3 -flto -fwhole-program
IFLAGS:=-I "include/"
Expand All @@ -17,7 +17,7 @@ LIBFLAGS:=-lm
# Query
QUERY_LEECH=leechgem/leechquery
QUERY_MANAGEM=managem/mgquery-alone managem/mgquery-amps managem/mgquery-setup managem/mgquery-omnia
QUERY_CRIT=critgem/critquery critgem/critquery-amps
QUERY_CRIT=$(basename $(wildcard critgem/critquery*.cpp))
QUERY_KILLGEM=killgem/kgquery-alone killgem/kgquery-amps killgem/kgquery-setup killgem/kgquery-omnia
QUERY_MGNGEM=managem/mgquery-ngems
QUERY_KGNGEM=killgem/kgquery-ngems
Expand All @@ -27,17 +27,17 @@ QUERY_ALL=$(QUERY_DIST) $(QUERY_MGNGEM) $(QUERY_KGNGEM)

# Build
BUILD_LEECH=leechgem/leechbuild
BUILD_MANAGEM=managem/mgbuild-appr managem/mgbuild-exact managem/mgbuild-c6
BUILD_CRIT=critgem/critbuild critgem/critbuild-c6
BUILD_KILLGEM=killgem/kgbuild-appr killgem/kgbuild-exact killgem/kgbuild-c6
BUILD_MANAGEM=$(basename $(wildcard managem/mgbuild-*.cpp))
BUILD_CRIT=$(basename $(wildcard critgem/critbuild*.cpp))
BUILD_KILLGEM=$(basename $(wildcard killgem/kgbuild-*.cpp))
BUILD_BLEED=bleedgem/bleedbuild
BUILD_ALL=$(BUILD_LEECH) $(BUILD_MANAGEM) $(BUILD_CRIT) $(BUILD_KILLGEM) $(BUILD_BLEED)

# Combine
COMBINE_LEECH=leechgem/leechcombine
COMBINE_MANAGEM=managem/mgcombine-appr
COMBINE_MANAGEM=$(basename $(wildcard managem/mgcombine-*.cpp))
COMBINE_CRIT=critgem/critcombine
COMBINE_KILLGEM=killgem/kgcombine-appr killgem/kgcombine-exact
COMBINE_KILLGEM=$(basename $(wildcard killgem/kgcombine-*.cpp))
COMBINE_BLEED=bleedgem/bleedcombine
COMBINE_ALL=$(COMBINE_LEECH) $(COMBINE_MANAGEM) $(COMBINE_CRIT) $(COMBINE_KILLGEM) $(COMBINE_BLEED)

Expand Down Expand Up @@ -93,6 +93,8 @@ set-crit: $(CRIT_ALL)
set-killgem: $(KILLGEM_ALL)
set-bleed: $(BLEED_ALL)

utilities: $(UTILITIES)

.PHONY: set-query set-build set-combine set-leech set-managem set-crit set-killgem

# Windows
Expand Down Expand Up @@ -150,7 +152,7 @@ $(TABLES_DIR):
# Clean
clean:
@rm -vf $(DEV_ALL)
@rm $(DEPS_DIR)/*
@rm -f $(DEPS_DIR)/*

clean-white:
@if [ -d "white" ]; then \
Expand Down
23 changes: 10 additions & 13 deletions include/bleed_utils.h → bleedgem/bleed_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#define _BLEED_UTILS_H

#include <algorithm>
#include <cstdio>

struct gem_R {
int grade; //using short does NOT improve time/memory usage
double bleed;
gem_R* father;
gem_R* mother;
int grade; //using short does NOT improve time/memory usage
double bleed;
};

// --------------------
Expand Down Expand Up @@ -84,19 +85,15 @@ inline void gem_init(gem_R *p_gem, int grd, double bleed)
p_gem->mother=NULL;
}

#include "build_utils_1D.h"

// -------------------
// Chain adder section
// -------------------
// -----------------
// Pool init section
// -----------------

#include "chain_adder.h"
#include "1D_utils.h"

gem_R* gem_putchain(const gem_R* pool, int pool_length, gem_R** gem_array)
{
return gem_putchain_templ(pool, pool_length, gem_array,
[](gem_R* arg) {gem_init(arg, 1, 0);},
[](const gem_R& arg) {return gem_power(arg);});
template<>
inline vector<pool_t<gem_R>> init_pool(int len, uint) {
return init_pool_1D<gem_R>(len);
}

#endif // _BLEED_UTILS_H
52 changes: 3 additions & 49 deletions bleedgem/bleedbuild.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,10 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <getopt.h>
#include <cstring>

#include "bleed_utils.h"
#include "gfon.h"
#include "cmdline_options.h"

using gem = gem_R;

void worker(const cmdline_options& options)
{
FILE* table=table_init(options.tables[0], 1); // init bleed

int len = options.target.len;
gem** pool = (gem**)malloc(len*sizeof(gem*)); // if not malloc-ed 690k is the limit
int* pool_length = (int*)malloc(len*sizeof(int));
pool[0] = (gem*)malloc(sizeof(gem));
gem_init(pool[0],1,1);
pool_length[0]=1;

int prevmax=pool_from_table(pool, pool_length, len, table); // pool filling
if (prevmax+1==len) {
fclose(table); // close
for (int i=0;i<len;++i) free(pool[i]); // free
free(pool); // free
free(pool_length); // free
printf("Table is longer than %d, no need to do anything\n\n",prevmax+1);
exit(1);
}
table=freopen(options.tables[0].c_str(), "a", table); // append -> updating possible

for (int i=prevmax+1; i<len; ++i) { // more building
int comb_tot = fill_pool_1D(pool, pool_length, i);

if (!options.output.quiet) {
printf("Value:\t%d\n",i+1);
if (options.output.debug) {
printf("Raw:\t%d\n",comb_tot);
printf("Pool:\t%d\n\n",pool_length[i]);
}
}
table_write_iteration(pool, pool_length, i, table); // write on file
}

fclose(table); // close
for (int i=0;i<len;++i) free(pool[i]); // free
free(pool); // free
free(pool_length); // free
}
#include "1D_utils.h"
#include "workers.h"

int main(int argc, char** argv)
{
Expand All @@ -62,7 +17,6 @@ int main(int argc, char** argv)
options.tables[0] = "table_bleed";
}

worker(options);
worker_build<gem_R>(options, fill_pool_1D<gem_R>);
return 0;
}

103 changes: 3 additions & 100 deletions bleedgem/bleedcombine.cpp
Original file line number Diff line number Diff line change
@@ -1,106 +1,10 @@
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <getopt.h>

#include "bleed_utils.h"
#include "print_utils.h"
#include "cmdline_options.h"

using gem = gem_R;

void worker(const cmdline_options& options)
{
printf("\n");
int len = options.target.len;
gem* gems = (gem*)malloc(len*sizeof(gem)); // if not malloc-ed 230k is the limit
gem* pool[len];
int pool_length[len];
pool[0] = (gem*)malloc(sizeof(gem));
gem_init(gems,1,1);
gem_init(pool[0],1,1);
pool_length[0]=1;
if (!options.output.quiet) gem_print(gems);

for (int i=1; i<len; ++i) {
int comb_tot = fill_pool_1D(pool, pool_length, i);

compression_1D(gems + i, pool[i], pool_length[i]);
if (!options.output.quiet) {
printf("Value:\t%d\n",i+1);
if (options.output.info)
printf("Growth:\t%f\n", log(gem_power(gems[i]))/log(i+1));
if (options.output.debug) {
printf("Raw:\t%d\n",comb_tot);
printf("Pool:\t%d\n",pool_length[i]);
}
gem_print(gems+i);
}
}

if (options.output.quiet) { // outputs last if we never seen any
printf("Value:\t%d\n",len);
printf("Growth:\t%f\n", log(gem_power(gems[len-1]))/log(len));
if (options.output.debug)
printf("Pool:\t%d\n",pool_length[len-1]);
gem_print(gems+len-1);
}

gem* gemf=gems+len-1; // gem that will be displayed

if (options.target.upto) {
double best_growth=-INFINITY;
int best_index=0;
for (int i=0; i<len; ++i) {
if (log(gem_power(gems[i]))/log(i+1) > best_growth) {
best_index=i;
best_growth=log(gem_power(gems[i]))/log(i+1);
}
}
printf("Best gem up to %d:\n\n", len);
printf("Value:\t%d\n",best_index+1);
printf("Growth:\t%f\n", best_growth);
gem_print(gems+best_index);
gemf = gems+best_index;
}

gem* gem_array = NULL;
if (options.target.chain) {
if (len < 2) printf("I could not add chain!\n\n");
else {
int value=gem_getvalue(gemf);
gemf = gem_putchain(pool[value-1], pool_length[value-1], &gem_array);
printf("Gem with chain added:\n\n");
printf("Value:\t%d\n", value); // made to work well with -u
printf("Growth:\t%f\n", log(gem_power(*gemf))/log(value));
gem_print(gemf);
}
}

if (options.print.parens) {
printf("Compressed combining scheme:\n");
print_parens_compressed(gemf);
printf("\n\n");
}
if (options.print.tree) {
printf("Gem tree:\n");
print_tree(gemf, "");
printf("\n");
}
if (options.print.table) print_table(gems, len);

if (options.print.equations) { // it ruins gems, must be last
printf("Equations:\n");
print_equations(gemf);
printf("\n");
}

for (int i=0;i<len;++i) free(pool[i]); // free
free(gems);
if (options.target.chain && len > 1) {
free(gem_array);
}
}
#include "1D_utils.h"
#include "workers.h"

int main(int argc, char** argv)
{
Expand All @@ -111,7 +15,6 @@ int main(int argc, char** argv)
if(!options.parse_args(argc, argv))
return 1;

worker(options);
worker_combine<gem_R>(options, fill_pool_1D<gem_R>);
return 0;
}

Loading

0 comments on commit 8258bc2

Please sign in to comment.