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 #include <vector>
00016
00017 #include "genericalgs.h"
00018
00019
00020 namespace Generic {
00021
00023 template<class key_type, class data_type>
00024 void delete_contents(std::map<key_type, data_type*>& m)
00025 {
00026 typedef typename std::map<key_type, data_type*>::iterator iterator;
00027 for (iterator i=m.begin(); i!=m.end(); i++) {
00028 delete (*i).second;
00029 m.erase(i);
00030 }
00031 }
00032
00033
00035 template <class T1, class T2>
00036 inline pair<const T1, T2> make_pair_leftconst(const T1& x, const T2& y) {
00037 return pair<const T1, T2>(x, y);
00038 }
00039
00040
00045 template <class Arg, class Result>
00046 struct unary_virtual_function : public unary_function<Arg, Result> {
00047 virtual ~unary_virtual_function() { }
00049 virtual Result operator() ( Arg ) =0;
00050 };
00051
00052
00053
00058 template <class T, class N=string>
00059 struct name_match : public unary_function<T*,bool> {
00060 name_match(const N& name_i) : name(name_i) { }
00062 bool operator() ( T* a) { return name == a->name(); }
00063 private:
00064 const N& name;
00065 };
00066
00067
00068
00076 template <class T, class C, class N>
00077 void insert_named(C& container, const N& name, T* objptr)
00078 {
00079 typename C::iterator i =
00080 std::find_if(container.begin(),container.end(),name_match<T>(name));
00081 if (i==container.end())
00082 container.push_back(objptr);
00083 else {
00084 delete (*i);
00085 (*i) = objptr;
00086 }
00087 }
00088
00089
00094 template <class T, class C, class N>
00095 const T* find_named(const C& container, const N& name)
00096 {
00097 typename C::const_iterator i =
00098 std::find_if(container.begin(),container.end(),name_match<T>(name));
00099 return (i==container.end() ? 0 : (*i));
00100 }
00101
00102
00103
00105 template <class T> inline T sum(const vector<T>& A)
00106 { return accumulate(A.begin(),A.end(),T(0)); }
00107
00108
00109 }
00110 #endif