Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

generic_algo.h

Go to the documentation of this file.
00001 #ifndef GENERIC_ALGO_H
00002 #define GENERIC_ALGO_H
00003 
00004 namespace rs {
00005 
00008     namespace gen_algo {
00009 
00011         template<class T> inline void swap(T& a, T& b) { T temp = a; a = b; b = temp; }
00012 
00014         template <class T> T min(const T& t1, const T& t2) { return t1<t2 ? t1 : t2; }
00016         template <class T> T max(const T& t1, const T& t2) { return t2<t1 ? t1 : t2; }
00017 
00019         namespace internal {
00020             template<class T, int I, int J> struct Swap {
00021                 static inline void compareAndSwap(T* array) {
00022                     if (array[I] > array[J]) swap<T>(array[I], array[J]);
00023                 }
00024             };
00025 
00026             template<class T, int I, int J> struct Loop {
00027                 static const bool go = (J <= I-2);
00028                 static inline void loop(T* array) {
00029                     Swap<T,J,J+1>::compareAndSwap(array);
00030                     Loop<T, (go ? I : 0), (go ? (J+1) : 0)>::loop(array);
00031                 }
00032             };
00033             template<class T> struct Loop<T,0,0> { static inline void loop(T*) {} };
00034         };
00036 
00039         template<class T, int N> struct BubbleSort {
00040             static inline void sort(T* array) {
00041                 internal::Loop<T, N-1, 0>::loop(array);
00042                 BubbleSort<T, N-1>::sort(array);
00043             }
00044         };
00045 
00047         template<class T> struct BubbleSort<T, 1> { static inline void sort(T*) {} };
00048 
00049     };
00050 
00051 };
00052 
00053 #else
00054 #error generic_algo.h included twice
00055 #endif // GENERIC_ALGO_H

Contact me in case of errors or questions.
This documentation is powered by Doxygen.
(C) 2000-2002 Doxygen