/*
 * 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 .
 */
/*
 * atom_bondedparticleinfo.cpp
 *
 *  Created on: Oct 19, 2009
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Assert.hpp"
#include "CodePatterns/Log.hpp"
#include "WorldTime.hpp"
#include "atom_bondedparticleinfo.hpp"
BondList BondedParticleInfo::emptyList;
void BondedParticleInfo::AppendTrajectoryStep(const unsigned int _step)
{
  ListOfBonds.insert( std::make_pair(_step, emptyList) );
  LOG(5,"BondedParticleInfo::AppendTrajectoryStep() called, size is " << ListOfBonds.size());
}
void BondedParticleInfo::removeTrajectorySteps(const unsigned int _firststep, const unsigned int _laststep)
{
  const BondTrajectory_t::iterator firstiter = ListOfBonds.lower_bound(_firststep);
  const BondTrajectory_t::iterator lastiter = ListOfBonds.upper_bound(_laststep);
  ListOfBonds.erase(firstiter, lastiter);
  LOG(5,"BondedParticleInfo::removeTrajectorySteps() called, size is " << ListOfBonds.size());
}
const BondList& BondedParticleInfo::getListOfBonds() const
{
  return getListOfBondsAtStep(WorldTime::getTime());
}
template 
void setEntryInList(
    List_t &_list,
    const typename List_t::key_type _step,
    const typename List_t::mapped_type &_value)
{
  typename List_t::iterator iter =
    _list.find(_step);
  if (iter != _list.end())
    iter->second = _value;
  else
    _list.insert( std::make_pair(_step, _value) );
}
template 
const typename List_t::mapped_type& getEntryInList(
    const List_t &_list,
    const typename List_t::key_type _step,
    const typename List_t::mapped_type &_empty)
{
  typename List_t::const_iterator iter =
    _list.find(_step);
  if (iter != _list.end())
    return iter->second;
  return _empty;
}
const BondList& BondedParticleInfo::getListOfBondsAtStep(unsigned int _step) const
{
  return getEntryInList(ListOfBonds, _step, emptyList);
}
const unsigned char& BondedParticleInfo::getMaxOrder() const
{
  return getMaxOrder(WorldTime::getTime());
}
const unsigned char& BondedParticleInfo::getMaxOrder(unsigned int _step) const
{
  static unsigned char emptyOrder=0;
  return getEntryInList(MaxOrder, _step, emptyOrder);
}
void BondedParticleInfo::setMaxOrder(unsigned char _value)
{
  setMaxOrder(WorldTime::getTime(), _value);
}
void BondedParticleInfo::setMaxOrder(unsigned int _step, const unsigned char _value)
{
  setEntryInList(MaxOrder, _step, _value);
}
const unsigned char& BondedParticleInfo::getAdaptiveOrder() const
{
  return getAdaptiveOrder(WorldTime::getTime());
}
const unsigned char& BondedParticleInfo::getAdaptiveOrder(unsigned int _step) const
{
  static unsigned char emptyOrder=0;
  return getEntryInList(AdaptiveOrder, _step, emptyOrder);
}
void BondedParticleInfo::setAdaptiveOrder(unsigned char _value)
{
  setAdaptiveOrder(WorldTime::getTime(), _value);
}
void BondedParticleInfo::setAdaptiveOrder(unsigned int _step, const unsigned char _value)
{
  setEntryInList(AdaptiveOrder, _step, _value);
}