Generic.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #if !defined(_GENERIC_H)
00019 #define _GENERIC_H
00020
00021 #include <stdint.h>
00022
00023 #include <list>
00024 #include <iostream>
00025 #include <ostream>
00026 #include <utility>
00027 #include <vector>
00028
00029 template <typename T>
00030 inline T abs(T x)
00031 {
00032 return (x < 0) ? -x : x;
00033 }
00034
00035
00036
00037
00038 template <typename T>
00039 inline T absDiff(T x, T y)
00040 {
00041 return (x < y) ? (y - x) : (x - y);
00042 }
00043
00044
00045
00046 template <typename T>
00047 inline T in(T x, T y, T z)
00048 {
00049 return (x >= y && x < z);
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 template <typename A, typename B>
00064 std::ostream &operator << (std::ostream &stream, std::pair<A, B> p)
00065 {
00066 stream << "(" << p.first << ", " << p.second << ")";
00067 return stream;
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 template <typename T>
00079 std::ostream &operator << (std::ostream &stream, std::vector<T> v)
00080 {
00081
00082 typename std::vector<T>::iterator i;
00083 for (i = v.begin(); i != v.end(); i++)
00084 {
00085 stream << (i - v.begin()) << ": " << *i << std::endl;
00086 }
00087 return stream;
00088 }
00089
00090
00091
00092
00093 template <typename T>
00094 std::ostream &operator << (std::ostream &stream, std::list<T> l)
00095 {
00096
00097 typename std::list<T>::iterator i;
00098 int j = 0;
00099 for (i = l.begin(); i != l.end(); i++, j++)
00100 {
00101 stream << j << ": " << *i << std::endl;
00102 }
00103 return stream;
00104 }
00105
00106 template <typename TITLE, typename ITEM, typename EXPECT, typename GOT>
00107 void check(int &returnCode, TITLE title, ITEM item, EXPECT expect, GOT got)
00108 {
00109 if (expect!=got)
00110 {
00111 std::cout << "Test " << title << ": expect " << item << " = " << expect << ", but got " << got << "." << std::endl;
00112 returnCode = 1;
00113 }
00114 }
00115
00116
00117
00118
00119
00120 template<typename T>
00121 std::istream &operator >> (std::istream &stream, std::vector<T> &vec)
00122 {
00123 T val;
00124 while (true)
00125 {
00126 stream >> val;
00127 if (!stream.good()) break;
00128 vec.push_back(val);
00129 }
00130 return stream;
00131 }
00132
00133
00134 #if 0
00135
00136
00137
00138 template <typename T>
00139 std::ostream &operator << (
00140 std::ostream &stream,
00141 std::vector<
00142 std::pair< std::vector<typename T>::iterator , std::vector< typename T>::iterator >
00143 > v
00144 )
00145 {
00146
00147 typename IteratorType i;
00148 typename std::vector<T>::iterator i;
00149 for (i = v.begin(); i != v.end(); i++)
00150 {
00151 stream << *i << std::endl;
00152 }
00153 return stream;
00154 }
00155
00156 #endif
00157
00158 #endif