Map.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00038 #ifndef BLOCXX_MAP_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_MAP_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/COWReference.hpp"
00042 #include <map>
00043 #include <functional>
00044 
00045 // This class is a wrapper around std::map<> and adds COW capabilities.
00046 namespace BLOCXX_NAMESPACE
00047 {
00048 
00049 
00050 // forward declarations are necessary for template friends.
00051 template<class Key, class T, class Compare > class Map;
00052 
00053 template<class Key, class T, class Compare>
00054 inline bool operator==(const Map<Key, T, Compare>& x,
00055    const Map<Key, T, Compare>& y);
00056 
00057 template<class Key, class T, class Compare>
00058 inline bool operator<(const Map<Key, T, Compare>& x,
00059    const Map<Key, T, Compare>& y);
00060 
00061 
00062 template<class Key, class T, class Compare = std::less<Key> > class Map
00063 {
00064    typedef std::map<Key, T, Compare > M;
00065    COWReference<M> m_impl;
00066 public:
00067    typedef typename M::key_type key_type;
00068    typedef typename M::mapped_type mapped_type;
00069    typedef typename M::value_type value_type;
00070    typedef typename M::key_compare key_compare;
00071    typedef typename M::value_compare value_compare;
00072    typedef typename M::pointer pointer;
00073    typedef typename M::const_pointer const_pointer;
00074    typedef typename M::reference reference;
00075    typedef typename M::const_reference const_reference;
00076    typedef typename M::iterator iterator;
00077    typedef typename M::const_iterator const_iterator;
00078    typedef typename M::reverse_iterator reverse_iterator;
00079    typedef typename M::const_reverse_iterator const_reverse_iterator;
00080    typedef typename M::size_type size_type;
00081    typedef typename M::difference_type difference_type;
00082    Map() : m_impl(new M) {  }
00083    explicit Map(M* toWrap) : m_impl(toWrap)
00084       { }
00085    explicit Map(const Compare& comp)
00086       : m_impl(new M(comp)) {  }
00087    template <class InputIterator>
00088    Map(InputIterator first, InputIterator last) :
00089       m_impl(new M(first, last))
00090    {
00091    }
00092    template <class InputIterator>
00093    Map(InputIterator first, InputIterator last, const Compare& comp) :
00094       m_impl(new M(first, last, comp))
00095    {
00096    }
00097    M* getImpl()
00098    {
00099       return &*m_impl;
00100    }
00101    key_compare key_comp() const
00102    {
00103       return m_impl->key_comp();
00104    }
00105    value_compare value_comp() const
00106    {
00107       return m_impl->value_comp();
00108    }
00109    iterator begin()
00110    {
00111       return m_impl->begin();
00112    }
00113    const_iterator begin() const
00114    {
00115       return m_impl->begin();
00116    }
00117    iterator end()
00118    {
00119       return m_impl->end();
00120    }
00121    const_iterator end() const
00122    {
00123       return m_impl->end();
00124    }
00125    reverse_iterator rbegin()
00126    {
00127       return m_impl->rbegin();
00128    }
00129    const_reverse_iterator rbegin() const
00130    {
00131       return m_impl->rbegin();
00132    }
00133    reverse_iterator rend()
00134    {
00135       return m_impl->rend();
00136    }
00137    const_reverse_iterator rend() const
00138    {
00139       return m_impl->rend();
00140    }
00141    bool empty() const
00142    {
00143       return m_impl->empty();
00144    }
00145    size_type size() const
00146    {
00147       return m_impl->size();
00148    }
00149    size_type max_size() const
00150    {
00151       return m_impl->max_size();
00152    }
00153    T& operator[](const key_type& k)
00154    {
00155       return m_impl->operator[](k);
00156    }
00157    void swap(Map<Key, T, Compare>& x)
00158    {
00159       m_impl.swap(x.m_impl);
00160    }
00161    std::pair<iterator, bool> insert(const value_type& x)
00162    {
00163       return m_impl->insert(x);
00164    }
00165    iterator insert(iterator position, const value_type& x)
00166    {
00167       return m_impl->insert(position, x);
00168    }
00169    template <class InputIterator>
00170    void insert(InputIterator first, InputIterator last)
00171    {
00172       m_impl->insert(first, last);
00173    }
00174    void erase(iterator position)
00175    {
00176       m_impl->erase(position);
00177    }
00178    size_type erase(const key_type& x)
00179    {
00180       return m_impl->erase(x);
00181    }
00182    void erase(iterator first, iterator last)
00183    {
00184       m_impl->erase(first, last);
00185    }
00186    void clear()
00187    {
00188       m_impl->clear();
00189    }
00190    iterator find(const key_type& x)
00191    {
00192       return m_impl->find(x);
00193    }
00194    const_iterator find(const key_type& x) const
00195    {
00196       return m_impl->find(x);
00197    }
00198    size_type count(const key_type& x) const
00199    {
00200       return m_impl->count(x);
00201    }
00202    iterator lower_bound(const key_type& x)
00203    {
00204       return m_impl->lower_bound(x);
00205    }
00206    const_iterator lower_bound(const key_type& x) const
00207    {
00208       return m_impl->lower_bound(x);
00209    }
00210    iterator upper_bound(const key_type& x)
00211    {
00212       return m_impl->upper_bound(x);
00213    }
00214    const_iterator upper_bound(const key_type& x) const
00215    {
00216       return m_impl->upper_bound(x);
00217    }
00218    std::pair<iterator, iterator> equal_range(const key_type& x)
00219    {
00220       return m_impl->equal_range(x);
00221    }
00222    std::pair<const_iterator, const_iterator>
00223       equal_range(const key_type& x) const
00224    {
00225       return m_impl->equal_range(x);
00226    }
00227    friend bool operator== <>(const Map<Key, T, Compare>& x,
00228       const Map<Key, T, Compare>& y);
00229    friend bool operator< <>(const Map<Key, T, Compare>& x,
00230       const Map<Key, T, Compare>& y);
00231 };
00232 template <class Key, class T, class Compare>
00233 std::map<Key, T, Compare>* COWReferenceClone(std::map<Key, T, Compare>* obj)
00234 {
00235    return new std::map<Key, T, Compare>(*obj);
00236 }
00237 template<class Key, class T, class Compare>
00238 inline bool operator==(const Map<Key, T, Compare>& x,
00239    const Map<Key, T, Compare>& y)
00240 {
00241    return *x.m_impl == *y.m_impl;
00242 }
00243 template<class Key, class T, class Compare>
00244 inline bool operator<(const Map<Key, T, Compare>& x,
00245    const Map<Key, T, Compare>& y)
00246 {
00247    return *x.m_impl < *y.m_impl;
00248 }
00249 template <class Key, class T, class Compare>
00250 inline void swap(Map<Key, T, Compare>& x,
00251    Map<Key, T, Compare>& y)
00252 {
00253    x.swap(y);
00254 }
00255 
00256 } // end namespace BLOCXX_NAMESPACE
00257 
00258 #endif

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6