OpenGM  2.3.x
Discrete Graphical Model Library
sorting.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_SORTING_HXX
3 #define OPENGM_SORTING_HXX
4 
6 
8 
9 namespace opengm {
10 
11 template<class ITERATOR,class TYPE_TO_FIND, class INDEXTYPE>
12 inline bool
13 findInSortedSequence
14 (
15  ITERATOR sequenceBegin,
16  const INDEXTYPE sequenceSize,
17  const TYPE_TO_FIND & toFind,
18  INDEXTYPE & position
19 ) {
20  if(toFind>static_cast<TYPE_TO_FIND>(sequenceBegin[sequenceSize-1]))
21  return false;
22  for(INDEXTYPE p=0;p<sequenceSize;++p) {
23  if(toFind<static_cast<TYPE_TO_FIND>(sequenceBegin[p]))
24  return false;
25  else if(toFind==static_cast<TYPE_TO_FIND>(sequenceBegin[p])) {
26  position=p;
27  return true;
28  }
29  }
30  return false;
31 }
32 
33 template<class Iterator>
34 inline bool
35 isSorted(Iterator begin, Iterator end) {
36  typedef typename std::iterator_traits<Iterator>::value_type ValueType;
37  if(std::distance(begin, end) > 1) {
38  ValueType tmp = static_cast<ValueType>(*begin);
39  while(begin != end) {
40  if(*begin < tmp) {
41  return false;
42  }
43  tmp = static_cast<ValueType>(*begin);
44  ++begin;
45  }
46  }
47  return true;
48 }
49 
50 template<class Iterator, class IteratorTag>
51 struct IteratorAt
52 {
53  inline typename std::iterator_traits<Iterator>::value_type operator()(Iterator iter, const size_t i) const {
54  iter += i;
55  return *iter;
56  }
57 };
58 
59 template<class Iterator>
60 struct IteratorAt<std::random_access_iterator_tag, Iterator>
61 {
62  inline typename std::iterator_traits<Iterator>::value_type operator()(Iterator iter, const size_t i) const {
63  return iter[i];
64  }
65 };
66 
67 template<class Iterator>
68 typename std::iterator_traits<Iterator>::value_type
69  iteratorAt(Iterator iterator, const size_t i) {
70  IteratorAt<Iterator, typename std::iterator_traits<Iterator>::iterator_category> iat;
71  return iat(iterator, i);
72 }
73 
74 template<class T>
75 struct sortPairByFirst {
76  bool operator()(const T & a, const T & b)
77  { return a.first < b.first; }
78 };
79 
80 template<class Iterator_A, class Iterator_B>
81 inline void
82 doubleSort(Iterator_A beginA, Iterator_A endA, Iterator_B beginB) {
83  typedef typename std::iterator_traits<Iterator_A>::value_type ValueType_a;
84  typedef typename std::iterator_traits<Iterator_B>::value_type ValueType_b;
85  typedef std::pair< ValueType_a, ValueType_b> PairType;
86  opengm::FastSequence< PairType > pairvector(std::distance(beginA, endA));
87  Iterator_A beginA_ = beginA;
88  Iterator_B beginB_ = beginB;
89  size_t counter = 0;
90  while(beginA_ != endA) {
91  pairvector[counter].first = *beginA_;
92  pairvector[counter].second = *beginB_;
93  ++beginA_;
94  ++beginB_;
95  ++counter;
96  }
97  sortPairByFirst<PairType > sortfunctor;
98  std::sort(pairvector.begin(), pairvector.end(), sortfunctor);
99  counter = 0;
100  while(beginA != endA) {
101  *beginA = pairvector[counter].first;
102  *beginB = pairvector[counter].second;
103  ++beginA;
104  ++beginB;
105  ++counter;
106  }
107 }
108 
109 } // namespace opengm
110 
112 
113 #endif // #ifndef OPENGM_SORTING_HXX
114 
The OpenGM namespace.
Definition: config.hxx:43
Vector that stores values on the stack if size is smaller than MAX_STACK.
T const * end() const
end iterator
T const * begin() const
begin iterator