00001
00008 #ifndef __GENERIC_STDLIB_H__
00009 #define __GENERIC_STDLIB_H__
00010
00011 #include <map>
00012 #include <utility>
00013 #include <functional>
00014 #include <string>
00015 using std::string;
00016 #include <vector>
00017 #include <complex>
00018
00019 #include "genericalgs.h"
00020
00021
00022 namespace Generic {
00023
00025 template<class key_type, class data_type>
00026 void delete_contents(std::map<key_type, data_type*>& m)
00027 {
00028 typedef typename std::map<key_type, data_type*>::iterator iterator;
00029 for (iterator i=m.begin(); i!=m.end(); i++) {
00030 delete (*i).second;
00031 m.erase(i);
00032 }
00033 }
00034
00035
00037 template <class T1, class T2>
00038 inline std::pair<const T1, T2> make_pair_leftconst(const T1& x, const T2& y) {
00039 return std::pair<const T1, T2>(x, y);
00040 }
00041
00042
00047 template <class Arg, class Result>
00048 struct unary_virtual_function : public std::unary_function<Arg, Result> {
00049 virtual ~unary_virtual_function() { }
00051 virtual Result operator() ( Arg ) =0;
00052 };
00053
00054
00055
00060 template <class T, class N=string>
00061 struct name_match : public std::unary_function<T*,bool> {
00062 name_match(const N& name_i) : name(name_i) { }
00064 bool operator() ( T* a) { return name == a->name(); }
00065 private:
00066 const N& name;
00067 };
00068
00069
00070
00073 template <class T>
00074 struct max_fnobj : public std::binary_function<T, T, T> {
00075 T operator()(const T& x, const T& y) const { return std::max(x,y); }
00076 };
00077
00078
00079
00087 template <class T, class C, class N>
00088 void insert_named(C& container, const N& name, T* objptr)
00089 {
00090 typename C::iterator i =
00091 std::find_if(container.begin(),container.end(),name_match<T>(name));
00092 if (i==container.end())
00093 container.push_back(objptr);
00094 else {
00095 delete (*i);
00096 (*i) = objptr;
00097 }
00098 }
00099
00100
00108 template <class T, class C, class N>
00109 T* find_named(C& container, const N& name)
00110 {
00111 typename C::iterator i =
00112 std::find_if(container.begin(),container.end(),name_match<T>(name));
00113 return (i==container.end() ? 0 : (*i));
00114 }
00115
00116
00117
00122 template <class T, class C, class N>
00123 const T* find_named_const(const C& container, const N& name)
00124 {
00125 typename C::const_iterator i =
00126 std::find_if(container.begin(),container.end(),name_match<T>(name));
00127 return (i==container.end() ? 0 : (*i));
00128 }
00129
00130
00131
00133 template <class T> inline T sum(const std::vector<T>& A)
00134 { return std::accumulate(A.begin(),A.end(),T(0)); }
00135
00136
00137
00141 template<class T>
00142 struct Polar : public std::binary_function<T,T,std::complex<T> > {
00143 inline std::complex<T> operator() (const T& rho,const T& theta) const
00144
00145
00146 #if (__GNUC__ && (__GNUC__ < 3))
00147 { return polar<T>(rho,theta); }
00148 #else
00149 { return std::polar<T>(rho,theta); }
00150 #endif
00151
00152 };
00153
00154
00155 }
00156 #endif