00001
00015 #ifndef _XSTL_H
00016 #define _XSTL_H
00017
00018 #include <functional>
00019 #include <Vector.H>
00020
00021 using namespace std;
00022
00023 namespace xchen
00024 {
00025
00026 typedef vector<int> intvector;
00027 typedef vector<char> charvector;
00028 typedef vector<double> doublevector;
00029
00030 typedef set<int, less<int> > intset;
00031 typedef set<char, less<char> > charset;
00032 typedef set<double, less<double> > doubleset;
00033
00034
00035 template<class T>
00036 inline ostream& print(T const& v, ostream& os = cout, char* delimiter = " ")
00037 {
00038 copy(v.begin(), v.end(), ostream_iterator<T>(os, delimiter));
00039 return os;
00040 }
00041
00042 inline char* cpystr(const char* from)
00043 {
00044 char* to = new char[strlen(from)+1];
00045 int i = 0;
00046 while( to[i]=from[i] ) i++;
00047 return to;
00048 }
00049
00051 template<class T>
00052 inline typename vector<T>::iterator findInVector(const T& e, vector<T>& v)
00053 {
00054 typename vector<T>::iterator itr = v.begin();
00055 for(; itr != v.end(); itr++)
00056 {
00057 if( (*itr) == e ) break;
00058 }
00059 return itr;
00060 }
00061
00063 template<class T>
00064 inline typename vector<T>::const_iterator findInVector(const T& e, const vector<T>& v)
00065 {
00066 typename vector<T>::const_iterator itr = v.begin();
00067 for(; itr != v.end(); itr++)
00068 if( (*itr) == e )
00069 break;
00070 return itr;
00071 }
00072
00074 template<class T>
00075 inline int eraseInVector(vector<T>& v, const T& e)
00076 {
00077 typename vector<T>::iterator itr = v.begin();
00078 for(; itr != v.end() && (*itr) != e; itr++);
00079
00080 if( itr != v.end() ) { v.erase(itr); return 1; }
00081
00082 return 0;
00083 }
00084
00085
00086
00088 template<typename T>
00089 class iota
00090 {
00091 T val;
00092 public:
00093 iota(T const& v) : val(v) { }
00094 T operator()() { return val++; }
00095 };
00096
00098 template<typename T>
00099 struct AffineCombineT1T2 : public binary_function<T, T const&, T const&>
00100 {
00101 AffineCombineT1T2(double t_1, double t_2) : t1(t_1), t2(t_2) { }
00102 T operator()(T const& p1, T const& p2) { return (p1 * t1 + p2 * t2) / (t1 + t2); }
00103 double t1, t2;
00104 };
00106 template<typename T>
00107 struct AffineCombine : public binary_function<T, T const&, T const&>
00108 {
00109 AffineCombine(double t_) : t(t_) { }
00110 T operator()(T const& p1, T const& p2) { return p1 * (1-t) + p2 * t; }
00111 double t;
00112 };
00113
00115 template<typename T>
00116 struct Average : public binary_function<T, T const&, T const&>
00117 {
00118 T operator()(T const& p1, T const& p2) { return (p1 + p2) / 2.0; }
00119 };
00120
00121
00122
00124 struct cross_product : public binary_function<dVector3D const&, dVector3D const&, dVector3D>
00125 {
00126 dVector3D operator()(dVector3D const& x, dVector3D const& y)
00127 {
00128 return x ^ y;
00129 }
00130 };
00131
00132
00134 template<typename T>
00135 struct ScaleAll : public unary_function<T const&, T>
00136 {
00137 ScaleAll(double s_) : s(s_) { }
00138 T operator()(T const& e) { return s * e; }
00139 double s;
00140 };
00141
00143 template<typename T>
00144 struct Scale1stAdd : public binary_function<T const&, T const&, T>
00145 {
00146 Scale1stAdd(double s_) : s(s_) { }
00147 T operator()(T const& e1, T const& e2) { return s * e1 + e2; }
00148 double s;
00149 };
00150
00151
00152
00153
00154
00155 }
00156
00157
00158 #endif