![]() |
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. 16, 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_INTERVAL_HPP 00029 #define WNS_INTERVAL_HPP 00030 00031 #include <WNS/Enumerator.hpp> 00032 #include <WNS/pyconfig/View.hpp> 00033 #include <WNS/Assure.hpp> 00034 00035 #include <string> 00036 00037 namespace wns 00038 { 00075 template<typename T> 00076 class Interval 00077 { 00078 friend class Endpoint; 00079 public: 00080 typedef T ValueType; 00081 00085 class Endpoint 00086 { 00087 friend class Interval; 00088 public: 00089 Endpoint(const ValueType& bound, const ValueType& extremum) 00090 : bound(bound), extremum(extremum) 00091 {} 00092 00093 bool operator==(const Endpoint& b) const 00094 { 00095 return (bound == b.bound) && (extremum == b.extremum); 00096 } 00097 00098 bool operator<(const Endpoint& b) const 00099 { 00100 return (extremum < b.extremum); 00101 } 00102 00103 bool operator<=(const Endpoint& b) const 00104 { 00105 return (extremum <= b.extremum); 00106 } 00107 00108 bool operator>(const Endpoint& b) const 00109 { 00110 return (extremum > b.extremum); 00111 } 00112 00113 bool operator>=(const Endpoint& b) const 00114 { 00115 return (extremum >= b.extremum); 00116 } 00117 00118 private: 00119 ValueType bound; 00120 ValueType extremum; 00121 }; 00122 00126 class FromInclExclEndpoint : public Endpoint 00127 { 00128 public: 00129 FromInclExclEndpoint(const ValueType& bound, const ValueType& extremum) 00130 : Endpoint(bound, extremum) 00131 {} 00132 00133 Interval ToIncluding(const ValueType& b) const 00134 { 00135 return Interval(*this, Endpoint(b, b)); 00136 } 00137 00138 Interval ToExcluding(const ValueType& b) const 00139 { 00140 return Interval(*this, Endpoint(b, --makeEnumerable(b))); 00141 } 00142 }; 00143 00147 class FromEndpoint : public Endpoint 00148 { 00149 public: 00150 FromEndpoint(const ValueType& bound, const ValueType& extremum) 00151 : Endpoint(bound, extremum) 00152 {} 00153 00154 Interval To(const ValueType& b) const 00155 { 00156 return Interval(*this, Endpoint(b, b)); 00157 } 00158 00159 }; 00160 00164 class BetweenEndpoint : public Endpoint 00165 { 00166 public: 00167 BetweenEndpoint(const ValueType& bound, const ValueType& extremum) 00168 : Endpoint(bound, extremum) 00169 {} 00170 00171 Interval And(const ValueType& b) const 00172 { 00173 return Interval(*this, Endpoint(b, --makeEnumerable(b))); 00174 } 00175 }; 00176 00183 static Interval CreateFrom(const pyconfig::View& config) 00184 { 00185 const ValueType min = config.get<ValueType>("lowerBound"); 00186 00187 const ValueType max = config.get<ValueType>("upperBound"); 00188 00189 const std::string intervalType = config.get<std::string>("intervalType"); 00190 00191 Endpoint a(min, (intervalType[0] == '(') ? ++makeEnumerable(min) : min); 00192 Endpoint b(max, (intervalType[1] == ')') ? --makeEnumerable(max) : max); 00193 return Interval<ValueType>(a, b); 00194 } 00195 00203 static BetweenEndpoint Between(const ValueType& a) 00204 { 00205 return BetweenEndpoint(a, ++makeEnumerable(a)); 00206 } 00207 00215 static FromEndpoint From(const ValueType& a) 00216 { 00217 return FromEndpoint(a, a); 00218 } 00219 00228 static FromInclExclEndpoint FromIncluding(const ValueType& a) 00229 { 00230 return FromInclExclEndpoint(a, a); 00231 } 00232 00241 static FromInclExclEndpoint FromExcluding(const ValueType& a) 00242 { 00243 return FromInclExclEndpoint(a, ++makeEnumerable(a)); 00244 } 00245 00248 static Interval EmptyInterval() 00249 { 00250 return Interval(); 00251 } 00252 00255 Interval() 00256 : a(Endpoint(ValueType(), ++makeEnumerable(ValueType()))), 00257 b(Endpoint(ValueType(), --makeEnumerable(ValueType()))) 00258 {} 00259 00263 bool operator==(const Interval& x) const 00264 { 00265 return (isEmpty() && x.isEmpty()) || ((a == x.a) && (b == x.b)); 00266 } 00267 00268 bool operator!=(const Interval& x) const 00269 { 00270 return !((*this) == x); 00271 } 00272 00278 bool isBelow(const ValueType& x) const 00279 { 00280 return !isEmpty() && (b.extremum < x); 00281 } 00282 00291 bool isBelow(const Interval& x) const 00292 { 00293 return !isEmpty() && !x.isEmpty() && (b.extremum < x.a.extremum); 00294 } 00295 00301 bool isAbove(const ValueType& x) const 00302 { 00303 return !isEmpty() && (x < a.extremum); 00304 } 00305 00314 bool isAbove(const Interval& x) const 00315 { 00316 return !isEmpty() && !x.isEmpty() && (a.extremum > x.b.extremum); 00317 } 00318 00324 bool isEmpty() const 00325 { 00326 return a.extremum > b.extremum; 00327 } 00328 00334 ValueType min() const 00335 { 00336 assure(!isEmpty(), "Error: Attempt to get minimum of an empty interval."); 00337 return a.extremum; 00338 } 00339 00345 ValueType max() const 00346 { 00347 assure(!isEmpty(), "Error: Attempt to get maximum of an empty interval."); 00348 return b.extremum; 00349 } 00350 00356 bool contains(const ValueType& x) const 00357 { 00358 return !isEmpty() && (a.extremum <= x) && (b.extremum >= x); 00359 } 00360 00366 bool contains(const Interval& x) const 00367 { 00368 return x.isEmpty() || (!isEmpty() && (a.extremum <= x.a.extremum) && (b.extremum >= x.b.extremum)); 00369 } 00370 00376 bool overlaps(const Interval& x) const 00377 { 00378 return !isEmpty() && !x.isEmpty() 00379 && (((x.a.extremum <= a.extremum) && (a.extremum <= x.b.extremum)) || 00380 ((a.extremum <= x.a.extremum) && (x.a.extremum <= b.extremum))); 00381 } 00382 00392 bool joinableWith(const Interval& x) const 00393 { 00394 return isEmpty() || x.isEmpty() || 00395 overlaps(Interval(Endpoint(--makeEnumerable(x.a.bound), --makeEnumerable(x.a.extremum)), x.b)) || 00396 x.overlaps(Interval(Endpoint(--makeEnumerable(a.bound), --makeEnumerable(a.extremum)), b)); 00397 } 00398 00411 Interval join(const Interval& x) const 00412 { 00413 assure(joinableWith(x), "Error: Attempt to join unjoinable intervals (must be at least adjacent)."); 00414 if (isEmpty()) return Interval(x); 00415 else if (x.isEmpty()) return Interval(*this); 00416 else { 00417 const Endpoint a(std::min(this->a, x.a)); 00418 const Endpoint b(std::max(this->b, x.b)); 00419 return Interval(a, b); 00420 } 00421 } 00422 00433 Interval intersect(const Interval& x) const 00434 { 00435 const Endpoint a(std::max(this->a, x.a)); 00436 const Endpoint b(std::min(this->b, x.b)); 00437 return Interval(a, b); 00438 } 00439 00440 private: 00441 00442 Interval(const Endpoint& a, const Endpoint& b) 00443 : a(a), b(b) 00444 {} 00445 00446 Endpoint a; 00447 Endpoint b; 00448 }; 00449 00450 } 00451 00452 #endif // _INTERVAL_HPP
1.5.5