Adds serialisation of levels and solutions
This commit is contained in:
parent
4baca8241a
commit
79f49893e4
6 changed files with 88 additions and 1 deletions
|
@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 2.8)
|
|||
include_directories("${PROJECT_SOURCE_DIR}/include/")
|
||||
add_definitions(-std=c++1y)
|
||||
|
||||
find_package(Boost REQUIRED COMPONENTS program_options system)
|
||||
find_package(Boost REQUIRED COMPONENTS program_options serialization system)
|
||||
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
|
||||
set(libs ${libs} ${Boost_LIBRARIES})
|
||||
|
||||
|
|
42
include/analyzed_grid.hpp
Normal file
42
include/analyzed_grid.hpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#pragma once
|
||||
|
||||
#include "dynamic_grid.hpp"
|
||||
#include "solver.hpp"
|
||||
#include "rules.hpp"
|
||||
|
||||
#include <boost/archive/text_oarchive.hpp>
|
||||
#include <boost/archive/text_iarchive.hpp>
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
template <typename Grid>
|
||||
struct AnalyzedGrid {
|
||||
Grid grid;
|
||||
Solution<Grid> analysis;
|
||||
BasicRules rules;
|
||||
|
||||
private:
|
||||
friend class boost::serialization::access;
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int version){
|
||||
ar & grid & analysis.solution_traces;
|
||||
}
|
||||
};
|
||||
|
||||
inline auto grid_from_file(std::string file_path){
|
||||
std::ifstream file(file_path);
|
||||
boost::archive::text_iarchive ar(file);
|
||||
|
||||
AnalyzedGrid<DynamicGrid> ret;
|
||||
ar >> ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline auto grid_to_file(AnalyzedGrid<DynamicGrid> const & grid, std::string file_path){
|
||||
std::ofstream file(file_path);
|
||||
boost::archive::text_oarchive ar(file);
|
||||
|
||||
ar << grid;
|
||||
}
|
||||
|
|
@ -2,6 +2,10 @@
|
|||
|
||||
#include "utilities.hpp"
|
||||
|
||||
#include <boost/serialization/vector.hpp>
|
||||
#include <boost/serialization/utility.hpp>
|
||||
#include <boost/serialization/serialization.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
@ -17,6 +21,8 @@ struct DynamicGrid{
|
|||
|
||||
DynamicGrid(int width, int height, std::vector<int> && data);
|
||||
|
||||
DynamicGrid() = default;
|
||||
|
||||
//! \brief Returns an array of all valid positions (may be empty)
|
||||
auto const & all_positions() const{
|
||||
return positions;
|
||||
|
@ -64,6 +70,9 @@ struct DynamicGrid{
|
|||
//! \brief Pretty prints grid to out
|
||||
void print(std::ostream& out) const;
|
||||
|
||||
//! \brief Return a hash of the grid (hopefully unique)
|
||||
size_t hash() const;
|
||||
|
||||
private:
|
||||
int W;
|
||||
int H;
|
||||
|
@ -78,6 +87,16 @@ private:
|
|||
|
||||
//! \brief Returns true if the whole column at x is empty
|
||||
auto empty_column(int x);
|
||||
|
||||
friend class boost::serialization::access;
|
||||
|
||||
//! \brief Serializes the grid
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int version){
|
||||
ar & W & H & grid & positions;
|
||||
// archiving positions is redundant (I'm being lazy here)
|
||||
// consider make_nvp for readable json
|
||||
}
|
||||
};
|
||||
|
||||
template <typename URNG>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
struct BasicRules {
|
||||
BasicRules() = default;
|
||||
BasicRules(unsigned int minimal_cluster_size)
|
||||
: min_size(minimal_cluster_size)
|
||||
{}
|
||||
|
@ -15,4 +16,10 @@ struct BasicRules {
|
|||
|
||||
private:
|
||||
unsigned int min_size;
|
||||
|
||||
friend class boost::serialization::access;
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar, const unsigned int version){
|
||||
ar & min_size;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include "dynamic_grid.hpp"
|
||||
#include "colored_output.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
//! \brief Helper function for all_positions()
|
||||
|
@ -95,3 +96,13 @@ void DynamicGrid::print(std::ostream& out) const {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t DynamicGrid::hash() const {
|
||||
std::string hs(W*H, '\0');
|
||||
auto it = hs.begin();
|
||||
for(auto&&p : all_positions()){
|
||||
*it++ = static_cast<char>(get(p));
|
||||
}
|
||||
|
||||
return std::hash<std::string>()(hs);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#include "clusters.hpp"
|
||||
#include "solver.hpp"
|
||||
#include "rules.hpp"
|
||||
#include "analyzed_grid.hpp"
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
|
@ -68,6 +69,13 @@ int main(int argc, char** argv){
|
|||
std::cout << "no solutions\n";
|
||||
}
|
||||
}
|
||||
|
||||
std::string s = solution.solution_traces.empty() ? "u" : "s";
|
||||
std::string filename = "levels/" + std::to_string(w) + "_" + std::to_string(h) + "_" + std::to_string(c) + "_" + s + "_" + std::to_string(field.hash()) + ".lvl";
|
||||
grid_to_file({std::move(field), std::move(solution), rules}, filename);
|
||||
if(verbose){
|
||||
std::cout << filename << "\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << solvable << " solvable\n";
|
||||
|
|
Reference in a new issue