/usr/include/gecode/iter/ranges-union.hpp is in libgecode-dev 4.4.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2004
*
* Last modified:
* $Date: 2013-07-11 19:23:04 +0200 (Thu, 11 Jul 2013) $ by $Author: schulte $
* $Revision: 13866 $
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <algorithm>
namespace Gecode { namespace Iter { namespace Ranges {
/**
* \brief Range iterator for computing union (binary)
*
* \ingroup FuncIterRanges
*/
template<class I, class J>
class Union : public MinMax {
protected:
/// First iterator
I i;
/// Second iterator
J j;
public:
/// \name Constructors and initialization
//@{
/// Default constructor
Union(void);
/// Initialize with iterator \a i and \a j
Union(I& i, J& j);
/// Initialize with iterator \a i and \a j
void init(I& i, J& j);
//@}
/// \name Iteration control
//@{
/// Move iterator to next range (if possible)
void operator ++(void);
//@}
};
/**
* \brief Range iterator for union of iterators
*
* \ingroup FuncIterRanges
*/
class NaryUnion : public RangeListIter {
protected:
/// Freelist used for allocation
RangeList* f;
/// Return range list for union of two iterators
template<class I, class J>
RangeList* two(I& i, J& j);
/// Insert ranges from \a i into \a u
template<class I>
void insert(I& i, RangeList*& u);
public:
/// \name Constructors and initialization
//@{
/// Default constructor
NaryUnion(void);
/// Initialize with single iterator \a i
template<class I>
NaryUnion(Region& r, I& i);
/// Initialize with iterators \a i and \a j
template<class I, class J>
NaryUnion(Region& r, I& i, J& j);
/// Initialize with \a n iterators in \a i
template<class I>
NaryUnion(Region& r, I* i, int n);
/// Initialize with single iterator \a i
template<class I>
void init(Region& r, I& i);
/// Initialize with iterators \a i and \a j
template<class I, class J>
void init(Region& r, I& i, J& j);
/// Initialize with \a n iterators in \a i
template<class I>
void init(Region& r, I* i, int n);
/// Add iterator \a i
template<class I>
void operator |=(I& i);
/// Assignment operator (both iterators must be allocated from the same region)
NaryUnion& operator =(const NaryUnion& m);
//@}
};
/*
* Binary union
*
*/
template<class I, class J>
inline void
Union<I,J>::operator ++(void) {
if (!i() && !j()) {
finish(); return;
}
if (!i() || (j() && (j.max()+1 < i.min()))) {
mi = j.min(); ma = j.max(); ++j; return;
}
if (!j() || (i() && (i.max()+1 < j.min()))) {
mi = i.min(); ma = i.max(); ++i; return;
}
mi = std::min(i.min(),j.min());
ma = std::max(i.max(),j.max());
++i; ++j;
next:
if (i() && (i.min() <= ma+1)) {
ma = std::max(ma,i.max()); ++i;
goto next;
}
if (j() && (j.min() <= ma+1)) {
ma = std::max(ma,j.max()); ++j;
goto next;
}
}
template<class I, class J>
forceinline
Union<I,J>::Union(void) {}
template<class I, class J>
forceinline
Union<I,J>::Union(I& i0, J& j0)
: i(i0), j(j0) {
operator ++();
}
template<class I, class J>
forceinline void
Union<I,J>::init(I& i0, J& j0) {
i = i0; j = j0;
operator ++();
}
/*
* Nary union
*
*/
template<class I, class J>
RangeListIter::RangeList*
NaryUnion::two(I& i, J& j) {
RangeList* h;
RangeList** c = &h;
while (i() && j())
if (i.max()+1 < j.min()) {
RangeList* t = range(i); ++i;
*c = t; c = &t->next;
} else if (j.max()+1 < i.min()) {
RangeList* t = range(j); ++j;
*c = t; c = &t->next;
} else {
int min = std::min(i.min(),j.min());
int max = std::max(i.max(),j.max());
++i; ++j;
nexta:
if (i() && (i.min() <= max+1)) {
max = std::max(max,i.max()); ++i;
goto nexta;
}
if (j() && (j.min() <= max+1)) {
max = std::max(max,j.max()); ++j;
goto nexta;
}
RangeList* t = range(min,max);
*c = t; c = &t->next;
}
for ( ; i(); ++i) {
RangeList* t = range(i);
*c = t; c = &t->next;
}
for ( ; j(); ++j) {
RangeList* t = range(j);
*c = t; c = &t->next;
}
*c = NULL;
return h;
}
template<class I>
void
NaryUnion::insert(I& i, RangeList*& u) {
// The current rangelist
RangeList** c = &u;
while ((*c != NULL) && i())
if ((*c)->max+1 < i.min()) {
// Keep range from union
c = &(*c)->next;
} else if (i.max()+1 < (*c)->min) {
// Copy range from iterator
RangeList* t = range(i,f); ++i;
// Insert
t->next = *c; *c = t; c = &t->next;
} else {
// Ranges overlap
// Compute new minimum
(*c)->min = std::min((*c)->min,i.min());
// Compute new maximum
int max = std::max((*c)->max,i.max());
// Scan from the next range in the union
RangeList* s = (*c)->next;
++i;
nextb:
if ((s != NULL) && (s->min <= max+1)) {
max = std::max(max,s->max);
RangeList* t = s;
s = s->next;
// Put deleted element into freelist
t->next = f; f = t;
goto nextb;
}
if (i() && (i.min() <= max+1)) {
max = std::max(max,i.max()); ++i;
goto nextb;
}
// Store computed max and shunt skipped ranges from union
(*c)->max = max; (*c)->next = s;
}
if (*c == NULL) {
// Copy remaining ranges from iterator
for ( ; i(); ++i) {
RangeList* t = range(i,f);
*c = t; c = &t->next;
}
*c = NULL;
}
}
forceinline
NaryUnion::NaryUnion(void)
: f(NULL) {}
template<class I>
forceinline void
NaryUnion::init(Region& r, I& i) {
RangeListIter::init(r);
f = NULL;
set(copy(i));
}
template<class I, class J>
forceinline void
NaryUnion::init(Region& r, I& i, J& j) {
RangeListIter::init(r);
f = NULL;
set(two(i,j));
}
template<class I>
forceinline void
NaryUnion::init(Region& r, I* i, int n) {
f = NULL;
RangeListIter::init(r);
int m = 0;
while ((m < n) && !i[m]())
m++;
// Union is empty
if (m >= n)
return;
n--;
while (!i[n]())
n--;
if (m == n) {
// Union is just a single iterator
set(copy(i[m]));
} else {
// At least two iterators
RangeList* u = two(i[m++],i[n--]);
// Insert the remaining iterators
for ( ; m<=n; m++)
insert(i[m], u);
set(u);
}
}
template<class I>
forceinline
NaryUnion::NaryUnion(Region& r, I& i) {
init(r, i);
}
template<class I, class J>
forceinline
NaryUnion::NaryUnion(Region& r, I& i, J& j) {
init(r, i, j);
}
template<class I>
forceinline
NaryUnion::NaryUnion(Region& r, I* i, int n) {
init(r, i, n);
}
template<class I>
forceinline void
NaryUnion::operator |=(I& i) {
RangeList* u = get();
insert(i, u);
set(u);
}
forceinline NaryUnion&
NaryUnion::operator =(const NaryUnion& m) {
f = NULL;
return static_cast<NaryUnion&>(RangeListIter::operator =(m));
}
}}}
// STATISTICS: iter-any
|