/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2011 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * LinkedCell_Controller.cpp * * Created on: Nov 15, 2011 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "Box.hpp" #include "LinkedCell_Controller.hpp" #include "LinkedCell_Model.hpp" #include "LinkedCell_View.hpp" #include "CodePatterns/Assert.hpp" using namespace LinkedCell; /** Constructor of class LinkedCell_Controller. * */ LinkedCell_Controller::LinkedCell_Controller(const Box &_domain) : domain(_domain) {} /** Destructor of class LinkedCell_Controller. * * Here, we free all LinkedCell_Model instances again. * */ LinkedCell_Controller::~LinkedCell_Controller() { // remove all linked cell models for(MapEdgelengthModel::iterator iter = ModelsMap.begin(); !ModelsMap.empty(); iter = ModelsMap.begin()) { delete iter->second; ModelsMap.erase(iter); } } /** Internal function to decide whether a suitable model is present or not. * * Here, the heuristic for deciding whether a new linked cell structure has to * be constructed or not is implemented. * * \note Dealing out a pointer is here (hopefully) safe because the function is * internal and we - inside this class - know what we are doing. * * @param distance edge length of the requested linked cell structure * @return NULL - there is no fitting LinkedCell_Model, else - pointer to instance */ const LinkedCell_Model *LinkedCell_Controller::getBestModel(const double distance) const { for(MapEdgelengthModel::const_iterator iter = ModelsMap.begin(); iter != ModelsMap.end(); ++iter) { // here, we use the heuristic if (true) { return iter->second; } } return NULL; } /** Returns the a suitable LinkedCell_Model contained in a LinkedCell_View * for the requested \a distance. * * \sa getBestModel() * * @param distance edge length of the requested linked cell structure * @return LinkedCell_View wrapping the best LinkedCell_Model */ LinkedCell_View LinkedCell_Controller::getView(const double distance) { // look for best instance const LinkedCell_Model *LCModel_best = getBestModel(distance); // construct new instance if none found if (LCModel_best == NULL) { LinkedCell_Model *LCModel_new = new LinkedCell_Model(distance, domain); std::pair< MapEdgelengthModel::iterator, bool> inserter = ModelsMap.insert( std::make_pair(distance, LCModel_new) ); ASSERT(inserter.second, "LinkedCell_Controller::getView() - LinkedCell_Model instance with distance " +toString(distance)+" already present."); LinkedCell_View interface(const_cast(*LCModel_new)); return interface; } else { // construct interface and return LinkedCell_View interface(*LCModel_best); return interface; } }