Generated on Mon Jul 6 18:09:09 2009 for Gecode by doxygen 1.5.9

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-01-20 23:44:27 +0100 (Tue, 20 Jan 2009) $ by $Author: schulte $
00011  *     $Revision: 8082 $
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 
00046   class GECODE_VTABLE_EXPORT TupleSet::TupleSetI
00047     : public SharedHandle::Object {
00048   public:
00050     int arity;
00052     int size;
00054     Tuple** tuples;
00056     Tuple* tuple_data;
00058     int* data;
00060     int excess;
00062     int min, max;
00064     unsigned int domsize;
00066     Tuple** last;
00068     Tuple* nullptr;
00069 
00071     template <class T>
00072     void add(T t);
00074     GECODE_INT_EXPORT void finalize(void);
00076     GECODE_INT_EXPORT void resize(void);
00078     bool finalized(void) const;
00080     TupleSetI(void);
00082     GECODE_INT_EXPORT virtual ~TupleSetI(void);
00084     GECODE_INT_EXPORT virtual SharedHandle::Object* copy(void) const;
00085   };
00086 
00087   forceinline bool
00088   TupleSet::TupleSetI::finalized(void) const {
00089     assert(((excess == -1) && (domsize > 0)) ||
00090            ((excess != -1) && (domsize == 0)));
00091     return excess == -1;
00092   }
00093 
00094   forceinline
00095   TupleSet::TupleSetI::TupleSetI(void)
00096     : arity(-1),
00097       size(0),
00098       tuples(NULL),
00099       tuple_data(NULL),
00100       data(NULL),
00101       excess(0),
00102       min(Int::Limits::max),
00103       max(Int::Limits::min),
00104       domsize(0),
00105       last(NULL),
00106       nullptr(NULL)
00107   {}
00108 
00109 
00110   template <class T>
00111   void
00112   TupleSet::TupleSetI::add(T t) {
00113     assert(arity  != -1); // Arity has been set
00114     assert(excess != -1); // Tuples may still be added
00115     if (excess == 0) resize();
00116     assert(excess >= 0);
00117     --excess;
00118     int end = size*arity;
00119     for (int i = arity; i--; ) {
00120       data[end+i] = t[i];
00121       if (t[i] < min) min = t[i];
00122       if (t[i] > max) max = t[i];
00123     }
00124     ++size;
00125   }
00126 
00127   forceinline
00128   TupleSet::TupleSet(void) {
00129   }
00130 
00131   forceinline
00132   TupleSet::TupleSet(const TupleSet& ts)
00133     : SharedHandle(ts) {}
00134 
00135   forceinline TupleSet::TupleSetI*
00136   TupleSet::implementation(void) {
00137     TupleSetI* imp = static_cast<TupleSetI*>(object());
00138     assert(imp);
00139     return imp;
00140   }
00141 
00142   inline void
00143   TupleSet::add(const IntArgs& tuple) {
00144     TupleSetI* imp = static_cast<TupleSetI*>(object());
00145     if (imp == NULL) {
00146       imp = new TupleSetI;
00147       object(imp);
00148     }
00149     assert(imp->arity == -1 ||
00150            imp->arity == tuple.size());
00151     imp->arity = tuple.size();
00152     imp->add(tuple);
00153   }
00154 
00155   forceinline void
00156   TupleSet::finalize(void) {
00157     TupleSetI* imp = static_cast<TupleSetI*>(object());
00158     assert(imp);
00159     if (!imp->finalized()) {
00160       imp->finalize();
00161     }
00162   }
00163 
00164   forceinline bool
00165   TupleSet::finalized(void) const {
00166     TupleSetI* imp = static_cast<TupleSetI*>(object());
00167     assert(imp);
00168     return imp->finalized();
00169   }
00170 
00171   forceinline int
00172   TupleSet::arity(void) const {
00173     TupleSetI* imp = static_cast<TupleSetI*>(object());
00174     assert(imp);
00175     assert(imp->arity != -1);
00176     return imp->arity;
00177   }
00178   forceinline int
00179   TupleSet::tuples(void) const {
00180     TupleSetI* imp = static_cast<TupleSetI*>(object());
00181     assert(imp);
00182     assert(imp->finalized());
00183     return imp->size-1;
00184   }
00185   forceinline TupleSet::Tuple
00186   TupleSet::operator [](int i) const {
00187     TupleSetI* imp = static_cast<TupleSetI*>(object());
00188     assert(imp);
00189     assert(imp->finalized());
00190     return imp->data + i*imp->arity;
00191   }
00192   forceinline int
00193   TupleSet::min(void) const {
00194     TupleSetI* imp = static_cast<TupleSetI*>(object());
00195     assert(imp);
00196     assert(imp->finalized());
00197     return imp->min;
00198   }
00199   forceinline int
00200   TupleSet::max(void) const {
00201     TupleSetI* imp = static_cast<TupleSetI*>(object());
00202     assert(imp);
00203     assert(imp->finalized());
00204     return imp->max;
00205   }
00206 
00207 
00208   template<class Char, class Traits, class T>
00209   std::basic_ostream<Char,Traits>&
00210   operator <<(std::basic_ostream<Char,Traits>& os, const TupleSet& ts) {
00211     std::basic_ostringstream<Char,Traits> s;
00212     s.copyfmt(os); s.width(0);
00213     s << "Number of tuples: " << ts.tuples() << std::endl
00214       << "Tuples:" << std::endl;
00215     for (int i = 0; i < ts.tuples(); ++i) {
00216       s << '\t';
00217       for (int j = 0; j < ts.arity(); ++j) {
00218         s.width(3);
00219         s << " " << ts[i][j];
00220       }
00221       s << std::endl;
00222     }
00223     return os << s.str();
00224   }
00225 
00226 }
00227 
00228 // STATISTICS: int-prop
00229