OpenGM  2.3.x
Discrete Graphical Model Library
randomaccessset.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_RANDOM_ACCESS_SET_HXX
3 #define OPENGM_RANDOM_ACCESS_SET_HXX
4 
5 #include <vector>
6 #include <algorithm>
7 #include <utility>
8 
9 namespace opengm {
10 
20 template<class Key,class Compare=std::less<Key>,class Alloc=std::allocator<Key> >
22 private:
24  typedef std::vector<Key,Alloc> VectorType;
25 
26 public:
27  // typedefs
29  typedef Key key_type;
31  typedef Key ValueType;
33  typedef Key value_type;
35  typedef Compare key_compare;
37  typedef Compare value_compare;
39  typedef Alloc allocator_type;
41  typedef typename Alloc::const_reference const_reference;
43  typedef typename VectorType::iterator iterator;
45  typedef typename VectorType::const_iterator const_iterator;
47  typedef typename VectorType::size_type size_type;
49  typedef typename VectorType::difference_type difference_type;
51  typedef typename VectorType::const_pointer const_pointer;
53  typedef typename VectorType::const_reverse_iterator const_reverse_iterator;
54 
55  // memeber functions:
56  // constructor
57  RandomAccessSet(const size_t, const Compare& compare=Compare(), const Alloc& alloc=Alloc());
58  RandomAccessSet(const Compare& compare=Compare(), const Alloc& alloc=Alloc());
59  template <class InputIterator>
60  RandomAccessSet(InputIterator, InputIterator, const Compare& compare =Compare(), const Alloc & alloc=Alloc());
62 
63  // operator=
65  // operator[]
66  const value_type& operator[](const size_type) const;
67  // iterators
68  const_iterator begin() const;
69  const_iterator end() const;
70  const_iterator rbegin() const;
71  const_iterator rend() const;
72 
73  iterator begin();
74  iterator end();
75  iterator rbegin();
76  iterator rend();
77  bool empty() const;
78  size_type size() const;
79  size_type max_size() const;
80  std::pair< const_iterator,bool> insert(const value_type&);
81  template <class InputIterator>
82  void insert(InputIterator, InputIterator);
83  const_iterator insert(const_iterator , const value_type&);
84  void erase(iterator position);
85  size_type erase(const key_type& );
86  void erase( const_iterator, const_iterator);
87  void swap(RandomAccessSet&);
88  void clear();
89  key_compare key_comp() const;
90  value_compare value_comp() const;
91  const_iterator find(const key_type&) const;
92  iterator find(const key_type&);
93  size_type count(const key_type&) const;
94  const_iterator lower_bound(const key_type&) const;
95  const_iterator upper_bound(const key_type&) const;
96  std::pair<const_iterator,const_iterator> equal_range(const key_type&) const;
97  iterator lower_bound(const key_type&) ;
98  iterator upper_bound(const key_type&) ;
99  std::pair<iterator,iterator> equal_range(const key_type&) ;
100  allocator_type get_allocator() const;
101 
102  // std vector functions
103  void reserve(const size_t size){
104  vector_.reserve(size);
105  }
106  size_t capacity()const{
107  return vector_.capacity();
108  }
109 
110  template<class SET>
111  void assignFromSet(const SET & set){
112  vector_.assign(set.begin(),set.end());
113  }
114 
115 private:
116  std::vector<Key> vector_;
117  Compare compare_;
118 };
119 
124 template<class Key, class Compare, class Alloc>
125 inline
127 (
128  const size_t reserveSize,
129  const Compare& compare,
130  const Alloc& alloc
131 )
132 : vector_(alloc),
133  compare_(compare) {
134  vector_.reserve(reserveSize);
135 }
136 
140 template<class Key, class Compare, class Alloc>
143 (
145 ) const
146 {
147  return vector_[index];
148 }
149 
153 template<class Key, class Compare, class Alloc>
154 inline
156 (
157  const Compare& compare,
158  const Alloc& alloc
159 )
160 : vector_(alloc),
161  compare_(compare)
162 {}
163 
168 template<class Key, class Compare, class Alloc>
169 template <class InputIterator>
170 inline
172 (
173  InputIterator beginInput,
174  InputIterator endInput,
175  const Compare& compare,
176  const Alloc& alloc
177 )
178 : vector_(alloc),
179  compare_(compare)
180 {
181  while(beginInput!=endInput) {
182  this->insert(*beginInput);
183  ++beginInput;
184  }
185 }
186 
189 template<class Key, class Compare, class Alloc>
190 inline
192 (
194 )
195 : vector_(src.vector_),
196  compare_(src.compare_) {
197 }
198 
201 template<class Key, class Compare, class Alloc>
204 (
206 )
207 {
208  if(this!=&src) {
209  vector_=src.vector_;
210  compare_=src.compare_;
211  }
212  return *this;
213 }
214 
217 template<class Key, class Compare, class Alloc>
220 {
221  return vector_.begin();
222 }
223 
226 template<class Key, class Compare, class Alloc>
229 {
230  return vector_.end();
231 }
234 template<class Key, class Compare, class Alloc>
237 {
238  return vector_.rbegin();
239 }
240 
243 template<class Key, class Compare, class Alloc>
246 {
247  return vector_.rend();
248 }
249 
252 template<class Key, class Compare, class Alloc>
255 {
256  return vector_.begin();
257 }
258 
261 template<class Key, class Compare, class Alloc>
264 {
265  return vector_.end();
266 }
267 
270 template<class Key, class Compare, class Alloc>
273 {
274  return vector_.rbegin();
275 }
276 
279 template<class Key, class Compare, class Alloc>
282 {
283  return vector_.rend();
284 }
285 
288 template<class Key, class Compare, class Alloc>
289 inline bool
291 {
292  return vector_.empty();
293 }
294 
297 template<class Key, class Compare, class Alloc>
300 {
301  return vector_.size();
302 }
303 
306 template<class Key, class Compare, class Alloc>
309 {
310  return vector_.max_size();
311 }
312 
313 // modifiers
320 template<class Key, class Compare, class Alloc>
321 inline std::pair<typename RandomAccessSet<Key,Compare,Alloc>::const_iterator,bool>
323 (
325 ) {
326  bool found(true);
327  iterator i(lower_bound(static_cast<Key>(value)));
328  if(i == end() || compare_(static_cast<Key>(value), *i)) {
329  i = vector_.insert(i, static_cast<Key>(value));
330  found = false;
331  }
332  return std::make_pair(i, !found);
333 }
334 
339 template<class Key, class Compare, class Alloc>
340 template <class InputIterator>
341 inline void
343 (
344  InputIterator first,
345  InputIterator last
346 )
347 {
348  while(first!=last) {
349  this->insert(*first);
350  ++first;
351  }
352 }
353 
358 template<class Key, class Compare, class Alloc>
361 (
364 )
365 {
366  if((position == begin() || this->operator()(*(position-1),value))
367  && (position == end() || this->operator()(value, *position))) {
368  return vector_.insert(position, value);
369  }
370  return insert(value).first;
371 }
372 
375 template<class Key, class Compare, class Alloc>
376 inline void
378 (
379  typename RandomAccessSet<Key,Compare,Alloc>::iterator position
380 )
381 {
382  vector_.erase(position);
383 }
384 
387 template<class Key, class Compare, class Alloc>
388 inline typename RandomAccessSet<Key,Compare,Alloc>::size_type
390 (
391  const typename RandomAccessSet<Key,Compare,Alloc>::key_type& x
392 )
393 {
394  iterator i =find(x);
395  if(i!=vector_.end())
396  {
397  erase(i);
398  return 1;
399  }
400  return 0;
401 }
402 
406 template<class Key, class Compare, class Alloc>
407 inline void
409 (
410  const typename RandomAccessSet<Key,Compare,Alloc>::const_iterator first,
411  const typename RandomAccessSet<Key,Compare,Alloc>::const_iterator last
412 )
413 {
414  vector_.erase(first,last);
415 }
416 
419 template<class Key, class Compare, class Alloc>
420 inline void
422 (
424 )
425 {
426  vector_.swap(rhs.vector_);
427  compare_=rhs.compare_;
428 }
429 
433 template<class Key, class Compare, class Alloc>
434 inline void
436 {
437  vector_.clear();
438 }
439 
442 template<class Key, class Compare, class Alloc>
445 {
446  return compare_;
447 }
448 
451 template<class Key, class Compare, class Alloc>
454 {
455  return compare_;
456 }
457 
462 template<class Key, class Compare, class Alloc>
465 (
467 ) const
468 {
469  const_iterator i(lower_bound(value));
470  if (i != end() && compare_(value, *i))
471  {
472  i = end();
473  }
474  return i;
475 }
476 
481 template<class Key, class Compare, class Alloc>
482 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
484 (
485  const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
486 )
487 {
488  iterator i(lower_bound(value));
489  if (i != end() && compare_(value, *i))
490  {
491  i = end();
492  }
493  return i;
494 }
495 
499 template<class Key, class Compare, class Alloc>
500 inline typename RandomAccessSet<Key,Compare,Alloc>::size_type
502 (
504 ) const
505 {
506  return find(value) != end();
507 }
508 
512 template<class Key, class Compare, class Alloc>
515 (
517 ) const
518 {
519  return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
520 }
521 
525 template<class Key, class Compare, class Alloc>
526 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
528 (
529  const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
530 )
531 {
532  return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
533 }
534 
538 template<class Key, class Compare, class Alloc>
539 inline typename RandomAccessSet<Key,Compare,Alloc>::const_iterator
541 (
542  const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
543 ) const
544 {
545  return std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
546 }
547 
551 template<class Key, class Compare, class Alloc>
552 inline typename RandomAccessSet<Key,Compare,Alloc>::iterator
554 (
555  const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
556 )
557 {
558  return std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
559 }
560 
564 template<class Key, class Compare, class Alloc>
565 inline std::pair<typename RandomAccessSet<Key,Compare,Alloc>::const_iterator,typename RandomAccessSet<Key,Compare,Alloc>::const_iterator>
567 (
568  const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
569 ) const
570 {
571  return std::equal_range(vector_.begin(), vector_.end(), value, compare_);
572 }
573 
577 template<class Key, class Compare, class Alloc>
578 inline std::pair<typename RandomAccessSet<Key,Compare,Alloc>::iterator,typename RandomAccessSet<Key,Compare,Alloc>::iterator>
580 (
581  const typename RandomAccessSet<Key,Compare,Alloc>::key_type& value
582 )
583 {
584  return std::equal_range(vector_.begin(), vector_.end(), value, compare_);
585 }
588 template<class Key, class Compare, class Alloc>
589 inline typename RandomAccessSet<Key,Compare,Alloc>::allocator_type
591 {
592  return vector_.get_allocator();
593 }
594 
595 } // namespace opengm
596 
597 #endif // OPENGM_RANDOM_ACCESS_SET_HXX
const_iterator upper_bound(const key_type &) const
size_type count(const key_type &) const
count elements
The OpenGM namespace.
Definition: config.hxx:43
const value_type & operator[](const size_type) const
const access values
const_iterator rend() const
reverse const end iterator
allocator_type get_allocator() const
allocators
Compare key_compare
comperator
Alloc allocator_type
acclocator
const_iterator rbegin() const
reverse const begin iterator
const_iterator find(const key_type &) const
void swap(RandomAccessSet &)
swap random access sets
VectorType::const_iterator const_iterator
const iterator type
bool empty() const
query if the set is empty
key_compare key_comp() const
key comparator
const_iterator begin() const
const begin iterator
Key ValueType
value type of the set
value_compare value_comp() const
value comparator
const_iterator end() const
const end iterator
VectorType::difference_type difference_type
difference type
std::pair< const_iterator, bool > insert(const value_type &)
RandomAccessSet & operator=(const RandomAccessSet &)
assignment operator
void reserve(const size_t size)
Key key_type
key type of the set
std::pair< const_iterator, const_iterator > equal_range(const key_type &) const
Key value_type
value type of the set
void erase(iterator position)
VectorType::iterator iterator
iterator type
set with O(n) insert and O(1) access
void assignFromSet(const SET &set)
VectorType::size_type size_type
size type
Alloc::const_reference const_reference
const reference type
VectorType::const_pointer const_pointer
const pointer type
RandomAccessSet(const size_t, const Compare &compare=Compare(), const Alloc &alloc=Alloc())
constructor
const_iterator lower_bound(const key_type &) const
size_type size() const
number of elements of the set
VectorType::const_reverse_iterator const_reverse_iterator
const reverse iterator
void clear()
clear the set
Compare value_compare
value comperator
size_type max_size() const
maximum size of the underlying container