/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. 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 .
 */
/*
 * UniqueFragments.cpp
 *
 *  Created on: Oct 18, 2011
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "UniqueFragments.hpp"
#include "CodePatterns/Assert.hpp"
#include "CodePatterns/Log.hpp"
#include "Atom/atom.hpp"
#include "Bond/bond.hpp"
#include "Element/element.hpp"
#include "Fragmentation/fragmentation_helpers.hpp"
#include "Fragmentation/Graph.hpp"
#include "Fragmentation/KeySet.hpp"
/** Constructor of class UniqueFragments.
 *
 * @param _TEFactor total energy factor
 * @param _Leaflet current graph to work on
 * @param _Root root atom
 */
UniqueFragments::UniqueFragments(double _TEFactor, std::vector &_Leaflets, atom *_Root) :
    FragmentCounter(0),
    Root(_Root),
    TEFactor(_TEFactor),
    Leaflets(_Leaflets)
{
  FragmentSet = new KeySet;
}
/** Destructor of class UniqueFragments.
 *
 */
UniqueFragments::~UniqueFragments()
{
  Cleanup();
}
/** Checking whether KeySet is not already present in Graph, if so just adds factor.
 *
 * \param order order at which to insert
 */
void UniqueFragments::InsertFragmentIntoGraph(const size_t order)
{
  GraphTestPair testGraphInsert;
  ASSERT( Leaflets.size() > order,
      "UniqueFragments::InsertFragmentIntoGraph() - Leaflets has only "
      +toString(Leaflets.size())+" entries, not enough for "+toString(order));
  testGraphInsert = Leaflets[order]->insert(GraphPair (*FragmentSet,std::pair(FragmentCounter,TEFactor)));  // store fragment number and current factor
  if (testGraphInsert.second) {
    LOG(2, "KeySet " << FragmentCounter << " successfully inserted.");
    FragmentCounter++;
  } else {
    LOG(2, "KeySet " << FragmentCounter << " failed to insert, present fragment is " << ((*(testGraphInsert.first)).second).first);
    ((*(testGraphInsert.first)).second).second += TEFactor;  // increase the "created" counter
    LOG(2, "New factor is " << ((*(testGraphInsert.first)).second).second << ".");
  }
};
/** Removes some allocated memory.
 *
 */
void UniqueFragments::Cleanup()
{
  delete FragmentSet;
}
/** Getter for UniqueFragments:Root.
 *
 * @return const ref to root atom.
 */
atom * const UniqueFragments::getRoot() const
{
  return Root;
}
/** Setter for UniqueFragments:Root.
 *
 * @param _root root atom to set
 */
void UniqueFragments::setRoot(atom *_root)
{
  Root=_root;
}