aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2024-07-26 02:38:02 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2024-07-26 02:54:02 +0100
commitd73ecff38a9894c226c5de63a1b3ff2b4f1a9f2d (patch)
tree54b1de722cdc63392a35559faf0545ca8b75161d
parent9ea4341a8093d4d8c907b2e854fb2d8d399da753 (diff)
downloadcw_tree-d73ecff38a9894c226c5de63a1b3ff2b4f1a9f2d.tar.gz
cw_tree-d73ecff38a9894c226c5de63a1b3ff2b4f1a9f2d.tar.bz2
cw_tree-d73ecff38a9894c226c5de63a1b3ff2b4f1a9f2d.zip
Made header file for a separate numerics file
Separates the (basically) completed cw_tree implementation in a separate module so we can spend time implementing graphics in main. NOTE: nodes are now generated from a node allocator, which just wraps around the vector. We also take an allocator and queue by reference in the iterate procedure now.
-rw-r--r--src/numerics.hpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/numerics.hpp b/src/numerics.hpp
new file mode 100644
index 0000000..be9d3db
--- /dev/null
+++ b/src/numerics.hpp
@@ -0,0 +1,61 @@
+/* Copyright (C) 2024 Aryadev Chavali
+
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
+ * details.
+
+ * You may distribute and modify this code under the terms of the GNU General
+ * Public License Version 2, which you should have received a copy of along with
+ * this program. If not, please go to <https://www.gnu.org/licenses/>.
+
+ * Created: 2024-07-26
+ * Author: Aryadev Chavali
+ * Description: Computation necessary for generating the tree
+ */
+
+#ifndef NUMERICS_HPP
+#define NUMERICS_HPP
+
+#include <cstdint>
+
+#include <queue>
+#include <string>
+#include <vector>
+
+#define MIN(A, B) ((A) < (B) ? (A) : (B))
+#define MAX(A, B) ((A) > (B) ? (A) : (B))
+typedef uint64_t word_t;
+
+struct Fraction
+{
+ word_t numerator, denominator;
+
+ Fraction(word_t numerator = 0, word_t denominator = 1);
+ bool operator<(const Fraction other);
+ bool operator==(const Fraction &other);
+};
+
+struct Node
+{
+ Fraction value;
+ int64_t left, right;
+
+ Node(Fraction val, int64_t left = -1, int64_t right = -1);
+};
+
+struct NodeAllocator
+{
+ std::vector<Node> vec;
+
+ NodeAllocator(word_t capacity);
+ word_t alloc_node(Node n);
+};
+
+word_t gcd(word_t a, word_t b);
+void iterate(std::queue<word_t> queue, NodeAllocator &allocator);
+
+std::string to_string(const Fraction &);
+std::string to_string(const Node &, int depth = 1);
+
+#endif