00001 #ifndef _TemplateUtil_
00002 #define _TemplateUtil_
00003
00004 #include <functional>
00005 #include <iterator>
00006 #include <iostream>
00007
00093 namespace TemplateUtil
00094 {
00095
00098
00099
00100
00101
00102
00103
00104
00105 template <class Predicate>
00106 struct binary_deref : public binary_function<
00107 typename Predicate::first_argument_type *,
00108 typename Predicate::second_argument_type *, bool>
00109 {
00110 typedef typename binary_function<typename Predicate::first_argument_type *,
00111 typename Predicate::second_argument_type *,
00112 bool>::first_argument_type first_argument_type;
00113 typedef typename binary_function<typename Predicate::first_argument_type *,
00114 typename Predicate::second_argument_type *,
00115 bool>::second_argument_type second_argument_type;
00116 typedef typename binary_function<typename Predicate::first_argument_type *,
00117 typename Predicate::second_argument_type *,
00118 bool>::result_type result_type;
00119
00120 explicit binary_deref(const Predicate& p)
00121 {
00122 myPred = p;
00123 }
00124 bool operator() (const first_argument_type &x,
00125 const second_argument_type &y) const
00126 {
00127 return myPred(*x, *y);
00128 }
00129
00130
00131 Predicate myPred;
00132 };
00133
00145 template <class Predicate>
00146 struct unary_deref : public unary_function<typename Predicate::argument_type*,
00147 bool>
00148 {
00149 typedef typename unary_function<typename Predicate::argument_type *,
00150 bool>::argument_type argument_type;
00151 typedef typename unary_function<typename Predicate::argument_type *,
00152 bool>::result_type result_type;
00153
00154 explicit unary_deref(const Predicate& p)
00155 {
00156 myPred = p;
00157 }
00158 bool operator()(const argument_type &arg) const
00159 {
00160 return myPred(*arg);
00161 }
00162 Predicate myPred;
00163 };
00164
00165 template <class Predicate>
00166 unary_deref<Predicate> deref1(const Predicate& p)
00167 {
00168 return unary_deref<Predicate>(p);
00169 }
00170 template <class Predicate>
00171 binary_deref<Predicate> deref2(const Predicate& p)
00172 {
00173 return binary_deref<Predicate>(p);
00174 }
00176
00179
00180 template <typename Return, typename T, typename Predicate>
00181 struct const_comp_mem_fun_ref_t : public binary_function<T, T, bool>
00182 {
00183 Return (T::*memberFunc) () const;
00184 Predicate myPred;
00185
00186 explicit const_comp_mem_fun_ref_t(Return (T::*func) () const, Predicate p) :
00187 memberFunc(func), myPred(p)
00188 {}
00189 bool operator() (const T& argl, const T& arg2)
00190 {
00191 return myPred((argl.*memberFunc) (), (arg2.*memberFunc) ());
00192 }
00193 };
00194
00200 template <typename Return, typename T, typename Pred>
00201 inline const_comp_mem_fun_ref_t<Return,T,Pred>
00202 const_comp_mem_fun_ref(Return (T::*f) () const, Pred p)
00203 {
00204 return const_comp_mem_fun_ref_t<Return,T,Pred>(f, p);
00205 }
00206
00207 template <typename Return, typename T, typename Predicate>
00208 struct const_comp_mem_fun_t : public binary_function<T*, T*, bool>
00209 {
00210 Return (T::*memberFunc) () const;
00211 Predicate myPred;
00212
00213 explicit const_comp_mem_fun_t(Return (T::*func) () const, Predicate p) :
00214 memberFunc(func), myPred(p)
00215 {}
00216 bool operator() (const T* arg1, const T* arg2)
00217 {
00218 return myPred((arg1->*memberFunc) (), (arg2->*memberFunc) ());
00219 }
00220 };
00221
00227 template <typename Return, typename T, typename Pred>
00228 inline const_comp_mem_fun_t<Return,T,Pred>
00229 const_comp_mem_fun(Return (T::*f) () const, Pred p)
00230 {
00231 return const_comp_mem_fun_t<Return,T,Pred>(f, p);
00232 }
00233
00234 template <typename Return, typename T, typename Predicate>
00235 struct comp_mem_fun_ref_t : public binary_function<T, T, bool>
00236 {
00237 Return (T::*memberFunc) ();
00238 Predicate myPred;
00239
00240 explicit comp_mem_fun_ref_t(Return (T::*func) (), Predicate p) :
00241 memberFunc(func), myPred(p)
00242 {}
00243 bool operator() (const T& argl, const T& arg2)
00244 {
00245 return myPred((argl.*memberFunc) (), (arg2.*memberFunc) ());
00246 }
00247 };
00248
00254 template <typename Return, typename T, typename Pred>
00255 inline comp_mem_fun_ref_t<Return,T,Pred> comp_mem_fun_ref(Return (T::*f) (),
00256 Pred p)
00257 {
00258 return comp_mem_fun_ref_t<Return,T,Pred>(f, p);
00259 }
00260
00261 template <typename Return, typename T, typename Predicate>
00262 struct comp_mem_fun_t : public binary_function<T*, T*, bool>
00263 {
00264 Return (T::*memberFunc) ();
00265 Predicate myPred;
00266
00267 explicit comp_mem_fun_t(Return (T::*func) (), Predicate p) :
00268 memberFunc(func), myPred(p)
00269 {}
00270 bool operator() (const T* argl, const T* arg2)
00271 {
00272 return myPred((argl->*memberFunc) (), (arg2->*memberFunc) ());
00273 }
00274 };
00275
00280 template <typename Return, typename T, typename Pred>
00281 inline comp_mem_fun_t<Return,T,Pred> comp_mem_fun(Return (T::*f) () , Pred p)
00282 {
00283 return comp_mem_fun_t<Return,T, Pred>(f, p);
00284 }
00286
00289
00290
00291
00292
00293 template <typename T>
00294 struct PtrLess : public binary_function<T*,T*,bool>
00295 {
00296 bool operator() (const T* left, const T* right) const
00297 {
00298 return (*left < *right);
00299 }
00300 };
00305 template <typename T>
00306 struct PtrGreater : public binary_function<T*,T*,bool>
00307 {
00308 bool operator() (const T* left, const T* right) const
00309 {
00310 return (*right < *left);
00311 }
00312 };
00313
00318 template <typename T>
00319 struct PtrLessEq : public binary_function<T*,T*,bool>
00320 {
00321 bool operator() (const T* left, const T* right) const
00322 {
00323 return (*left < *right) || (*left == *right);
00324 }
00325 };
00326
00331 template <typename T>
00332 struct PtrGreaterEq: public binary_function<T*,T*,bool>
00333 {
00334 bool operator() (const T* left, const T* right) const
00335 {
00336 return !(*left < *right);
00337 }
00338 };
00339
00344 template <typename T>
00345 struct PtrEq: public binary_function<T*,T*,bool>
00346 {
00347 bool operator() (const T* left, const T* right) const
00348 {
00349 return (*left == *right);
00350 }
00351 };
00352
00357 template <typename T>
00358 struct PtrNotEq: public binary_function<T*,T*,bool>
00359 {
00360 bool operator() (const T* left, const T* right) const
00361 {
00362 return !(*left == *right);
00363 }
00364 };
00365
00371 template<typename Arg>
00372 struct AlwaysTrueComp : public unary_function<Arg, bool>
00373 {
00374 bool operator() (const Arg&) { return true; }
00375 };
00376
00378
00381
00382
00383
00384
00385
00386 template <typename T>
00387 struct DeleteObj : public unary_function<T, void>
00388 {
00389 void operator() (T ptr) const
00390 {
00391 delete ptr;
00392 }
00393 };
00394
00399 template<typename Base, typename Arg>
00400 struct InheritedFrom : public unary_function<Arg*, bool>
00401 {
00402 bool operator() (const Arg* a)
00403 {
00404 return (dynamic_cast<const Base*>(a)!=0);
00405 }
00406 };
00408
00411
00412
00413
00414
00415
00416
00417 template <typename C, typename Function>
00418 void for_all(C &collection, Function f)
00419 {
00420 for_each(collection.begin(), collection.end(), f);
00421 }
00422
00430 template <typename T, typename Comparator>
00431 void erase_if(T &collection, const Comparator &c)
00432 {
00433 collection.erase(remove_if(collection.begin(), collection.end(), c),
00434 collection.end());
00435 }
00436
00442 template <typename T, typename InputIterator, typename Comparator>
00443 void erase_if(T &collection, InputIterator begin,
00444 InputIterator end, const Comparator &c)
00445 {
00446 collection.erase(remove_if(begin, end, c), end);
00447 }
00448
00455 template <typename C>
00456 void delete_all(C &collection)
00457 {
00458 for_each(collection.begin(), collection.end(),
00459 DeleteObj<typename C::value_type>());
00460 collection.erase(collection.begin(), collection.end());
00461 }
00462
00470 template <typename C>
00471 void delete_all_second(C &collection)
00472 {
00473 for( typename C::iterator i = collection.begin(); i != collection.end(); ++i)
00474 delete i->second;
00475 collection.erase(collection.begin(), collection.end());
00476 }
00477
00484 template <typename C>
00485 void print_col(C &collection, ostream &os = cout, const char *d = "\n")
00486 {
00487 copy(collection.begin(), collection.end(),
00488 ostream_iterator<typename C::value_type>(os,d));
00489 }
00490
00495 template <typename C>
00496 void print_ptr_col(C &collection, ostream &os = cout, const char *d = "\n")
00497 {
00498 for ( typename C::const_iterator i = collection.begin();
00499 i!=collection.end(); ++i)
00500 os<<**i<<d;
00501 }
00502
00509 template <typename C>
00510 void merge_ptr_col(C &dest, const C&source)
00511 {
00512 for ( typename C::const_iterator i = source.begin(); i!=source.end(); ++i)
00513 dest.push_back((*i)->clone());
00514 }
00515
00521 template <typename InputIterator, typename T>
00522 InputIterator find_second( InputIterator first, InputIterator last,
00523 const T& v)
00524 {
00525 while( first != last && !(first->second == v))
00526 ++first;
00527 return first;
00528 }
00529
00535 template <typename InputIterator, typename Type, typename Predicate>
00536 InputIterator find_second_if( InputIterator first, InputIterator last,
00537 const Predicate &c)
00538 {
00539 while( first != last && !c(first->second))
00540 ++first;
00541 return first;
00542 }
00543
00550 template <typename InputIterator, typename T>
00551 InputIterator find_first( InputIterator first, InputIterator last,
00552 const T& v)
00553 {
00554 while( first != last && !(first->first == v))
00555 ++first;
00556 return first;
00557 }
00558
00564 template <typename InputIterator, typename Type, typename Predicate>
00565 InputIterator find_first_if( InputIterator first, InputIterator last,
00566 const Predicate &c)
00567 {
00568 while( first != last && !c(first->first))
00569 ++first;
00570 return first;
00571 }
00572
00583 template <typename InputIterator, typename OutputIterator, typename Predicate>
00584 OutputIterator copy_if( InputIterator first,
00585 InputIterator last,
00586 OutputIterator dest,
00587 Predicate pred)
00588 {
00589 while( first != last)
00590 {
00591 if( pred( *first)) *dest++ = *first;
00592 ++first;
00593 }
00594 return dest;
00595 }
00596
00605 template <typename InputIterator, typename OutputIterator, typename Predicate>
00606 OutputIterator copy_second_if( InputIterator first,
00607 InputIterator last,
00608 OutputIterator dest,
00609 Predicate pred)
00610 {
00611 while( first != last)
00612 {
00613 if( pred(first->second)) *dest++ = first->second;
00614 ++first;
00615 }
00616 return dest;
00617 }
00619
00624 template <typename type>
00625 const type& max(const type &a, const type &b)
00626 {
00627 return b<a? a:b;
00628 }
00629
00634 template <typename type>
00635 const type& min(const type &a, const type &b)
00636 {
00637 return b<a? b:a;
00638 }
00639
00640 }
00641
00642 #endif