home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / FUNCTION.H < prev    next >
C/C++ Source or Header  |  1997-02-14  |  10KB  |  341 lines

  1. #ifndef __STD_FUNCTIONAL__
  2. #define __STD_FUNCTIONAL__
  3. /* $Revision:   8.1  $ */
  4.  
  5. /***************************************************************************
  6.  *
  7.  * functional - global template functions
  8.  *
  9.  * $Id: functional,v 1.22 1995/09/27 19:03:47 lijewski Exp $
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994
  14.  * Hewlett-Packard Company
  15.  *
  16.  * Permission to use, copy, modify, distribute and sell this software
  17.  * and its documentation for any purpose is hereby granted without fee,
  18.  * provided that the above copyright notice appear in all copies and
  19.  * that both that copyright notice and this permission notice appear
  20.  * in supporting documentation.  Hewlett-Packard Company makes no
  21.  * representations about the suitability of this software for any
  22.  * purpose.  It is provided "as is" without express or implied warranty.
  23.  *
  24.  *
  25.  ***************************************************************************
  26.  *
  27.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  28.  * ALL RIGHTS RESERVED
  29.  *
  30.  * The software and information contained herein are proprietary to, and
  31.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  32.  * intends to preserve as trade secrets such software and information.
  33.  * This software is furnished pursuant to a written license agreement and
  34.  * may be used, copied, transmitted, and stored only in accordance with
  35.  * the terms of such license and with the inclusion of the above copyright
  36.  * notice.  This software and information or any other copies thereof may
  37.  * not be provided or otherwise made available to any other person.
  38.  *
  39.  * Notwithstanding any other lease or license that may pertain to, or
  40.  * accompany the delivery of, this computer software and information, the
  41.  * rights of the Government regarding its use, reproduction and disclosure
  42.  * are as set forth in Section 52.227-19 of the FARS Computer
  43.  * Software-Restricted Rights clause.
  44.  *
  45.  * Use, duplication, or disclosure by the Government is subject to
  46.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  47.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  48.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  49.  * P.O. Box 2328, Corvallis, Oregon 97339.
  50.  *
  51.  * This computer software and information is distributed with "restricted
  52.  * rights."  Use, duplication or disclosure is subject to restrictions as
  53.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  54.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  55.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  56.  * then the "Alternate III" clause applies.
  57.  *
  58.  **************************************************************************/
  59.  
  60. #include <stdcomp.h>
  61.  
  62. #ifndef RWSTD_NO_NAMESPACE
  63. namespace std {
  64. #endif
  65.  
  66. //
  67. // The bases of many of the function objects here.
  68. //
  69.  
  70. template <class Arg, class Result>
  71. struct unary_function
  72. {
  73.     typedef Arg argument_type;
  74.     typedef Result result_type;
  75. };
  76.  
  77. template <class Arg1, class Arg2, class Result>
  78. struct binary_function
  79. {
  80.     typedef Arg1 first_argument_type;
  81.     typedef Arg2 second_argument_type;
  82.     typedef Result result_type;
  83. };
  84.  
  85. //
  86. // Arithmetic operators.
  87. //
  88.  
  89. template <class T>
  90. struct plus : public binary_function<T, T, T>
  91. {
  92.     T operator() (const T& x, const T& y) const { return x + y; }
  93. };
  94.  
  95. template <class T>
  96. struct minus : public binary_function<T, T, T>
  97. {
  98.     T operator() (const T& x, const T& y) const { return x - y; }
  99. };
  100.  
  101. template <class T>
  102. struct times : public binary_function<T, T, T>
  103. {
  104.     T operator() (const T& x, const T& y) const { return x * y; }
  105. };
  106.  
  107. template <class T>
  108. struct divides : public binary_function<T, T, T>
  109. {
  110.     T operator() (const T& x, const T& y) const { return x / y; }
  111. };
  112.  
  113. template <class T>
  114. struct modulus : public binary_function<T, T, T>
  115. {
  116.     T operator() (const T& x, const T& y) const { return x % y; }
  117. };
  118.  
  119. template <class T>
  120. struct negate : public unary_function<T, T>
  121. {
  122.     T operator() (const T& x) const { return -x; }
  123. };
  124.  
  125. //
  126. // Comparisons.
  127. //
  128.  
  129. template <class T>
  130. struct equal_to : public binary_function<T, T, bool>
  131. {
  132.     bool operator() (const T& x, const T& y) const { return x == y; }
  133. };
  134.  
  135. template <class T>
  136. struct not_equal_to : public binary_function<T, T, bool>
  137. {
  138.     bool operator() (const T& x, const T& y) const { return x != y; }
  139. };
  140.  
  141. template <class T>
  142. struct greater : public binary_function<T, T, bool>
  143. {
  144.     bool operator() (const T& x, const T& y) const { return x > y; }
  145. };
  146.  
  147. template <class T>
  148. struct less : public binary_function<T, T, bool>
  149. {
  150.     bool operator() (const T& x, const T& y) const { return x < y; }
  151. };
  152.  
  153. template <class T>
  154. struct greater_equal : public binary_function<T, T, bool>
  155. {
  156.     bool operator() (const T& x, const T& y) const { return x >= y; }
  157. };
  158.  
  159. template <class T>
  160. struct less_equal : public binary_function<T, T, bool>
  161. {
  162.     bool operator() (const T& x, const T& y) const { return x <= y; }
  163. };
  164.  
  165. //
  166. // Logical operations.
  167. //
  168.  
  169. template <class T>
  170. struct logical_and : public binary_function<T, T, bool>
  171. {
  172.     bool operator() (const T& x, const T& y) const { return x && y; }
  173. };
  174.  
  175. template <class T>
  176. struct logical_or : public binary_function<T, T, bool>
  177. {
  178.     bool operator() (const T& x, const T& y) const { return x || y; }
  179. };
  180.  
  181. template <class T>
  182. struct logical_not : public unary_function<T, bool>
  183. {
  184.     bool operator() (const T& x) const { return !x; }
  185. };
  186.  
  187. //
  188. // Negators.
  189. //
  190.  
  191. template <class Predicate>
  192. class unary_negate : public unary_function<typename Predicate::argument_type,
  193.                                            bool>
  194. {
  195.   protected:
  196.     Predicate pred;
  197.   public:
  198.     explicit unary_negate (const Predicate& x) : pred(x) {}
  199.     bool operator() (const argument_type& x) const { return !pred(x); }
  200. };
  201.  
  202. template <class Predicate>
  203. unary_negate<Predicate> not1(const Predicate& pred)
  204. {
  205.     return unary_negate<Predicate>(pred);
  206. }
  207.  
  208. template <class Predicate>
  209. class binary_negate
  210.     : public binary_function<typename Predicate::first_argument_type,
  211.                              typename Predicate::second_argument_type, bool>
  212. {
  213.   protected:
  214.     Predicate pred;
  215.   public:
  216.     explicit binary_negate (const Predicate& x) : pred(x) {}
  217.     bool operator() (const first_argument_type& x,
  218.                      const second_argument_type& y) const
  219.     {
  220.         return !pred(x, y);
  221.     }
  222. };
  223.  
  224. template <class Predicate>
  225. binary_negate<Predicate> not2(const Predicate& pred)
  226. {
  227.     return binary_negate<Predicate>(pred);
  228. }
  229.  
  230. //
  231. // Binders.
  232. //
  233.  
  234. template <class Operation>
  235. class binder1st :public unary_function<typename Operation::second_argument_type,
  236.                                        typename Operation::result_type>
  237. {
  238.   protected:
  239.     Operation op;
  240.     typename Operation::first_argument_type value;
  241.   public:
  242.     binder1st (const Operation& x, const typename
  243.                                    Operation::first_argument_type& y)
  244.         : op(x), value(y) {}
  245.     result_type operator() (const argument_type& x) const
  246.     {
  247.         return op(value, x);
  248.     }
  249. };
  250.  
  251. template <class Operation, class T>
  252. binder1st<Operation> bind1st (const Operation& op, const T& x)
  253. {
  254.     return binder1st<Operation>(op, Operation::first_argument_type(x));
  255. }
  256.  
  257. template <class Operation>
  258. class binder2nd : public unary_function<typename Operation::first_argument_type,
  259.                                         typename Operation::result_type>
  260. {
  261.   protected:
  262.     Operation op;
  263.     typename Operation::second_argument_type value;
  264.   public:
  265.     binder2nd (const Operation& x, const typename
  266.                                    Operation::second_argument_type& y)
  267.         : op(x), value(y) {}
  268.     result_type operator() (const argument_type& x) const
  269.     {
  270.         return op(x, value);
  271.     }
  272. };
  273.  
  274. template <class Operation, class T>
  275. binder2nd<Operation> bind2nd (const Operation& op, const T& x)
  276. {
  277.    return binder2nd<Operation>(op, Operation::second_argument_type(x));
  278. }
  279.  
  280. template <class Operation1, class Operation2>
  281. class unary_compose : public unary_function<typename Operation2::argument_type,
  282.                                             typename Operation1::result_type>
  283. {
  284.   protected:
  285.     Operation1 op1;
  286.     Operation2 op2;
  287.   public:
  288.     unary_compose (const Operation1& x, const Operation2& y) : op1(x), op2(y) {}
  289.     result_type operator() (const argument_type& x) const
  290.     {
  291.         return op1(op2(x));
  292.     }
  293. };
  294.  
  295. //
  296. // Adaptors.
  297. //
  298.  
  299. template <class Arg, class Result>
  300. class pointer_to_unary_function : public unary_function<Arg, Result>
  301. {
  302.   protected:
  303.     Result (*ptr)(Arg);
  304.   public:
  305.     explicit pointer_to_unary_function (Result (*x)(Arg)) : ptr(x) {}
  306.     Result operator() (const Arg& x) const { return ptr(x); }
  307. };
  308.  
  309. template <class Arg, class Result>
  310. pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg))
  311. {
  312.     return pointer_to_unary_function<Arg, Result>(x);
  313. }
  314.  
  315. template <class Arg1, class Arg2, class Result>
  316. class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result>
  317. {
  318.   protected:
  319.     Result (*ptr)(Arg1, Arg2);
  320.   public:
  321.     explicit pointer_to_binary_function (Result (*x)(Arg1, Arg2)) : ptr(x) {}
  322.     Result operator() (const Arg1& x, const Arg2& y) const
  323.     {
  324.         return ptr(x, y);
  325.     }
  326. };
  327.  
  328. template <class Arg1, class Arg2, class Result>
  329. pointer_to_binary_function<Arg1, Arg2, Result>
  330. ptr_fun(Result (*x)(Arg1, Arg2))
  331. {
  332.     return pointer_to_binary_function<Arg1, Arg2, Result>(x);
  333. }
  334.  
  335. #ifndef RWSTD_NO_NAMESPACE
  336. }
  337. #endif
  338.  
  339. #endif /*__STD_FUNCTIONAL__*/
  340.  
  341.