00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00035 #ifndef OW_SORTED_VECTOR_SET_HPP_
00036 #define OW_SORTED_VECTOR_SET_HPP_
00037 #include "OW_config.h"
00038 #include "OW_COWReference.hpp"
00039 #include "OW_vector.hpp"
00040 #include <utility> 
00041 #include <functional> 
00042 #include <algorithm>
00043 
00044 namespace OW_NAMESPACE
00045 {
00046 
00047 template<class T, class Compare >
00048 class SortedVectorSet;
00049 
00050 template<class T, class Compare>
00051 inline bool operator==(const SortedVectorSet<T, Compare>& x,
00052    const SortedVectorSet<T, Compare>& y);
00053 
00054 template<class T, class Compare>
00055 inline bool operator<(const SortedVectorSet<T, Compare>& x,
00056    const SortedVectorSet<T, Compare>& y);
00057 
00058 template<class T, class Compare = std::less<T> >
00059 class SortedVectorSet
00060 {
00061    typedef std::vector<T> container_t;
00062 #ifdef OW_WIN32
00063 #pragma warning (push)
00064 #pragma warning (disable: 4251)
00065 #endif
00066    COWReference<container_t> m_impl;
00067 #ifdef OW_WIN32
00068 #pragma warning (pop)
00069 #endif
00070 public:
00071    typedef          T key_type;
00072    typedef          T data_type;
00073    typedef          T value_type;
00074    typedef          Compare key_compare;
00075    typedef          Compare value_compare;
00076    typedef typename container_t::pointer pointer;
00077    typedef typename container_t::const_pointer const_pointer;
00078    typedef typename container_t::reference reference;
00079    typedef typename container_t::const_reference const_reference;
00080    typedef typename container_t::iterator iterator;
00081    typedef typename container_t::const_iterator const_iterator;
00082    typedef typename container_t::reverse_iterator reverse_iterator;
00083    typedef typename container_t::const_reverse_iterator const_reverse_iterator;
00084    typedef typename container_t::size_type size_type;
00085    typedef typename container_t::difference_type difference_type;
00086    SortedVectorSet() : m_impl(new container_t) {  }
00087    explicit SortedVectorSet(container_t* toWrap) : m_impl(toWrap)
00088       { }
00089    template <class InputIterator>
00090    SortedVectorSet(InputIterator first, InputIterator last) :
00091       m_impl(new container_t(first, last))
00092    {
00093       std::sort(m_impl->begin(), m_impl->end(), Compare());
00094    }
00095    const_iterator begin() const { return m_impl->begin(); }
00096    const_iterator end() const { return m_impl->end(); }
00097    const_reverse_iterator rbegin() const { return m_impl->rbegin(); }
00098    const_reverse_iterator rend() const { return m_impl->rend(); }
00099    bool empty() const { return m_impl->empty(); }
00100    size_type size() const { return m_impl->size(); }
00101    size_type max_size() const { return m_impl->max_size(); }
00102    void swap(SortedVectorSet<T, Compare>& x)
00103    {
00104       m_impl.swap(x.m_impl);
00105    }
00106    std::pair<iterator, bool> insert(const value_type& x)
00107    {
00108       iterator i = std::lower_bound(m_impl->begin(), m_impl->end(), x, Compare());
00109       if (i != m_impl->end() && equivalent(*i, x))
00110       {
00111          return std::pair<iterator, bool>(i, false);
00112       }
00113       else
00114       {
00115          return std::pair<iterator, bool>(m_impl->insert(i, x), true);
00116       }
00117    }
00118    iterator insert(iterator, const value_type& x)
00119    {
00120       iterator i = std::lower_bound(m_impl->begin(), m_impl->end(), x, Compare());
00121       
00122       return m_impl->insert(i, x);
00123    }
00124    template <class InputIterator>
00125    void insert(InputIterator first, InputIterator last)
00126    {
00127       for (; first != last; ++first)
00128       {
00129          m_impl->push_back(*first);
00130       }
00131       std::sort(m_impl->begin(), m_impl->end(), Compare());
00132    }
00133    void erase(iterator position)
00134    {
00135       m_impl->erase(position);
00136    }
00137    size_type erase(const key_type& x)
00138    {
00139       iterator i = std::lower_bound(m_impl->begin(), m_impl->end(), x, Compare());
00140       if (i != m_impl->end() && equivalent(*i, x))
00141       {
00142          m_impl->erase(i);
00143          return 1;
00144       }
00145       else
00146       {
00147          return 0;
00148       }
00149    }
00150    void erase(iterator first, iterator last)
00151    {
00152       m_impl->erase(first, last);
00153    }
00154    void clear()
00155    {
00156       m_impl->clear();
00157    }
00158    iterator find(const key_type& x)
00159    {
00160       iterator pos = std::lower_bound(m_impl->begin(), m_impl->end(), x, Compare());
00161       if (pos != m_impl->end() && equivalent(*pos, x)) 
00162       {
00163          return pos;
00164       }
00165       else
00166       {
00167          return m_impl->end();
00168       }
00169    }
00170    const_iterator find(const key_type& x) const
00171    {
00172       const_iterator pos = std::lower_bound(m_impl->begin(), m_impl->end(), x, Compare());
00173       if (pos != m_impl->end() && equivalent(*pos, x)) 
00174       {
00175          return pos;
00176       }
00177       else
00178       {
00179          return m_impl->end();
00180       }
00181    }
00182    size_type count(const key_type& x) const
00183    {
00184       if (std::binary_search(m_impl->begin(), m_impl->end(), x, Compare()))
00185       {
00186          return 1;
00187       }
00188       else
00189       {
00190          return 0;
00191       }
00192    }
00193    iterator lower_bound(const key_type& x)
00194    {
00195       return std::lower_bound(m_impl->begin(), m_impl->end(), x, Compare());
00196    }
00197    const_iterator lower_bound(const key_type& x) const
00198    {
00199       return std::lower_bound(m_impl->begin(), m_impl->end(), x, Compare());
00200    }
00201    iterator upper_bound(const key_type& x)
00202    {
00203       return std::upper_bound(m_impl->begin(), m_impl->end(), x, Compare());
00204    }
00205    const_iterator upper_bound(const key_type& x) const
00206    {
00207       return std::upper_bound(m_impl->begin(), m_impl->end(), x, Compare());
00208    }
00209 
00210    std::pair<iterator, iterator>
00211       equal_range(const key_type& x)
00212    {
00213       return std::equal_range(m_impl->begin(), m_impl->end(), x, Compare());
00214    }
00215 
00216    std::pair<const_iterator, const_iterator>
00217       equal_range(const key_type& x) const
00218    {
00219       return std::equal_range(m_impl->begin(), m_impl->end(), x, Compare());
00220    }
00221 
00222    friend bool operator== <>(const SortedVectorSet<T, Compare>& x,
00223       const SortedVectorSet<T, Compare>& y);
00224    friend bool operator< <>(const SortedVectorSet<T, Compare>& x,
00225       const SortedVectorSet<T, Compare>& y);
00226 
00227 private:
00228    bool equivalent(const key_type& x, const key_type& y) const
00229    {
00230       
00231       return (!Compare()(x, y) && !Compare()(y, x)); 
00232    }
00233 };
00234 template<class T, class Compare>
00235 inline bool operator==(const SortedVectorSet<T, Compare>& x,
00236    const SortedVectorSet<T, Compare>& y)
00237 {
00238    return *x.m_impl == *y.m_impl;
00239 }
00240 template<class T, class Compare>
00241 inline bool operator<(const SortedVectorSet<T, Compare>& x,
00242    const SortedVectorSet<T, Compare>& y)
00243 {
00244    return *x.m_impl < *y.m_impl;
00245 }
00246 template <class T, class Compare>
00247 inline void swap(SortedVectorSet<T, Compare>& x,
00248    SortedVectorSet<T, Compare>& y)
00249 {
00250    x.swap(y);
00251 }
00252 
00253 } 
00254 
00255 #endif