expr_map.h

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /*!
00003  * \file expr_map.h
00004  * 
00005  * Author: Sergey Berezin
00006  * 
00007  * Created: Dec 11 01:22:49 GMT 2002
00008  *
00009  * <hr>
00010  *
00011  * License to use, copy, modify, sell and/or distribute this software
00012  * and its documentation for any purpose is hereby granted without
00013  * royalty, subject to the terms and conditions defined in the \ref
00014  * LICENSE file provided with this distribution.
00015  * 
00016  * <hr>
00017  * 
00018  */
00019 /*****************************************************************************/
00020 // CLASS: ExprMap<Data>
00021 //
00022 // AUTHOR: Sergey Berezin, 12/10/2002
00023 //
00024 // Abstract:
00025 //
00026 // An abstract interface mapping Expr values to Data.  The
00027 // implementation is a hash table.
00028 //
00029 // Subclassing is NOT allowed; this would lose the iterators (can we
00030 // fix it?  Maybe not worth the trouble.)
00031 //
00032 // Functions follow the style of STL 'map' container.
00033 // 
00034 // ExprMap<Data>()    [Default constructor] Creates an empty map
00035 // int count(Expr e)  Counts the number of elements mapped from e.
00036 //                    Normally, returns 0 or 1.
00037 // Data& operator[](e)  Returns Data associated with e.  If e is not mapped,
00038 //                    insert new Data() into ExprMap.
00039 //                    Can be used to populate ExprMap as follows:
00040 //          ExprMap map;
00041 //          map[e1] = data1; map[e2] = data2; ...
00042 //          Caveat: Data must have a default constructor and be assignable.
00043 // void erase(Expr e)  Erase e->data mapping from ExprMap.
00044 // void insert(Expr e, Data d)  Insert e->d mapping.
00045 // iterator begin()   Return simple "input" iterators for ExprMap
00046 // iterator end()     (as defined in STL)
00047 // size_t size()      Return size of the map
00048 // bool empty()       Check for emptiness     
00049 ///////////////////////////////////////////////////////////////////////////////
00050 #ifndef _cvc3__expr_h_
00051 #include "expr.h"
00052 #endif
00053 
00054 #ifndef _cvc3__expr_map_h_
00055 #define _cvc3__expr_map_h_
00056 
00057 #include "expr_hash.h"
00058 
00059 namespace CVC3 {
00060 
00061   template<class Data>
00062   class ExprMap {
00063   private:
00064 
00065     typedef std::map<Expr, Data> ExprMapType;
00066     // Private members
00067     ExprMapType d_map;
00068 
00069   public:
00070 
00071     //////////////////////////////////////////////////////////////////////////
00072     // Class: ExprMap::iterator
00073     // Author: Sergey Berezin
00074     // Created: Tue Dec 10 16:25:19 2002
00075     // Description: 
00076     //////////////////////////////////////////////////////////////////////////
00077 
00078     class iterator: public std::iterator<std::input_iterator_tag, std::pair<Expr,Data>,std::ptrdiff_t> {
00079       friend class ExprMap;
00080     private:
00081       typename ExprMapType::const_iterator d_it;
00082       // Private constructor
00083       iterator(const typename ExprMapType::const_iterator& it)
00084   : d_it(it) { }
00085     public:
00086       // Default constructor
00087       iterator() { }
00088       // (Dis)equality
00089       bool operator==(const iterator& i) const { return d_it == i.d_it; }
00090       bool operator!=(const iterator& i) const { return d_it != i.d_it; }
00091       // Dereference operators.
00092       const std::pair<const Expr,Data>& operator*() const { return *d_it; }
00093       const std::pair<const Expr,Data>* operator->() const {
00094   return d_it.operator->();
00095       }
00096       // Prefix increment
00097       iterator& operator++() { ++d_it; return *this; }
00098       // Postfix increment: requires a Proxy object to hold the
00099       // intermediate value for dereferencing
00100       class Proxy {
00101   const std::pair<const Expr,Data>& d_pair;
00102       public:
00103   Proxy(const std::pair<Expr,Data>& pair) : d_pair(pair) { }
00104   std::pair<const Expr,Data> operator*() { return d_pair; }
00105       }; // end of class Proxy
00106       // Actual postfix increment: returns Proxy with the old value.
00107       // Now, an expression like *i++ will return the current *i, and
00108       // then advance the iterator.  However, don't try to use Proxy for
00109       // anything else.
00110       Proxy operator++(int) {
00111   Proxy tmp(*(*this));
00112   ++(*this);
00113   return tmp;
00114       }
00115     }; // end of class iterator
00116 
00117     //////////////////////////////////////////////////////////////////////////
00118     // Public methods
00119     //////////////////////////////////////////////////////////////////////////
00120 
00121     // Default constructor
00122     ExprMap() { }
00123     // Copy constructor
00124     ExprMap(const ExprMap& map): d_map(map.d_map) { }
00125 
00126     // Other methods
00127     bool empty() const { return d_map.empty(); }
00128     size_t size() const { return d_map.size(); }
00129 
00130     size_t count(const Expr& e) const { return d_map.count(e); }
00131     Data& operator[](const Expr& e) { return d_map[e]; }
00132     void clear() { d_map.clear(); }
00133 
00134     void insert(const Expr& e, const Data& d) { d_map[e] = d; }
00135     void erase(const Expr& e) { d_map.erase(e); }
00136 
00137     template<class InputIterator>
00138       void insert(InputIterator l, InputIterator r) { d_map.insert(l,r); }
00139 
00140     template<class InputIterator>
00141       void erase(InputIterator l, InputIterator r) {
00142       for(; l!=r; ++l) {
00143   d_map.erase((*l).first);
00144       }
00145     }
00146 
00147     iterator begin() const { return iterator(d_map.begin()); }
00148     iterator end() const { return iterator(d_map.end()); }
00149     iterator find(const Expr& e) const { return iterator(d_map.find(e)); }
00150 
00151     friend bool operator==(const ExprMap& m1, const ExprMap& m2) {
00152       return m1.d_map == m2.d_map;
00153     }
00154     friend bool operator!=(const ExprMap& m1, const ExprMap& m2) {
00155       return !(m1 == m2);
00156     }
00157   }; // end of class ExprMap
00158 
00159   template<class Data>
00160   class ExprHashMap {
00161   private:
00162 
00163     typedef std::hash_map<Expr, Data> ExprHashMapType;
00164     // Private members
00165     ExprHashMapType d_map;
00166 
00167   public:
00168 
00169     //////////////////////////////////////////////////////////////////////////
00170     // Class: ExprHashMap::iterator
00171     // Author: Sergey Berezin
00172     // Created: Tue Dec 10 16:25:19 2002
00173     // Description: 
00174     //////////////////////////////////////////////////////////////////////////
00175 
00176     class iterator: public std::iterator<std::input_iterator_tag, std::pair<Expr,Data>,std::ptrdiff_t> {
00177       friend class ExprHashMap;
00178     private:
00179       typename ExprHashMapType::const_iterator d_it;
00180       // Private constructor
00181       iterator(const typename ExprHashMapType::const_iterator& it)
00182   : d_it(it) { }
00183     public:
00184       // Default constructor
00185       iterator() { }
00186       // (Dis)equality
00187       bool operator==(const iterator& i) const { return d_it == i.d_it; }
00188       bool operator!=(const iterator& i) const { return d_it != i.d_it; }
00189       // Dereference operators.
00190       const std::pair<const Expr,Data>& operator*() const { return *d_it; }
00191       const std::pair<const Expr,Data>* operator->() const {
00192   return d_it.operator->();
00193       }
00194       // Prefix increment
00195       iterator& operator++() { ++d_it; return *this; }
00196       // Postfix increment: requires a Proxy object to hold the
00197       // intermediate value for dereferencing
00198       class Proxy {
00199   const std::pair<const Expr,Data>& d_pair;
00200       public:
00201   Proxy(const std::pair<Expr,Data>& pair) : d_pair(pair) { }
00202   std::pair<const Expr,Data> operator*() { return d_pair; }
00203       }; // end of class Proxy
00204       // Actual postfix increment: returns Proxy with the old value.
00205       // Now, an expression like *i++ will return the current *i, and
00206       // then advance the iterator.  However, don't try to use Proxy for
00207       // anything else.
00208       Proxy operator++(int) {
00209   Proxy tmp(*(*this));
00210   ++(*this);
00211   return tmp;
00212       }
00213     }; // end of class iterator
00214 
00215     //////////////////////////////////////////////////////////////////////////
00216     // Public methods
00217     //////////////////////////////////////////////////////////////////////////
00218 
00219     //! Default constructor
00220     ExprHashMap() { }
00221     //! Constructor specifying the initial number of buckets
00222     ExprHashMap(size_t n): d_map(n) { }
00223     // Copy constructor
00224     ExprHashMap(const ExprHashMap& map): d_map(map.d_map) { }
00225 
00226     // Other methods
00227     bool empty() const { return d_map.empty(); }
00228     size_t size() const { return d_map.size(); }
00229 
00230     size_t count(const Expr& e) const { return d_map.count(e); }
00231     Data& operator[](const Expr& e) { return d_map[e]; }
00232     void clear() { d_map.clear(); }
00233 
00234     void insert(const Expr& e, const Data& d) { d_map[e] = d; }
00235     void erase(const Expr& e) { d_map.erase(e); }
00236 
00237     template<class InputIterator>
00238       void insert(InputIterator l, InputIterator r) { d_map.insert(l,r); }
00239 
00240     template<class InputIterator>
00241       void erase(InputIterator l, InputIterator r) {
00242       for(; l!=r; ++l) {
00243   d_map.erase((*l).first);
00244       }
00245     }
00246 
00247     iterator begin() const { return iterator(d_map.begin()); }
00248     iterator end() const { return iterator(d_map.end()); }
00249     iterator find(const Expr& e) const { return iterator(d_map.find(e)); }
00250 
00251     friend bool operator==(const ExprHashMap& m1, const ExprHashMap& m2) {
00252       return m1.d_map == m2.d_map;
00253     }
00254     friend bool operator!=(const ExprHashMap& m1, const ExprHashMap& m2) {
00255       return !(m1 == m2);
00256     }
00257   }; // end of class ExprHashMap
00258 
00259 } // end of namespace CVC3
00260 
00261 #endif

Generated on Tue Jul 3 14:33:37 2007 for CVC3 by  doxygen 1.5.1