tuple-set.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Mikael Lagerkvist <lagerkvist@gecode.org> 00005 * 00006 * Copyright: 00007 * Mikael Lagerkvist, 2007 00008 * 00009 * Last modified: 00010 * $Date: 2009-09-30 13:59:39 +0200 (Wed, 30 Sep 2009) $ by $Author: tack $ 00011 * $Revision: 9784 $ 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 #include <sstream> 00039 00040 namespace Gecode { 00041 00042 forceinline bool 00043 TupleSet::TupleSetI::finalized(void) const { 00044 assert(((excess == -1) && (domsize > 0)) || 00045 ((excess != -1) && (domsize == 0))); 00046 return excess == -1; 00047 } 00048 00049 forceinline 00050 TupleSet::TupleSetI::TupleSetI(void) 00051 : arity(-1), 00052 size(0), 00053 tuples(NULL), 00054 tuple_data(NULL), 00055 data(NULL), 00056 excess(0), 00057 min(Int::Limits::max), 00058 max(Int::Limits::min), 00059 domsize(0), 00060 last(NULL), 00061 nullpointer(NULL) 00062 {} 00063 00064 00065 template<class T> 00066 void 00067 TupleSet::TupleSetI::add(T t) { 00068 assert(arity != -1); // Arity has been set 00069 assert(excess != -1); // Tuples may still be added 00070 if (excess == 0) resize(); 00071 assert(excess >= 0); 00072 --excess; 00073 int end = size*arity; 00074 for (int i = arity; i--; ) { 00075 data[end+i] = t[i]; 00076 if (t[i] < min) min = t[i]; 00077 if (t[i] > max) max = t[i]; 00078 } 00079 ++size; 00080 } 00081 00082 forceinline 00083 TupleSet::TupleSet(void) { 00084 } 00085 00086 forceinline 00087 TupleSet::TupleSet(const TupleSet& ts) 00088 : SharedHandle(ts) {} 00089 00090 forceinline TupleSet::TupleSetI* 00091 TupleSet::implementation(void) { 00092 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00093 assert(imp); 00094 return imp; 00095 } 00096 00097 inline void 00098 TupleSet::add(const IntArgs& tuple) { 00099 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00100 if (imp == NULL) { 00101 imp = new TupleSetI; 00102 object(imp); 00103 } 00104 assert(imp->arity == -1 || 00105 imp->arity == tuple.size()); 00106 imp->arity = tuple.size(); 00107 imp->add(tuple); 00108 } 00109 00110 forceinline void 00111 TupleSet::finalize(void) { 00112 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00113 assert(imp); 00114 if (!imp->finalized()) { 00115 imp->finalize(); 00116 } 00117 } 00118 00119 forceinline bool 00120 TupleSet::finalized(void) const { 00121 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00122 assert(imp); 00123 return imp->finalized(); 00124 } 00125 00126 forceinline int 00127 TupleSet::arity(void) const { 00128 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00129 assert(imp); 00130 assert(imp->arity != -1); 00131 return imp->arity; 00132 } 00133 forceinline int 00134 TupleSet::tuples(void) const { 00135 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00136 assert(imp); 00137 assert(imp->finalized()); 00138 return imp->size-1; 00139 } 00140 forceinline TupleSet::Tuple 00141 TupleSet::operator [](int i) const { 00142 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00143 assert(imp); 00144 assert(imp->finalized()); 00145 return imp->data + i*imp->arity; 00146 } 00147 forceinline int 00148 TupleSet::min(void) const { 00149 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00150 assert(imp); 00151 assert(imp->finalized()); 00152 return imp->min; 00153 } 00154 forceinline int 00155 TupleSet::max(void) const { 00156 TupleSetI* imp = static_cast<TupleSetI*>(object()); 00157 assert(imp); 00158 assert(imp->finalized()); 00159 return imp->max; 00160 } 00161 00162 00163 template<class Char, class Traits, class T> 00164 std::basic_ostream<Char,Traits>& 00165 operator <<(std::basic_ostream<Char,Traits>& os, const TupleSet& ts) { 00166 std::basic_ostringstream<Char,Traits> s; 00167 s.copyfmt(os); s.width(0); 00168 s << "Number of tuples: " << ts.tuples() << std::endl 00169 << "Tuples:" << std::endl; 00170 for (int i = 0; i < ts.tuples(); ++i) { 00171 s << '\t'; 00172 for (int j = 0; j < ts.arity(); ++j) { 00173 s.width(3); 00174 s << " " << ts[i][j]; 00175 } 00176 s << std::endl; 00177 } 00178 return os << s.str(); 00179 } 00180 00181 } 00182 00183 // STATISTICS: int-prop 00184