diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..4cd8284 --- /dev/null +++ b/run.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +BOLD="\033[1m" +RESET="\033[0m" + +SUCCESS="\033[38;2;80;220;100m" +FAIL="\033[38;2;255;90;90m" +INFO="\033[38;2;100;200;255m" +DIM="\033[2m" + +LOG="./out/out.log" + +# Start +printf "${DIM}────────────────────────────────────────${RESET}\n" +printf "${INFO}${BOLD}> Running...${RESET}\n" +printf "${DIM}────────────────────────────────────────${RESET}\n\n" + +# Send both stdout and stderr to the console and the log file. +start=$(date +%s.%N) +./out/out "$@" 2>&1 | tee "$LOG" +status=${PIPESTATUS[0]} +end=$(date +%s.%N) +elapsed=$(awk "BEGIN { printf \"%.3f\", $end - $start }") + +# Status! +printf "\n${DIM}────────────────────────────────────────${RESET}\n" +if (( status == 0 )); then + printf "${SUCCESS}${BOLD}✓ Success${RESET} (exit %d)\n" "$status" +else + printf "${FAIL}${BOLD}✗ Failed${RESET} (exit %d)\n" "$status" +fi +printf "${INFO}${BOLD}> Time${RESET} ${elapsed}s\n" +printf "${INFO}${BOLD}> Log${RESET} %s\n" "$LOG" +printf "${DIM}────────────────────────────────────────${RESET}" +exit "$status" diff --git a/src/bigmath/BigMath.cpp b/src/bigmath/BigMath.cpp index b926b4f..8113168 100644 --- a/src/bigmath/BigMath.cpp +++ b/src/bigmath/BigMath.cpp @@ -8,15 +8,8 @@ using namespace bigmath; int main() { - segment s; - s.set(0, 8); - s.set(1, 5); - s.set(2, 5); - - s.set(125, 8); - s.set(126, 1); - s.set(127, 2); + segment s(~u64(0)); buffers::hexdump(data_view(s.bytes(), segment::byte_count), std::cout); std::cout << std::endl; - std::cout << s.describe() << std::endl; + std::cout << std::string(s) << std::endl; } \ No newline at end of file diff --git a/src/bigmath/chain/segment.cpp b/src/bigmath/chain/segment.cpp index e0b8b13..761a3e2 100644 --- a/src/bigmath/chain/segment.cpp +++ b/src/bigmath/chain/segment.cpp @@ -1,7 +1,6 @@ #include "segment.hpp" -#include -#include +#include namespace bigmath { @@ -10,6 +9,15 @@ namespace bigmath { segment::segment() : digits{} {} + segment::segment(u64 n) : segment() { + u8 i = 0; + while (n) { + u8 d10 = u8(n % 10); + n /= 10; + this->set(i++, d10); + } + } + segment::~segment() {} u8 segment::operator[](u8 i) const { @@ -32,44 +40,8 @@ namespace bigmath { return digits; } - std::string segment::describe() const { - std::ostringstream out; - - // We'll iterate backwards - isize i = digit_count; - u32 use_space = 0; - while (i > 0) { - u8 val = operator[](u8(i - 1)); - - // Count repetitions - isize count = 1; - while (i > count && operator[](u8(i - count - 1)) == val) ++count; - - // temporal string - std::ostringstream one; - one << u32(val); - auto str = one.str(); - - // Decide whether to compress - if (count > 5) { - if(use_space) out << " "; - out << "[" << str << " x " << count << "]"; - use_space = 4; - } else { - for (isize j = 0; j < count; ++j) { - if(use_space > 3) { - out << " "; - use_space = 1; - } - use_space++; - out << str; - } - } - - i -= count; - } - - return out.str(); + segment::operator std::string() const { + return describe(*this); } } \ No newline at end of file diff --git a/src/bigmath/chain/segment.hpp b/src/bigmath/chain/segment.hpp index 13d7cd4..d204867 100644 --- a/src/bigmath/chain/segment.hpp +++ b/src/bigmath/chain/segment.hpp @@ -21,6 +21,8 @@ namespace bigmath { segment(); + segment(u64 n); + ~segment(); public: @@ -33,7 +35,7 @@ namespace bigmath { const u8* bytes() const; - std::string describe() const; + operator std::string() const; }; diff --git a/src/bigmath/util/describe.cpp b/src/bigmath/util/describe.cpp new file mode 100644 index 0000000..17e1e75 --- /dev/null +++ b/src/bigmath/util/describe.cpp @@ -0,0 +1,71 @@ +#include "describe.hpp" + +#include + +#include +#include +#include +#include + +namespace bigmath { + + std::string describe(const segment& s) { + std::string buf; + buf.reserve(segment::digit_count * 2); + u32 group_count = 0; + + isize i = 0; + while (i < segment::digit_count) { + u8 val = s[static_cast(i)]; + + // Count repetitions + isize count = 1; + while (i + count < segment::digit_count && s[static_cast(i + count)] == val) { + ++count; + } + + if (count > 5) { + if (!buf.empty() && buf.back() != ' ') { + buf.push_back(' '); + } + + // Append reversed tag directly: "]count x val[" + buf.push_back(']'); + + // Fast reverse-append for count digits without heap allocation + isize temp_count = count; + do { + buf.push_back(static_cast('0' + (temp_count % 10))); + temp_count /= 10; + } while (temp_count > 0); + + buf.append(" x "); + buf.push_back(static_cast('0' + val)); + buf.push_back('['); + + buf.push_back(' '); + group_count = 0; + } else { + char ch = static_cast('0' + val); + for (isize j = 0; j < count; ++j) { + if (group_count == 3) { + buf.push_back(' '); + group_count = 0; + } + buf.push_back(ch); + group_count++; + } + } + + i += count; + } + + if (!buf.empty() && buf.back() == ' ') { + buf.pop_back(); + } + + std::reverse(buf.begin(), buf.end()); + return buf; + } + +} diff --git a/src/bigmath/util/describe.hpp b/src/bigmath/util/describe.hpp new file mode 100644 index 0000000..8b41e02 --- /dev/null +++ b/src/bigmath/util/describe.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace bigmath { + + std::string describe(const segment& s); + +}