this repo has no description
at develop 5.0 kB view raw
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Copyright: 7 * Christian Schulte, 2004 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34namespace Gecode { namespace Iter { namespace Ranges { 35 36 /** 37 * \brief %Range iterator for appending two range iterators 38 * 39 * The iterators are allowed to be adjacent but not to 40 * overlap (in this case, use Gecode::Iter::Union). 41 * 42 * \ingroup FuncIterRanges 43 */ 44 45 template<class I, class J> 46 class Append : public MinMax { 47 protected: 48 /// First iterator 49 I i; 50 /// Iterator to be appended 51 J j; 52 public: 53 /// \name Constructors and initialization 54 //@{ 55 /// Default constructor 56 Append(void); 57 /// Initialize with iterator \a i and \a j 58 Append(I& i, J& j); 59 /// Initialize with iterator \a i and \a j 60 void init(I& i, J& j); 61 //@} 62 63 /// \name Iteration control 64 //@{ 65 /// Move iterator to next range (if possible) 66 void operator ++(void); 67 //@} 68 }; 69 70 71 /** 72 * \brief Range iterator for appending arbitrarily many iterators 73 * 74 * The iterators are allowed to be adjacent but not to 75 * overlap (in this case, use Gecode::Iter::NaryUnion) 76 * 77 * \ingroup FuncIterRanges 78 */ 79 80 template<class I> 81 class NaryAppend : public MinMax { 82 protected: 83 /// The array of iterators to be appended 84 I* r; 85 /// Number of iterators 86 int n; 87 /// Number of current iterator being processed 88 int active; 89 public: 90 /// \name Constructors and initialization 91 //@{ 92 /// Default constructor 93 NaryAppend(void); 94 /// Initialize with \a n iterators in \a i 95 NaryAppend(I* i, int n); 96 /// Initialize with \a n iterators in \a i 97 void init(I* i, int n); 98 //@} 99 100 /// \name Iteration control 101 //@{ 102 /// Move iterator to next range (if possible) 103 void operator ++(void); 104 //@} 105 }; 106 107 108 /* 109 * Binary Append 110 * 111 */ 112 113 template<class I, class J> 114 inline void 115 Append<I,J>::operator ++(void) { 116 if (i()) { 117 mi = i.min(); ma = i.max(); 118 ++i; 119 if (!i() && j() && (j.min() == ma+1)) { 120 ma = j.max(); 121 ++j; 122 } 123 } else if (j()) { 124 mi = j.min(); ma = j.max(); 125 ++j; 126 } else { 127 finish(); 128 } 129 } 130 131 132 template<class I, class J> 133 forceinline 134 Append<I,J>::Append(void) {} 135 136 template<class I, class J> 137 forceinline 138 Append<I,J>::Append(I& i0, J& j0) 139 : i(i0), j(j0) { 140 if (i() || j()) 141 operator ++(); 142 else 143 finish(); 144 } 145 146 template<class I, class J> 147 forceinline void 148 Append<I,J>::init(I& i0, J& j0) { 149 i = i0; j = j0; 150 if (i() || j()) 151 operator ++(); 152 else 153 finish(); 154 } 155 156 157 /* 158 * Nary Append 159 * 160 */ 161 162 template<class I> 163 inline void 164 NaryAppend<I>::operator ++(void) { 165 mi = r[active].min(); 166 ma = r[active].max(); 167 ++r[active]; 168 while (!r[active]()) { 169 //Skip empty iterators: 170 do { 171 active++; 172 if (active >= n) { 173 finish(); return; 174 } 175 } while (!r[active]()); 176 if (r[active].min() == ma+1){ 177 ma = r[active].max(); 178 ++r[active]; 179 } else { 180 return; 181 } 182 } 183 } 184 185 template<class I> 186 forceinline 187 NaryAppend<I>::NaryAppend(void) {} 188 189 template<class I> 190 inline 191 NaryAppend<I>::NaryAppend(I* r0, int n0) 192 : r(r0), n(n0), active(0) { 193 while (active < n && !r[active]()) 194 active++; 195 if (active < n){ 196 operator ++(); 197 } else { 198 finish(); 199 } 200 } 201 202 template<class I> 203 inline void 204 NaryAppend<I>::init(I* r0, int n0) { 205 r = r0; n = n0; active = 0; 206 while (active < n && !r[active]()) 207 active++; 208 if (active < n){ 209 operator ++(); 210 } else { 211 finish(); 212 } 213 } 214 215}}} 216 217// STATISTICS: iter-any 218