00001 #ifndef ML_CONTAINER_SORTED_VECTOR_HPP
00002 #define ML_CONTAINER_SORTED_VECTOR_HPP
00003
00004 #include <functional>
00005 #include <algorithm>
00006 #include <vector>
00007
00008 namespace ml_container
00009 {
00023 template <typename T, typename Compare = std::less<T>, typename Alloc = std::allocator<T> >
00024 class sorted_vector
00025 {
00026 typedef typename std::vector<T, Alloc> container_type;
00027
00028 std::vector<T, Alloc> container;
00029 Compare cmp;
00030
00031 public:
00032
00033 typedef typename container_type::value_type value_type;
00034 typedef value_type key_type;
00035 typedef Compare key_compare;
00036 typedef key_compare value_compare;
00037 typedef typename container_type::pointer pointer;
00038 typedef typename container_type::reference reference;
00039 typedef typename container_type::const_reference const_reference;
00040 typedef typename container_type::size_type size_type;
00041 typedef typename container_type::difference_type difference_type;
00042 typedef typename container_type::iterator iterator;
00043 typedef typename container_type::const_iterator const_iterator;
00044 typedef typename container_type::reverse_iterator reverse_iterator;
00045 typedef typename container_type::const_reverse_iterator const_reverse_iterator;
00046
00047 iterator begin()
00048 { return container.begin(); }
00049
00050 const_iterator begin() const
00051 { return container.begin(); }
00052
00053 iterator end()
00054 { return container.end(); }
00055
00056 const_iterator end() const
00057 { return container.end(); }
00058
00059 reverse_iterator rbegin()
00060 { return container.rbegin(); }
00061
00062 const_reverse_iterator rbegin() const
00063 { return container.rbegin(); }
00064
00065 reverse_iterator rend()
00066 { return container.rend(); }
00067
00068 const_reverse_iterator rend() const
00069 { return container.rend(); }
00070
00071 size_type size() const
00072 { return container.size(); }
00073
00074 size_type max_size() const
00075 { return container.max_size(); }
00076
00077 bool empty() const
00078 { return container.empty(); }
00079
00080 key_compare key_comp() const
00081 { return cmp; }
00082
00083 value_compare value_comp() const
00084 { return key_comp(); }
00085
00086 explicit
00087 sorted_vector(const Compare& c = Compare())
00088 : container(), cmp(c)
00089 { }
00090
00091 template <class InputIterator>
00092 sorted_vector(InputIterator first, InputIterator last, const Compare& c = Compare())
00093 : container(first, last), cmp(c)
00094 { std::sort(begin(), end(), cmp); }
00095
00096 sorted_vector(const sorted_vector& other)
00097 : container(other.container), cmp(other.cmp)
00098 { }
00099
00100 sorted_vector& operator=(const sorted_vector& other)
00101 {
00102 if (this == &other) return *this;
00103 container = other.container;
00104 cmp = other.cmp;
00105 return *this;
00106 }
00107
00108 void swap(sorted_vector& other)
00109 { container.swap(other); }
00110
00111 std::pair<iterator, bool> insert(const value_type& x)
00112 {
00113 iterator i = lower_bound(x);
00114 return (i == end() || cmp(x, *i)) ?
00115 std::make_pair(container.insert(i, x), true) :
00116 std::make_pair(i, false);
00117 }
00118
00119 iterator insert(iterator pos, const value_type& x)
00120 { return insert(x)->first; }
00121
00122 template <class InputIterator>
00123 void insert(InputIterator first, InputIterator last)
00124 {
00125 container.insert(first, last);
00126 std::sort(container.begin(), container.end(), cmp);
00127 }
00128
00129 void erase(iterator first, iterator last)
00130 { container.erase(first, last); }
00131
00132 void clear()
00133 { container.clear(); }
00134
00135 iterator find(const key_type& k)
00136 {
00137 iterator i = lower_bound(k);
00138 return (i == end() || cmp(k, *i)) ? end() : i;
00139 }
00140
00141 const_iterator find(const key_type& k) const
00142 {
00143 const_iterator i = lower_bound(k);
00144 return (i == end() || cmp(k, *i)) ? end() : i;
00145 }
00146
00147 size_type count(const key_type& k) const
00148 {
00149 std::pair<const_iterator, const_iterator> res = equal_range(k);
00150 return res.second - res.first;
00151 }
00152
00153 iterator lower_bound(const key_type& k)
00154 { return std::lower_bound(container.begin(), container.end(), k, cmp); }
00155
00156 const_iterator lower_bound(const key_type& k) const
00157 { return std::lower_bound(container.begin(), container.end(), k, cmp); }
00158
00159 iterator upper_bound(const key_type& k)
00160 { return std::upper_bound(container.begin(), container.end(), k, cmp); }
00161
00162 const_iterator upper_bound(const key_type& k) const
00163 { return std::upper_bound(container.begin(), container.end(), k, cmp); }
00164
00165 std::pair<iterator, iterator> equal_range(const key_type& k)
00166 { return std::equal_range(container.begin(), container.end(), k, cmp); }
00167
00168 std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const
00169 { return std::equal_range(container.begin(), container.end(), k, cmp); }
00170 };
00171
00172 template <typename T>
00173 inline bool operator==(const sorted_vector<T>& lhs, const sorted_vector<T>& rhs)
00174 { return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); }
00175
00176 template <typename T>
00177 inline bool operator<(const sorted_vector<T>& lhs, const sorted_vector<T>& rhs)
00178 { return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); }
00179
00180 }
00181
00182 #endif
00183