summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaron Kent-Dobias <jaron@kent-dobias.com>2022-10-10 17:08:07 +0200
committerJaron Kent-Dobias <jaron@kent-dobias.com>2022-10-10 17:08:07 +0200
commit0d20e69294b7550496f83056e3c291b08492c680 (patch)
tree83adf6fdbca480561fb6e9d53a0e5c918eeea68e
parentff5e284201b2a464655b5bc4b488004905039b59 (diff)
downloadcode-0d20e69294b7550496f83056e3c291b08492c680.tar.gz
code-0d20e69294b7550496f83056e3c291b08492c680.tar.bz2
code-0d20e69294b7550496f83056e3c291b08492c680.zip
Some refactoring.
-rw-r--r--uniform.cpp64
1 files 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 <functional>
#include <iostream>
-#include <fstream>
#include <list>
-#include <cmath>
-#include <random>
-
#include "randutils/randutils.hpp"
#include "pcg-cpp/include/pcg_random.hpp"
using Rng = randutils::random_generator<pcg32>;
using Real = long double;
-class Edge {
-public:
+typedef struct Edge {
std::list<Real> weights;
Real probability = 0;
-};
+} Edge;
-class Face {
-public:
- std::array<std::reference_wrapper<Edge>, 4> edges;
-
- Face(Edge& a, Edge& b, Edge& c, Edge& d) : edges({a, b, c, d}) {}
-};
+using Face = std::array<std::reference_wrapper<Edge>, 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<Face>& 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 <<std::endl;
+ std::cout << std::endl;
return 0;
}