1 // set_tools.h 2 // e.moon 7may99 3 // 4 // PURPOSE 5 // Tools to manipulate STL set types. 6 // 7 // HISTORY 8 // e.moon 27jul99 moved into cortex namespace 9 // e.moon 7may99 created 10 11 #ifndef __SET_TOOLS_H__ 12 #define __SET_TOOLS_H__ 13 14 #include "cortex_defs.h" 15 __BEGIN_CORTEX_NAMESPACE 16 17 // delete range of pointer values from set 18 template<class iter> 19 void ptr_set_delete(iter begin, iter end) { 20 while(begin != end) { 21 if(*begin) 22 delete *begin; 23 ++begin; 24 } 25 } 26 27 // delete range of pointer values from map 28 template<class iter> 29 void ptr_map_delete(iter begin, iter end) { 30 while(begin != end) { 31 if((*begin).second) 32 delete (*begin).second; 33 ++begin; 34 } 35 } 36 37 // a simple equality-test functor for maps 38 template<class key, class value> 39 class map_value_equal_to : 40 public std::binary_function<std::pair<key,value>, value, bool> { 41 42 public: 43 bool operator()(const std::pair<key,value>& p, const value& v) const { 44 return p.second == v; 45 } 46 }; 47 48 //// a predicate functor adaptor for maps 49 //// e.moon 28jul99 50 //template<class key, class value> 51 //class map_value_predicate_t : 52 // public unary_function<pair<key,value>, bool> { 53 // 54 // const unary_function<const value, bool>& fn; 55 // 56 //public: 57 // map_value_predicate_t(const unary_function<const value, bool>& _fn) : fn(_fn) {} 58 // bool operator()(const std::pair<key,value>& p) const { 59 // return fn(p.second); 60 // } 61 //}; 62 // 63 //template<class key, class value> 64 //inline map_value_predicate_t<key,value> map_value_predicate( 65 // const unary_function<const value, bool>& fn) { 66 // return map_value_predicate_t<key,value>(fn); 67 //} 68 69 // copy values from a map subset 70 template<class input_iter, class output_iter> 71 void map_value_copy(input_iter begin, input_iter end, output_iter to) { 72 while(begin != end) { 73 *to = (*begin).second; 74 ++to; 75 ++begin; 76 } 77 } 78 79 // adapt a unary functor to a map (eek) 80 template <class pairT, class opT> 81 class unary_map_function_t : 82 public std::unary_function<typename opT::argument_type, typename opT::result_type> { 83 84 opT f; 85 86 public: 87 unary_map_function_t(const opT& _f) : f(_f) {} 88 89 typename opT::result_type 90 operator()(pairT& p) const { 91 return f(p.second); 92 } 93 }; 94 95 template <class mapT, class opT> 96 inline unary_map_function_t<typename mapT::value_type, opT> 97 unary_map_function( 98 const mapT& map, 99 const opT& f) { 100 return unary_map_function_t<typename mapT::value_type, opT>(f); 101 } 102 103 104 __END_CORTEX_NAMESPACE 105 #endif /* __SET_TOOLS_H__ */ 106