| 1 | /* | 
| 2 | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 | * | 
| 4 | * The University of Notre Dame grants you ("Licensee") a | 
| 5 | * non-exclusive, royalty free, license to use, modify and | 
| 6 | * redistribute this software in source and binary code form, provided | 
| 7 | * that the following conditions are met: | 
| 8 | * | 
| 9 | * 1. Redistributions of source code must retain the above copyright | 
| 10 | *    notice, this list of conditions and the following disclaimer. | 
| 11 | * | 
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 | *    notice, this list of conditions and the following disclaimer in the | 
| 14 | *    documentation and/or other materials provided with the | 
| 15 | *    distribution. | 
| 16 | * | 
| 17 | * This software is provided "AS IS," without a warranty of any | 
| 18 | * kind. All express or implied conditions, representations and | 
| 19 | * warranties, including any implied warranty of merchantability, | 
| 20 | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 | * be liable for any damages suffered by licensee as a result of | 
| 23 | * using, modifying or distributing the software or its | 
| 24 | * derivatives. In no event will the University of Notre Dame or its | 
| 25 | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 | * direct, indirect, special, consequential, incidental or punitive | 
| 27 | * damages, however caused and regardless of the theory of liability, | 
| 28 | * arising out of the use of or inability to use software, even if the | 
| 29 | * University of Notre Dame has been advised of the possibility of | 
| 30 | * such damages. | 
| 31 | * | 
| 32 | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 | * research, please cite the appropriate papers when you publish your | 
| 34 | * work.  Good starting points are: | 
| 35 | * | 
| 36 | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). | 
| 39 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | */ | 
| 42 |  | 
| 43 | #include <functional> | 
| 44 | #include <iterator> | 
| 45 | #include <utility> | 
| 46 |  | 
| 47 | #include "brains/PairList.hpp" | 
| 48 |  | 
| 49 | namespace OpenMD { | 
| 50 |  | 
| 51 | int *PairList::getPairList() { | 
| 52 |  | 
| 53 | if (modified_) { | 
| 54 | pairList_.clear(); | 
| 55 |  | 
| 56 | for (std::set<std::pair<int,int> >::iterator i = pairSet_.begin(); | 
| 57 | i != pairSet_.end(); ++i) { | 
| 58 | pairList_.push_back(i->first + 1); | 
| 59 | pairList_.push_back(i->second + 1); | 
| 60 | } | 
| 61 | modified_ = false; | 
| 62 | } | 
| 63 |  | 
| 64 | return pairList_.size() > 0 ? &(pairList_[0]) : NULL; | 
| 65 | } | 
| 66 |  | 
| 67 | void PairList::addPair(int i, int j) { | 
| 68 |  | 
| 69 | if (i == j) { | 
| 70 | return; | 
| 71 | } else if (i > j) { | 
| 72 | std::swap(i, j); | 
| 73 | } | 
| 74 |  | 
| 75 | std::set<std::pair<int, int> >::iterator iter = pairSet_.find(std::make_pair(i, j)); | 
| 76 |  | 
| 77 | if (iter == pairSet_.end()) { | 
| 78 | pairSet_.insert(std::make_pair(i, j)); | 
| 79 | modified_ = true; | 
| 80 | } | 
| 81 | } | 
| 82 |  | 
| 83 | void PairList::addPairs(std::set<int>& set1, std::set<int>& set2) { | 
| 84 | for (std::set<int>::iterator iter1 = set1.begin(); | 
| 85 | iter1 !=  set1.end(); ++ iter1) { | 
| 86 | for(std::set<int>::iterator iter2 = set2.begin(); | 
| 87 | iter2 != set2.end(); ++ iter2) { | 
| 88 | this->addPair(*iter1, *iter2); | 
| 89 | } | 
| 90 | } | 
| 91 | } | 
| 92 |  | 
| 93 | template<typename IterType1, typename IterType2> | 
| 94 | void PairList::addPairs(IterType1 iter1_first, IterType1 iter1_last, IterType2 iter2_first, IterType2 iter2_last) { | 
| 95 | for (IterType1 iter1 = iter1_first; iter1 != iter1_last; ++ iter1) { | 
| 96 | for(IterType2 iter2 = iter2_first; iter2 != iter2_last; ++ iter2) { | 
| 97 | this->addPair(*iter1, * iter2); | 
| 98 | } | 
| 99 | } | 
| 100 | } | 
| 101 |  | 
| 102 | void PairList::removePair(int i, int j) { | 
| 103 | if (i == j) { | 
| 104 | return; | 
| 105 | } else if (i > j) { | 
| 106 | std::swap(i, j); | 
| 107 | } | 
| 108 |  | 
| 109 |  | 
| 110 | std::set<std::pair<int, int> >::iterator iter = pairSet_.find(std::make_pair(i, j)); | 
| 111 |  | 
| 112 | if (iter != pairSet_.end()) { | 
| 113 | pairSet_.erase(iter); | 
| 114 | modified_ = true; | 
| 115 | } | 
| 116 | } | 
| 117 |  | 
| 118 | void PairList::removePairs(std::set<int>& set1, std::set<int>& set2) { | 
| 119 | for (std::set<int>::iterator iter1 = set1.begin(); | 
| 120 | iter1 !=  set1.end(); ++ iter1) { | 
| 121 | for(std::set<int>::iterator iter2 = set2.begin(); | 
| 122 | iter2 != set2.end(); ++ iter2) { | 
| 123 | this->removePair(*iter1, * iter2); | 
| 124 | } | 
| 125 | } | 
| 126 | } | 
| 127 |  | 
| 128 | template<typename IterType1, typename IterType2> | 
| 129 | void PairList::removePairs(IterType1 iter1_first, IterType1 iter1_last, IterType2 iter2_first, IterType2 iter2_last) { | 
| 130 | for (IterType1 iter1 = iter1_first; iter1 != iter1_last; ++ iter1) { | 
| 131 | for(IterType2 iter2 = iter2_first; iter2 != iter2_last; ++ iter2) { | 
| 132 | this->removePair(*iter1, * iter2); | 
| 133 | } | 
| 134 | } | 
| 135 | } | 
| 136 |  | 
| 137 | bool PairList::hasPair(int i, int j) { | 
| 138 |  | 
| 139 | if (i == j) { | 
| 140 | return false; | 
| 141 | } else if (i > j) { | 
| 142 | std::swap(i, j); | 
| 143 | } | 
| 144 |  | 
| 145 | std::set<std::pair<int, int> >::iterator  iter = pairSet_.find(std::make_pair(i, j)); | 
| 146 | return iter == pairSet_.end() ? false : true; | 
| 147 | } | 
| 148 |  | 
| 149 | int PairList::getSize() { | 
| 150 | return pairSet_.size(); | 
| 151 | } | 
| 152 |  | 
| 153 | std::ostream& operator <<(std::ostream& o, PairList& e) { | 
| 154 | std::set<std::pair<int, int> >::iterator i; | 
| 155 |  | 
| 156 | int index; | 
| 157 |  | 
| 158 | index = 0; | 
| 159 |  | 
| 160 | for(i = e.pairSet_.begin(); i != e.pairSet_.end(); ++i) { | 
| 161 | o << "pairList[" << index << "] i, j: " << (*i).first << " - " | 
| 162 | << (*i).second << "\n"; | 
| 163 | index++; | 
| 164 | } | 
| 165 |  | 
| 166 | return o; | 
| 167 | } | 
| 168 |  | 
| 169 | } | 
| 170 |  | 
| 171 |  |