int-set.icc
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, 2003 00008 * 00009 * Last modified: 00010 * $Date: 2008-07-11 09:31:51 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $ 00011 * $Revision: 7288 $ 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 { 00039 00040 /* 00041 * Integer sets 00042 * 00043 */ 00044 forceinline 00045 IntSet::IntSet(void) {} 00046 00047 template <class I> 00048 IntSet::IntSet(I& i) { 00049 Support::DynamicArray<Range> d; 00050 int n=0; 00051 while (i()) { 00052 d[n].min = i.min(); d[n].max = i.max(); 00053 ++n; ++i; 00054 } 00055 if (n > 0) { 00056 IntSetObject* o = IntSetObject::allocate(n); 00057 for (int j=n; j--; ) 00058 o->r[j]=d[j]; 00059 object(o); 00060 } 00061 } 00062 00063 #ifndef __INTEL_COMPILER 00064 template <> 00065 #endif 00066 forceinline 00067 IntSet::IntSet(const IntSet& s) 00068 : SharedHandle(s) {} 00069 00070 #ifndef __INTEL_COMPILER 00071 template <> 00072 #endif 00073 forceinline 00074 IntSet::IntSet(IntSet& s) 00075 : SharedHandle(s) {} 00076 00077 forceinline 00078 IntSet::IntSet(const int r[][2], int n) { 00079 init(r,n); 00080 } 00081 00082 forceinline 00083 IntSet::IntSet(const int r[], int n) { 00084 init(r,n); 00085 } 00086 00087 forceinline 00088 IntSet::IntSet(int n, int m) { 00089 init(n,m); 00090 } 00091 00092 forceinline int 00093 IntSet::min(int i) const { 00094 assert(object() != NULL); 00095 return static_cast<IntSetObject*>(object())->r[i].min; 00096 } 00097 00098 forceinline int 00099 IntSet::max(int i) const { 00100 assert(object() != NULL); 00101 return static_cast<IntSetObject*>(object())->r[i].max; 00102 } 00103 00104 forceinline unsigned int 00105 IntSet::width(int i) const { 00106 assert(object() != NULL); 00107 IntSetObject* o = static_cast<IntSetObject*>(object()); 00108 return static_cast<unsigned int>(o->r[i].max-o->r[i].min)+1; 00109 } 00110 00111 forceinline int 00112 IntSet::size(void) const { 00113 IntSetObject* o = static_cast<IntSetObject*>(object()); 00114 return (o == NULL) ? 0 : o->n; 00115 } 00116 00117 forceinline int 00118 IntSet::min(void) const { 00119 IntSetObject* o = static_cast<IntSetObject*>(object()); 00120 return (o == NULL) ? 1 : o->r[0].min; 00121 } 00122 00123 forceinline int 00124 IntSet::max(void) const { 00125 IntSetObject* o = static_cast<IntSetObject*>(object()); 00126 return (o == NULL) ? 0 : o->r[o->n-1].max; 00127 } 00128 00129 00130 /* 00131 * Range iterator for integer sets 00132 * 00133 */ 00134 00135 forceinline 00136 IntSetRanges::IntSetRanges(void) {} 00137 forceinline 00138 void 00139 IntSetRanges::init(const IntSet& s) { 00140 int n = s.size(); 00141 if (n > 0) { 00142 i = &static_cast<IntSet::IntSetObject*>(s.object())->r[0]; e = i+n; 00143 } else { 00144 i = e = NULL; 00145 } 00146 } 00147 forceinline 00148 IntSetRanges::IntSetRanges(const IntSet& s) { init(s); } 00149 00150 00151 forceinline void 00152 IntSetRanges::operator++(void) { 00153 i++; 00154 } 00155 forceinline bool 00156 IntSetRanges::operator()(void) const { 00157 return i<e; 00158 } 00159 00160 forceinline int 00161 IntSetRanges::min(void) const { 00162 return i->min; 00163 } 00164 forceinline int 00165 IntSetRanges::max(void) const { 00166 return i->max; 00167 } 00168 forceinline unsigned int 00169 IntSetRanges::width(void) const { 00170 return static_cast<unsigned int>(i->max - i->min) + 1; 00171 } 00172 00173 /* 00174 * Value iterator for integer sets 00175 * 00176 */ 00177 forceinline 00178 IntSetValues::IntSetValues(void) {} 00179 00180 forceinline 00181 IntSetValues::IntSetValues(const IntSet& s) { 00182 IntSetRanges r(s); 00183 Iter::Ranges::ToValues<IntSetRanges>::init(r); 00184 } 00185 00186 forceinline void 00187 IntSetValues::init(const IntSet& s) { 00188 IntSetRanges r(s); 00189 Iter::Ranges::ToValues<IntSetRanges>::init(r); 00190 } 00191 00192 } 00193 00194 // STATISTICS: int-var 00195