ALBA
sorted_vector.h
Go to the documentation of this file.
1/* STL-conforming "sorted vector" container
2 *
3 * (C) 2002 Martin Holzherr (holzherr@infobrain.com). All rights reserved.
4 *
5 * Permission is granted to use, distribute and modify this code provided that:
6 * · this copyright notice appears,
7 * ·
8 * The author welcomes any suggestions on the code or reportings of actual
9 * use of the code. Please send your comments to holzherr@infobrain.com.
10 *
11 * The author makes NO WARRANTY or representation, either express or implied,
12 * with respect to this code, its quality, accuracy, merchantability, or
13 * fitness for a particular purpose. This software is provided "AS IS", and
14 * you, its user, assume the entire risk as to its quality and accuracy.
15 *
16 * Created: November 19th, 2002
17 * Last modified: November 27th, 2002
18 (changed namespace from std to codeproject;
19 uses template member functions for MSCVER>=1300)
20
21 */
22
23#ifndef SORTED_VECTOR_
24#define SORTED_VECTOR_
25#define VERSION_SORTED_VECTOR_ 0x00010010
26
27
28#include <algorithm>
29#include <vector>
30#include <utility>
31#include <functional>
32
33#pragma pack(push,8)
34#pragma warning(push,3)
35
36
37namespace codeproject{
38 // TEMPLATE CLASS sorted_vector
39
40 template<class K, bool bNoDuplicates= false,class Pr = std::less<K>, class A = std::allocator<K> >
42public:
44 typedef std::vector<K,A> Cont;
45 typedef Cont::allocator_type allocator_type;
46 typedef Cont::size_type size_type;
47 typedef Cont::difference_type difference_type;
48 typedef Cont::reference reference;
49 typedef Cont::const_reference const_reference;
50 typedef Cont::value_type value_type;
51 typedef K key_type;
52 typedef Cont::iterator iterator;
53 typedef Cont::const_iterator const_iterator;
54 typedef Pr key_compare;
55 typedef Pr value_compare;
56
57 typedef Cont::const_reverse_iterator
59 typedef Cont::reverse_iterator reverse_iterator;
60
61 typedef std::pair<iterator, iterator> Pairii_;
62 typedef std::pair<const_iterator, const_iterator> Paircc_;
63 typedef std::pair<iterator, bool> Pairib_;
64 explicit sorted_vector(const Pr& pred = Pr(),const A& al = A())
65 :key_compare_(pred),vec_(al){}
66#if (_MSC_VER >= 1300)
67 template<class It>
68 sorted_vector(It first, It beyond,
69 const Pr& pred = Pr(),const A& al = A())
70 :key_compare_(pred),vec_(first,beyond,al)
71 {stable_sort();}
72#else
74 const Pr& pred = Pr(),const A& al = A())
75 :key_compare_(pred),vec_(first,beyond,al)
76 {stable_sort();}
77#endif
80 {}
82 Myt_& operator=(const Myt_& x) {vec_.operator=(x.vec_);
84 return *this;}
85 Myt_& operator=(const Cont& x){vec_.operator=(x);
86 sort();return *this;}
87
88 void reserve(size_type n) {vec_.reserve(n);}
89 iterator begin() {return vec_.begin(); }
90 const_iterator begin() const {return vec_.begin(); }
91 iterator end() {return vec_.end();}
92 const_iterator end() const {return vec_.end();}
93 reverse_iterator rbegin() {return vec_.rbegin();}
95 {return vec_.rbegin();}
96
97 reverse_iterator rend() {return vec_.rend();}
99 {return vec_.rend();}
100
101
102 size_type size() const {return vec_.size();}
103 size_type max_size() const {return vec_.max_size();}
104 bool empty() const {return vec_.empty();}
105 A get_allocator() const {return vec_.get_allocator();}
106 const_reference at(size_type p) const {return vec_.at(p);}
107 reference at(size_type p) {return vec_.at(p);}
109 {return vec_.operator[](p);}
110
111 reference operator[](size_type p) {return vec_.operator[](p);}
112 reference front() {return vec_.front();}
113 const_reference front() const {return vec_.front();}
114 reference back() {return vec_.back();}
115 const_reference back() const {return vec_.back();}
116 void pop_back() {vec_.pop_back();}
117
119 {vec_.assign(first,beyond);}
120 void assign(size_type n, const K& x = K())
121 {vec_.assign(n,x);}
122/*insert members*/
124 {
125 if(bNoDuplicates){
126 iterator p= lower_bound(x);
127 if(p==end()||key_compare_(x,*p)){
128 return Pairib_(InsertImpl_(p,x),true);
129 }else{
130 return Pairib_(p,false);
131 }
132 }else{
133 iterator p= upper_bound(x);
134 return Pairib_(InsertImpl_(p,x),true);
135 }
136 }
137 iterator insert(iterator it, const value_type& x)//it is the hint
138 {
139 if(it!=end() ){
140 if(bNoDuplicates){
141 if(key_compare_(*it,x)){
142 if((it+1)==end()||KeyCompare_Gt_(*(it+1),x)){//use hint
143 return InsertImpl_(it+1,x);
144 }else if(KeyCompare_Geq_(*(it+1),x)){
145 return end();
146 }
147 }
148 }else{
149 if( KeyCompare_Leq_(*it,x)
150 &&((it+1)==end()||KeyCompare_Geq_(*(it+1),x))){
151 return InsertImpl_(it+1,x);
152 }
153 }
154 }
155 return insert(x).first;
156 }
157#if (_MSC_VER >= 1300)
158 template<class It>
159 void insert(It first, It beyond)
160 {
161 size_type n= std::distance(first,beyond);
162 reserve(size()+n);
163 for( ;first!=beyond;++first){
164 insert(*first);
165 }
166 }
167#else
169 {
170 size_type n= std::distance(first,beyond);
171 reserve(size()+n);
172 for( ;first!=beyond;++first){
173 insert(*first);
174 }
175 }
176#endif
177 iterator erase(iterator p) {return vec_.erase(p);}
179 {return vec_.erase(first,beyond);}
180 size_type erase(const K& key)
181 {
182 Pairii_ begEnd= equal_range(key);
183 size_type n= std::distance(begEnd.first,begEnd.second);
184 erase(begEnd.first,begEnd.second);
185 return n;
186 }
187 void clear() {vec_.clear();}
188
189 bool Eq_(const Myt_& x) const
190 {return (size() == x.size()
191 && std::equal(begin(), end(), x.begin())); }
192 bool Lt_(const Myt_& x) const
193 {return (std::lexicographical_compare(begin(), end(),
194 x.begin(), x.end()));}
195 void swap(Myt_& x)
196 {vec_.swap(x.vec_);std::swap(key_compare_,x.key_compare_);}
197
198 friend void swap(Myt_& x, Myt_& Y_)
199 {x.swap(Y_); }
200
202 value_compare value_comp() const {return (key_comp()); }
203 iterator find(const K& k)
204 { iterator p = lower_bound(k);
205 return (p==end()||key_compare_(k, *p))? end():p;
206 }
207 const_iterator find(const K& k) const
209 return (p==end()||key_compare_(k,*p))?end():p;}
210 size_type count(const K& k) const
211 {Paircc_ Ans_ = equal_range(k);
212 size_type n = std::distance(Ans_.first, Ans_.second);
213 return (n); }
215 {return std::lower_bound(begin(), end(), k, key_compare_); }
216 const_iterator lower_bound(const K& k) const
217 {return std::lower_bound(begin(), end(), k, key_compare_); }
219 {return std::upper_bound(begin(), end(), k, key_compare_); }
220 const_iterator upper_bound(const K& k) const
221 {return std::upper_bound(begin(), end(), k, key_compare_); }
223 {return std::equal_range(begin(), end(), k, key_compare_); }
224 Paircc_ equal_range(const K& k) const
225 {return std::equal_range(begin(), end(), k, key_compare_); }
226
227/*functions for use with direct std::vector-access*/
229 {return vec_;}
230 void sort()//restore sorted order after low level access
231 { std::sort(vec_.begin(),vec_.end(),key_compare_);
232 if( bNoDuplicates ){
233 vec_.erase(Unique_(),vec_.end());
234 }
235 }
236 void stable_sort()//restore sorted order after low level access
237 { std::stable_sort(vec_.begin(),vec_.end(),key_compare_);
238 if( bNoDuplicates ){
239 erase(Unique_(),end());
240 }
241 }
242protected:
244 { iterator front_= vec_.begin(),out_= vec_.end(),end_=vec_.end();
245 bool bCopy_= false;
246 for(iterator prev_; (prev_=front_)!=end_ && ++front_!=end_; ){
247 if( key_compare_(*prev_,*front_)){
248 if(bCopy_){
249 *out_= *front_;
250 out_++;
251 }
252 }else{
253 if(!bCopy_){out_=front_;bCopy_=true;}
254 }
255 }
256 return out_;
257 }
259 {return vec_.insert(p,x);}
260 bool KeyCompare_Leq_(const K& ty0,const K& ty1)
261 {return !key_compare_(ty1,ty0);}
262 bool KeyCompare_Geq_(const K& ty0,const K& ty1)
263 {return !key_compare_(ty0,ty1);}
264 bool KeyCompare_Gt_(const K& ty0,const K& ty1)
265 {return key_compare_(ty1,ty0);}
266
269};
270
271
272template<class K,bool bNoDuplicates,class Pr, class A> inline
275 {return x.Eq_(Y_); }
276template<class K,bool bNoDuplicates,class Pr, class A> inline
279 {return !(x == Y_); }
280template<class K,bool bNoDuplicates,class Pr, class A> inline
283 {return x.Lt_(Y_);}
284template<class K,bool bNoDuplicates,class Pr,class A> inline
287 {return Y_ < x; }
288template<class K,bool bNoDuplicates,class Pr, class A> inline
291 {return !(Y_ < x); }
292template<class K, bool bNoDuplicates,class Pr,class A> inline
295 {return (!(x < Y_)); }
296}
297#pragma warning(pop)
298#pragma pack(pop)
299#elif VERSION_SORTED_VECTOR_ != 0x00010010
300#error You have included two sorted_vector.h with different version numbers
301#endif
sorted_vector(const Myt_ &x)
Definition: sorted_vector.h:78
const_reference operator[](size_type p) const
const_reverse_iterator rbegin() const
Definition: sorted_vector.h:94
bool KeyCompare_Geq_(const K &ty0, const K &ty1)
Cont::reference reference
Definition: sorted_vector.h:48
iterator erase(iterator first, iterator beyond)
void insert(const_iterator first, const_iterator beyond)
Cont::const_iterator const_iterator
Definition: sorted_vector.h:53
void assign(size_type n, const K &x=K())
Cont::difference_type difference_type
Definition: sorted_vector.h:47
const_reference back() const
const_reference at(size_type p) const
Cont::reverse_iterator reverse_iterator
Definition: sorted_vector.h:59
std::vector< K, A > Cont
Definition: sorted_vector.h:44
bool KeyCompare_Gt_(const K &ty0, const K &ty1)
std::pair< iterator, bool > Pairib_
Definition: sorted_vector.h:63
std::pair< iterator, iterator > Pairii_
Definition: sorted_vector.h:61
Pairib_ insert(const value_type &x)
void assign(const_iterator first, const_iterator beyond)
Pairii_ equal_range(const K &k)
sorted_vector(const Pr &pred=Pr(), const A &al=A())
Definition: sorted_vector.h:64
reverse_iterator rbegin()
Definition: sorted_vector.h:93
size_type size() const
const_iterator find(const K &k) const
Myt_ & operator=(const Myt_ &x)
Definition: sorted_vector.h:82
const_iterator begin() const
Definition: sorted_vector.h:90
reverse_iterator rend()
Definition: sorted_vector.h:97
const_iterator upper_bound(const K &k) const
friend void swap(Myt_ &x, Myt_ &Y_)
bool Lt_(const Myt_ &x) const
iterator upper_bound(const K &k)
iterator insert(iterator it, const value_type &x)
bool Eq_(const Myt_ &x) const
Cont::allocator_type allocator_type
Definition: sorted_vector.h:45
std::pair< const_iterator, const_iterator > Paircc_
Definition: sorted_vector.h:62
const_reference front() const
iterator lower_bound(const K &k)
size_type count(const K &k) const
reference at(size_type p)
Cont::size_type size_type
Definition: sorted_vector.h:46
key_compare key_comp() const
reference operator[](size_type p)
sorted_vector< K, bNoDuplicates, Pr, A > Myt_
Definition: sorted_vector.h:43
size_type max_size() const
const_iterator lower_bound(const K &k) const
Paircc_ equal_range(const K &k) const
iterator InsertImpl_(iterator p, const value_type &x)
const_reverse_iterator rend() const
Definition: sorted_vector.h:98
void reserve(size_type n)
Definition: sorted_vector.h:88
Myt_ & operator=(const Cont &x)
Definition: sorted_vector.h:85
value_compare value_comp() const
Cont::const_reference const_reference
Definition: sorted_vector.h:49
iterator erase(iterator p)
Cont::const_reverse_iterator const_reverse_iterator
Definition: sorted_vector.h:58
Cont::value_type value_type
Definition: sorted_vector.h:50
sorted_vector(const_iterator first, const_iterator beyond, const Pr &pred=Pr(), const A &al=A())
Definition: sorted_vector.h:73
const_iterator end() const
Definition: sorted_vector.h:92
bool KeyCompare_Leq_(const K &ty0, const K &ty1)
size_type erase(const K &key)
iterator find(const K &k)
bool operator==(const sorted_vector< K, bNoDuplicates, Pr, A > &x, const sorted_vector< K, bNoDuplicates, Pr, A > &Y_)
bool operator!=(const sorted_vector< K, bNoDuplicates, Pr, A > &x, const sorted_vector< K, bNoDuplicates, Pr, A > &Y_)
bool operator<=(const sorted_vector< K, bNoDuplicates, Pr, A > &x, const sorted_vector< K, bNoDuplicates, Pr, A > &Y_)
bool operator>=(const sorted_vector< K, bNoDuplicates, Pr, A > &x, const sorted_vector< K, bNoDuplicates, Pr, A > &Y_)
bool operator<(const sorted_vector< K, bNoDuplicates, Pr, A > &x, const sorted_vector< K, bNoDuplicates, Pr, A > &Y_)
bool operator>(const sorted_vector< K, bNoDuplicates, Pr, A > &x, const sorted_vector< K, bNoDuplicates, Pr, A > &Y_)