--- trunk/OOPSE-2.0/src/brains/Exclude.cpp 2004/09/24 04:16:43 1490 +++ trunk/OOPSE-2.0/src/brains/Exclude.cpp 2005/11/16 23:10:02 2448 @@ -1,101 +1,166 @@ -#include -#include +/* + * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. + * + * The University of Notre Dame grants you ("Licensee") a + * non-exclusive, royalty free, license to use, modify and + * redistribute this software in source and binary code form, provided + * that the following conditions are met: + * + * 1. Acknowledgement of the program authors must be made in any + * publication of scientific results based in part on use of the + * program. An acceptable form of acknowledgement is citation of + * the article in which the program was described (Matthew + * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher + * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented + * Parallel Simulation Engine for Molecular Dynamics," + * J. Comput. Chem. 26, pp. 252-271 (2005)) + * + * 2. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 3. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * This software is provided "AS IS," without a warranty of any + * kind. All express or implied conditions, representations and + * warranties, including any implied warranty of merchantability, + * fitness for a particular purpose or non-infringement, are hereby + * excluded. The University of Notre Dame and its licensors shall not + * be liable for any damages suffered by licensee as a result of + * using, modifying or distributing the software or its + * derivatives. In no event will the University of Notre Dame or its + * licensors be liable for any lost revenue, profit or data, or for + * direct, indirect, special, consequential, incidental or punitive + * damages, however caused and regardless of the theory of liability, + * arising out of the use of or inability to use software, even if the + * University of Notre Dame has been advised of the possibility of + * such damages. + */ + +#include +#include +#include -#include "Exclude.hpp" +#include "brains/Exclude.hpp" -Exclude* Exclude::_instance = 0; +namespace oopse { -Exclude* Exclude::Instance() { - if (_instance == 0) { - _instance = new Exclude; + int *Exclude::getExcludeList() { + + if (modified_) { + excludeList_.clear(); + + for (std::set >::iterator i = excludeSet_.begin();i != excludeSet_.end(); ++i) { + excludeList_.push_back(i->first + 1); + excludeList_.push_back(i->second + 1); + } + modified_ = false; + } + + return excludeList_.size() > 0 ? &(excludeList_[0]) : NULL; } - return _instance; -} -Exclude::Exclude(){ - exPairs = NULL; - newFortranArrayNeeded = 1; -} + void Exclude::addPair(int i, int j) { -Exclude::~Exclude() { - if (exPairs != NULL) { - delete[] exPairs; + if (i == j) { + return; + } else if (i > j) { + std::swap(i, j); + } + + std::set >::iterator iter = excludeSet_.find(std::make_pair(i, j)); + + if (iter == excludeSet_.end()) { + excludeSet_.insert(std::make_pair(i, j)); + modified_ = true; + } } - delete _instance; + +void Exclude::addPairs(std::set& set1, std::set& set2) { + for (std::set::iterator iter1 = set1.begin(); iter1 != set1.end(); ++ iter1) { + for(std::set::iterator iter2 = set2.begin(); iter2 != set2.end(); ++ iter2) { + this->addPair(*iter1, * iter2); + } + } } - -int* Exclude::getFortranArray(){ - - set >::iterator i; - int j; - if (newFortranArrayNeeded != 0) { - delete[] exPairs; - exPairs = new int[2*getSize()]; - j = 0; - for(i = excludeSet.begin(); i != excludeSet.end(); ++i) { - exPairs[j] = (*i).first; - j++; - exPairs[j] = (*i).second; - j++; +template +void Exclude::addPairs(IterType1 iter1_first, IterType1 iter1_last, IterType2 iter2_first, IterType2 iter2_last) { + for (IterType1 iter1 = iter1_first; iter1 != iter1_last; ++ iter1) { + for(IterType2 iter2 = iter2_first; iter2 != iter2_last; ++ iter2) { + this->addPair(*iter1, * iter2); + } } - newFortranArrayNeeded = 0; - } - - return exPairs; } + void Exclude::removePair(int i, int j) { -void Exclude::addPair(int i, int j) { - - if (!hasPair(i, j)) { - - if (i != j) { - - if (i < j) - excludeSet.insert(make_pair(i, j)); - else - excludeSet.insert(make_pair(j, i)); + if (i == j) { + return; + } else if (i > j) { + std::swap(i, j); } - newFortranArrayNeeded = 1; + + std::set >::iterator iter = excludeSet_.find(std::make_pair(i, j)); + + if (iter != excludeSet_.end()) { + excludeSet_.erase(iter); + modified_ = true; + } } +void Exclude::removePairs(std::set& set1, std::set& set2) { + for (std::set::iterator iter1 = set1.begin(); iter1 != set1.end(); ++ iter1) { + for(std::set::iterator iter2 = set2.begin(); iter2 != set2.end(); ++ iter2) { + this->removePair(*iter1, * iter2); + } + } } +template +void Exclude::removePairs(IterType1 iter1_first, IterType1 iter1_last, IterType2 iter2_first, IterType2 iter2_last) { + for (IterType1 iter1 = iter1_first; iter1 != iter1_last; ++ iter1) { + for(IterType2 iter2 = iter2_first; iter2 != iter2_last; ++ iter2) { + this->removePair(*iter1, * iter2); + } + } +} -void Exclude::printMe( void ){ + bool Exclude::hasPair(int i, int j) { - set >::iterator i; - int index; - - index = 0; - for(i = excludeSet.begin(); i != excludeSet.end(); ++i) { + if (i == j) { + return false; + } else if (i > j) { + std::swap(i, j); + } - std::cerr << "exclude[" << index << "] i, j: " << (*i).first << " - " << (*i).second << "\n"; - index++; - - } -} + std::set >::iterator iter = excludeSet_.find(std::make_pair(i, j)); + return iter == excludeSet_.end() ? false : true; + } -int Exclude::hasPair(int i, int j) { + int Exclude::getSize() { + return excludeSet_.size(); + } - set >::iterator position; + std::ostream& operator <<(std::ostream& o, Exclude& e) { + std::set >::iterator i; - if (i != j) { - if (i < j) - position = excludeSet.find(make_pair(i, j)); - else - position = excludeSet.find(make_pair(j, i)); + int index; - if (position != excludeSet.end()) - return 1; - else - return 0; - } else - return 0; -} + index = 0; -int Exclude::getSize() { - return excludeSet.size(); + for(i = e.excludeSet_.begin(); i != e.excludeSet_.end(); ++i) { + o << "exclude[" << index << "] i, j: " << (*i).first << " - " + << (*i).second << "\n"; + index++; + } + + return o; + } + } + +