home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / digital marsC compier / dm / stl / stl_function.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-08  |  22.3 KB  |  726 lines

  1. /*
  2.  *
  3.  * Copyright (c) 1994
  4.  * Hewlett-Packard Company
  5.  *
  6.  * Permission to use, copy, modify, distribute and sell this software
  7.  * and its documentation for any purpose is hereby granted without fee,
  8.  * provided that the above copyright notice appear in all copies and
  9.  * that both that copyright notice and this permission notice appear
  10.  * in supporting documentation.  Hewlett-Packard Company makes no
  11.  * representations about the suitability of this software for any
  12.  * purpose.  It is provided "as is" without express or implied warranty.
  13.  *
  14.  *
  15.  * Copyright (c) 1996-1998
  16.  * Silicon Graphics Computer Systems, Inc.
  17.  *
  18.  * Permission to use, copy, modify, distribute and sell this software
  19.  * and its documentation for any purpose is hereby granted without fee,
  20.  * provided that the above copyright notice appear in all copies and
  21.  * that both that copyright notice and this permission notice appear
  22.  * in supporting documentation.  Silicon Graphics makes no
  23.  * representations about the suitability of this software for any
  24.  * purpose.  It is provided "as is" without express or implied warranty.
  25.  */
  26.  
  27. /* NOTE: This is an internal header file, included by other STL headers.
  28.  *   You should not attempt to use it directly.
  29.  */
  30.  
  31. #ifndef __SGI_STL_INTERNAL_FUNCTION_H
  32. #define __SGI_STL_INTERNAL_FUNCTION_H
  33.  
  34. __STL_BEGIN_NAMESPACE
  35.  
  36. template <class _Arg, class _Result>
  37. struct unary_function {
  38.   typedef _Arg argument_type;
  39.   typedef _Result result_type;
  40. };
  41.  
  42. template <class _Arg1, class _Arg2, class _Result>
  43. struct binary_function {
  44.   typedef _Arg1 first_argument_type;
  45.   typedef _Arg2 second_argument_type;
  46.   typedef _Result result_type;
  47. };      
  48.  
  49. template <class _Tp>
  50. struct plus : public binary_function<_Tp,_Tp,_Tp> {
  51.   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
  52. };
  53.  
  54. template <class _Tp>
  55. struct minus : public binary_function<_Tp,_Tp,_Tp> {
  56.   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
  57. };
  58.  
  59. template <class _Tp>
  60. struct multiplies : public binary_function<_Tp,_Tp,_Tp> {
  61.   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
  62. };
  63.  
  64. template <class _Tp>
  65. struct divides : public binary_function<_Tp,_Tp,_Tp> {
  66.   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
  67. };
  68.  
  69. // identity_element (not part of the C++ standard).
  70.  
  71. template <class _Tp> inline _Tp identity_element(plus<_Tp>) {
  72.   return _Tp(0);
  73. }
  74. template <class _Tp> inline _Tp identity_element(multiplies<_Tp>) {
  75.   return _Tp(1);
  76. }
  77.  
  78. template <class _Tp>
  79. struct modulus : public binary_function<_Tp,_Tp,_Tp> 
  80. {
  81.   _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
  82. };
  83.  
  84. template <class _Tp>
  85. struct negate : public unary_function<_Tp,_Tp> 
  86. {
  87.   _Tp operator()(const _Tp& __x) const { return -__x; }
  88. };
  89.  
  90. template <class _Tp>
  91. struct equal_to : public binary_function<_Tp,_Tp,bool> 
  92. {
  93.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x == __y; }
  94. };
  95.  
  96. template <class _Tp>
  97. struct not_equal_to : public binary_function<_Tp,_Tp,bool> 
  98. {
  99.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
  100. };
  101.  
  102. template <class _Tp>
  103. struct greater : public binary_function<_Tp,_Tp,bool> 
  104. {
  105.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
  106. };
  107.  
  108. template <class _Tp>
  109. struct less : public binary_function<_Tp,_Tp,bool> 
  110. {
  111.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }
  112. };
  113.  
  114. template <class _Tp>
  115. struct greater_equal : public binary_function<_Tp,_Tp,bool>
  116. {
  117.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
  118. };
  119.  
  120. template <class _Tp>
  121. struct less_equal : public binary_function<_Tp,_Tp,bool> 
  122. {
  123.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
  124. };
  125.  
  126. template <class _Tp>
  127. struct logical_and : public binary_function<_Tp,_Tp,bool>
  128. {
  129.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
  130. };
  131.  
  132. template <class _Tp>
  133. struct logical_or : public binary_function<_Tp,_Tp,bool>
  134. {
  135.   bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
  136. };
  137.  
  138. template <class _Tp>
  139. struct logical_not : public unary_function<_Tp,bool>
  140. {
  141.   bool operator()(const _Tp& __x) const { return !__x; }
  142. };
  143.  
  144. template <class _Predicate>
  145. class unary_negate
  146.   : public unary_function<typename _Predicate::argument_type, bool> {
  147. protected:
  148.   _Predicate _M_pred;
  149. public:
  150.   explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
  151.   bool operator()(const typename _Predicate::argument_type& __x) const {
  152.     return !_M_pred(__x);
  153.   }
  154. };
  155.  
  156. template <class _Predicate>
  157. inline unary_negate<_Predicate> 
  158. not1(const _Predicate& __pred)
  159. {
  160.   return unary_negate<_Predicate>(__pred);
  161. }
  162.  
  163. template <class _Predicate> 
  164. class binary_negate 
  165.   : public binary_function<typename _Predicate::first_argument_type,
  166.                            typename _Predicate::second_argument_type,
  167.                            bool> {
  168. protected:
  169.   _Predicate _M_pred;
  170. public:
  171.   explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {}
  172.   bool operator()(const typename _Predicate::first_argument_type& __x, 
  173.                   const typename _Predicate::second_argument_type& __y) const
  174.   {
  175.     return !_M_pred(__x, __y); 
  176.   }
  177. };
  178.  
  179. template <class _Predicate>
  180. inline binary_negate<_Predicate> 
  181. not2(const _Predicate& __pred)
  182. {
  183.   return binary_negate<_Predicate>(__pred);
  184. }
  185.  
  186. template <class _Operation> 
  187. class binder1st
  188.   : public unary_function<typename _Operation::second_argument_type,
  189.                           typename _Operation::result_type> {
  190. protected:
  191.   _Operation op;
  192.   typename _Operation::first_argument_type value;
  193. public:
  194.   binder1st(const _Operation& __x,
  195.             const typename _Operation::first_argument_type& __y)
  196.       : op(__x), value(__y) {}
  197.   typename _Operation::result_type
  198.   operator()(const typename _Operation::second_argument_type& __x) const {
  199.     return op(value, __x); 
  200.   }
  201. };
  202.  
  203. template <class _Operation, class _Tp>
  204. inline binder1st<_Operation> 
  205. bind1st(const _Operation& __fn, const _Tp& __x) 
  206. {
  207.   typedef typename _Operation::first_argument_type _Arg1_type;
  208.   return binder1st<_Operation>(__fn, _Arg1_type(__x));
  209. }
  210.  
  211. template <class _Operation> 
  212. class binder2nd
  213.   : public unary_function<typename _Operation::first_argument_type,
  214.                           typename _Operation::result_type> {
  215. protected:
  216.   _Operation op;
  217.   typename _Operation::second_argument_type value;
  218. public:
  219.   binder2nd(const _Operation& __x,
  220.             const typename _Operation::second_argument_type& __y) 
  221.       : op(__x), value(__y) {}
  222.   typename _Operation::result_type
  223.   operator()(const typename _Operation::first_argument_type& __x) const {
  224.     return op(__x, value); 
  225.   }
  226. };
  227.  
  228. template <class _Operation, class _Tp>
  229. inline binder2nd<_Operation> 
  230. bind2nd(const _Operation& __fn, const _Tp& __x) 
  231. {
  232.   typedef typename _Operation::second_argument_type _Arg2_type;
  233.   return binder2nd<_Operation>(__fn, _Arg2_type(__x));
  234. }
  235.  
  236. // unary_compose and binary_compose (extensions, not part of the standard).
  237.  
  238. template <class _Operation1, class _Operation2>
  239. class unary_compose
  240.   : public unary_function<typename _Operation2::argument_type,
  241.                           typename _Operation1::result_type> 
  242. {
  243. protected:
  244.   _Operation1 _M_fn1;
  245.   _Operation2 _M_fn2;
  246. public:
  247.   unary_compose(const _Operation1& __x, const _Operation2& __y) 
  248.     : _M_fn1(__x), _M_fn2(__y) {}
  249.   typename _Operation1::result_type
  250.   operator()(const typename _Operation2::argument_type& __x) const {
  251.     return _M_fn1(_M_fn2(__x));
  252.   }
  253. };
  254.  
  255. template <class _Operation1, class _Operation2>
  256. inline unary_compose<_Operation1,_Operation2> 
  257. compose1(const _Operation1& __fn1, const _Operation2& __fn2)
  258. {
  259.   return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
  260. }
  261.  
  262. template <class _Operation1, class _Operation2, class _Operation3>
  263. class binary_compose
  264.   : public unary_function<typename _Operation2::argument_type,
  265.                           typename _Operation1::result_type> {
  266. protected:
  267.   _Operation1 _M_fn1;
  268.   _Operation2 _M_fn2;
  269.   _Operation3 _M_fn3;
  270. public:
  271.   binary_compose(const _Operation1& __x, const _Operation2& __y, 
  272.                  const _Operation3& __z) 
  273.     : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
  274.   typename _Operation1::result_type
  275.   operator()(const typename _Operation2::argument_type& __x) const {
  276.     return _M_fn1(_M_fn2(__x), _M_fn3(__x));
  277.   }
  278. };
  279.  
  280. template <class _Operation1, class _Operation2, class _Operation3>
  281. inline binary_compose<_Operation1, _Operation2, _Operation3> 
  282. compose2(const _Operation1& __fn1, const _Operation2& __fn2, 
  283.          const _Operation3& __fn3)
  284. {
  285.   return binary_compose<_Operation1,_Operation2,_Operation3>
  286.     (__fn1, __fn2, __fn3);
  287. }
  288.  
  289. template <class _Arg, class _Result>
  290. class pointer_to_unary_function : public unary_function<_Arg, _Result> {
  291. protected:
  292.   _Result (*_M_ptr)(_Arg);
  293. public:
  294.   pointer_to_unary_function() {}
  295.   explicit pointer_to_unary_function(_Result (*__x)(_Arg)) : _M_ptr(__x) {}
  296.   _Result operator()(_Arg __x) const { return _M_ptr(__x); }
  297. };
  298.  
  299. template <class _Arg, class _Result>
  300. inline pointer_to_unary_function<_Arg, _Result> ptr_fun(_Result (*__x)(_Arg))
  301. {
  302.   return pointer_to_unary_function<_Arg, _Result>(__x);
  303. }
  304.  
  305. template <class _Arg1, class _Arg2, class _Result>
  306. class pointer_to_binary_function : 
  307.   public binary_function<_Arg1,_Arg2,_Result> {
  308. protected:
  309.     _Result (*_M_ptr)(_Arg1, _Arg2);
  310. public:
  311.     pointer_to_binary_function() {}
  312.     explicit pointer_to_binary_function(_Result (*__x)(_Arg1, _Arg2)) 
  313.       : _M_ptr(__x) {}
  314.     _Result operator()(_Arg1 __x, _Arg2 __y) const {
  315.       return _M_ptr(__x, __y);
  316.     }
  317. };
  318.  
  319. template <class _Arg1, class _Arg2, class _Result>
  320. inline pointer_to_binary_function<_Arg1,_Arg2,_Result> 
  321. ptr_fun(_Result (*__x)(_Arg1, _Arg2)) {
  322.   return pointer_to_binary_function<_Arg1,_Arg2,_Result>(__x);
  323. }
  324.  
  325. // identity is an extensions: it is not part of the standard.
  326. template <class _Tp>
  327. struct _Identity : public unary_function<_Tp,_Tp> {
  328.   const _Tp& operator()(const _Tp& __x) const { return __x; }
  329. };
  330.  
  331. template <class _Tp> struct identity : public _Identity<_Tp> {};
  332.  
  333. // select1st and select2nd are extensions: they are not part of the standard.
  334. template <class _Pair>
  335. struct _Select1st : public unary_function<_Pair, typename _Pair::first_type> {
  336.   const typename _Pair::first_type& operator()(const _Pair& __x) const {
  337.     return __x.first;
  338.   }
  339. };
  340.  
  341. template <class _Pair>
  342. struct _Select2nd : public unary_function<_Pair, typename _Pair::second_type>
  343. {
  344.   const typename _Pair::second_type& operator()(const _Pair& __x) const {
  345.     return __x.second;
  346.   }
  347. };
  348.  
  349. template <class _Pair> struct select1st : public _Select1st<_Pair> {};
  350. template <class _Pair> struct select2nd : public _Select2nd<_Pair> {};
  351.  
  352. // project1st and project2nd are extensions: they are not part of the standard
  353. template <class _Arg1, class _Arg2>
  354. struct _Project1st : public binary_function<_Arg1, _Arg2, _Arg1> {
  355.   _Arg1 operator()(const _Arg1& __x, const _Arg2&) const { return __x; }
  356. };
  357.  
  358. template <class _Arg1, class _Arg2>
  359. struct _Project2nd : public binary_function<_Arg1, _Arg2, _Arg2> {
  360.   _Arg2 operator()(const _Arg1&, const _Arg2& __y) const { return __y; }
  361. };
  362.  
  363. template <class _Arg1, class _Arg2> 
  364. struct project1st : public _Project1st<_Arg1, _Arg2> {};
  365.  
  366. template <class _Arg1, class _Arg2>
  367. struct project2nd : public _Project2nd<_Arg1, _Arg2> {};
  368.  
  369. // constant_void_fun, constant_unary_fun, and constant_binary_fun are
  370. // extensions: they are not part of the standard.  (The same, of course,
  371. // is true of the helper functions constant0, constant1, and constant2.)
  372.  
  373. template <class _Result>
  374. struct _Constant_void_fun {
  375.   typedef _Result result_type;
  376.   result_type _M_val;
  377.  
  378.   _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
  379.   const result_type& operator()() const { return _M_val; }
  380. };  
  381.  
  382. template <class _Result, class _Argument>
  383. struct _Constant_unary_fun {
  384.   typedef _Argument argument_type;
  385.   typedef  _Result  result_type;
  386.   result_type _M_val;
  387.  
  388.   _Constant_unary_fun(const result_type& __v) : _M_val(__v) {}
  389.   const result_type& operator()(const _Argument&) const { return _M_val; }
  390. };
  391.  
  392. template <class _Result, class _Arg1, class _Arg2>
  393. struct _Constant_binary_fun {
  394.   typedef  _Arg1   first_argument_type;
  395.   typedef  _Arg2   second_argument_type;
  396.   typedef  _Result result_type;
  397.   _Result _M_val;
  398.  
  399.   _Constant_binary_fun(const _Result& __v) : _M_val(__v) {}
  400.   const result_type& operator()(const _Arg1&, const _Arg2&) const {
  401.     return _M_val;
  402.   }
  403. };
  404.  
  405. template <class _Result>
  406. struct constant_void_fun : public _Constant_void_fun<_Result> {
  407.   constant_void_fun(const _Result& __v) : _Constant_void_fun<_Result>(__v) {}
  408. };  
  409.  
  410.  
  411. template <class _Result,
  412.           class _Argument __STL_DEPENDENT_DEFAULT_TMPL(_Result)>
  413. struct constant_unary_fun : public _Constant_unary_fun<_Result, _Argument>
  414. {
  415.   constant_unary_fun(const _Result& __v)
  416.     : _Constant_unary_fun<_Result, _Argument>(__v) {}
  417. };
  418.  
  419.  
  420. template <class _Result,
  421.           class _Arg1 __STL_DEPENDENT_DEFAULT_TMPL(_Result),
  422.           class _Arg2 __STL_DEPENDENT_DEFAULT_TMPL(_Arg1)>
  423. struct constant_binary_fun
  424.   : public _Constant_binary_fun<_Result, _Arg1, _Arg2>
  425. {
  426.   constant_binary_fun(const _Result& __v)
  427.     : _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
  428. };
  429.  
  430. template <class _Result>
  431. inline constant_void_fun<_Result> constant0(const _Result& __val)
  432. {
  433.   return constant_void_fun<_Result>(__val);
  434. }
  435.  
  436. template <class _Result>
  437. inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val)
  438. {
  439.   return constant_unary_fun<_Result,_Result>(__val);
  440. }
  441.  
  442. template <class _Result>
  443. inline constant_binary_fun<_Result,_Result,_Result> 
  444. constant2(const _Result& __val)
  445. {
  446.   return constant_binary_fun<_Result,_Result,_Result>(__val);
  447. }
  448.  
  449. // subtractive_rng is an extension: it is not part of the standard.
  450. // Note: this code assumes that int is 32 bits.
  451. class subtractive_rng : public unary_function<unsigned int, unsigned int> {
  452. private:
  453.   unsigned int _M_table[55];
  454.   size_t _M_index1;
  455.   size_t _M_index2;
  456. public:
  457.   unsigned int operator()(unsigned int __limit) {
  458.     _M_index1 = (_M_index1 + 1) % 55;
  459.     _M_index2 = (_M_index2 + 1) % 55;
  460.     _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
  461.     return _M_table[_M_index1] % __limit;
  462.   }
  463.  
  464.   void _M_initialize(unsigned int __seed)
  465.   {
  466.     unsigned int __k = 1;
  467.     _M_table[54] = __seed;
  468.     size_t __i;
  469.     for (__i = 0; __i < 54; __i++) {
  470.         size_t __ii = (21 * (__i + 1) % 55) - 1;
  471.         _M_table[__ii] = __k;
  472.         __k = __seed - __k;
  473.         __seed = _M_table[__ii];
  474.     }
  475.     for (int __loop = 0; __loop < 4; __loop++) {
  476.         for (__i = 0; __i < 55; __i++)
  477.             _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
  478.     }
  479.     _M_index1 = 0;
  480.     _M_index2 = 31;
  481.   }
  482.  
  483.   subtractive_rng(unsigned int __seed) { _M_initialize(__seed); }
  484.   subtractive_rng() { _M_initialize(161803398u); }
  485. };
  486.  
  487.  
  488. // Adaptor function objects: pointers to member functions.
  489.  
  490. // There are a total of 16 = 2^4 function objects in this family.
  491. //  (1) Member functions taking no arguments vs member functions taking
  492. //       one argument.
  493. //  (2) Call through pointer vs call through reference.
  494. //  (3) Member function with void return type vs member function with
  495. //      non-void return type.
  496. //  (4) Const vs non-const member function.
  497.  
  498. // Note that choice (3) is nothing more than a workaround: according
  499. //  to the draft, compilers should handle void and non-void the same way.
  500. //  This feature is not yet widely implemented, though.  You can only use
  501. //  member functions returning void if your compiler supports partial
  502. //  specialization.
  503.  
  504. // All of this complexity is in the function objects themselves.  You can
  505. //  ignore it by using the helper function mem_fun and mem_fun_ref,
  506. //  which create whichever type of adaptor is appropriate.
  507. //  (mem_fun1 and mem_fun1_ref are no longer part of the C++ standard,
  508. //  but they are provided for backward compatibility.)
  509.  
  510.  
  511. template <class _Ret, class _Tp>
  512. class mem_fun_t : public unary_function<_Tp*,_Ret> {
  513. public:
  514.   explicit mem_fun_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
  515.   _Ret operator()(_Tp* __p) const { return (__p->*_M_f)(); }
  516. private:
  517.   _Ret (_Tp::*_M_f)();
  518. };
  519.  
  520. template <class _Ret, class _Tp>
  521. class const_mem_fun_t : public unary_function<const _Tp*,_Ret> {
  522. public:
  523.   explicit const_mem_fun_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
  524.   _Ret operator()(const _Tp* __p) const { return (__p->*_M_f)(); }
  525. private:
  526.   _Ret (_Tp::*_M_f)() const;
  527. };
  528.  
  529.  
  530. template <class _Ret, class _Tp>
  531. class mem_fun_ref_t : public unary_function<_Tp,_Ret> {
  532. public:
  533.   explicit mem_fun_ref_t(_Ret (_Tp::*__pf)()) : _M_f(__pf) {}
  534.   _Ret operator()(_Tp& __r) const { return (__r.*_M_f)(); }
  535. private:
  536.   _Ret (_Tp::*_M_f)();
  537. };
  538.  
  539. template <class _Ret, class _Tp>
  540. class const_mem_fun_ref_t : public unary_function<_Tp,_Ret> {
  541. public:
  542.   explicit const_mem_fun_ref_t(_Ret (_Tp::*__pf)() const) : _M_f(__pf) {}
  543.   _Ret operator()(const _Tp& __r) const { return (__r.*_M_f)(); }
  544. private:
  545.   _Ret (_Tp::*_M_f)() const;
  546. };
  547.  
  548. template <class _Ret, class _Tp, class _Arg>
  549. class mem_fun1_t : public binary_function<_Tp*,_Arg,_Ret> {
  550. public:
  551.   explicit mem_fun1_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
  552.   _Ret operator()(_Tp* __p, _Arg __x) const { return (__p->*_M_f)(__x); }
  553. private:
  554.   _Ret (_Tp::*_M_f)(_Arg);
  555. };
  556.  
  557. template <class _Ret, class _Tp, class _Arg>
  558. class const_mem_fun1_t : public binary_function<const _Tp*,_Arg,_Ret> {
  559. public:
  560.   explicit const_mem_fun1_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
  561.   _Ret operator()(const _Tp* __p, _Arg __x) const
  562.     { return (__p->*_M_f)(__x); }
  563. private:
  564.   _Ret (_Tp::*_M_f)(_Arg) const;
  565. };
  566.  
  567. template <class _Ret, class _Tp, class _Arg>
  568. class mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {
  569. public:
  570.   explicit mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
  571.   _Ret operator()(_Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }
  572. private:
  573.   _Ret (_Tp::*_M_f)(_Arg);
  574. };
  575.  
  576. template <class _Ret, class _Tp, class _Arg>
  577. class const_mem_fun1_ref_t : public binary_function<_Tp,_Arg,_Ret> {
  578. public:
  579.   explicit const_mem_fun1_ref_t(_Ret (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
  580.   _Ret operator()(const _Tp& __r, _Arg __x) const { return (__r.*_M_f)(__x); }
  581. private:
  582.   _Ret (_Tp::*_M_f)(_Arg) const;
  583. };
  584.  
  585. #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
  586.  
  587. template <class _Tp>
  588. class mem_fun_t<void, _Tp> : public unary_function<_Tp*,void> {
  589. public:
  590.   explicit mem_fun_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
  591.   void operator()(_Tp* __p) const { (__p->*_M_f)(); }
  592. private:
  593.   void (_Tp::*_M_f)();
  594. };
  595.  
  596. template <class _Tp>
  597. class const_mem_fun_t<void, _Tp> : public unary_function<const _Tp*,void> {
  598. public:
  599.   explicit const_mem_fun_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
  600.   void operator()(const _Tp* __p) const { (__p->*_M_f)(); }
  601. private:
  602.   void (_Tp::*_M_f)() const;
  603. };
  604.  
  605. template <class _Tp>
  606. class mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
  607. public:
  608.   explicit mem_fun_ref_t(void (_Tp::*__pf)()) : _M_f(__pf) {}
  609.   void operator()(_Tp& __r) const { (__r.*_M_f)(); }
  610. private:
  611.   void (_Tp::*_M_f)();
  612. };
  613.  
  614. template <class _Tp>
  615. class const_mem_fun_ref_t<void, _Tp> : public unary_function<_Tp,void> {
  616. public:
  617.   explicit const_mem_fun_ref_t(void (_Tp::*__pf)() const) : _M_f(__pf) {}
  618.   void operator()(const _Tp& __r) const { (__r.*_M_f)(); }
  619. private:
  620.   void (_Tp::*_M_f)() const;
  621. };
  622.  
  623. template <class _Tp, class _Arg>
  624. class mem_fun1_t<void, _Tp, _Arg> : public binary_function<_Tp*,_Arg,void> {
  625. public:
  626.   explicit mem_fun1_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
  627.   void operator()(_Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
  628. private:
  629.   void (_Tp::*_M_f)(_Arg);
  630. };
  631.  
  632. template <class _Tp, class _Arg>
  633. class const_mem_fun1_t<void, _Tp, _Arg> 
  634.   : public binary_function<const _Tp*,_Arg,void> {
  635. public:
  636.   explicit const_mem_fun1_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
  637.   void operator()(const _Tp* __p, _Arg __x) const { (__p->*_M_f)(__x); }
  638. private:
  639.   void (_Tp::*_M_f)(_Arg) const;
  640. };
  641.  
  642. template <class _Tp, class _Arg>
  643. class mem_fun1_ref_t<void, _Tp, _Arg>
  644.   : public binary_function<_Tp,_Arg,void> {
  645. public:
  646.   explicit mem_fun1_ref_t(void (_Tp::*__pf)(_Arg)) : _M_f(__pf) {}
  647.   void operator()(_Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }
  648. private:
  649.   void (_Tp::*_M_f)(_Arg);
  650. };
  651.  
  652. template <class _Tp, class _Arg>
  653. class const_mem_fun1_ref_t<void, _Tp, _Arg>
  654.   : public binary_function<_Tp,_Arg,void> {
  655. public:
  656.   explicit const_mem_fun1_ref_t(void (_Tp::*__pf)(_Arg) const) : _M_f(__pf) {}
  657.   void operator()(const _Tp& __r, _Arg __x) const { (__r.*_M_f)(__x); }
  658. private:
  659.   void (_Tp::*_M_f)(_Arg) const;
  660. };
  661.  
  662. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  663.  
  664. // Mem_fun adaptor helper functions.  There are only two:
  665. //  mem_fun and mem_fun_ref.  (mem_fun1 and mem_fun1_ref 
  666. //  are provided for backward compatibility, but they are no longer
  667. //  part of the C++ standard.)
  668.  
  669. template <class _Ret, class _Tp>
  670. inline mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)())
  671.   { return mem_fun_t<_Ret,_Tp>(__f); }
  672.  
  673. template <class _Ret, class _Tp>
  674. inline const_mem_fun_t<_Ret,_Tp> mem_fun(_Ret (_Tp::*__f)() const)
  675.   { return const_mem_fun_t<_Ret,_Tp>(__f); }
  676.  
  677. template <class _Ret, class _Tp>
  678. inline mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)()) 
  679.   { return mem_fun_ref_t<_Ret,_Tp>(__f); }
  680.  
  681. template <class _Ret, class _Tp>
  682. inline const_mem_fun_ref_t<_Ret,_Tp> mem_fun_ref(_Ret (_Tp::*__f)() const)
  683.   { return const_mem_fun_ref_t<_Ret,_Tp>(__f); }
  684.  
  685. template <class _Ret, class _Tp, class _Arg>
  686. inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg))
  687.   { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
  688.  
  689. template <class _Ret, class _Tp, class _Arg>
  690. inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun(_Ret (_Tp::*__f)(_Arg) const)
  691.   { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
  692.  
  693. template <class _Ret, class _Tp, class _Arg>
  694. inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun_ref(_Ret (_Tp::*__f)(_Arg))
  695.   { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
  696.  
  697. template <class _Ret, class _Tp, class _Arg>
  698. inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>
  699. mem_fun_ref(_Ret (_Tp::*__f)(_Arg) const)
  700.   { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
  701.  
  702. template <class _Ret, class _Tp, class _Arg>
  703. inline mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg))
  704.   { return mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
  705.  
  706. template <class _Ret, class _Tp, class _Arg>
  707. inline const_mem_fun1_t<_Ret,_Tp,_Arg> mem_fun1(_Ret (_Tp::*__f)(_Arg) const)
  708.   { return const_mem_fun1_t<_Ret,_Tp,_Arg>(__f); }
  709.  
  710. template <class _Ret, class _Tp, class _Arg>
  711. inline mem_fun1_ref_t<_Ret,_Tp,_Arg> mem_fun1_ref(_Ret (_Tp::*__f)(_Arg))
  712.   { return mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
  713.  
  714. template <class _Ret, class _Tp, class _Arg>
  715. inline const_mem_fun1_ref_t<_Ret,_Tp,_Arg>
  716. mem_fun1_ref(_Ret (_Tp::*__f)(_Arg) const)
  717.   { return const_mem_fun1_ref_t<_Ret,_Tp,_Arg>(__f); }
  718.  
  719. __STL_END_NAMESPACE
  720.  
  721. #endif /* __SGI_STL_INTERNAL_FUNCTION_H */
  722.  
  723. // Local Variables:
  724. // mode:C++
  725. // End:
  726.