set_vector.h

00001 #ifndef __UTIL_SET_VECTOR_H
00002 #define __UTIL_SET_VECTOR_H
00003 
00004 #include <vector>
00005 #include <functional>
00006 #include <algorithm>
00007 
00008 namespace util
00009 {
00010 
00011   template <typename T, typename Equal>
00012   struct __set_vector_FindEqual
00013   {
00014     __set_vector_FindEqual(const T& t)
00015       : value(t)
00016       {}
00017 
00018     bool operator()(const T& other) const
00019     {
00020       return equal(value, other);
00021     }
00022 
00023     const T& value;
00024     static const Equal equal;
00025   };
00026 
00027   template <typename T, typename Equal>
00028     const Equal __set_vector_FindEqual<T,Equal>::equal = Equal();
00029 
00030 
00031 
00035   template <typename T, typename Equal = std::equal_to<T>, typename Alloc = std::allocator<T> >
00036     struct set_vector : public std::vector<T,Alloc>
00037     {
00038       typedef std::vector<T,Alloc> base;
00039       typedef typename base::value_type key_type;
00040       typedef typename base::value_type value_type;
00041       typedef typename base::reference reference;
00042       typedef typename base::const_reference const_reference;
00043       typedef typename base::difference_type difference_type;
00044       typedef typename base::size_type size_type;
00045       typedef typename base::allocator_type allocator_type;
00046       typedef typename base::pointer pointer;
00047       typedef typename base::const_pointer const_pointer;
00048 
00049       typedef Equal key_equal;
00050       typedef typename base::iterator iterator;
00051       typedef typename base::const_iterator const_iterator;
00052 
00053       typedef typename base::reverse_iterator reverse_iterator;
00054       typedef typename base::const_reverse_iterator const_reverse_iterator;
00055 
00056       set_vector() : base() {}
00057 
00058       set_vector(const set_vector& copy) : base(copy) {}
00059 
00060       set_vector(const base& copy) : base(copy) {}
00061 
00062 #ifdef USE_CXX0X
00063       set_vector(set_vector&&) = default;
00064       set_vector& operator=(set_vector&&) = default;
00065 
00066       set_vector(base&& copy)
00067         : base(std::move(copy))
00068       { }
00069 
00070       set_vector& operator=(base&& other)
00071       {
00072         base::operator=(std::move(other));
00073         return *this;
00074       }
00075 #endif
00076 
00077       std::pair<iterator,bool> insert(const T& v, bool test_presence = true)
00078       {
00079         if(test_presence)
00080         {
00081           iterator it_found = this->find(v);
00082           if(it_found != this->end())
00083             return std::make_pair(it_found, false);
00084         }
00085         this->push_back(v);
00086         iterator e = this->end();
00087         --e;
00088         return std::make_pair(e, true);
00089       }
00090 
00091       int erase(const T& v)
00092       {
00093         iterator it_found = this->find(v);
00094         if(it_found != this->end())
00095         {
00096           this->erase(it_found);
00097           return true;
00098         }
00099         return false;
00100       }
00101 
00102       iterator erase(iterator it)
00103       {
00104         return base::erase(it);
00105       }
00106 
00107       int count(const T& v) const
00108       {
00109         if(this->find(v) != this->end())
00110           return 1;
00111         return 0;
00112       }
00113 
00114       iterator find(const T& v)
00115       {
00116         return std::find_if(this->begin(), this->end(), __set_vector_FindEqual<value_type,key_equal>(v));
00117       }
00118 
00119       const_iterator find(const T& v) const
00120       {
00121         return std::find_if(this->begin(), this->end(), __set_vector_FindEqual<value_type,key_equal>(v));
00122       }
00123 
00124       set_vector& operator=(const set_vector& other)
00125       {
00126         base::operator=(other);
00127         return *this;
00128       }
00129     };
00130 
00131 }
00132 
00133 #endif // __UTIL_SET_VECTOR_H
00134 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Fri May 31 15:37:53 2013 for VVE by  doxygen 1.6.3