00001 // $Id: APPSPACK_List.cpp,v 1.8 2004/04/12 17:43:55 tgkolda Exp $ 00002 // $Source: /space/CVS-Acro/acro/packages/appspack/appspack/src/APPSPACK_List.cpp,v $ 00003 00004 //@HEADER 00005 // ************************************************************************ 00006 // 00007 // APPSPACK: Asynchronous Parallel Pattern Search 00008 // Copyright (2003) Sandia Corporation 00009 // 00010 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00011 // license for use of this work by or on behalf of the U.S. Government. 00012 // 00013 // This library is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU Lesser General Public License as 00015 // published by the Free Software Foundation; either version 2.1 of the 00016 // License, or (at your option) any later version. 00017 // 00018 // This library is distributed in the hope that it will be useful, but 00019 // WITHOUT ANY WARRANTY; without even the implied warranty of 00020 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00021 // Lesser General Public License for more details. 00022 // 00023 // You should have received a copy of the GNU Lesser General Public 00024 // License along with this library; if not, write to the Free Software 00025 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00026 // USA. . 00027 // 00028 // Questions? Contact Tammy Kolda (tgkolda@sandia.gov) 00029 // 00030 // ************************************************************************ 00031 //@HEADER 00032 00038 #include "APPSPACK_List.hpp" 00039 #include "APPSPACK_Print.hpp" 00040 00041 APPSPACK::List::List() : 00042 isValidBest(false) 00043 { 00044 00045 } 00046 00047 APPSPACK::List::~List() 00048 { 00049 prune(); 00050 } 00051 00052 bool APPSPACK::List::isEmpty() const 00053 { 00054 return tpl.empty(); 00055 } 00056 00057 bool APPSPACK::List::isNotEmpty() const 00058 { 00059 return !(tpl.empty()); 00060 } 00061 00062 int APPSPACK::List::size() const 00063 { 00064 return tpl.size(); 00065 } 00066 00067 void APPSPACK::List::push(Point* pushed) 00068 { 00069 isValidBest = false; 00070 tpl.push_front(pushed); 00071 } 00072 00073 void APPSPACK::List::insertList(List& source) 00074 { 00075 isValidBest = false; 00076 tpl.insert(tpl.begin(), source.tpl.begin(), source.tpl.end()); 00077 source.tpl.clear(); 00078 } 00079 00080 void APPSPACK::List::prune(int n) 00081 { 00082 if (n <= 0) 00083 { 00084 for (TPL::iterator tpi = tpl.begin(); tpi != tpl.end(); tpi ++) 00085 delete *tpi; 00086 00087 tpl.clear(); 00088 } 00089 else 00090 { 00091 int p = size() - n; 00092 Point* popped; 00093 for (int i = 0; i < p; i ++) 00094 { 00095 popped = pop(); 00096 delete popped; 00097 } 00098 } 00099 } 00100 00101 APPSPACK::Point* APPSPACK::List::pop() 00102 { 00103 if (tpl.empty()) 00104 return NULL; 00105 00106 isValidBest = false; 00107 Point* popped = tpl.back(); 00108 tpl.pop_back(); 00109 return popped; 00110 } 00111 00112 APPSPACK::Point* APPSPACK::List::pop(int tag) 00113 { 00114 if (tpl.empty()) 00115 return NULL; 00116 00117 TPL::iterator tpi = tpl.begin(); 00118 for (; tpi != tpl.end(); tpi ++) 00119 if (((*tpi)->getTag()) == tag) 00120 break; 00121 00122 if (tpi == tpl.end()) 00123 return NULL; 00124 00125 isValidBest = false; 00126 Point* popped = *tpi; 00127 tpl.erase(tpi); 00128 00129 return popped; 00130 } 00131 00132 void APPSPACK::List::moveBestToEndOfList() 00133 { 00134 if (isValidBest) 00135 return; 00136 00137 if (tpl.empty()) 00138 { 00139 cerr << "APPSPACK::List::moveBestToEndOfList - " 00140 << "Trying to find the best in an empty list!" << endl; 00141 throw "APPSPACK Error"; 00142 } 00143 00144 if (tpl.size() == 1) 00145 return; 00146 00147 // Find the index of the best point 00148 TPL::iterator bestIterator = tpl.begin(); 00149 TPL::iterator tpi = tpl.begin(); 00150 00151 for (tpi ++; tpi != tpl.end(); tpi ++) 00152 if (**tpi < **bestIterator) 00153 bestIterator = tpi; 00154 00155 // Swap the best point to the end of the list 00156 Point* tmp = *bestIterator; 00157 *bestIterator = tpl.back(); 00158 tpl.back() = tmp; 00159 00160 isValidBest = true; 00161 } 00162 00163 const APPSPACK::Point& APPSPACK::List::best() 00164 { 00165 moveBestToEndOfList(); 00166 return *tpl.back(); 00167 } 00168 00169 APPSPACK::Point* APPSPACK::List::popBest() 00170 { 00171 moveBestToEndOfList(); 00172 return pop(); 00173 } 00174 00175 void APPSPACK::List::print(const string label) const 00176 { 00177 cout << "\n" << label << ":\n"; 00178 00179 if (tpl.empty()) 00180 { 00181 cout << "<empty>" << endl; 00182 return; 00183 } 00184 00185 for (TPL::const_reverse_iterator tpi = tpl.rbegin(); tpi != tpl.rend(); tpi ++) 00186 cout << *(*tpi) << endl; 00187 }
© Sandia Corporation | Site Contact | Privacy and Security
Generated on Wed Dec 14 18:41:04 2005 for APPSPACK 4.0.2 by
1.3.8 written by Dimitri van Heesch,
© 1997-2002