![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 5, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #ifndef WNS_ROUNDROBIN_HPP 00029 #define WNS_ROUNDROBIN_HPP 00030 00031 #include <WNS/Assure.hpp> 00032 00033 #include <vector> 00034 #include <algorithm> 00035 00036 namespace wns { 00037 00038 template <typename T> 00039 class RoundRobin 00040 { 00041 typedef T Element; 00042 typedef std::vector<Element> Container; 00043 public: 00044 explicit 00045 RoundRobin() : 00046 elements(), 00047 first(0), 00048 inRound(false), 00049 cursor(0) 00050 { 00051 } 00052 00059 void 00060 startRound() const 00061 { 00062 assure(!inRound, "May only be called after endRound or after constructor"); 00063 inRound = true; 00064 countdown = size(); 00065 cursor = first; 00066 } 00067 00073 void 00074 endRound() 00075 { 00076 assure(inRound, "May only be called after startRound"); 00077 inRound = false; 00078 first = cursor; 00079 } 00080 00084 void 00085 cancelRound() const 00086 { 00087 assure(inRound, "May only be called after startRound"); 00088 inRound = false; 00089 cursor = first; 00090 } 00091 00095 bool 00096 isInRound() const 00097 { 00098 return(inRound); 00099 } 00100 00107 const Element 00108 current() const 00109 { 00110 assure(cursor < size(), "Cursor beyond bounds"); 00111 return elements[cursor]; 00112 } 00113 00114 00120 const Element 00121 next() const 00122 { 00123 assure(inRound, "May only be called after startRound"); 00124 assure(countdown > 0, "No more Elements"); 00125 assure(cursor < size(), "Cursor beyond bounds"); 00126 --countdown; 00127 int current = cursor; 00128 advanceCursor(); 00129 return elements[current]; 00130 } 00131 00138 bool 00139 hasNext() const 00140 { 00141 assure(inRound, "May only be called after startRound"); 00142 return countdown > 0; 00143 } 00144 00148 long int 00149 size() const 00150 { 00151 return elements.size(); 00152 } 00153 00157 bool 00158 empty() const 00159 { 00160 return elements.empty(); 00161 } 00162 00171 void 00172 add(Element e) 00173 { 00174 assure(std::find(elements.begin(), 00175 elements.end(), 00176 e) == elements.end(), 00177 "element to be added must be unique"); 00178 00179 assure(!inRound, "May only be called when not in round"); 00180 if(first==0) { 00181 elements.push_back(e); 00182 } else { 00183 elements.insert(elements.begin()+first-1, e); 00184 } 00185 } 00186 00195 void 00196 remove(Element e) 00197 { 00198 assure(!inRound, "May only be called when not in round"); 00199 assure(std::find(elements.begin(), 00200 elements.end(), 00201 e) != elements.end(), 00202 "element to be removed not contained"); 00203 00204 for(size_t ii = 0; 00205 ii < elements.size(); 00206 ++ii) 00207 { 00208 if(elements[ii] == e) 00209 { 00210 elements.erase(elements.begin()+ii); 00211 if(ii < static_cast<size_t>(first)) 00212 { 00213 --first; 00214 } 00215 } 00216 } 00217 } 00218 00219 void 00220 clear() 00221 { 00222 assure(!inRound, "May only be called when not in round"); 00223 elements.clear(); 00224 } 00225 00231 Container 00232 getAllElements() const 00233 { 00234 return elements; 00235 } 00236 00237 private: 00238 void advanceCursor() const 00239 { 00240 ++cursor %= size(); 00241 } 00242 Container elements; 00243 long int first; 00244 mutable bool inRound; 00245 mutable long int cursor; 00246 mutable long int countdown; 00247 }; 00248 } // wns 00249 00250 #endif // not defined WNS_ROUNDROBIN_HPP 00251 00252 00253
1.5.5