/* * Main authors: * Guido Tack * * Copyright: * Guido Tack, 2006 * * Last modified: * $Date: 2006-08-17 16:58:48 +0200 (Thu, 17 Aug 2006) $ by $Author: tack $ * $Revision: 3548 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ namespace Gecode { /* * Operations for set expression code * */ forceinline SetExprCode::SetExprCode(void) : c(5), n(0) {} forceinline SetExprCode::SetExprCode(const SetExprCode& sc) : c(sc.c), n(sc.n) {} forceinline void SetExprCode::update(bool share, SetExprCode& sc) { n=sc.n; c.update(share, sc.c); } forceinline void SetExprCode::add(int i) { c.ensure(n+1); c[n] = i; n++; } forceinline int SetExprCode::size(void) const { return n; } forceinline int SetExprCode::operator[](int i) const { return c[i]; } /* * Operations for expressions * */ forceinline SetExpr::SetExpr(void) : ax(NULL), sign(1) {} forceinline SetExpr operator-(const SetExpr& s) { return SetExpr(s, -1); } forceinline SetExpr operator||(const SetExpr& s, const SetExpr& t) { return SetExpr(s, 1, SetExpr::REL_UNION, t, 1); } forceinline SetExpr operator&&(const SetExpr& s, const SetExpr& t) { return SetExpr(s, 1, SetExpr::REL_INTER, t, 1); } forceinline SetExpr operator-(const SetExpr& s, const SetExpr& t) { return SetExpr(s, 1, SetExpr::REL_INTER, t, -1); } /** * \brief Range iterator for set expressions */ class SetExprRanges { private: /// Reference-counted range iterators with virtual member functions class Iter; /// The actual iterator Iter *i; const SetExprRanges& operator=(const SetExprRanges&); public: /// \name Constructors and destructor //@{ /// Copy constructor SetExprRanges(const SetExprRanges&); /** Initialize with the views \a x, a set expression \a s, * and a flag whether \a s is to be interpreted in a monotone or * anti-monotone way */ SetExprRanges(const ViewArray& x, SetExpr& s, bool monotone); /** Initialize with the views \a x, a set expression code \a c, * and a flag whether \a c is to be interpreted in a monotone or * anti-monotone way */ SetExprRanges(const ViewArray& x, const SetExprCode& s, bool monotone); /// Destructor ~SetExprRanges(void); //@} /// \name Iteration control //@{ /// Move iterator to next range (if possible) bool operator()(void); /// Test whether iterator is still at a range or done void operator++(void); //@} /// \name Range access //@{ /// Return smallest value of range int min(void) const; /// Return largest value of range int max(void) const; /// Return width of range (distance between minimum and maximum) unsigned int width(void) const; //@} }; /** * \brief Reference-counted range iterators with virtual member functions * */ class SetExprRanges::Iter { public: /// The actual iterator Gecode::Iter::Ranges::Virt::Iterator* i; /// Reference count int num; /// Construct from iterator Iter(Gecode::Iter::Ranges::Virt::Iterator*); /// Destructor ~Iter(void); /// Increment reference count void increment(void); /// Decrement reference count bool decrement(void); }; forceinline SetExprRanges::Iter::Iter(Gecode::Iter::Ranges::Virt::Iterator* i0) : i(i0), num(1) {} forceinline SetExprRanges::Iter::~Iter(void) { delete i; } forceinline void SetExprRanges::Iter::increment(void) { ++num; } forceinline bool SetExprRanges::Iter::decrement(void) { return --num==0; } forceinline SetExprRanges::SetExprRanges(const SetExprRanges& s) : i(s.i) { i->increment(); } forceinline SetExprRanges::~SetExprRanges(void) { if (i->decrement()) delete i; } forceinline bool SetExprRanges::operator()(void) { return (*(i->i))(); } forceinline void SetExprRanges::operator++(void) { ++(*(i->i)); } forceinline int SetExprRanges::min(void) const { return i->i->min(); } forceinline int SetExprRanges::max(void) const { return i->i->max(); } forceinline unsigned int SetExprRanges::width(void) const { return i->i->width(); } } // STATISTICS: set-prop