Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

pfed.h

Go to the documentation of this file.
00001 // -*- Mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; -*-
00002 ///////////////////////////////////////////////////////////////////////////////
00003 //
00004 // This file is a part of the UPPAAL toolkit.
00005 // Copyright (c) 2006, Uppsala University and Aalborg University.
00006 // All right reserved.
00007 //
00008 ///////////////////////////////////////////////////////////////////////////////
00009 
00010 #ifndef DBM_PFED_H
00011 #define DBM_PFED_H
00012 
00013 #include <stdexcept>
00014 #include "dbm/Valuation.h"
00015 #include "dbm/priced.h"
00016 #include "base/slist.h"
00017 
00018 namespace dbm
00019 {
00020     class pdbm_t;
00021 
00022     /**
00023      * A priced federation is a list of priced DBMs.
00024      * 
00025      * Priced federations implement copy on write using reference
00026      * counting.
00027      */
00028     class pfed_t
00029     {
00030     public:
00031         typedef std::slist<PDBM>::const_iterator const_iterator;
00032 
00033     protected:
00034         typedef std::slist<PDBM>::iterator iterator;
00035 
00036         struct pfed_s
00037         {
00038             std::slist<PDBM> zones;
00039             uint32_t count;
00040             cindex_t dim;
00041         };
00042 
00043         static std::allocator<pfed_s> alloc;
00044 
00045         /** Pointer to record holding the federation. */
00046         struct pfed_s *ptr;
00047 
00048         /** Increment reference count. */
00049         void incRef();
00050 
00051         /** Decrement reference count. */
00052         void decRef();
00053 
00054         /** Prepare federation for modification. */
00055         void prepare();
00056 
00057         /** Returns a mutable iterator to the beginning of the federation. */
00058         iterator beginMutable();
00059 
00060         /** Returns a mutable iterator to the end of the federation. */
00061         iterator endMutable();
00062 
00063         /**
00064          * Erases a DBM from the federation and returns an itertor to the
00065          * successor element.
00066          */
00067         iterator erase(iterator);
00068 
00069         /**
00070          * Adds \a pdbm to the federation. The reference count on pdbm
00071          * is incremented by one.
00072          */
00073         void add(const PDBM pdbm, size_t dim);    
00074     public:
00075 
00076         /** Allocate empty priced federation of dimension 0. */
00077         pfed_t();
00078 
00079         /** Allocate empty priced federation of dimension \a dim. */
00080         explicit pfed_t(cindex_t dim);
00081 
00082         /**
00083          * Allocate a priced federation of dimension \a dim initialised to
00084          * \a pdbm.
00085          */
00086         pfed_t(const PDBM pdbm, cindex_t dim);
00087 
00088         /** The copy constructor implements copy on write. */
00089         pfed_t(const pfed_t &);
00090 
00091         /**
00092          * The destructor decrements the reference count and deallocates
00093          * the priced DBM when the count reaches zero.
00094          */
00095         ~pfed_t();
00096 
00097         /**
00098          * Constrain x(i) to value. 
00099          *
00100          * @return Returns true if non-empty after operation.
00101          */
00102         bool constrain(cindex_t i, uint32_t value);
00103 
00104         /**
00105          * Constrain federation to valuations satisfying x(i) - x(j) ~
00106          * constraint.
00107          *
00108          * @return Returns true if non-empty after operation.
00109          */
00110         bool constrain(cindex_t i, cindex_t j, raw_t constraint);
00111 
00112         /**
00113          * Constrain federation to valuations satisfying x(i) - x(j) ~
00114          * (b, isStrict).
00115          *
00116          * @return Returns true if non-empty after operation.
00117          */
00118         bool constrain(cindex_t i, cindex_t j, int32_t b, bool isStrict);
00119 
00120         /**
00121          * Constrain federation to valuations satisfying the
00122          * constraints.
00123          *
00124          * @param constraints Array of length \a n of constraints.
00125          * @param n           length of \a constraints.
00126          * @pre               n > 0
00127          * @return Returns true if non-empty after operation.
00128          */
00129         bool constrain(const constraint_t *constraints, size_t n);
00130 
00131         /**
00132          * Constrain federation to valuations satisfying \a c.
00133          *
00134          * @return Returns true if non-empty after operation.
00135          */
00136         bool constrain(const constraint_t& c);
00137 
00138         /** Returns the infimum of the federation. */
00139         int32_t getInfimum() const;
00140 
00141         /**
00142          * Check if the federation satisfies a given constraint.        
00143          *
00144          * @return Returns true if the federation satisfies \a c.
00145          */
00146         bool satisfies(const constraint_t& c) const;
00147 
00148         /**
00149          * Check if the federation satisfies a given constraint.        
00150          *
00151          * @return Returns true if the federation satisfies x(i) -
00152          * x(j) ~ constraint.
00153          */
00154         bool satisfies(cindex_t i, cindex_t j, raw_t constraint) const;
00155 
00156         /** Returns true iff the federation is empty. */
00157         bool isEmpty() const;
00158 
00159         
00160         bool isUnbounded() const;
00161 
00162         /** Contains a hash of the federation. */
00163         uint32_t hash(uint32_t seed) const;
00164 
00165         /** Returns true iff the federation contains \v. */
00166         bool contains(const DoubleValuation &v) const;
00167 
00168         /** Returns true iff the federation contains \v. */
00169         bool contains(const IntValuation &v) const;
00170 
00171         /** Delay with the current delay rate. */
00172         void up();
00173 
00174         /** Delay with rate \a rate. */
00175         void up(int32_t rate);
00176 
00177         /** Set x(clock) to \a value. */
00178         void updateValue(cindex_t clock, uint32_t value);
00179 
00180         /** 
00181          * Set x(clock) to \a value. x(zero) must be on a zero-cycle with
00182          * x(clock).
00183          */
00184         void updateValueZero(cindex_t clock, int32_t value, cindex_t zero);
00185 
00186         void extrapolateMaxBounds(int32_t *max);
00187         void diagonalExtrapolateMaxBounds(int32_t *max);
00188         void diagonalExtrapolateLUBounds(int32_t *lower, int32_t *upper);
00189 
00190         void incrementCost(int32_t value);
00191     
00192         int32_t getCostOfValuation(const IntValuation &valuation) const;
00193         void relax();
00194 
00195         void freeClock(cindex_t clock);
00196 
00197         /**
00198          * Resets the federation to the federation containing only the
00199          * origin, with a cost of 0.
00200          */
00201         void setZero();
00202 
00203         /**
00204          * Resets the federation to the federation containing all
00205          * valuations, with a cost of 0.
00206          */
00207         void setInit();
00208 
00209         /**
00210          * Resets the federation to the empty federation.
00211          */
00212         void setEmpty();
00213 
00214         /**
00215          * Returns an iterator to the first zone of the federation.
00216          */
00217         const_iterator begin() const;
00218 
00219         /**
00220          * Returns an iterator to the position after the last zone of
00221          * the federation.
00222          */
00223         const_iterator end() const;
00224 
00225         bool operator == (const pfed_t &) const;
00226 
00227         /** Assignment operator. */
00228         pfed_t &operator = (const pfed_t &);
00229 
00230         /** Assignment operator. */
00231         pfed_t& operator = (const PDBM);
00232 
00233         /** Union operator. */
00234         pfed_t &operator |= (const pfed_t &);
00235 
00236         /** Union operator. */
00237         pfed_t &operator |= (const PDBM);
00238 
00239         /** Not implemented. */
00240         pfed_t &operator -= (const pfed_t &);
00241 
00242         /** Not implemented. */
00243         pfed_t &operator += (const pfed_t &);
00244 
00245         /** Not implemented. */
00246         pfed_t &operator &= (const pfed_t &);
00247 
00248         /** Not implemented. */
00249         bool intersects(const pfed_t &) const;
00250 
00251         /** Not implemented. */
00252         pfed_t operator & (const pfed_t& b) const;
00253 
00254         /** Not implemented. */
00255         pfed_t operator - (const pfed_t& b) const;
00256 
00257         /** Not implemented. */
00258         void down();
00259 
00260         /** Not implemented. */
00261         int32_t getUpperMinimumCost(cindex_t) const;
00262 
00263         /** Not implemented. */
00264         void relaxUp();
00265 
00266         /** Not implemented. */
00267         void getValuation(DoubleValuation& cval, bool *freeC = NULL) const;
00268 
00269         /** Not implemented. */
00270         void swapClocks(cindex_t, cindex_t);
00271 
00272         /** Not implemented. */        
00273         void splitExtrapolate(const constraint_t *, const constraint_t *,
00274                               const int32_t *);
00275 
00276         /**
00277          * Relation between two priced federations: SUBSET is returned
00278          * if all zones of this federation are contained in some zone
00279          * of fed. SUPERSET is returned if all zones of \a fed are
00280          * contained in some zone of this federation. EQUAL is
00281          * returned if both conditions are true. DIFFERENT is returned
00282          * if none of them are true.
00283          *
00284          * Notice that this implementation of set inclusion is not
00285          * complete in the sense that DIFFERENT could be returned even
00286          * though the two federations are comparable.
00287          */
00288         relation_t relation(const pfed_t &fed) const;
00289 
00290         /**
00291          * Returns the infimum of the federation given a partial
00292          * valuation.
00293          */
00294         int32_t getInfimumValuation(IntValuation &valuation, const bool *free = NULL) const;
00295 
00296         /** Returns the number of zones in the federation. */
00297         size_t size() const;
00298 
00299         /** Returns the dimension of the federation. */
00300         size_t getDimension() const;
00301 
00302         /**
00303          * Implementation of the free up operation for priced
00304          * federations.
00305          *
00306          * @see pdbm_freeUp
00307          */
00308         void freeUp(cindex_t);
00309 
00310         /**
00311          * Implementation of the free down operation for priced
00312          * federations.
00313          *
00314          * @see pdbm_freeDown
00315          */
00316         void freeDown(cindex_t);
00317 
00318         template <typename Predicate>
00319         void erase_if(Predicate p);
00320 
00321         template <typename Predicate>
00322         void erase_if_not(Predicate p);
00323         
00324         // The following operate on the first zone only.
00325 
00326         /**
00327          * Returns the bound on x(i) - x(j) of the first zone of the
00328          * federation.
00329          *
00330          * @pre size() == 1
00331          */
00332         raw_t operator () (cindex_t i, cindex_t j) const;
00333 
00334         /**
00335          *
00336          * @pre size() == 1
00337          */
00338         int32_t getRate(cindex_t) const;
00339 
00340         /**
00341          *
00342          * @pre size() == 1
00343          */
00344         size_t analyzeForMinDBM(uint32_t *bitMatrix) const;
00345 
00346         /**
00347          *
00348          * @pre size() == 1
00349          */
00350         int32_t getOffsetCost() const;
00351 
00352         /**
00353          *
00354          * @pre size() == 1
00355          */
00356         int32_t getSlopeOfDelayTrajectory() const;
00357 
00358         void setOffsetCost(int32_t);
00359         void setRate(cindex_t, int32_t);
00360 
00361         /** @see dbm_writeToMinDBMWithOffset() */
00362         int32_t *writeToMinDBMWithOffset(bool minimizeGraph,
00363                                          bool tryConstraints16,
00364                                          allocator_t c_alloc,
00365                                          uint32_t offset) const;
00366 
00367 
00368         static size_t getSizeOfMinDBM(size_t dimension, const int32_t *);
00369 
00370         static pfed_t readFromMinDBM(size_t dimension, const int32_t *);
00371 
00372         /** @see pdbm_relationWithMinDBM() */
00373         relation_t relation(const int32_t *, raw_t *) const;
00374         
00375         /**
00376          * Returns true and a clock in \a result that is on a zero
00377          * cycle with \a clock or false if no such clock exists.
00378          */
00379         bool isOnZeroCycle(cindex_t clock, cindex_t &result) const;
00380 
00381         const pfed_t &const_dbmt() const;
00382     };
00383 
00384     template <typename Predicate>
00385     inline void pfed_t::erase_if(Predicate p)
00386     {
00387         iterator first = beginMutable();
00388         iterator last = endMutable();
00389         while (first != last)
00390         {
00391             if (p(*first))
00392             {
00393                 first = erase(first);
00394             }
00395             else
00396             {
00397                 ++first;
00398             }
00399         }
00400     }
00401 
00402     template <typename Predicate>
00403     void pfed_t::erase_if_not(Predicate p) 
00404     {
00405         iterator first = beginMutable();
00406         iterator last = endMutable();
00407         while (first != last)
00408         {
00409             if (p(*first))
00410             {
00411                 ++first;
00412             }
00413             else
00414             {
00415                 first = erase(first);
00416             }
00417         }
00418     }
00419 
00420 
00421     inline pfed_t::pfed_t(const pfed_t &pfed) : ptr(pfed.ptr)
00422     {
00423         incRef();
00424     }
00425 
00426     inline pfed_t::~pfed_t()
00427     {
00428         assert(ptr->count > 0);
00429         decRef();
00430     }
00431 
00432     inline pfed_t::iterator pfed_t::beginMutable()
00433     {
00434         assert(ptr->count <= 1);
00435         return ptr->zones.begin();
00436     }
00437 
00438     inline pfed_t::iterator pfed_t::endMutable()
00439     {
00440         assert(ptr->count <= 1);
00441         return ptr->zones.end();
00442     }
00443 
00444     inline pfed_t::const_iterator pfed_t::begin() const
00445     {
00446         return ptr->zones.begin();
00447     }
00448  
00449     inline pfed_t::const_iterator pfed_t::end() const
00450     {
00451         return ptr->zones.end();
00452     }
00453 
00454     inline size_t pfed_t::getDimension() const
00455     {
00456         return ptr->dim;
00457     }
00458 
00459     inline size_t pfed_t::size() const
00460     {
00461         return ptr->zones.size();
00462     }
00463 
00464     inline void pfed_t::incRef()
00465     {
00466         ptr->count++;
00467     }
00468 
00469 
00470     inline bool pfed_t::isEmpty() const
00471     {
00472         return ptr->zones.empty();
00473     }
00474 
00475     inline const pfed_t &pfed_t::const_dbmt() const 
00476     {
00477         assert(size() == 1);
00478         return *this;
00479     }
00480 
00481     inline pfed_t& pfed_t::operator = (const PDBM pdbm)
00482     {
00483         *this = pfed_t(pdbm, ptr->dim);
00484         return *this;
00485     }
00486 
00487     inline bool pfed_t::constrain(cindex_t i, cindex_t j, int32_t b, bool isStrict)
00488     {
00489         return constrain(i, j, dbm_boundbool2raw(b, isStrict));
00490     }
00491 
00492     inline bool pfed_t::constrain(const constraint_t& c)
00493     {
00494         return constrain(c.i, c.j, c.value);
00495     }
00496 
00497     inline bool pfed_t::satisfies(const constraint_t& c) const 
00498     {
00499         return satisfies(c.i, c.j, c.value);
00500     }
00501 }
00502 
00503 #endif

Generated on Fri Jun 30 00:02:45 2006 for Module dbm by  doxygen 1.4.2