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: 2009-09-08 21:10:29 +0200 (Tue, 08 Sep 2009) $ by $Author: schulte $ 00011 * $Revision: 9692 $ 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 private: 00054 IsRangeIter<I> constraintI; IsRangeIter<J> constraintJ; 00055 public: 00057 00058 00059 Diff(void); 00061 Diff(I& i, J& j); 00063 void init(I& i, J& j); 00065 00067 00068 00069 void operator ++(void); 00071 }; 00072 00073 00074 00075 template<class I, class J> 00076 forceinline void 00077 Diff<I,J>::operator ++(void) { 00078 // Precondition: mi <= ma 00079 // Task: find next mi greater than ma 00080 while (true) { 00081 if (!i()) break; 00082 mi = ma+1; 00083 ma = i.max(); 00084 if (mi > i.max()) { 00085 ++i; 00086 if (!i()) break; 00087 mi = i.min(); 00088 ma = i.max(); 00089 } 00090 while (j() && (j.max() < mi)) 00091 ++j; 00092 if (j() && (j.min() <= ma)) { 00093 // Now the interval [mi ... ma] must be shrunken 00094 // Is [mi ... ma] completely consumed? 00095 if ((mi >= j.min()) && (ma <= j.max())) 00096 continue; 00097 // Does [mi ... ma] overlap on the left? 00098 if (j.min() <= mi) { 00099 mi = j.max()+1; 00100 // Search for max! 00101 ++j; 00102 if (j() && (j.min() <= ma)) 00103 ma = j.min()-1; 00104 } else { 00105 ma = j.min()-1; 00106 } 00107 } 00108 return; 00109 } 00110 finish(); 00111 } 00112 00113 template<class I, class J> 00114 forceinline 00115 Diff<I,J>::Diff(void) {} 00116 00117 template<class I, class J> 00118 forceinline 00119 Diff<I,J>::Diff(I& i0, J& j0) 00120 : i(i0), j(j0) { 00121 if (!i()) { 00122 finish(); 00123 } else { 00124 mi = i.min()-1; ma = mi; 00125 operator ++(); 00126 } 00127 } 00128 00129 template<class I, class J> 00130 forceinline void 00131 Diff<I,J>::init(I& i0, J& j0) { 00132 i = i0; j = j0; 00133 if (!i()) { 00134 finish(); 00135 } else { 00136 mi = i.min()-1; ma = mi; 00137 operator ++(); 00138 } 00139 } 00140 00141 }}} 00142 00143 // STATISTICS: iter-any 00144