From 0d20e69294b7550496f83056e3c291b08492c680 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Mon, 10 Oct 2022 17:08:07 +0200 Subject: Some refactoring. --- uniform.cpp | 64 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 37 deletions(-) diff --git a/uniform.cpp b/uniform.cpp index 0c6c125..519e6c0 100644 --- a/uniform.cpp +++ b/uniform.cpp @@ -1,29 +1,19 @@ #include #include -#include #include -#include -#include - #include "randutils/randutils.hpp" #include "pcg-cpp/include/pcg_random.hpp" using Rng = randutils::random_generator; using Real = long double; -class Edge { -public: +typedef struct Edge { std::list weights; Real probability = 0; -}; +} Edge; -class Face { -public: - std::array, 4> edges; - - Face(Edge& a, Edge& b, Edge& c, Edge& d) : edges({a, b, c, d}) {} -}; +using Face = std::array, 4>; class AztecDiamond { public: @@ -37,12 +27,12 @@ public: for (unsigned j = 0; j < pow(i, 2); j++) { unsigned x = 2 * (j % i); unsigned y = 2 * (j / i); - faces[n - i].push_back(Face( + faces[n - i].push_back({ edges[2 * n * (x0 + y) + x0 + x], edges[2 * n * (x0 + y) + x0 + x + 1], edges[2 * n * (x0 + y + 1) + x0 + x], edges[2 * n * (x0 + y + 1) + x0 + x + 1] - )); + }); } } } @@ -50,17 +40,17 @@ public: void computeWeights() { for (std::vector& fs : faces) { for (Face& f : fs) { - Real w = f.edges[0].get().weights.back(); - Real x = f.edges[1].get().weights.back(); - Real y = f.edges[2].get().weights.back(); - Real z = f.edges[3].get().weights.back(); + Real w = f[0].get().weights.back(); + Real x = f[1].get().weights.back(); + Real y = f[2].get().weights.back(); + Real z = f[3].get().weights.back(); Real cellFactor = w * z + x * y; - f.edges[0].get().weights.push_back(z / cellFactor); - f.edges[1].get().weights.push_back(y / cellFactor); - f.edges[2].get().weights.push_back(x / cellFactor); - f.edges[3].get().weights.push_back(w / cellFactor); + f[0].get().weights.push_back(z / cellFactor); + f[1].get().weights.push_back(y / cellFactor); + f[2].get().weights.push_back(x / cellFactor); + f[3].get().weights.push_back(w / cellFactor); } } @@ -73,25 +63,25 @@ public: void computeProbabilities() { for (auto it = faces.rbegin(); it != faces.rend(); it++) { for (Face& f : *it) { - Real p = f.edges[0].get().probability; - Real q = f.edges[1].get().probability; - Real r = f.edges[2].get().probability; - Real s = f.edges[3].get().probability; + Real p = f[0].get().probability; + Real q = f[1].get().probability; + Real r = f[2].get().probability; + Real s = f[3].get().probability; - Real w = f.edges[0].get().weights.back(); - Real x = f.edges[1].get().weights.back(); - Real y = f.edges[2].get().weights.back(); - Real z = f.edges[3].get().weights.back(); + Real w = f[0].get().weights.back(); + Real x = f[1].get().weights.back(); + Real y = f[2].get().weights.back(); + Real z = f[3].get().weights.back(); Real cellFactor = w * z + x * y; Real deficit = 1 - p - q - r - s; - f.edges[0].get().probability = s + deficit * w * z / cellFactor; - f.edges[1].get().probability = r + deficit * x * y / cellFactor; - f.edges[2].get().probability = q + deficit * x * y / cellFactor; - f.edges[3].get().probability = p + deficit * w * z / cellFactor; + f[0].get().probability = s + deficit * w * z / cellFactor; + f[1].get().probability = r + deficit * x * y / cellFactor; + f[2].get().probability = q + deficit * x * y / cellFactor; + f[3].get().probability = p + deficit * w * z / cellFactor; - for (Edge& e : f.edges) { + for (Edge& e : f) { e.weights.pop_back(); } } @@ -144,7 +134,7 @@ int main(int argc, char* argv[]) { for (Real& x : avgProbabilities) { std::cout << x << " "; } - std::cout <