QAlgorithmsPrivate Namespace Reference


Functions

template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE void qSortHelper (RandomAccessIterator start, RandomAccessIterator end, const T &t, LessThan lessThan)
template<typename RandomAccessIterator, typename T>
void qSortHelper (RandomAccessIterator begin, RandomAccessIterator end, const T &dummy)
template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE void qStableSortHelper (RandomAccessIterator start, RandomAccessIterator end, const T &t, LessThan lessThan)
template<typename RandomAccessIterator, typename T>
void qStableSortHelper (RandomAccessIterator, RandomAccessIterator, const T &)
template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qLowerBoundHelper (RandomAccessIterator begin, RandomAccessIterator end, const T &value, LessThan lessThan)
template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qUpperBoundHelper (RandomAccessIterator begin, RandomAccessIterator end, const T &value, LessThan lessThan)
template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qBinaryFindHelper (RandomAccessIterator begin, RandomAccessIterator end, const T &value, LessThan lessThan)


Function Documentation

template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE RandomAccessIterator QAlgorithmsPrivate::qBinaryFindHelper ( RandomAccessIterator  begin,
RandomAccessIterator  end,
const T &  value,
LessThan  lessThan 
)

Definition at line 486 of file qalgorithms.h.

References i, and l.

Referenced by qBinaryFind().

00487 {
00488     int l = 0;
00489     int r = end - begin - 1;
00490     if (r < 0)
00491         return end;
00492     int i = (l + r + 1) / 2;
00493 
00494     while (r != l) {
00495         if (lessThan(value, begin[i]))
00496             r = i - 1;
00497         else
00498             l = i;
00499         i = (l + r + 1) / 2;
00500     }
00501     if (lessThan(begin[i], value) || lessThan(value, begin[i]))
00502         return end;
00503     else
00504         return begin + i;
00505 }

template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE RandomAccessIterator QAlgorithmsPrivate::qLowerBoundHelper ( RandomAccessIterator  begin,
RandomAccessIterator  end,
const T &  value,
LessThan  lessThan 
)

Definition at line 445 of file qalgorithms.h.

References n.

Referenced by qLowerBound().

00446 {
00447     RandomAccessIterator middle;
00448     int n = end - begin;
00449     int half;
00450 
00451     while (n > 0) {
00452         half = n >> 1;
00453         middle = begin + half;
00454         if (lessThan(*middle, value)) {
00455             begin = middle + 1;
00456             n -= half + 1;
00457         } else {
00458             n = half;
00459         }
00460     }
00461     return begin;
00462 }

template<typename RandomAccessIterator, typename T>
void QAlgorithmsPrivate::qSortHelper ( RandomAccessIterator  begin,
RandomAccessIterator  end,
const T &  dummy 
) [inline]

Definition at line 399 of file qalgorithms.h.

References qSortHelper().

00400 {
00401     qSortHelper(begin, end, dummy, qLess<T>());
00402 }

Here is the call graph for this function:

template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE void QAlgorithmsPrivate::qSortHelper ( RandomAccessIterator  start,
RandomAccessIterator  end,
const T &  t,
LessThan  lessThan 
)

Definition at line 346 of file qalgorithms.h.

References qSwap().

Referenced by qSort(), and qSortHelper().

00347 {
00348 top:
00349     int span = end - start;
00350     if (span < 2)
00351         return;
00352 
00353     --end;
00354     RandomAccessIterator low = start, high = end - 1;
00355     RandomAccessIterator pivot = start + span / 2;
00356 
00357     if (lessThan(*end, *start))
00358         qSwap(*end, *start);
00359     if (span == 2)
00360         return;
00361 
00362     if (lessThan(*pivot, *start))
00363         qSwap(*pivot, *start);
00364     if (lessThan(*end, *pivot))
00365         qSwap(*end, *pivot);
00366     if (span == 3)
00367         return;
00368 
00369     qSwap(*pivot, *end);
00370 
00371     while (low < high) {
00372         while (low < high && lessThan(*low, *end))
00373             ++low;
00374 
00375         while (high > low && lessThan(*end, *high))
00376             --high;
00377 
00378         if (low < high) {
00379             qSwap(*low, *high);
00380             ++low;
00381             --high;
00382         } else {
00383             break;
00384         }
00385     }
00386 
00387     if (lessThan(*low, *end))
00388         ++low;
00389 
00390     qSwap(*end, *low);
00391     qSortHelper(start, low, t, lessThan);
00392 
00393     start = low + 1;
00394     ++end;
00395     goto top;
00396 }

Here is the call graph for this function:

template<typename RandomAccessIterator, typename T>
void QAlgorithmsPrivate::qStableSortHelper ( RandomAccessIterator  ,
RandomAccessIterator  ,
const T &   
) [inline]

Definition at line 439 of file qalgorithms.h.

References qStableSortHelper().

00440 {
00441     qStableSortHelper(begin, end, dummy, qLess<T>());
00442 }

Here is the call graph for this function:

template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE void QAlgorithmsPrivate::qStableSortHelper ( RandomAccessIterator  start,
RandomAccessIterator  end,
const T &  t,
LessThan  lessThan 
)

Definition at line 405 of file qalgorithms.h.

References i, T, and value.

Referenced by qStableSort(), and qStableSortHelper().

00406 {
00407     const int span = end - start;
00408     if (span < 2)
00409        return;
00410         
00411     // Split in half and sort halves.
00412     RandomAccessIterator middle = start + span / 2;
00413     qStableSortHelper(start, middle, t, lessThan);
00414     qStableSortHelper(middle, end, t, lessThan);
00415     
00416     // Merge
00417     RandomAccessIterator lo = start;
00418     RandomAccessIterator hi = middle;
00419     
00420     while (lo != middle && hi != end) {
00421         if (!lessThan(*hi, *lo)) {
00422             ++lo; // OK, *lo is in its correct position
00423         } else {
00424             // Move *hi to lo's position, shift values
00425             // between lo and hi - 1 one place up.
00426             T value = *hi;
00427             for (RandomAccessIterator i = hi; i != lo; --i) {
00428                 *i = *(i-1); 
00429             }
00430             *lo = value;
00431             ++hi;
00432             ++lo;
00433             ++middle;
00434         } 
00435     }
00436 }

template<typename RandomAccessIterator, typename T, typename LessThan>
Q_OUTOFLINE_TEMPLATE RandomAccessIterator QAlgorithmsPrivate::qUpperBoundHelper ( RandomAccessIterator  begin,
RandomAccessIterator  end,
const T &  value,
LessThan  lessThan 
)

Definition at line 466 of file qalgorithms.h.

References n.

Referenced by qUpperBound().

00467 {
00468     RandomAccessIterator middle;
00469     int n = end - begin;
00470     int half;
00471 
00472     while (n > 0) {
00473         half = n >> 1;
00474         middle = begin + half;
00475         if (lessThan(value, *middle)) {
00476             n = half;
00477         } else {
00478             begin = middle + 1;
00479             n -= half + 1;
00480         }
00481     }
00482     return begin;
00483 }


Generated on Thu Mar 15 20:20:43 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1