From 58b4a320df4dec9132ca23b54a06f6cec6303ef8 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Thu, 13 Feb 2014 22:43:10 +0100 Subject: [PATCH] Refactored Field -> Grid. Adds dynamic grid (without template) --- include/dynamic_grid.hpp | 61 +++++++++++++++++---------------- include/generator.hpp | 2 +- include/{field.hpp => grid.hpp} | 24 +++---------- include/utilities.hpp | 19 ++++++++++ 4 files changed, 57 insertions(+), 49 deletions(-) rename include/{field.hpp => grid.hpp} (87%) create mode 100644 include/utilities.hpp diff --git a/include/dynamic_grid.hpp b/include/dynamic_grid.hpp index a7a3260..2757ad7 100644 --- a/include/dynamic_grid.hpp +++ b/include/dynamic_grid.hpp @@ -1,23 +1,23 @@ #pragma once -#include "field.hpp" +#include "utilities.hpp" #include #include #include #include #include -#include +#include #include // Position {0,0} is bottom left. // Position {W-1, H-1} is top right. -template -struct DynamicField{ +// template +struct DynamicGrid{ using Position = std::pair; template - DynamicField(int W, int H, Input it) + DynamicGrid(int W, int H, Input it) : W(W) , H(H) , grid(W*H, 0) @@ -56,26 +56,14 @@ struct DynamicField{ return ret; } - //! \brief Let the block fall (all the way) - auto collapse(){ - while(vcollapse()); - while(hcollapse()); - } - - //! \brief Returns an array of all valid positions - auto const & all_positions() const { - static auto v = all_positions_impl(W, H); - return v; - } - //! \brief Get (mutable) value at p - T& get(Position const & p){ + int & get(Position const & p){ assert(valid(p)); return grid[static_cast(p.first + p.second*W)]; } //! \brief Get value at p - T const& get(Position const & p) const { + int const& get(Position const & p) const { assert(valid(p)); return grid[static_cast(p.first + p.second*W)]; } @@ -92,7 +80,17 @@ struct DynamicField{ private: int W; int H; - std::vector grid; + std::vector grid; + + //! \brief Returns true if the whole column at x is empty + auto empty_column(int x){ + for(auto y = 0; y < H; ++y){ + if(!empty({x, y})){ + return false; + } + } + return true; + } //! \brief Helper function for all_positions() static auto all_positions_impl(int W, int H) { @@ -103,6 +101,14 @@ private: return r; } +public: + //! \brief Returns an array of all valid positions + auto const & all_positions() const { + static auto v = all_positions_impl(W, H); + return v; + } + +private: //! \brief Single vertical collapsing step auto vcollapse(){ using namespace std; @@ -143,14 +149,11 @@ private: return some_change; } - //! \brief Returns true if the whole column at x is empty - auto empty_column(int x){ - for(auto y = 0; y < H; ++y){ - if(!empty({x, y})){ - return false; - } - } - return true; +public: + //! \brief Let the block fall (all the way) + auto collapse(){ + while(vcollapse()); + while(hcollapse()); } }; @@ -159,5 +162,5 @@ auto random_dynamic_grid(int W, int H, URNG&& r){ std::uniform_int_distribution dis(1, 2); std::vector v(W*H); std::generate_n(std::begin(v), W*H, [&]{ return dis(r); }); - return DynamicField(W, H, std::begin(v)); + return DynamicGrid(W, H, std::begin(v)); } diff --git a/include/generator.hpp b/include/generator.hpp index 51c9050..b81fa34 100644 --- a/include/generator.hpp +++ b/include/generator.hpp @@ -1,6 +1,6 @@ #pragma once -#include "field.hpp" +#include "grid.hpp" #include diff --git a/include/field.hpp b/include/grid.hpp similarity index 87% rename from include/field.hpp rename to include/grid.hpp index 0685fc6..2732b2b 100644 --- a/include/field.hpp +++ b/include/grid.hpp @@ -1,34 +1,20 @@ #pragma once +#include "utilities.hpp" + #include #include #include #include #include -// Simple static vector (much faster) -// Also slightly faster than boost::container::static_vector -// No checking is performed! -template -struct small_vector{ - std::array arr; - size_t elements = 0; - - void push_back(T const & t){ - arr[elements++] = t; - } - - auto begin() const { return &arr[0]; } - auto end() const { return &arr[elements]; } -}; - // Position {0,0} is bottom left. // Position {W-1, H-1} is top right. template -struct Field{ +struct Grid{ using Position = std::pair; - Field(std::initializer_list g){ + Grid(std::initializer_list g){ std::copy(std::begin(g), std::end(g), std::begin(grid)); } @@ -162,5 +148,5 @@ private: //! \brief Helper function to create a field (T will be deducted) template auto create_rectangular_field(std::initializer_list grid){ - return Field(grid); + return Grid(grid); } diff --git a/include/utilities.hpp b/include/utilities.hpp new file mode 100644 index 0000000..c5c3b0b --- /dev/null +++ b/include/utilities.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +// Simple static vector (much faster) +// Also slightly faster than boost::container::static_vector +// No checking is performed! +template +struct small_vector{ + std::array arr; + size_t elements = 0; + + void push_back(T const & t){ + arr[elements++] = t; + } + + auto begin() const { return &arr[0]; } + auto end() const { return &arr[elements]; } +};