ranges-diff.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2004 00008 * 00009 * Last modified: 00010 * $Date: 2010-07-28 17:35:33 +0200 (Wed, 28 Jul 2010) $ by $Author: schulte $ 00011 * $Revision: 11294 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 namespace Gecode { namespace Iter { namespace Ranges { 00039 00046 template<class I, class J> 00047 class Diff : public MinMax { 00048 protected: 00050 I i; 00052 J j; 00053 public: 00055 00056 00057 Diff(void); 00059 Diff(I& i, J& j); 00061 void init(I& i, J& j); 00063 00065 00066 00067 void operator ++(void); 00069 }; 00070 00071 00072 00073 template<class I, class J> 00074 forceinline void 00075 Diff<I,J>::operator ++(void) { 00076 // Precondition: mi <= ma 00077 // Task: find next mi greater than ma 00078 while (true) { 00079 if (!i()) break; 00080 mi = ma+1; 00081 ma = i.max(); 00082 if (mi > i.max()) { 00083 ++i; 00084 if (!i()) break; 00085 mi = i.min(); 00086 ma = i.max(); 00087 } 00088 while (j() && (j.max() < mi)) 00089 ++j; 00090 if (j() && (j.min() <= ma)) { 00091 // Now the interval [mi ... ma] must be shrunken 00092 // Is [mi ... ma] completely consumed? 00093 if ((mi >= j.min()) && (ma <= j.max())) 00094 continue; 00095 // Does [mi ... ma] overlap on the left? 00096 if (j.min() <= mi) { 00097 mi = j.max()+1; 00098 // Search for max! 00099 ++j; 00100 if (j() && (j.min() <= ma)) 00101 ma = j.min()-1; 00102 } else { 00103 ma = j.min()-1; 00104 } 00105 } 00106 return; 00107 } 00108 finish(); 00109 } 00110 00111 template<class I, class J> 00112 forceinline 00113 Diff<I,J>::Diff(void) {} 00114 00115 template<class I, class J> 00116 forceinline 00117 Diff<I,J>::Diff(I& i0, J& j0) 00118 : i(i0), j(j0) { 00119 if (!i()) { 00120 finish(); 00121 } else { 00122 mi = i.min()-1; ma = mi; 00123 operator ++(); 00124 } 00125 } 00126 00127 template<class I, class J> 00128 forceinline void 00129 Diff<I,J>::init(I& i0, J& j0) { 00130 i = i0; j = j0; 00131 if (!i()) { 00132 finish(); 00133 } else { 00134 mi = i.min()-1; ma = mi; 00135 operator ++(); 00136 } 00137 } 00138 00139 }}} 00140 00141 // STATISTICS: iter-any 00142