src/Utility/vmap.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /***************************************************************************
00003  *
00004  * The IPPL Framework
00005  * 
00006  *
00007  * Visit http://people.web.psi.ch/adelmann/ for more details
00008  *
00009  ***************************************************************************/
00010 
00011 #ifndef VMAP_H
00012 #define VMAP_H
00013 
00015 /*
00016 
00017   class vmap
00018 
00019   A class with the same interface as a map but implemented 
00020   with a sorted vector.
00021 
00022   This then has less overhead than a map for access but higher overhead 
00023   for insert and delete operations.  It is appropriate for cases where
00024   the contents of the vmap are read much more than they are written.
00025 
00026   Searching for a tag takes log time because a binary search is used.
00027 
00028   Insert and delete take linear time when inserting in the middle, but 
00029   constant time for inserting at the end.  The efficient way to fill 
00030   the map is to:
00031   1. Call reserve(size_type) with the number of elements you will be adding.
00032   2. Add elements in increasing order with end() as the hint.
00033   This will fill the vmap in linear time.
00034 
00035 */
00037 
00038 // include files
00039 #ifdef IPPL_STDSTL
00040 #include <vector>
00041 // Standard STL has pair class in Utility header
00042 #include <utility>
00043 // Standard STL calls this header "functional"
00044 #include <functional>
00045 using namespace std;
00046 #else
00047 #include <vector.h>
00048 #include <pair.h>
00049 #include <function.h>
00050 #endif // IPPL_STDSTL
00051 
00052 
00053 
00055 
00056 template<class Key>
00057 class dummy_less
00058 {
00059 public:
00060 #ifdef IPPL_PURIFY
00061   // Add explicit default/copy constructors and op= to avoid UMR's.
00062   dummy_less() {}
00063   dummy_less(const dummy_less<Key> &) {}
00064   dummy_less<Key>& operator=(const dummy_less<Key> &) { return *this; }
00065 #endif
00066 
00067   bool operator()(const Key l, const Key r) const
00068   {
00069     return l < r;
00070   }
00071 };
00072 
00074 
00075 template<class Key, class T, class Compare = dummy_less<Key> >
00076 class vmap
00077 {
00078 public:
00079   // typedefs:
00080 
00081   typedef Key key_type;
00082   typedef pair<Key, T> value_type;
00083   typedef Compare key_compare;
00084     
00085   class value_compare : public 
00086 #ifdef IPPL_STDSTL
00087 std::
00088 #endif
00089 binary_function<value_type, value_type, bool>
00090   {
00091   private:
00092     Compare comp;
00093   public:
00094     value_compare(const Compare &c) : comp(c) {}
00095 #ifdef IPPL_PURIFY
00096     value_compare() {}
00097     value_compare(const value_compare &model) : comp(model.comp) {}
00098     value_compare& operator=(const value_compare &rhs)
00099     {
00100       comp = rhs.comp;
00101       return *this;
00102     }
00103 #endif
00104     bool operator()(const value_type& x, const value_type& y) const
00105     {
00106       return comp(x.first, y.first);
00107     }
00108   };
00109 
00110 private:
00111   typedef vector< value_type > rep_type;
00112 
00113   // Here is the actual storage.
00114   rep_type V_c;
00115 
00116   // The comparator object and some convenient permutations.
00117   Compare Lt;
00118   bool Gt(const key_type& a, const key_type& b) const { return Lt(b,a); }
00119   bool Ge(const key_type& a, const key_type& b) const { return !Lt(a,b); }
00120   bool Le(const key_type& a, const key_type& b) const { return !Lt(b,a); }
00121 
00122 public: 
00123 
00124   // More typedefs.
00125 
00126   //  typedef typename rep_type::pointer pointer;
00127   typedef typename rep_type::reference reference;
00128   typedef typename rep_type::const_reference const_reference;
00129   typedef typename rep_type::iterator iterator;
00130   typedef typename rep_type::const_iterator const_iterator;
00131   typedef typename rep_type::reverse_iterator reverse_iterator;
00132   typedef typename rep_type::const_reverse_iterator const_reverse_iterator;
00133   typedef typename rep_type::size_type size_type;
00134   typedef typename rep_type::difference_type difference_type;
00135 
00136   // accessors:
00137 
00138   iterator         begin()  { return V_c.begin(); }
00139   iterator         end()    { return V_c.end(); }
00140   reverse_iterator rbegin() { return V_c.rbegin(); }
00141   reverse_iterator rend()   { return V_c.rend(); }
00142 
00143   const_iterator         begin()  const { return V_c.begin(); }
00144   const_iterator         end()    const { return V_c.end(); }
00145   const_reverse_iterator rbegin() const { return V_c.rbegin(); }
00146   const_reverse_iterator rend()   const { return V_c.rend(); }
00147 
00148   key_compare   key_comp()   const { return Lt; }
00149   value_compare value_comp() const { return value_compare(Lt); }
00150   bool          empty()      const { return V_c.empty(); }
00151   size_type     size()       const { return V_c.size(); }
00152   size_type     max_size()   const { return V_c.max_size(); }
00153   size_type     capacity()   const { return V_c.capacity(); }
00154 
00155   void swap(vmap<Key, T, Compare>& x) { V_c.swap(x.V_c); }
00156   void reserve( size_type n ) { V_c.reserve(n); }
00157 
00158   // allocation/deallocation/assignment
00159 
00160   vmap(const Compare& comp = Compare()) : Lt(comp) {}
00161   vmap(const vmap<Key, T, Compare>& x);
00162   vmap<Key, T, Compare>& operator=(const vmap<Key, T, Compare>& x);
00163 
00164   // insert functions.
00165 
00166   pair<iterator,bool> insert(const value_type& x);
00167   iterator            insert(iterator hint_i, const value_type& x); 
00168   void                insert(const value_type* first, const value_type* last);
00169 
00170   // erase functions.
00171 
00172   void      erase(iterator position_i);
00173   void      erase(iterator first_i, iterator last_i);
00174   size_type erase(const key_type& x);
00175 
00176   // map operations:
00177 
00178   T&        operator[](const key_type& k);
00179   iterator  find(const key_type& x);      
00180   iterator  lower_bound(const key_type& x);
00181   iterator  upper_bound(const key_type& x);
00182   pair<iterator,iterator> equal_range(const key_type& x);
00183 
00184   // const map operations.
00185 
00186   size_type      count(const key_type& x) const;
00187   const T&       operator[](const key_type& k) const;
00188   const_iterator find(const key_type& x) const;
00189   const_iterator lower_bound(const key_type& x) const;
00190   const_iterator upper_bound(const key_type& x) const;
00191   pair<const_iterator,const_iterator> equal_range(const key_type& x) const;
00192 
00193 };
00194 
00195 template <class Key, class T, class Compare>
00196 inline bool operator==(const vmap<Key, T, Compare>& x, 
00197                        const vmap<Key, T, Compare>& y) {
00198     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
00199 }
00200 
00201 template <class Key, class T, class Compare>
00202 inline bool operator<(const vmap<Key, T, Compare>& x, 
00203                       const vmap<Key, T, Compare>& y) {
00204     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
00205 }
00206 
00207 
00209 
00210 #include "Utility/vmap.cpp"
00211 
00212 #endif // VMAP_H
00213 
00214 /***************************************************************************
00215  * $RCSfile: vmap.h,v $   $Author: adelmann $
00216  * $Revision: 1.1.1.1 $   $Date: 2003/01/23 07:40:34 $
00217  * IPPL_VERSION_ID: $Id: vmap.h,v 1.1.1.1 2003/01/23 07:40:34 adelmann Exp $ 
00218  ***************************************************************************/

Generated on Mon Jan 16 13:23:59 2006 for IPPL by  doxygen 1.4.6