/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2017 Frederik Heber. All rights reserved.
 *
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with MoleCuilder.  If not, see .
 */
/*
 * BreadthFirstSearchGatherer.cpp
 *
 *  Created on: May 17, 2017
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "BreadthFirstSearchGatherer.hpp"
#include "CodePatterns/Log.hpp"
#include "Graph/BoostGraphCreator.hpp"
#include 
/**
 * I have no idea why this is so complicated with BGL ...
 *
 * This is taken from the book "The Boost Graph Library: User Guide and Reference Manual, Portable Documents",
 * chapter "Basic Graph Algorithms", example on calculating the bacon number.
 */
template 
class distance_recorder : public boost::default_bfs_visitor
{
public:
  distance_recorder(DistanceMap dist) : d(dist) {}
  template 
  void tree_edge(Edge e, const Graph &g) const {
    typename boost::graph_traits::vertex_descriptor u = source(e,g), v = target(e,g);
    d[v] = d[u] + 1;
  }
private:
  DistanceMap d;
};
template 
distance_recorder record_distance(DistanceMap d)
{
  return distance_recorder(d);
}
BreadthFirstSearchGatherer::BreadthFirstSearchGatherer(BoostGraphCreator &_BGcreator) :
    BGcreator(_BGcreator)
{}
std::vector BreadthFirstSearchGatherer::operator()(const atomId_t &_discoverfrom)
{
  std::vector returnids;
  // Do BFS through graph from given vertex
  const size_t num_vertices = BGcreator.getNumVertices();
  distances_t distances(num_vertices, num_vertices+1); // set distance to num+1
  const BoostGraphCreator::UndirectedGraph &BGgraph = BGcreator.get();
  const BoostGraphCreator::nodeId_t discoverfrom_nodeid = BGcreator.getNodeId(_discoverfrom);
  distances[ boost::vertex(discoverfrom_nodeid, BGgraph) ] = 0;
  boost::breadth_first_search(
      BGgraph,
      boost::vertex(discoverfrom_nodeid, BGgraph),
      boost::visitor(record_distance(&distances[0])));
  LOG(3, "DEBUG: From atom #" << _discoverfrom
      << " BFS discovered the following distances " << distances);
  // any node was discovered whose distances is less than num_vertices+1
  const BoostGraphCreator::const_name_map_t name_map = boost::get(boost::vertex_name, BGgraph);
  BoostGraphCreator::vertex_iter vp, vpend;
  for (boost::tie(vp, vpend) = vertices(BGgraph); vp != vpend; ++vp) {
    BoostGraphCreator::Vertex v = *vp;
    if (distances[v] != (num_vertices+1)) {
      returnids.push_back( boost::get(name_map, v) );
    }
  }
  return returnids;
}