00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __UT_Algorithm__
00022 #define __UT_Algorithm__
00023
00024 #include <SYS/SYS_Types.h>
00025
00026 #define UT_SWAPPER_SIMPLE(T) \
00027 inline void UTswap(T &a, T &b) { T tmp = a; a = b; b = tmp; }
00028 #define UT_SWAPPER_CLASS(T) \
00029 inline void UTswap(T &a, T &b) { a.swap(b); }
00030 #define UT_SWAPPER_TEMPLATE(T) \
00031 template< typename TA > \
00032 inline void UTswap(T<TA> &a, T<TA> &b) { a.swap(b); }
00033
00034 template< typename T >
00035 inline void
00036 UTswap(T *&a, T *&b)
00037 {
00038 T *tmp = a;
00039
00040 a = b;
00041 b = tmp;
00042 }
00043
00044 UT_SWAPPER_SIMPLE(bool);
00045 UT_SWAPPER_SIMPLE(unsigned char);
00046 UT_SWAPPER_SIMPLE(char);
00047 UT_SWAPPER_SIMPLE(uint16);
00048 UT_SWAPPER_SIMPLE(int16);
00049 UT_SWAPPER_SIMPLE(uint32);
00050 UT_SWAPPER_SIMPLE(int32);
00051 UT_SWAPPER_SIMPLE(uint64);
00052 UT_SWAPPER_SIMPLE(int64);
00053 UT_SWAPPER_SIMPLE(fpreal32);
00054 UT_SWAPPER_SIMPLE(fpreal64);
00055 #ifdef MBSD
00056 UT_SWAPPER_SIMPLE(size_t);
00057 #endif
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 template <typename T>
00082 class UT_CycleDetect
00083 {
00084 public:
00085 UT_CycleDetect()
00086 {
00087 reset();
00088 }
00089
00090 void reset()
00091 {
00092 myCount = 2;
00093 myCycleDetected = false;
00094 }
00095
00096 bool detect(const T &tail)
00097 {
00098 if (myCycleDetected)
00099 return true;
00100
00101
00102
00103
00104
00105 if ((myCount & (myCount - 1)) == 0)
00106 {
00107 myHead = tail;
00108 }
00109 else if (myHead == tail)
00110 {
00111 myCycleDetected = true;
00112 return true;
00113 }
00114
00115 myCount++;
00116 return false;
00117 }
00118
00119 private:
00120 T myHead;
00121 int64 myCount;
00122 bool myCycleDetected;
00123 };
00124
00125 #endif
00126