AndroMeta  2.0.0
AndroMeta/include/AndroMeta/MMap.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009-2012 AndroMeta LLC. All rights reserved.
00003  *
00004  * AndroMeta LLC retains all intellectual property and proprietary 
00005  * rights to this software and related documentation and any modifications 
00006  * thereto.  Any use, reproduction, disclosure, or distribution of this 
00007  * software and related documentation without an express license agreement 
00008  * from AndroMeta LLC is strictly prohibited.
00009  * 
00010  */
00011 
00019 #include <map>
00020 #include <ostream>
00021 
00022 #ifndef ANDROMETA_M_MAP_H
00023 #define ANDROMETA_M_MAP_H
00024 
00025 namespace Meta{
00026   
00036   template<class Key, class T, class Compare = std::less<Key>,
00037   class Allocator = std::allocator<std::pair<const Key,T> > >
00038   class MMap{
00039   public:
00040     typedef std::map<Key, T, Compare, Allocator> Map_;
00041     
00042     typedef typename Map_::iterator iterator;
00043     typedef typename Map_::const_iterator const_iterator;
00044     typedef typename Map_::key_type key_type;
00045     typedef typename Map_::allocator_type allocator_type;
00046     typedef typename Map_::value_type value_type;
00047     typedef typename Map_::key_compare key_compare;
00048     typedef typename Map_::reverse_iterator reverse_iterator;
00049     typedef typename Map_::const_reverse_iterator const_reverse_iterator;
00050     typedef typename Map_::value_compare value_compare;
00051     
00052     explicit MMap(const Compare& comp=Compare(),
00053                   const Allocator& allocator=Allocator())
00054     : map_(comp, allocator){
00055       
00056     }
00057     
00058     template<class InputIterator>
00059     MMap(InputIterator first,
00060          InputIterator last,
00061          const Compare& comp=Compare(),
00062          const Allocator& allocator=Allocator())
00063     : map_(first, last, comp, allocator){
00064       
00065     }
00066     
00067     MMap(const MMap<Key, T, Compare, Allocator>& x)
00068     : map_(x.map_){
00069     }
00070     
00071     virtual ~MMap(){
00072       
00073     }
00074     
00075     const Map_ map() const{
00076       return map_;
00077     }
00078     
00079     Map_ map(){
00080       return map_;
00081     }
00082     
00083     iterator begin(){
00084       return map_.begin();
00085     }
00086     
00087     const_iterator begin() const{
00088       return map_.begin();
00089     }
00090     
00091     void clear(){
00092       map_.clear();
00093     }
00094     
00095     size_t count(const key_type& x) const{
00096       return map_.count(x);
00097     }
00098     
00099     bool empty() const{
00100       return map_.empty();
00101     }
00102     
00103     bool isEmpty() const{
00104       return map_.isEmpty();
00105     }
00106     
00107     iterator end(){
00108       return map_.end();
00109     }
00110     
00111     const_iterator end() const{
00112       return map_.end();
00113     }
00114     
00115     std::pair<iterator,iterator>
00116     equal_range(const key_type& x){
00117       return map_.equal_range(x);
00118     }
00119     
00120     std::pair<const_iterator,const_iterator>
00121     equal_range(const key_type& x) const{
00122       return map_.equal_range(x);
00123     }
00124     
00125     std::pair<iterator,iterator>
00126     equalRange(const key_type& x){
00127       return map_.equal_range(x);
00128     }
00129     
00130     std::pair<const_iterator,const_iterator>
00131     equalRange(const key_type& x) const{
00132       return map_.equal_range(x);
00133     }
00134     
00135     void erase(iterator position){
00136       map_.erase(position);
00137     }
00138     
00139     size_t erase(const key_type& x){
00140       return map_.erase(x);
00141     }
00142     
00143     void erase(iterator first, iterator last){
00144       map_.erase(first, last);
00145     }
00146     
00147     iterator find(const key_type& x){
00148       return map_.find(x);
00149     }
00150     
00151     bool hasKey(const key_type& x) const{
00152       return map_.find(x) != map_.end();
00153     }
00154     
00155     const_iterator find(const key_type& x) const{
00156       return map_.find(x);
00157     }
00158     
00159     allocator_type get_allocator() const{
00160       return map_.get_allocator();
00161     }
00162     
00163     allocator_type getAllocator() const{
00164       return map_.get_allocator();
00165     }
00166     
00167     std::pair<iterator, bool> insert(const value_type& x){
00168       return map_.insert(x);
00169     }
00170     
00171     iterator insert(iterator position, const value_type& x){
00172       return map_.insert(position, x);
00173     }
00174     
00175     void merge(const MMap<Key,T,Compare,Allocator>& m){
00176       map_.insert(m.begin(), m.end());
00177     }
00178     
00179     void outerMerge(const MMap<Key,T,Compare,Allocator>& m){
00180       typename MMap<Key,T,Compare,Allocator>::const_iterator i = m.begin();
00181       while(i != m.end()){
00182         typename MMap<Key,T,Compare,Allocator>::iterator j = map_.find(i->first);
00183         if(j != map_.end()){
00184           j->second = i->second;
00185         }
00186         else{
00187           map_.insert(std::pair<Key,T>(i->first, i->second));
00188         }
00189         ++i;
00190       }
00191     }
00192     
00193     template<class InputIterator>
00194     void insert(InputIterator first, InputIterator last){
00195       map_.insert(first, last);
00196     }
00197     
00198     MMap<Key,T,Compare,Allocator>&
00199     add(const Key& k, const T& t){
00200       map_.insert(std::make_pair(k, t));
00201       return *this;
00202     }
00203     
00204     key_compare key_comp() const{
00205       return map_.key_comp();
00206     }
00207     
00208     key_compare keyCompare() const{
00209       return map_.key_comp();
00210     }
00211     
00212     iterator lower_bound(const key_type& x){
00213       return map_.lower_bound(x);
00214     }
00215     
00216     const_iterator lower_bound(const key_type& x) const{
00217       return map_.lower_bound(x);
00218     }
00219     
00220     iterator lowerBound(const key_type& x){
00221       return map_.lower_bound(x);
00222     }
00223     
00224     const_iterator lowerBound(const key_type& x) const{
00225       return map_.lower_bound(x);
00226     }
00227     
00228     size_t max_size() const{
00229       return map_.max_size();
00230     }
00231     
00232     size_t maxSize() const{
00233       return map_.maxSize();
00234     }
00235     
00236     MMap<Key,T,Compare,Allocator>&
00237     operator=(const MMap<Key,T,Compare,Allocator>& x){
00238       map_ = x.map_;
00239       return *this;
00240     }
00241     
00242     T& operator[](const key_type& x){
00243       return map_[x];
00244     }
00245     
00246     reverse_iterator rbegin(){
00247       return map_.rbegin();
00248     }
00249     
00250     const_reverse_iterator rbegin() const{
00251       return map_.rbegin();
00252     }
00253     
00254     reverse_iterator rend(){
00255       return map_.rend();
00256     }
00257     
00258     const_reverse_iterator rend() const{
00259       return map_.rend();
00260     }
00261     
00262     size_t size() const{
00263       return map_.size();
00264     }
00265     
00266     void swap(MMap<Key,T,Compare,Allocator>& mp){
00267       map_.swap(mp);
00268     }
00269     
00270     iterator upper_bound(const key_type& x){
00271       return map_.upper_bound(x);
00272     }
00273     
00274     const_iterator upper_bound(const key_type& x) const{
00275       return map_.upper_bound(x);
00276     }
00277     
00278     iterator upperBound(const key_type& x){
00279       return map_.upper_bound(x);
00280     }
00281     
00282     const_iterator upperBound(const key_type& x) const{
00283       return map_.upper_bound(x);
00284     }
00285     
00286     value_compare value_comp() const{
00287       return map_.value_comp();
00288     }
00289     
00290     value_compare valueCompare() const{
00291       return map_.value_comp();
00292     }
00293     
00294   private:
00295     Map_ map_;
00296   };
00297   
00298   template<class Key, class T, class Compare, class Allocator>
00299   bool operator==(const MMap<Key,T,Compare,Allocator>& x,
00300                   const MMap<Key,T,Compare,Allocator>& y){
00301     return x.map() == y.map();
00302   }
00303   
00304   template<class Key, class T, class Compare, class Allocator>
00305   bool operator!=(const MMap<Key,T,Compare,Allocator>& x,
00306                   const MMap<Key,T,Compare,Allocator>& y){
00307     return x.map() != y.map();
00308   }
00309   
00310   template<class Key, class T, class Compare, class Allocator>
00311   bool operator<(const MMap<Key,T,Compare,Allocator>& x,
00312                  const MMap<Key,T,Compare,Allocator>& y){
00313     return x.map() < y.map();
00314   }
00315   
00316   template<class Key, class T, class Compare, class Allocator>
00317   bool operator>(const MMap<Key,T,Compare,Allocator>& x,
00318                  const MMap<Key,T,Compare,Allocator>& y){
00319     return x.map() > y.map();
00320   }
00321   
00322   template<class Key, class T, class Compare, class Allocator>
00323   bool operator<=(const MMap<Key,T,Compare,Allocator>& x,
00324                   const MMap<Key,T,Compare,Allocator>& y){
00325     return x.map() <= y.map();
00326   }
00327   
00328   template<class Key, class T, class Compare, class Allocator>
00329   bool operator>=(const MMap<Key,T,Compare,Allocator>& x,
00330                   const MMap<Key,T,Compare,Allocator>& y){
00331     return x.map() >= y.map();
00332   }
00333   
00334   template<typename K, typename T>
00335   std::ostream& operator<<(std::ostream& ostr, const MMap<K,T>& m){
00336     typename MMap<K,T>::const_iterator itr = m.begin();
00337     ostr << "[";
00338     while(itr != m.end()){
00339       if(itr != m.begin()){
00340         ostr << ", ";
00341       }
00342       ostr << itr->first << ":";
00343       ostr << itr->second;
00344       ++itr;
00345     }
00346     ostr << "]";
00347     return ostr;
00348   }
00349   
00350 } // end namespace Meta
00351 
00352 #endif // ANDROMETA_M_MAP_H