From 28940ddd2e34437f3a639259c304a56e316f1028 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Tue, 3 Sep 2024 17:58:25 -0500 Subject: [PATCH] compress C/C++ code --- c/src/include/bcd.h | 14 +++++------- c/src/include/digits.h | 5 +---- c/src/include/factors.h | 5 +---- c/src/include/fibonacci.h | 5 +---- c/src/include/iterator.h | 5 +---- c/src/include/macros.h | 5 +---- c/src/include/math.h | 5 +---- c/src/include/primes.h | 10 ++++----- c/src/include/utils.h | 37 +++++++++++--------------------- c/src/p0008.c | 4 +--- c/src/p0017.c | 3 +-- c/src/p0025.c | 4 ++-- c/src/p0030.c | 4 ++-- c/src/p0076.c | 3 +-- cplusplus/src/include/macros.hpp | 5 +---- cplusplus/src/include/math.hpp | 5 +---- cplusplus/src/include/utils.hpp | 20 +++++++---------- cplusplus/src/p0002.cpp | 16 ++++++-------- cplusplus/src/p0014.cpp | 1 - cplusplus/src/p0022.cpp | 3 +-- cplusplus/src/p0076.cpp | 3 +-- 21 files changed, 52 insertions(+), 110 deletions(-) diff --git a/c/src/include/bcd.h b/c/src/include/bcd.h index 99e39291..b8431ade 100644 --- a/c/src/include/bcd.h +++ b/c/src/include/bcd.h @@ -96,16 +96,13 @@ BCD_int BCD_from_bytes(const uint8_t *str, size_t chars, bool negative, bool lit c.zero = false; c.negative = negative; c.digits = (packed_BCD_pair *) malloc(sizeof(packed_BCD_pair) * chars); - if (little_endian) { - for (i = 0; i < chars; i++) { + if (little_endian) + for (i = 0; i < chars; i++) c.digits[i] = str[i]; - } - } else { size_t j; - for (i = 0, j = chars - 1; i < chars; i++, j--) { + for (i = 0, j = chars - 1; i < chars; i++, j--) c.digits[i] = str[j]; - } } for (i = chars - 1; i != -1; i--) { if (c.digits[i] & 0xF0) { @@ -371,9 +368,8 @@ BCD_int mul_bcd(BCD_int x, BCD_int y) { addend = mul_bcd_pow_10(tmp, pow_10); // this was not added to performance analysis free_BCD_int(tmp); } - else { - addend = new_BCD_int(staging, false);; - } + else + addend = new_BCD_int(staging, false); tmp = add_bcd(answer, addend); free_BCD_int(addend); free_BCD_int(answer); diff --git a/c/src/include/digits.h b/c/src/include/digits.h index f0650e22..e2b8f933 100644 --- a/c/src/include/digits.h +++ b/c/src/include/digits.h @@ -1,5 +1,4 @@ -#ifndef DIGITS_H -#define DIGITS_H +#pragma once #include #include "macros.h" @@ -47,5 +46,3 @@ void free_digit_counter(digit_counter dc) { if (dc.digits != NULL) free(dc.digits); } - -#endif diff --git a/c/src/include/factors.h b/c/src/include/factors.h index b8b37bdb..55e18adb 100644 --- a/c/src/include/factors.h +++ b/c/src/include/factors.h @@ -1,5 +1,4 @@ -#ifndef DIVISORS_H -#define DIVISORS_H +#pragma once #include #include "iterator.h" @@ -43,5 +42,3 @@ inline uintmax_t proper_divisor_count(uintmax_t target) { } return ret; } - -#endif diff --git a/c/src/include/fibonacci.h b/c/src/include/fibonacci.h index 76e5fe3c..183c52a4 100644 --- a/c/src/include/fibonacci.h +++ b/c/src/include/fibonacci.h @@ -1,5 +1,4 @@ -#ifndef FIBONACCI_H -#define FIBONACCI_H +#pragma once #include #include "iterator.h" @@ -36,5 +35,3 @@ fibonacci fibonacci0(); inline fibonacci fibonacci0() { return fibonacci1(-1); } - -#endif diff --git a/c/src/include/iterator.h b/c/src/include/iterator.h index 6bc1e9c5..9377a944 100644 --- a/c/src/include/iterator.h +++ b/c/src/include/iterator.h @@ -1,5 +1,4 @@ -#ifndef ITERATOR_H -#define ITERATOR_H +#pragma once #include #include @@ -139,5 +138,3 @@ inline counter counter1(uintmax_t stop) { */ return counter2(0, stop); } - -#endif diff --git a/c/src/include/macros.h b/c/src/include/macros.h index 101b19f7..455e1013 100644 --- a/c/src/include/macros.h +++ b/c/src/include/macros.h @@ -1,5 +1,4 @@ -#ifndef MACROS_H -#define MACROS_H +#pragma once // compiler info section @@ -102,5 +101,3 @@ #define POW_OF_MAX_POW_10_64 19 #define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64) #define POW_OF_MAX_POW_10_128 38 - -#endif diff --git a/c/src/include/math.h b/c/src/include/math.h index ad136f08..ea55431f 100644 --- a/c/src/include/math.h +++ b/c/src/include/math.h @@ -1,5 +1,4 @@ -#ifndef EULER_MATH_H -#define EULER_MATH_H +#pragma once #include "macros.h" @@ -110,5 +109,3 @@ uintmax_t n_choose_r(uint32_t n, uint32_t r) { return ret + 1; } #endif - -#endif diff --git a/c/src/include/primes.h b/c/src/include/primes.h index f0484306..8d6d5896 100644 --- a/c/src/include/primes.h +++ b/c/src/include/primes.h @@ -1,5 +1,4 @@ -#ifndef PRIMES_H -#define PRIMES_H +#pragma once #include #include @@ -84,10 +83,11 @@ uintmax_t advance_prime_counter(prime_counter *pc) { if (!broken) { // is prime if (pc->idx == prime_cache_idx) { #ifdef PRIME_CACHE_SIZE_LIMIT - if (prime_cache_size == prime_cache_idx && prime_cache_size < PRIME_CACHE_SIZE_LIMIT) { + if (prime_cache_size == prime_cache_idx && prime_cache_size < PRIME_CACHE_SIZE_LIMIT) #else - if (prime_cache_size == prime_cache_idx) { + if (prime_cache_size == prime_cache_idx) #endif + { size_t new_size = prime_cache_size * 2; #ifdef PRIME_CACHE_SIZE_LIMIT if (new_size > PRIME_CACHE_SIZE_LIMIT) @@ -340,5 +340,3 @@ inline bool is_prime(uintmax_t n) { */ return n && n != 1 && !is_composite(n); } - -#endif diff --git a/c/src/include/utils.h b/c/src/include/utils.h index fcebffb0..e333e6d9 100644 --- a/c/src/include/utils.h +++ b/c/src/include/utils.h @@ -132,6 +132,10 @@ typedef struct { AnswerType type; } Answer; +#ifdef _WIN32 +#define strtok_r strtok_s +#endif + Answer EMSCRIPTEN_KEEPALIVE get_answer(uint16_t id) { Answer ret = { .id = id, @@ -147,54 +151,33 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(uint16_t id) { char s_id[6]; snprintf(s_id, sizeof(s_id), "%" PRIu16, id); -#ifdef _WIN32 - char *line = strtok_s(answers, "\n", &linepointer); // skip header - while ((line = strtok_s(NULL, "\n", &linepointer)) != NULL) { -#else char *line = strtok_r(answers, "\n", &linepointer); // skip header while ((line = strtok_r(NULL, "\n", &linepointer)) != NULL) { -#endif -#ifdef _WIN32 - char *token = strtok_s(line, "\t", &tabpointer); -#else char *token = strtok_r(line, "\t", &tabpointer); -#endif if (strcmp(token, s_id) != 0) continue; -#ifdef _WIN32 - token = strtok_s(NULL, "\t", &tabpointer); -#else token = strtok_r(NULL, "\t", &tabpointer); -#endif if (!token) continue; - if (strcmp(token, "uint") == 0) { + if (strcmp(token, "uint") == 0) ret.type = UINT8T; // will adjust size later - } else if (strcmp(token, "int") == 0) { + else if (strcmp(token, "int") == 0) ret.type = INT8T; // will adjust size later - } else if (strcmp(token, "str") == 0) { + else if (strcmp(token, "str") == 0) ret.type = STRINGT; - } else { + else { fprintf(stderr, "Error: Unknown type '%s'\n", token); return ret; } -#ifdef _WIN32 - token = strtok_s(NULL, "\t", &tabpointer); -#else token = strtok_r(NULL, "\t", &tabpointer); -#endif if (!token) continue; size_t size = strtoull(token, NULL, 10); -#ifdef _WIN32 - token = strtok_s(NULL, "\t", &tabpointer); -#else token = strtok_r(NULL, "\t", &tabpointer); -#endif if (!token) continue; @@ -273,3 +256,7 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(uint16_t id) { free(answers); return ret; } + +#ifdef _WIN32 +#undef strtok_r +#endif \ No newline at end of file diff --git a/c/src/p0008.c b/c/src/p0008.c index ed88279c..ba2fd6ae 100644 --- a/c/src/p0008.c +++ b/c/src/p0008.c @@ -65,10 +65,8 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0008() { digits[i] = plain_digits[i] - '0'; for (i = 0; i < 1000 - 13; i++) { tmp = digits[i]; - for (j = i + 1; j < i + 13; j++) { - // printf("%" PRIu64 " * %d\n", tmp, digits[j]); + for (j = i + 1; j < i + 13; j++) tmp *= digits[j]; - } answer = max(answer, tmp); } return answer; diff --git a/c/src/p0017.c b/c/src/p0017.c index 69928382..f238e710 100644 --- a/c/src/p0017.c +++ b/c/src/p0017.c @@ -25,9 +25,8 @@ British usage. uint32_t to_string_len(uint64_t n); uint32_t to_string_len(uint64_t n) { - if (n >= 1000) { + if (n >= 1000) return to_string_len(n / 1000 % 100) + 8; //len("thousand") - } else if (n >= 100) { uint32_t hundreds = to_string_len(n / 100 % 10) + 7; // len("hundred") if (n % 100) diff --git a/c/src/p0025.c b/c/src/p0025.c index 09ddf4ac..f83e8cdb 100644 --- a/c/src/p0025.c +++ b/c/src/p0025.c @@ -37,11 +37,11 @@ What is the index of the first term in the Fibonacci sequence to contain 1000 di uint64_t EMSCRIPTEN_KEEPALIVE p0025() { uint64_t answer = 2; BCD_int a = BCD_one, b = BCD_one; - while (b.decimal_digits < 1000) { + do { iadd_bcd(&a, b); swap(a, b); answer++; - } + } while (b.decimal_digits < 1000) free_BCD_int(a); free_BCD_int(b); return answer; diff --git a/c/src/p0030.c b/c/src/p0030.c index 0c8ea96b..4baae34e 100644 --- a/c/src/p0030.c +++ b/c/src/p0030.c @@ -28,10 +28,10 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0030() { for (uint64_t i = 2; i < 1000000; i++) { digit_counter dc = digits(i); sum = 0; - while (!dc.exhausted) { + do { tmp = next(dc); sum += tmp * tmp * tmp * tmp * tmp; - } + } while (!dc.exhausted); if (sum == i) answer += i; free_digit_counter(dc); diff --git a/c/src/p0076.c b/c/src/p0076.c index df4f2f12..e9ba5549 100644 --- a/c/src/p0076.c +++ b/c/src/p0076.c @@ -54,8 +54,7 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0076() { answer += (100 + counts[2] - sum) / 2; idx = 2; do { - counts[idx] = 0; - ++idx; + counts[idx++] = 0; counts[idx] += idx; sum = counts[2]; for (i = 3; i < 100; ++i) diff --git a/cplusplus/src/include/macros.hpp b/cplusplus/src/include/macros.hpp index 8fe49ca4..4c636635 100644 --- a/cplusplus/src/include/macros.hpp +++ b/cplusplus/src/include/macros.hpp @@ -1,5 +1,4 @@ -#ifndef MACROS_H -#define MACROS_H +#pragma once // compiler info section @@ -74,5 +73,3 @@ #define POW_OF_MAX_POW_10_64 19 #define MAX_POW_10_128 ((uintmax_t) MAX_POW_10_64 * (uintmax_t) MAX_POW_10_64) #define POW_OF_MAX_POW_10_128 38 - -#endif diff --git a/cplusplus/src/include/math.hpp b/cplusplus/src/include/math.hpp index cc7184d4..8d4b4ed3 100644 --- a/cplusplus/src/include/math.hpp +++ b/cplusplus/src/include/math.hpp @@ -1,5 +1,4 @@ -#ifndef EULER_MATH_H -#define EULER_MATH_H +#pragma once #include "macros.hpp" #include @@ -78,5 +77,3 @@ uintmax_t n_choose_r(uint32_t n, uint32_t r) { free(factors); return answer; } - -#endif diff --git a/cplusplus/src/include/utils.hpp b/cplusplus/src/include/utils.hpp index b526121c..adc56723 100644 --- a/cplusplus/src/include/utils.hpp +++ b/cplusplus/src/include/utils.hpp @@ -28,26 +28,24 @@ std::string get_parent_directory(const std::string &path, const uint32_t levels) std::string dir = path; for (uint32_t i = 0; i < levels; ++i) { size_t pos = dir.find_last_of(PATH_SEPARATOR); - if (pos != std::string::npos) { + if (pos != std::string::npos) dir.erase(pos); - } else { + else break; - } } return dir; } std::string get_data_file(const std::string &name) { char absolute_path[MAX_PATH]; + std::ostringstream oss; #ifdef _WIN32 if (!_fullpath(absolute_path, __FILE__, sizeof(absolute_path))) { - std::ostringstream oss; oss << "_fullpath failed with error code " << GetLastError(); throw std::runtime_error(oss.str()); } #else if (!realpath(__FILE__, absolute_path)) { - std::ostringstream oss; oss << "realpath failed with error code " << errno; throw std::runtime_error(oss.str()); } @@ -56,7 +54,6 @@ std::string get_data_file(const std::string &name) { std::string file_path = get_parent_directory(std::string(absolute_path), 4) + PATH_SEPARATOR "_data" PATH_SEPARATOR + name; std::ifstream file(file_path.c_str(), std::ios::in | std::ios::binary); if (!file) { - std::ostringstream oss; oss << "Failed to open file: " << file_path << " with error code " << errno; throw std::runtime_error(oss.str()); } @@ -64,7 +61,6 @@ std::string get_data_file(const std::string &name) { std::ostringstream content_stream; content_stream << file.rdbuf(); if (!file) { - std::ostringstream oss; oss << "Error reading file: " << file_path << " with error code " << errno; throw std::runtime_error(oss.str()); } @@ -131,13 +127,13 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(const uint16_t id) { if (!std::getline(lineStream, token, '\t')) continue; - if (token == "uint") { + if (token == "uint") answer.type = UINT8T; // will adjust size later - } else if (token == "int") { + else if (token == "int") answer.type = INT8T; // will adjust size later - } else if (token == "str") { + else if (token == "str") answer.type = STRINGT; - } else { + else { std::cerr << "Error: Unknown type '" << token << "'\n"; return answer; } @@ -202,7 +198,7 @@ Answer EMSCRIPTEN_KEEPALIVE get_answer(const uint16_t id) { strncpy(answer.value.string, token.c_str(), size); answer.value.string[size] = 0; } else { - std::cerr << "Error: Memory allocation failed for string\n"; + std::cerr << "Error: Memory allocation failed for string\n"; Answer err = {{0}}; return err; } diff --git a/cplusplus/src/p0002.cpp b/cplusplus/src/p0002.cpp index 9877063e..1ccbf65c 100644 --- a/cplusplus/src/p0002.cpp +++ b/cplusplus/src/p0002.cpp @@ -23,17 +23,13 @@ four million, find the sum of the even-valued terms. uint64_t EMSCRIPTEN_KEEPALIVE p0002() { uint64_t answer = 0, a = 1, b = 2, t; - while (b < 4000000) { - // odd (1, 3, 13, 55, ...) - // odd (1, 5, 21, 89, ...) - // even (2, 8, 34, 144, ...) + do { answer += b; - for (uint8_t z = 0; z < 3; z++) { - t = b; - b = a + b; - a = t; - } - } + // see python fibonacci file for proof this works + t = b; + b = a + b * 4; + a = t; + } while (b < 4000000); return answer; } diff --git a/cplusplus/src/p0014.cpp b/cplusplus/src/p0014.cpp index 1f57fb67..e3647eef 100644 --- a/cplusplus/src/p0014.cpp +++ b/cplusplus/src/p0014.cpp @@ -30,7 +30,6 @@ NOTE: Once the chain starts the terms are allowed to go above one million. static uint32_t collatz_len_cache[CACHE_SIZE] = {0, 1, 0}; uint32_t collatz_len(uint64_t n); - uint32_t collatz_len(uint64_t n) { if (n < CACHE_SIZE && collatz_len_cache[n]) return collatz_len_cache[n]; diff --git a/cplusplus/src/p0022.cpp b/cplusplus/src/p0022.cpp index 829ec7bf..22841fa1 100644 --- a/cplusplus/src/p0022.cpp +++ b/cplusplus/src/p0022.cpp @@ -31,8 +31,7 @@ uint64_t EMSCRIPTEN_KEEPALIVE p0022() { while (i < fstring.length() && fstring[i] != ',') i++; const size_t len = i - pi - 2; - names[idx] = fstring.substr(pi + 1, len); - idx++; + names[idx++] = fstring.substr(pi + 1, len); pi = ++i; } while (i < fstring.length()); std::sort(names.begin(), names.end()); diff --git a/cplusplus/src/p0076.cpp b/cplusplus/src/p0076.cpp index 7b45ab22..bb568985 100644 --- a/cplusplus/src/p0076.cpp +++ b/cplusplus/src/p0076.cpp @@ -53,8 +53,7 @@ uint32_t EMSCRIPTEN_KEEPALIVE p0076() { answer += (100 + counts[2] - sum) / 2; idx = 2; do { - counts[idx] = 0; - ++idx; + counts[idx++] = 0; counts[idx] += idx; sum = counts[2]; for (i = 3; i < 100; ++i)