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-02-19 16:09:54 +0100 (Tue, 19 Feb 2008) $ by $Author: tack $ 00011 * $Revision: 6236 $ 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 template <> 00064 forceinline 00065 IntSet::IntSet(const IntSet& s) 00066 : SharedHandle(s) {} 00067 00068 template <> 00069 forceinline 00070 IntSet::IntSet(IntSet& s) 00071 : SharedHandle(s) {} 00072 00073 forceinline 00074 IntSet::IntSet(const int r[][2], int n) { 00075 init(r,n); 00076 } 00077 00078 forceinline 00079 IntSet::IntSet(const int r[], int n) { 00080 init(r,n); 00081 } 00082 00083 forceinline 00084 IntSet::IntSet(int n, int m) { 00085 init(n,m); 00086 } 00087 00088 forceinline int 00089 IntSet::min(int i) const { 00090 assert(object() != NULL); 00091 return static_cast<IntSetObject*>(object())->r[i].min; 00092 } 00093 00094 forceinline int 00095 IntSet::max(int i) const { 00096 assert(object() != NULL); 00097 return static_cast<IntSetObject*>(object())->r[i].max; 00098 } 00099 00100 forceinline unsigned int 00101 IntSet::width(int i) const { 00102 assert(object() != NULL); 00103 IntSetObject* o = static_cast<IntSetObject*>(object()); 00104 return static_cast<unsigned int>(o->r[i].max-o->r[i].min)+1; 00105 } 00106 00107 forceinline int 00108 IntSet::size(void) const { 00109 IntSetObject* o = static_cast<IntSetObject*>(object()); 00110 return (o == NULL) ? 0 : o->n; 00111 } 00112 00113 forceinline int 00114 IntSet::min(void) const { 00115 IntSetObject* o = static_cast<IntSetObject*>(object()); 00116 return (o == NULL) ? 1 : o->r[0].min; 00117 } 00118 00119 forceinline int 00120 IntSet::max(void) const { 00121 IntSetObject* o = static_cast<IntSetObject*>(object()); 00122 return (o == NULL) ? 0 : o->r[o->n-1].max; 00123 } 00124 00125 00126 /* 00127 * Range iterator for integer sets 00128 * 00129 */ 00130 00131 forceinline 00132 IntSetRanges::IntSetRanges(void) {} 00133 forceinline 00134 void 00135 IntSetRanges::init(const IntSet& s) { 00136 int n = s.size(); 00137 if (n > 0) { 00138 i = &static_cast<IntSet::IntSetObject*>(s.object())->r[0]; e = i+n; 00139 } else { 00140 i = e = NULL; 00141 } 00142 } 00143 forceinline 00144 IntSetRanges::IntSetRanges(const IntSet& s) { init(s); } 00145 00146 00147 forceinline void 00148 IntSetRanges::operator++(void) { 00149 i++; 00150 } 00151 forceinline bool 00152 IntSetRanges::operator()(void) const { 00153 return i<e; 00154 } 00155 00156 forceinline int 00157 IntSetRanges::min(void) const { 00158 return i->min; 00159 } 00160 forceinline int 00161 IntSetRanges::max(void) const { 00162 return i->max; 00163 } 00164 forceinline unsigned int 00165 IntSetRanges::width(void) const { 00166 return static_cast<unsigned int>(i->max - i->min) + 1; 00167 } 00168 00169 /* 00170 * Value iterator for integer sets 00171 * 00172 */ 00173 forceinline 00174 IntSetValues::IntSetValues(void) {} 00175 00176 forceinline 00177 IntSetValues::IntSetValues(const IntSet& s) { 00178 IntSetRanges r(s); 00179 Iter::Ranges::ToValues<IntSetRanges>::init(r); 00180 } 00181 00182 forceinline void 00183 IntSetValues::init(const IntSet& s) { 00184 IntSetRanges r(s); 00185 Iter::Ranges::ToValues<IntSetRanges>::init(r); 00186 } 00187 00188 } 00189 00190 // STATISTICS: int-var 00191