From e4dd2fa788530142949c144e773eb4ffbd1ed5ed Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Sat, 20 Mar 2021 20:22:27 +0100 Subject: Tried again with maps, now it overflows the stack. --- glass.cpp | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/glass.cpp b/glass.cpp index b6241a2..7623765 100644 --- a/glass.cpp +++ b/glass.cpp @@ -1,8 +1,10 @@ #include +#include #include #include #include #include +#include #include "pcg-cpp/include/pcg_random.hpp" #include "randutils/randutils.hpp" @@ -64,14 +66,14 @@ template class Vertex { public: Vector position; std::vector> adjacentEdges; - typename std::list>::iterator occupant; + typename std::unordered_map>::iterator occupant; }; template class System { public: const unsigned L; std::vector> vertices; - std::list> particles; + std::unordered_map> particles; unsigned vectorToIndex(const Vector& x) const { unsigned i = 0; @@ -115,8 +117,8 @@ template class System { } } - std::list>::iterator> overlaps(const Particle& p, bool excludeSelf = false) const { - std::list>::iterator> o; + std::list>::iterator> overlaps(const Particle& p, bool excludeSelf = false) const { + std::list>::iterator> o; if (!excludeSelf && !isEmpty(p.position)) { o.push_back(p.position.occupant); @@ -124,7 +126,7 @@ template class System { for (const HalfEdge& e : p.position.adjacentEdges) { if (!isEmpty(e.neighbor)) { - if (p.state.σ.dot(e.Δx) == 1 || e.neighbor.occupant->state.σ.dot(e.Δx) == -1) { + if (p.state.σ.dot(e.Δx) == 1 || e.neighbor.occupant->second.state.σ.dot(e.Δx) == -1) { o.push_back(e.neighbor.occupant); } } @@ -133,9 +135,10 @@ template class System { return o; } - bool insert(const Particle& p) { + bool insert(const Particle& p, Rng& r) { if (overlaps(p).empty()) { - particles.push_back(p); + typename std::unordered_map>::iterator it; + std::tie(it, std::ignore) = particles.insert({r.uniform((unsigned)0, (unsigned)pow(2, 28)), p}); p.position.occupant = std::prev(particles.end()); return true; } else { @@ -143,8 +146,8 @@ template class System { } } - void remove(typename std::list>::iterator ip) { - ip->position.occupant = particles.end(); + void remove(typename std::unordered_map>::iterator ip) { + ip->second.position.occupant = particles.end(); particles.erase(ip); } @@ -179,11 +182,11 @@ template class System { } bool randomSwap(Rng& r) { - return swap(r.pick(particles), r.pick(particles)); + return swap(r.pick(particles).second, r.pick(particles).second); } - void setGroundState() { - for (typename std::list>::iterator ip = particles.begin(); ip != particles.end(); ip++) { + void setGroundState(Rng& r) { + for (typename std::unordered_map>::iterator ip = particles.begin(); ip != particles.end(); ip++) { remove(ip); } @@ -197,10 +200,10 @@ template class System { if (a > 0) { if (a <= D) { State s(a - 1, -1); - insert(Particle(v, s)); + insert(Particle(v, s), r); } else if (D < a) { State s(2 * D - a, 1); - insert(Particle(v, s)); + insert(Particle(v, s), r); } } } @@ -234,7 +237,7 @@ template class System { while (true) { Vertex& v = r.pick(vertices); if (isEmpty(v)) { - insert(Particle(v, State(r))); + insert(Particle(v, State(r)), r); break; } } @@ -334,7 +337,7 @@ int main() { } */ - s.setGroundState(); + s.setGroundState(r); for (unsigned n = 0; n < 10; n++) { s.sweepGrandCanonical(exp(1/0.15), r); -- cgit v1.2.3-70-g09d2