From 8cbc60279937adcd8edfcb12866cd56ecd1f1376 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 26 Jul 2024 17:00:59 +0100 Subject: Changed from int64_t to optional for pointers in Node --- src/numerics.cpp | 9 ++++++--- src/numerics.hpp | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/numerics.cpp b/src/numerics.cpp index f992873..608a724 100644 --- a/src/numerics.cpp +++ b/src/numerics.cpp @@ -43,7 +43,7 @@ bool Fraction::operator==(const Fraction &other) return numerator == other.numerator && denominator == other.denominator; } -Node::Node(Fraction val, int64_t left, int64_t right) +Node::Node(Fraction val, index_t left, index_t right) : value{val}, left{left}, right{right} { } @@ -107,10 +107,13 @@ void indent_depth(int depth, std::stringstream &ss) ss << " "; } -std::string to_string(const NodeAllocator &allocator, const word_t n, int depth) +std::string to_string(const NodeAllocator &allocator, const index_t n, + int depth) { + if (!n.has_value()) + return "NIL"; std::stringstream ss; - Node x = allocator.vec[n]; + Node x = allocator.getVal(n.value()); ss << "(" << to_string(x.value) << "\n"; indent_depth(depth, ss); if (x.left == -1) diff --git a/src/numerics.hpp b/src/numerics.hpp index 9fdaf56..3d74202 100644 --- a/src/numerics.hpp +++ b/src/numerics.hpp @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -26,6 +27,7 @@ #define MIN(A, B) ((A) < (B) ? (A) : (B)) #define MAX(A, B) ((B) < (A) ? (A) : (B)) typedef uint64_t word_t; +typedef std::optional index_t; struct Fraction { @@ -40,9 +42,10 @@ struct Fraction struct Node { Fraction value; - int64_t left, right; + index_t left, right; - Node(Fraction val = {}, int64_t left = -1, int64_t right = -1); + Node(Fraction val = {}, index_t left = std::nullopt, + index_t right = std::nullopt); }; struct NodeAllocator @@ -57,6 +60,6 @@ word_t gcd(word_t a, word_t b); Fraction iterate(std::queue &queue, NodeAllocator &allocator); std::string to_string(const Fraction &); -std::string to_string(const NodeAllocator &, const word_t, int depth = 1); +std::string to_string(const NodeAllocator &, const index_t, int depth = 1); #endif -- cgit v1.2.3-13-gbd6f