00001 #pragma once
00002 #ifndef OPENGM_RANDOM_ACCESS_SET_HXX
00003 #define OPENGM_RANDOM_ACCESS_SET_HXX
00004
00005 #include <vector>
00006 #include <algorithm>
00007 #include <utility>
00008
00009 namespace opengm {
00010
00020 template<class Key,class Compare=std::less<Key>,class Alloc=std::allocator<Key> >
00021 class RandomAccessSet {
00022 private:
00024 typedef std::vector<Key,Alloc> VectorType;
00025
00026 public:
00027
00029 typedef Key key_type;
00031 typedef Key ValueType;
00033 typedef Key value_type;
00035 typedef Compare key_compare;
00037 typedef Compare value_compare;
00039 typedef Alloc allocator_type;
00041 typedef typename Alloc::const_reference const_reference;
00043 typedef typename VectorType::iterator iterator;
00045 typedef typename VectorType::const_iterator const_iterator;
00047 typedef typename VectorType::size_type size_type;
00049 typedef typename VectorType::difference_type difference_type;
00051 typedef typename VectorType::const_pointer const_pointer;
00053 typedef typename VectorType::const_reverse_iterator const_reverse_iterator;
00054
00055
00056
00057 RandomAccessSet(const size_t, const Compare& compare=Compare(), const Alloc& alloc=Alloc());
00058 RandomAccessSet(const Compare& compare=Compare(), const Alloc& alloc=Alloc());
00059 template <class InputIterator>
00060 RandomAccessSet(InputIterator, InputIterator, const Compare& compare =Compare(), const Alloc & alloc=Alloc());
00061 RandomAccessSet(const RandomAccessSet&);
00062
00063
00064 RandomAccessSet& operator=(const RandomAccessSet &);
00065
00066 const value_type& operator[](const size_type) const;
00067
00068 const_iterator begin() const;
00069 const_iterator end() const;
00070 const_iterator rbegin() const;
00071 const_iterator rend() const;
00072
00073 iterator begin();
00074 iterator end();
00075 iterator rbegin();
00076 iterator rend();
00077 bool empty() const;
00078 size_type size() const;
00079 size_type max_size() const;
00080 std::pair< const_iterator,bool> insert(const value_type&);
00081 template <class InputIterator>
00082 void insert(InputIterator, InputIterator);
00083 const_iterator insert(const_iterator , const value_type&);
00084 void erase(iterator position);
00085 size_type erase(const key_type& );
00086 void erase( const_iterator, const_iterator);
00087 void swap(RandomAccessSet&);
00088 void clear();
00089 key_compare key_comp() const;
00090 value_compare value_comp() const;
00091 const_iterator find(const key_type&) const;
00092 iterator find(const key_type&);
00093 size_type count(const key_type&) const;
00094 const_iterator lower_bound(const key_type&) const;
00095 const_iterator upper_bound(const key_type&) const;
00096 std::pair<const_iterator,const_iterator> equal_range(const key_type&) const;
00097 iterator lower_bound(const key_type&) ;
00098 iterator upper_bound(const key_type&) ;
00099 std::pair<iterator,iterator> equal_range(const key_type&) ;
00100 allocator_type get_allocator() const;
00101
00102
00103 void reserve(const size_t size){
00104 vector_.reserve(size);
00105 }
00106 size_t capacity()const{
00107 return vector_.capacity();
00108 }
00109
00110 private:
00111 std::vector<Key> vector_;
00112 Compare compare_;
00113 };
00114
00119 template<class Key, class Compare, class Alloc>
00120 inline
00121 RandomAccessSet<Key,Compare,Alloc>::RandomAccessSet
00122 (
00123 const size_t reserveSize,
00124 const Compare& compare,
00125 const Alloc& alloc
00126 )
00127 : vector_(alloc),
00128 compare_(compare) {
00129 vector_.reserve(reserveSize);
00130 }
00131
00135 template<class Key, class Compare, class Alloc>
00136 inline const typename RandomAccessSet<Key,Compare,Alloc>::value_type&
00137 RandomAccessSet<Key,Compare,Alloc>::operator[]
00138 (
00139 const typename RandomAccessSet<Key,Compare,Alloc>::size_type index
00140 ) const
00141 {
00142 return vector_[index];
00143 }
00144
00148 template<class Key, class Compare, class Alloc>
00149 inline
00150 RandomAccessSet<Key,Compare,Alloc>::RandomAccessSet
00151 (
00152 const Compare& compare,
00153 const Alloc& alloc
00154 )
00155 : vector_(alloc),
00156 compare_(compare)
00157 {}
00158
00163 template<class Key, class Compare, class Alloc>
00164 template <class InputIterator>
00165 inline
00166 RandomAccessSet<Key,Compare,Alloc>::RandomAccessSet
00167 (
00168 InputIterator beginInput,
00169 InputIterator endInput,
00170 const Compare& compare,
00171 const Alloc& alloc
00172 )
00173 : vector_(alloc),
00174 compare_(compare)
00175 {
00176 while(beginInput!=endInput) {
00177 this->insert(*beginInput);
00178 ++beginInput;
00179 }
00180 }
00181
00184 template<class Key, class Compare, class Alloc>
00185 inline
00186 RandomAccessSet<Key,Compare,Alloc>::RandomAccessSet
00187 (
00188 const RandomAccessSet<Key,Compare,Alloc>& src
00189 )
00190 : vector_(src.vector_),
00191 compare_(src.compare_) {
00192 }
00193
00196 template<class Key, class Compare, class Alloc>
00197 inline RandomAccessSet<Key,Compare,Alloc>&
00198 RandomAccessSet<Key,Compare,Alloc>::operator=
00199 (
00200 const RandomAccessSet<Key,Compare,Alloc> & src
00201 )
00202 {
00203 if(this!=&src) {
00204 vector_=src.vector_;
00205 compare_=src.compare_;
00206 }
00207 return *this;
00208 }
00209
00212 template<class Key, class Compare, class Alloc>
00213 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00214 RandomAccessSet<Key,Compare,Alloc>::begin() const
00215 {
00216 return vector_.begin();
00217 }
00218
00221 template<class Key, class Compare, class Alloc>
00222 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00223 RandomAccessSet<Key,Compare,Alloc>::end() const
00224 {
00225 return vector_.end();
00226 }
00229 template<class Key, class Compare, class Alloc>
00230 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00231 RandomAccessSet<Key,Compare,Alloc>::rbegin() const
00232 {
00233 return vector_.rbegin();
00234 }
00235
00238 template<class Key, class Compare, class Alloc>
00239 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00240 RandomAccessSet<Key,Compare,Alloc>::rend() const
00241 {
00242 return vector_.rend();
00243 }
00244
00247 template<class Key, class Compare, class Alloc>
00248 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
00249 RandomAccessSet<Key,Compare,Alloc>::begin()
00250 {
00251 return vector_.begin();
00252 }
00253
00256 template<class Key, class Compare, class Alloc>
00257 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
00258 RandomAccessSet<Key,Compare,Alloc>::end()
00259 {
00260 return vector_.end();
00261 }
00262
00265 template<class Key, class Compare, class Alloc>
00266 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
00267 RandomAccessSet<Key,Compare,Alloc>::rbegin()
00268 {
00269 return vector_.rbegin();
00270 }
00271
00274 template<class Key, class Compare, class Alloc>
00275 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
00276 RandomAccessSet<Key,Compare,Alloc>::rend()
00277 {
00278 return vector_.rend();
00279 }
00280
00283 template<class Key, class Compare, class Alloc>
00284 inline bool
00285 RandomAccessSet<Key,Compare,Alloc>::empty() const
00286 {
00287 return vector_.empty();
00288 }
00289
00292 template<class Key, class Compare, class Alloc>
00293 inline typename RandomAccessSet<Key,Compare,Alloc>::size_type
00294 RandomAccessSet<Key,Compare,Alloc>::size() const
00295 {
00296 return vector_.size();
00297 }
00298
00301 template<class Key, class Compare, class Alloc>
00302 inline typename RandomAccessSet<Key,Compare,Alloc>::size_type
00303 RandomAccessSet<Key,Compare,Alloc>::max_size() const
00304 {
00305 return vector_.max_size();
00306 }
00307
00308
00315 template<class Key, class Compare, class Alloc>
00316 inline std::pair<typename RandomAccessSet<Key,Compare,Alloc>::const_iterator,bool>
00317 RandomAccessSet<Key,Compare,Alloc>::insert
00318 (
00319 const typename RandomAccessSet<Key,Compare,Alloc>::value_type& value
00320 ) {
00321 bool found(true);
00322 iterator i(lower_bound(static_cast<Key>(value)));
00323 if(i == end() || compare_(static_cast<Key>(value), *i)) {
00324 i = vector_.insert(i, static_cast<Key>(value));
00325 found = false;
00326 }
00327 return std::make_pair(i, !found);
00328 }
00329
00334 template<class Key, class Compare, class Alloc>
00335 template <class InputIterator>
00336 inline void
00337 RandomAccessSet<Key,Compare,Alloc>::insert
00338 (
00339 InputIterator first,
00340 InputIterator last
00341 )
00342 {
00343 while(first!=last) {
00344 this->insert(*first);
00345 ++first;
00346 }
00347 }
00348
00353 template<class Key, class Compare, class Alloc>
00354 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00355 RandomAccessSet<Key,Compare,Alloc>::insert
00356 (
00357 typename RandomAccessSet<Key,Compare,Alloc>::const_iterator position,
00358 const typename RandomAccessSet<Key,Compare,Alloc>::value_type& value
00359 )
00360 {
00361 if((position == begin() || this->operator()(*(position-1),value))
00362 && (position == end() || this->operator()(value, *position))) {
00363 return vector_.insert(position, value);
00364 }
00365 return insert(value).first;
00366 }
00367
00370 template<class Key, class Compare, class Alloc>
00371 inline void
00372 RandomAccessSet<Key,Compare,Alloc>::erase
00373 (
00374 typename RandomAccessSet<Key,Compare,Alloc>::iterator position
00375 )
00376 {
00377 vector_.erase(position);
00378 }
00379
00382 template<class Key, class Compare, class Alloc>
00383 inline typename RandomAccessSet<Key,Compare,Alloc>::size_type
00384 RandomAccessSet<Key,Compare,Alloc>::erase
00385 (
00386 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& x
00387 )
00388 {
00389 iterator i =find(x);
00390 if(i!=vector_.end())
00391 {
00392 erase(i);
00393 return 1;
00394 }
00395 return 0;
00396 }
00397
00401 template<class Key, class Compare, class Alloc>
00402 inline void
00403 RandomAccessSet<Key,Compare,Alloc>::erase
00404 (
00405 const typename RandomAccessSet<Key,Compare,Alloc>::const_iterator first,
00406 const typename RandomAccessSet<Key,Compare,Alloc>::const_iterator last
00407 )
00408 {
00409 vector_.erase(first,last);
00410 }
00411
00414 template<class Key, class Compare, class Alloc>
00415 inline void
00416 RandomAccessSet<Key,Compare,Alloc>::swap
00417 (
00418 RandomAccessSet<Key,Compare,Alloc>& rhs
00419 )
00420 {
00421 vector_.swap(rhs.vector_);
00422 compare_=rhs.compare_;
00423 }
00424
00428 template<class Key, class Compare, class Alloc>
00429 inline void
00430 RandomAccessSet<Key,Compare,Alloc>::clear()
00431 {
00432 vector_.clear();
00433 }
00434
00437 template<class Key, class Compare, class Alloc>
00438 inline typename RandomAccessSet<Key,Compare,Alloc>::key_compare
00439 RandomAccessSet<Key,Compare,Alloc>::key_comp() const
00440 {
00441 return compare_;
00442 }
00443
00446 template<class Key, class Compare, class Alloc>
00447 inline typename RandomAccessSet<Key,Compare,Alloc>::value_compare
00448 RandomAccessSet<Key,Compare,Alloc>::value_comp() const
00449 {
00450 return compare_;
00451 }
00452
00457 template<class Key, class Compare, class Alloc>
00458 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00459 RandomAccessSet<Key,Compare,Alloc>::find
00460 (
00461 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00462 ) const
00463 {
00464 const_iterator i(lower_bound(value));
00465 if (i != end() && compare_(value, *i))
00466 {
00467 i = end();
00468 }
00469 return i;
00470 }
00471
00476 template<class Key, class Compare, class Alloc>
00477 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
00478 RandomAccessSet<Key,Compare,Alloc>::find
00479 (
00480 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00481 )
00482 {
00483 iterator i(lower_bound(value));
00484 if (i != end() && compare_(value, *i))
00485 {
00486 i = end();
00487 }
00488 return i;
00489 }
00490
00494 template<class Key, class Compare, class Alloc>
00495 inline typename RandomAccessSet<Key,Compare,Alloc>::size_type
00496 RandomAccessSet<Key,Compare,Alloc>::count
00497 (
00498 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00499 ) const
00500 {
00501 return find(value) != end();
00502 }
00503
00507 template<class Key, class Compare, class Alloc>
00508 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00509 RandomAccessSet<Key,Compare,Alloc>::lower_bound
00510 (
00511 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00512 ) const
00513 {
00514 return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
00515 }
00516
00520 template<class Key, class Compare, class Alloc>
00521 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
00522 RandomAccessSet<Key,Compare,Alloc>::lower_bound
00523 (
00524 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00525 )
00526 {
00527 return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
00528 }
00529
00533 template<class Key, class Compare, class Alloc>
00534 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
00535 RandomAccessSet<Key,Compare,Alloc>::upper_bound
00536 (
00537 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00538 ) const
00539 {
00540 return std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
00541 }
00542
00546 template<class Key, class Compare, class Alloc>
00547 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
00548 RandomAccessSet<Key,Compare,Alloc>::upper_bound
00549 (
00550 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00551 )
00552 {
00553 return std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
00554 }
00555
00559 template<class Key, class Compare, class Alloc>
00560 inline std::pair<typename RandomAccessSet<Key,Compare,Alloc>::const_iterator,typename RandomAccessSet<Key,Compare,Alloc>::const_iterator>
00561 RandomAccessSet<Key,Compare,Alloc>::equal_range
00562 (
00563 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00564 ) const
00565 {
00566 return std::equal_range(vector_.begin(), vector_.end(), value, compare_);
00567 }
00568
00572 template<class Key, class Compare, class Alloc>
00573 inline std::pair<typename RandomAccessSet<Key,Compare,Alloc>::iterator,typename RandomAccessSet<Key,Compare,Alloc>::iterator>
00574 RandomAccessSet<Key,Compare,Alloc>::equal_range
00575 (
00576 const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
00577 )
00578 {
00579 return std::equal_range(vector_.begin(), vector_.end(), value, compare_);
00580 }
00583 template<class Key, class Compare, class Alloc>
00584 inline typename RandomAccessSet<Key,Compare,Alloc>::allocator_type
00585 RandomAccessSet<Key,Compare,Alloc>::get_allocator() const
00586 {
00587 return vector_.get_allocator();
00588 }
00589
00590 }
00591
00592 #endif // OPENGM_RANDOM_ACCESS_SET_HXX