/*
 * 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 .
 */
/*
 * MoleculeOrderDescriptor.cpp
 *
 *  Created on: Dec 7, 2010
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "CodePatterns/MemDebug.hpp"
#include "MoleculeOrderDescriptor.hpp"
#include "MoleculeOrderDescriptor_impl.hpp"
#include "molecule.hpp"
using namespace std;
MoleculeOrderDescriptor_impl::MoleculeOrderDescriptor_impl(int _id) :
  id(_id)
{}
MoleculeOrderDescriptor_impl::~MoleculeOrderDescriptor_impl()
{}
bool MoleculeOrderDescriptor_impl::predicate(std::pair _molecule)
{
  molecule *mol = find();
  return (mol == _molecule.second);
}
MoleculeDescriptor MoleculeByOrder(int id){
  return MoleculeDescriptor(MoleculeDescriptor::impl_ptr(new MoleculeOrderDescriptor_impl(id)));
}
World::MoleculeSet& MoleculeOrderDescriptor_impl::getMolecules()
{
  return World::getInstance().molecules;
}
molecule* MoleculeOrderDescriptor_impl::find() {
  World::MoleculeSet &molecules = getMolecules();
  int i=0;
  molecule *mol = NULL;
  if (id == 0) {
    return NULL;
  } else if (id > 0) {
    World::MoleculeSet::internal_iterator res = molecules.begin_internal();
    for (; res != molecules.end_internal(); ++res) { // when iterator is normal, ++ goes forward!
      ++i;
      if (id == i) {
        mol = res->second;
        break;
      }
    }
  } else {
    World::MoleculeSet::reverse_internal_iterator res = molecules.rbegin_internal();
    for (; res != molecules.rend_internal(); ++res) {  // when iterator is reverse, ++ goes backward!
      --i;
      if (id == i) {
        mol = res->second;
        break;
      }
    }
  }
  return mol;
}
vector MoleculeOrderDescriptor_impl::findAll(){
  molecule *res = find();
  return (res)?(vector(1,res)):(vector());
}