this repo has no description
at develop 4.1 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 * \defgroup FuncIterRangesOp Operations on range iterators 38 * 39 * \ingroup FuncIterRanges 40 */ 41 42 //@{ 43 /// Size of all ranges of range iterator \a i 44 template<class I> 45 unsigned int size(I& i); 46 47 /// Check whether range iterators \a i and \a j are equal 48 template<class I, class J> 49 bool equal(I& i, J& j); 50 51 /// Check whether range iterator \a i is subset of range iterator \a j 52 template<class I, class J> 53 bool subset(I& i, J& j); 54 55 /// Check whether range iterators \a i and \a j are disjoint 56 template<class I, class J> 57 bool disjoint(I& i, J& j); 58 59 /// Comapre two iterators with each other 60 enum CompareStatus { 61 CS_SUBSET, ///< First is subset of second iterator 62 CS_DISJOINT, ///< Intersection is empty 63 CS_NONE ///< Neither of the above 64 }; 65 66 /// Check whether range iterator \a i is a subset of \a j, or whether they are disjoint 67 template<class I, class J> 68 CompareStatus compare(I& i, J& j); 69 //@} 70 71 72 template<class I> 73 inline unsigned int 74 size(I& i) { 75 unsigned int s = 0; 76 while (i()) { 77 s += i.width(); ++i; 78 } 79 return s; 80 } 81 82 template<class I, class J> 83 forceinline bool 84 equal(I& i, J& j) { 85 // Are i and j equal? 86 while (i() && j()) 87 if ((i.min() == j.min()) && (i.max() == j.max())) { 88 ++i; ++j; 89 } else { 90 return false; 91 } 92 return !i() && !j(); 93 } 94 95 template<class I, class J> 96 forceinline bool 97 subset(I& i, J& j) { 98 // Is i subset of j? 99 while (i() && j()) 100 if (j.max() < i.min()) { 101 ++j; 102 } else if ((i.min() >= j.min()) && (i.max() <= j.max())) { 103 ++i; 104 } else { 105 return false; 106 } 107 return !i(); 108 } 109 110 template<class I, class J> 111 forceinline bool 112 disjoint(I& i, J& j) { 113 // Are i and j disjoint? 114 while (i() && j()) 115 if (j.max() < i.min()) { 116 ++j; 117 } else if (i.max() < j.min()) { 118 ++i; 119 } else { 120 return false; 121 } 122 return true; 123 } 124 125 template<class I, class J> 126 forceinline CompareStatus 127 compare(I& i, J& j) { 128 bool subset = true; 129 bool disjoint = true; 130 while (i() && j()) { 131 if (j.max() < i.min()) { 132 ++j; 133 } else if (i.max() < j.min()) { 134 ++i; subset = false; 135 } else if ((i.min() >= j.min()) && (i.max() <= j.max())) { 136 ++i; disjoint = false; 137 } else if (i.max() <= j.max()) { 138 ++i; disjoint = false; subset = false; 139 } else if (j.max() <= i.max()) { 140 ++j; disjoint = false; subset = false; 141 } 142 } 143 if (i()) 144 subset = false; 145 if (subset) 146 return CS_SUBSET; 147 return disjoint ? CS_DISJOINT : CS_NONE; 148 } 149 150}}} 151 152// STATISTICS: iter-any 153