home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / function.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  8KB  |  283 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.  
  16. #ifndef FUNCTION_H
  17. #define FUNCTION_H
  18.  
  19. #ifndef __GNUG__
  20. #include <bool.h>
  21. #endif
  22.  
  23. template <class T1, class T2>
  24. inline bool operator!=(const T1& x, const T2& y) {
  25.     return !(x == y);
  26. }
  27.  
  28. template <class T1, class T2>
  29. inline bool operator>(const T1& x, const T2& y) {
  30.     return y < x;
  31. }
  32.  
  33. template <class T1, class T2>
  34. inline bool operator<=(const T1& x, const T2& y) {
  35.     return !(y < x);
  36. }
  37.  
  38. template <class T1, class T2>
  39. inline bool operator>=(const T1& x, const T2& y) {
  40.     return !(x < y);
  41. }
  42.  
  43. template <class Arg, class Result>
  44. struct unary_function {
  45.     typedef Arg argument_type;
  46.     typedef Result result_type;
  47. };
  48.  
  49. template <class Arg1, class Arg2, class Result>
  50. struct binary_function {
  51.     typedef Arg1 first_argument_type;
  52.     typedef Arg2 second_argument_type;
  53.     typedef Result result_type;
  54. };      
  55.  
  56. template <class T>
  57. struct plus : binary_function<T, T, T> {
  58.     T operator()(const T& x, const T& y) const { return x + y; }
  59. };
  60.  
  61. template <class T>
  62. struct minus : binary_function<T, T, T> {
  63.     T operator()(const T& x, const T& y) const { return x - y; }
  64. };
  65.  
  66. template <class T>
  67. struct times : binary_function<T, T, T> {
  68.     T operator()(const T& x, const T& y) const { return x * y; }
  69. };
  70.  
  71. template <class T>
  72. struct divides : binary_function<T, T, T> {
  73.     T operator()(const T& x, const T& y) const { return x / y; }
  74. };
  75.  
  76. template <class T>
  77. #ifdef __GNU__
  78. struct modulus {
  79.     typedef T first_argument_type;
  80.     typedef T second_argument_type;
  81.     typedef T result_type;
  82.     T operator()(const T& x, const T& y) const { return x % y; }
  83. };
  84. #else
  85. struct modulus : binary_function<T, T, T> {
  86.     T operator()(const T& x, const T& y) const { return x % y; }
  87. };
  88. #endif
  89.  
  90. template <class T>
  91. struct negate : unary_function<T, T> {
  92.     T operator()(const T& x) const { return -x; }
  93. };
  94.  
  95. template <class T>
  96. struct equal_to : binary_function<T, T, bool> {
  97.     bool operator()(const T& x, const T& y) const { return x == y; }
  98. };
  99.  
  100. template <class T>
  101. struct not_equal_to : binary_function<T, T, bool> {
  102.     bool operator()(const T& x, const T& y) const { return x != y; }
  103. };
  104.  
  105. template <class T>
  106. struct greater : binary_function<T, T, bool> {
  107.     bool operator()(const T& x, const T& y) const { return x > y; }
  108. };
  109.  
  110. template <class T>
  111. struct less : binary_function<T, T, bool> {
  112.     bool operator()(const T& x, const T& y) const { return x < y; }
  113. };
  114.  
  115. template <class T>
  116. struct greater_equal : binary_function<T, T, bool> {
  117.     bool operator()(const T& x, const T& y) const { return x >= y; }
  118. };
  119.  
  120. template <class T>
  121. struct less_equal : binary_function<T, T, bool> {
  122.     bool operator()(const T& x, const T& y) const { return x <= y; }
  123. };
  124.  
  125. template <class T>
  126. struct logical_and : binary_function<T, T, bool> {
  127.     bool operator()(const T& x, const T& y) const { return x && y; }
  128. };
  129.  
  130. template <class T>
  131. struct logical_or : binary_function<T, T, bool> {
  132.     bool operator()(const T& x, const T& y) const { return x || y; }
  133. };
  134.  
  135. template <class T>
  136. struct logical_not : unary_function<T, bool> {
  137.     bool operator()(const T& x) const { return !x; }
  138. };
  139.  
  140. template <class Predicate>
  141. class unary_negate : public unary_function<Predicate::argument_type, bool> {
  142. protected:
  143.     Predicate pred;
  144. public:
  145.     unary_negate(const Predicate& x) : pred(x) {}
  146.     bool operator()(const argument_type& x) const { return !pred(x); }
  147. };
  148.  
  149. template <class Predicate>
  150. unary_negate<Predicate> not1(const Predicate& pred) {
  151.     return unary_negate<Predicate>(pred);
  152. }
  153.  
  154. template <class Predicate> 
  155. class binary_negate 
  156.     : public binary_function<Predicate::first_argument_type,
  157.                  Predicate::second_argument_type, bool> {
  158. protected:
  159.     Predicate pred;
  160. public:
  161.     binary_negate(const Predicate& x) : pred(x) {}
  162.     bool operator()(const first_argument_type& x, 
  163.             const second_argument_type& y) const {
  164.     return !pred(x, y); 
  165.     }
  166. };
  167.  
  168. template <class Predicate>
  169. binary_negate<Predicate> not2(const Predicate& pred) {
  170.     return binary_negate<Predicate>(pred);
  171. }
  172.  
  173. template <class Operation> 
  174. class binder1st : public unary_function<Operation::second_argument_type,
  175.                     Operation::result_type> {
  176. protected:
  177.     Operation op;
  178.     Operation::first_argument_type value;
  179. public:
  180.     binder1st(const Operation& x, const Operation::first_argument_type& y)
  181.     : op(x), value(y) {}
  182.     result_type operator()(const argument_type& x) const {
  183.     return op(value, x); 
  184.     }
  185. };
  186.  
  187. template <class Operation, class T>
  188. binder1st<Operation> bind1st(const Operation& op, const T& x) {
  189.     return binder1st<Operation>(op, Operation::first_argument_type(x));
  190. }
  191.  
  192. template <class Operation> 
  193. class binder2nd : public unary_function<Operation::first_argument_type,
  194.                     Operation::result_type> {
  195. protected:
  196.     Operation op;
  197.     Operation::second_argument_type value;
  198. public:
  199.     binder2nd(const Operation& x, const Operation::second_argument_type& y) 
  200.     : op(x), value(y) {}
  201.     result_type operator()(const argument_type& x) const {
  202.     return op(x, value); 
  203.     }
  204. };
  205.  
  206. template <class Operation, class T>
  207. binder2nd<Operation> bind2nd(const Operation& op, const T& x) {
  208.     return binder2nd<Operation>(op, Operation::second_argument_type(x));
  209. }
  210.  
  211. template <class Operation1, class Operation2>
  212. class unary_compose : public unary_function<Operation2::argument_type,
  213.                                             Operation1::result_type> {
  214. protected:
  215.     Operation1 op1;
  216.     Operation2 op2;
  217. public:
  218.     unary_compose(const Operation1& x, const Operation2& y) : op1(x), op2(y) {}
  219.     result_type operator()(const argument_type& x) const {
  220.     return op1(op2(x));
  221.     }
  222. };
  223.  
  224. template <class Operation1, class Operation2>
  225. unary_compose<Operation1, Operation2> compose1(const Operation1& op1, 
  226.                            const Operation2& op2) {
  227.     return unary_compose<Operation1, Operation2>(op1, op2);
  228. }
  229.  
  230. template <class Operation1, class Operation2, class Operation3>
  231. class binary_compose : public unary_function<Operation2::argument_type,
  232.                                              Operation1::result_type> {
  233. protected:
  234.     Operation1 op1;
  235.     Operation2 op2;
  236.     Operation3 op3;
  237. public:
  238.     binary_compose(const Operation1& x, const Operation2& y, 
  239.            const Operation3& z) : op1(x), op2(y), op3(z) { }
  240.     result_type operator()(const argument_type& x) const {
  241.     return op1(op2(x), op3(x));
  242.     }
  243. };
  244.  
  245. template <class Operation1, class Operation2, class Operation3>
  246. binary_compose<Operation1, Operation2, Operation3> 
  247. compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3) {
  248.     return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);
  249. }
  250.  
  251. template <class Arg, class Result>
  252. class pointer_to_unary_function : public unary_function<Arg, Result> {
  253. protected:
  254.     Result (*ptr)(Arg);
  255. public:
  256.     pointer_to_unary_function() {}
  257.     pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}
  258.     Result operator()(Arg x) const { return ptr(x); }
  259. };
  260.  
  261. template <class Arg, class Result>
  262. pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg)) {
  263.     return pointer_to_unary_function<Arg, Result>(x);
  264. }
  265.  
  266. template <class Arg1, class Arg2, class Result>
  267. class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> {
  268. protected:
  269.     Result (*ptr)(Arg1, Arg2);
  270. public:
  271.     pointer_to_binary_function() {}
  272.     pointer_to_binary_function(Result (*x)(Arg1, Arg2)) : ptr(x) {}
  273.     Result operator()(Arg1 x, Arg2 y) const { return ptr(x, y); }
  274. };
  275.  
  276. template <class Arg1, class Arg2, class Result>
  277. pointer_to_binary_function<Arg1, Arg2, Result> 
  278. ptr_fun(Result (*x)(Arg1, Arg2)) {
  279.     return pointer_to_binary_function<Arg1, Arg2, Result>(x);
  280. }
  281.  
  282. #endif
  283.