X-Git-Url: http://git.scottworley.com/slidingtile/blobdiff_plain/dfc82124a67c1721299a1db521b457b99191b384..refs/heads/master:/sliding_tile_lib.cc diff --git a/sliding_tile_lib.cc b/sliding_tile_lib.cc index 551bc1f..83d6297 100644 --- a/sliding_tile_lib.cc +++ b/sliding_tile_lib.cc @@ -1,6 +1,7 @@ #include "sliding_tile_lib.h" #include +#include #include #include #include @@ -9,6 +10,7 @@ #include #include #include +#include #include #include @@ -189,7 +191,7 @@ std::ostream& operator<<(std::ostream& os, const Step& step) { return os; } -std::shared_ptr find_path(const std::string& start, const std::string& goal) { +std::shared_ptr find_path(const std::string& start, const std::string& goal, unsigned max_frontier) { std::istringstream iss_start{start}, iss_goal{goal}; Board board_start, board_goal; iss_start >> board_start; @@ -200,10 +202,19 @@ std::shared_ptr find_path(const std::string& start, const std::string& goa if (iss_goal.fail() || !iss_goal.eof()) { throw std::runtime_error("Could not parse the goal board: " + goal); } - return find_path(board_start, board_goal); + return find_path(board_start, board_goal, max_frontier); } -std::shared_ptr find_path(const Board& start, const Board& goal) { +static void show_memory_stats() { + std::ifstream statm{"/proc/self/statm"}; + if (statm.is_open()) { + std::string statm_data; + std::getline(statm, statm_data); + std::cerr << "Memory stats: " << statm_data << std::endl; + } +} + +std::shared_ptr find_path(const Board& start, const Board& goal, unsigned max_frontier) { InvertedBoard invgoal = goal.invert(); std::multimap> todo; std::set seen; @@ -215,6 +226,7 @@ std::shared_ptr find_path(const Board& start, const Board& goal) { auto cur = todo.begin()->second; todo.erase(todo.begin()); if (cur->board == goal) { + show_memory_stats(); return cur; } std::vector> successors = cur->successors(cur); @@ -230,6 +242,9 @@ std::shared_ptr find_path(const Board& start, const Board& goal) { } } } + while (todo.size() > max_frontier) { + todo.erase(--todo.end()); + } } throw std::runtime_error("No path from start to goal"); }