sorting.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_SORTING_HXX
00003 #define OPENGM_SORTING_HXX
00004 
00006 
00007 #include "opengm/datastructures/fast_sequence.hxx"
00008 
00009 namespace opengm {
00010    
00011 template<class ITERATOR,class TYPE_TO_FIND, class INDEXTYPE>
00012 inline bool 
00013 findInSortedSequence
00014 (
00015    ITERATOR sequenceBegin,
00016    const INDEXTYPE sequenceSize,
00017    const TYPE_TO_FIND & toFind,
00018    INDEXTYPE & position
00019 ) {
00020    if(toFind>static_cast<TYPE_TO_FIND>(sequenceBegin[sequenceSize-1]))
00021       return false;
00022    for(INDEXTYPE p=0;p<sequenceSize;++p) {
00023       if(toFind<static_cast<TYPE_TO_FIND>(sequenceBegin[p]))
00024          return false;
00025       else if(toFind==static_cast<TYPE_TO_FIND>(sequenceBegin[p])) {
00026          position=p;
00027          return true;
00028       }
00029    }
00030    return false;
00031 }
00032    
00033 template<class Iterator>
00034 inline bool
00035 isSorted(Iterator begin, Iterator end) {
00036    typedef typename std::iterator_traits<Iterator>::value_type ValueType;
00037    if(std::distance(begin, end) > 1) {
00038       ValueType tmp = static_cast<ValueType>(*begin);
00039       while(begin != end) {
00040          if(*begin < tmp) {
00041             return false;
00042          }
00043          tmp = static_cast<ValueType>(*begin);
00044          ++begin;
00045       }
00046    }
00047    return true;
00048 }
00049 
00050 template<class Iterator, class IteratorTag>
00051 struct IteratorAt
00052 {
00053    inline typename std::iterator_traits<Iterator>::value_type operator()(Iterator iter, const size_t i) const {
00054       iter += i;
00055       return *iter;
00056    }
00057 };
00058 
00059 template<class Iterator>
00060 struct IteratorAt<std::random_access_iterator_tag, Iterator>
00061 {
00062    inline typename std::iterator_traits<Iterator>::value_type operator()(Iterator iter, const size_t i) const {
00063       return iter[i];
00064    }
00065 };
00066 
00067 template<class Iterator>
00068 typename std::iterator_traits<Iterator>::value_type
00069    iteratorAt(Iterator iterator, const size_t i) {
00070       IteratorAt<Iterator, typename std::iterator_traits<Iterator>::iterator_category> iat;
00071       return iat(iterator, i);
00072 }
00073 
00074 template<class T>
00075 struct sortPairByFirst {
00076    bool operator()(const T & a, const T & b)
00077       { return a.first < b.first; }
00078 };
00079 
00080 template<class Iterator_A, class Iterator_B>
00081 inline void
00082 doubleSort(Iterator_A beginA, Iterator_A endA, Iterator_B beginB) {
00083    typedef typename std::iterator_traits<Iterator_A>::value_type ValueType_a;
00084    typedef typename std::iterator_traits<Iterator_B>::value_type ValueType_b;
00085    typedef std::pair< ValueType_a, ValueType_b> PairType;
00086    opengm::FastSequence< PairType > pairvector(std::distance(beginA, endA));
00087    Iterator_A beginA_ = beginA;
00088    Iterator_B beginB_ = beginB;
00089    size_t counter = 0;
00090    while(beginA_ != endA) {
00091       pairvector[counter].first = *beginA_;
00092       pairvector[counter].second = *beginB_;
00093       ++beginA_;
00094       ++beginB_;
00095       ++counter;
00096    }
00097    sortPairByFirst<PairType > sortfunctor;
00098    std::sort(pairvector.begin(), pairvector.end(), sortfunctor);
00099    counter = 0;
00100    while(beginA != endA) {
00101       *beginA = pairvector[counter].first;
00102       *beginB = pairvector[counter].second;
00103       ++beginA;
00104       ++beginB;
00105       ++counter;
00106    }
00107 }
00108 
00109 } // namespace opengm
00110 
00112 
00113 #endif // #ifndef OPENGM_SORTING_HXX
00114 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:06 2013 for OpenGM by  doxygen 1.6.3