_function.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996-1998
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_FUNCTION_H
  29. #define _STLP_INTERNAL_FUNCTION_H
  30. #ifndef _STLP_TYPE_TRAITS_H
  31. # include <stl/type_traits.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  34. # include <stl/_function_base.h>
  35. #endif
  36. _STLP_BEGIN_NAMESPACE
  37. template <class _Tp>
  38. struct not_equal_to : public binary_function<_Tp, _Tp, bool> {
  39. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x != __y; }
  40. };
  41. template <class _Tp>
  42. struct greater : public binary_function<_Tp, _Tp, bool> {
  43. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x > __y; }
  44. };
  45. template <class _Tp>
  46. struct greater_equal : public binary_function<_Tp, _Tp, bool> {
  47. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x >= __y; }
  48. };
  49. template <class _Tp>
  50. struct less_equal : public binary_function<_Tp, _Tp, bool> {
  51. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x <= __y; }
  52. };
  53. template <class _Tp>
  54. struct divides : public binary_function<_Tp, _Tp, _Tp> {
  55. _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
  56. };
  57. template <class _Tp>
  58. struct modulus : public binary_function<_Tp, _Tp, _Tp> {
  59. _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
  60. };
  61. template <class _Tp>
  62. struct negate : public unary_function<_Tp, _Tp> {
  63. _Tp operator()(const _Tp& __x) const { return -__x; }
  64. };
  65. template <class _Tp>
  66. struct logical_and : public binary_function<_Tp, _Tp, bool> {
  67. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x && __y; }
  68. };
  69. template <class _Tp>
  70. struct logical_or : public binary_function<_Tp, _Tp,bool> {
  71. bool operator()(const _Tp& __x, const _Tp& __y) const { return __x || __y; }
  72. };
  73. template <class _Tp>
  74. struct logical_not : public unary_function<_Tp, bool> {
  75. bool operator()(const _Tp& __x) const { return !__x; }
  76. };
  77. #if !defined (_STLP_NO_EXTENSIONS)
  78. // identity_element (not part of the C++ standard).
  79. template <class _Tp> inline _Tp identity_element(plus<_Tp>) { return _Tp(0); }
  80. template <class _Tp> inline _Tp identity_element(multiplies<_Tp>) { return _Tp(1); }
  81. #endif
  82. #if defined (_STLP_BASE_TYPEDEF_BUG)
  83. // this workaround is needed for SunPro 4.0.1
  84. // suggested by "Martin Abernethy" <gma@paston.co.uk>:
  85. // We have to introduce the XXary_predicate_aux structures in order to
  86. // access the argument and return types of predicate functions supplied
  87. // as type parameters. SUN C++ 4.0.1 compiler gives errors for template type parameters
  88. // of the form 'name1::name2', where name1 is itself a type parameter.
  89. template <class _Pair>
  90. struct __pair_aux : private _Pair {
  91. typedef typename _Pair::first_type first_type;
  92. typedef typename _Pair::second_type second_type;
  93. };
  94. template <class _Operation>
  95. struct __unary_fun_aux : private _Operation {
  96. typedef typename _Operation::argument_type argument_type;
  97. typedef typename _Operation::result_type result_type;
  98. };
  99. template <class _Operation>
  100. struct __binary_fun_aux : private _Operation {
  101. typedef typename _Operation::first_argument_type first_argument_type;
  102. typedef typename _Operation::second_argument_type second_argument_type;
  103. typedef typename _Operation::result_type result_type;
  104. };
  105. # define __UNARY_ARG(__Operation,__type) __unary_fun_aux<__Operation>::__type
  106. # define __BINARY_ARG(__Operation,__type) __binary_fun_aux<__Operation>::__type
  107. # define __PAIR_ARG(__Pair,__type) __pair_aux<__Pair>::__type
  108. #else
  109. # define __UNARY_ARG(__Operation,__type) __Operation::__type
  110. # define __BINARY_ARG(__Operation,__type) __Operation::__type
  111. # define __PAIR_ARG(__Pair,__type) __Pair::__type
  112. #endif
  113. template <class _Predicate>
  114. class unary_negate
  115. : public unary_function<typename __UNARY_ARG(_Predicate, argument_type), bool> {
  116. typedef unary_function<typename __UNARY_ARG(_Predicate, argument_type), bool> _Base;
  117. public:
  118. typedef typename _Base::argument_type argument_type;
  119. private:
  120. typedef typename __call_traits<argument_type>::const_param_type _ArgParamType;
  121. protected:
  122. _Predicate _M_pred;
  123. public:
  124. explicit unary_negate(const _Predicate& __x) : _M_pred(__x) {}
  125. bool operator()(_ArgParamType __x) const {
  126. return !_M_pred(__x);
  127. }
  128. };
  129. template <class _Predicate>
  130. inline unary_negate<_Predicate>
  131. not1(const _Predicate& __pred) {
  132. return unary_negate<_Predicate>(__pred);
  133. }
  134. template <class _Predicate>
  135. class binary_negate
  136. : public binary_function<typename __BINARY_ARG(_Predicate, first_argument_type),
  137. typename __BINARY_ARG(_Predicate, second_argument_type),
  138. bool> {
  139. typedef binary_function<typename __BINARY_ARG(_Predicate, first_argument_type),
  140. typename __BINARY_ARG(_Predicate, second_argument_type),
  141. bool> _Base;
  142. public:
  143. typedef typename _Base::first_argument_type first_argument_type;
  144. typedef typename _Base::second_argument_type second_argument_type;
  145. private:
  146. typedef typename __call_traits<first_argument_type>::const_param_type _FstArgParamType;
  147. typedef typename __call_traits<second_argument_type>::const_param_type _SndArgParamType;
  148. protected:
  149. _Predicate _M_pred;
  150. public:
  151. explicit binary_negate(const _Predicate& __x) : _M_pred(__x) {}
  152. bool operator()(_FstArgParamType __x, _SndArgParamType __y) const {
  153. return !_M_pred(__x, __y);
  154. }
  155. };
  156. template <class _Predicate>
  157. inline binary_negate<_Predicate>
  158. not2(const _Predicate& __pred) {
  159. return binary_negate<_Predicate>(__pred);
  160. }
  161. template <class _Operation>
  162. class binder1st :
  163. public unary_function<typename __BINARY_ARG(_Operation, second_argument_type),
  164. typename __BINARY_ARG(_Operation, result_type) > {
  165. typedef unary_function<typename __BINARY_ARG(_Operation, second_argument_type),
  166. typename __BINARY_ARG(_Operation, result_type) > _Base;
  167. public:
  168. typedef typename _Base::argument_type argument_type;
  169. typedef typename _Base::result_type result_type;
  170. private:
  171. typedef typename __call_traits<argument_type>::param_type _ArgParamType;
  172. typedef typename __call_traits<argument_type>::const_param_type _ConstArgParamType;
  173. typedef typename __call_traits<typename _Operation::first_argument_type>::const_param_type _ValueParamType;
  174. protected:
  175. //op is a Standard name (20.3.6.1), do no make it STLport naming convention compliant.
  176. _Operation op;
  177. typename _Operation::first_argument_type _M_value;
  178. public:
  179. binder1st(const _Operation& __x, _ValueParamType __y)
  180. : op(__x), _M_value(__y) {}
  181. result_type operator()(_ConstArgParamType __x) const
  182. { return op(_M_value, __x); }
  183. // DR 109 Missing binders for non-const sequence elements
  184. result_type operator()(_ArgParamType __x) const
  185. { return op(_M_value, __x); }
  186. };
  187. template <class _Operation, class _Tp>
  188. inline binder1st<_Operation>
  189. bind1st(const _Operation& __fn, const _Tp& __x) {
  190. typedef typename _Operation::first_argument_type _Arg1_type;
  191. return binder1st<_Operation>(__fn, _Arg1_type(__x));
  192. }
  193. template <class _Operation>
  194. class binder2nd
  195. : public unary_function<typename __BINARY_ARG(_Operation, first_argument_type),
  196. typename __BINARY_ARG(_Operation, result_type)> {
  197. typedef unary_function<typename __BINARY_ARG(_Operation, first_argument_type),
  198. typename __BINARY_ARG(_Operation, result_type)> _Base;
  199. public:
  200. typedef typename _Base::argument_type argument_type;
  201. typedef typename _Base::result_type result_type;
  202. private:
  203. typedef typename __call_traits<argument_type>::param_type _ArgParamType;
  204. typedef typename __call_traits<argument_type>::const_param_type _ConstArgParamType;
  205. typedef typename __call_traits<typename _Operation::second_argument_type>::const_param_type _ValueParamType;
  206. protected:
  207. //op is a Standard name (20.3.6.3), do no make it STLport naming convention compliant.
  208. _Operation op;
  209. typename _Operation::second_argument_type value;
  210. public:
  211. binder2nd(const _Operation& __x, _ValueParamType __y)
  212. : op(__x), value(__y) {}
  213. result_type operator()(_ConstArgParamType __x) const
  214. { return op(__x, value); }
  215. // DR 109 Missing binders for non-const sequence elements
  216. result_type operator()(_ArgParamType __x) const
  217. { return op(__x, value); }
  218. };
  219. template <class _Operation, class _Tp>
  220. inline binder2nd<_Operation>
  221. bind2nd(const _Operation& __fn, const _Tp& __x) {
  222. typedef typename _Operation::second_argument_type _Arg2_type;
  223. return binder2nd<_Operation>(__fn, _Arg2_type(__x));
  224. }
  225. #if !defined (_STLP_NO_EXTENSIONS)
  226. // unary_compose and binary_compose (extensions, not part of the standard).
  227. template <class _Operation1, class _Operation2>
  228. class unary_compose :
  229. public unary_function<typename __UNARY_ARG(_Operation2, argument_type),
  230. typename __UNARY_ARG(_Operation1, result_type)> {
  231. typedef unary_function<typename __UNARY_ARG(_Operation2, argument_type),
  232. typename __UNARY_ARG(_Operation1, result_type)> _Base;
  233. public:
  234. typedef typename _Base::argument_type argument_type;
  235. typedef typename _Base::result_type result_type;
  236. private:
  237. typedef typename __call_traits<argument_type>::const_param_type _ArgParamType;
  238. protected:
  239. _Operation1 _M_fn1;
  240. _Operation2 _M_fn2;
  241. public:
  242. unary_compose(const _Operation1& __x, const _Operation2& __y)
  243. : _M_fn1(__x), _M_fn2(__y) {}
  244. result_type operator()(_ArgParamType __x) const {
  245. return _M_fn1(_M_fn2(__x));
  246. }
  247. };
  248. template <class _Operation1, class _Operation2>
  249. inline unary_compose<_Operation1,_Operation2>
  250. compose1(const _Operation1& __fn1, const _Operation2& __fn2) {
  251. return unary_compose<_Operation1,_Operation2>(__fn1, __fn2);
  252. }
  253. template <class _Operation1, class _Operation2, class _Operation3>
  254. class binary_compose :
  255. public unary_function<typename __UNARY_ARG(_Operation2, argument_type),
  256. typename __BINARY_ARG(_Operation1, result_type)> {
  257. typedef unary_function<typename __UNARY_ARG(_Operation2, argument_type),
  258. typename __BINARY_ARG(_Operation1, result_type)> _Base;
  259. public:
  260. typedef typename _Base::argument_type argument_type;
  261. typedef typename _Base::result_type result_type;
  262. private:
  263. typedef typename __call_traits<argument_type>::const_param_type _ArgParamType;
  264. protected:
  265. _Operation1 _M_fn1;
  266. _Operation2 _M_fn2;
  267. _Operation3 _M_fn3;
  268. public:
  269. binary_compose(const _Operation1& __x, const _Operation2& __y,
  270. const _Operation3& __z)
  271. : _M_fn1(__x), _M_fn2(__y), _M_fn3(__z) { }
  272. result_type operator()(_ArgParamType __x) const {
  273. return _M_fn1(_M_fn2(__x), _M_fn3(__x));
  274. }
  275. };
  276. template <class _Operation1, class _Operation2, class _Operation3>
  277. inline binary_compose<_Operation1, _Operation2, _Operation3>
  278. compose2(const _Operation1& __fn1, const _Operation2& __fn2,
  279. const _Operation3& __fn3) {
  280. return binary_compose<_Operation1,_Operation2,_Operation3>(__fn1, __fn2, __fn3);
  281. }
  282. // identity is an extension: it is not part of the standard.
  283. template <class _Tp> struct identity : public _STLP_PRIV _Identity<_Tp> {};
  284. // select1st and select2nd are extensions: they are not part of the standard.
  285. template <class _Pair> struct select1st : public _STLP_PRIV _Select1st<_Pair> {};
  286. template <class _Pair> struct select2nd : public _STLP_PRIV _Select2nd<_Pair> {};
  287. template <class _Arg1, class _Arg2>
  288. struct project1st : public _STLP_PRIV _Project1st<_Arg1, _Arg2> {};
  289. template <class _Arg1, class _Arg2>
  290. struct project2nd : public _STLP_PRIV _Project2nd<_Arg1, _Arg2> {};
  291. // constant_void_fun, constant_unary_fun, and constant_binary_fun are
  292. // extensions: they are not part of the standard. (The same, of course,
  293. // is true of the helper functions constant0, constant1, and constant2.)
  294. _STLP_MOVE_TO_PRIV_NAMESPACE
  295. template <class _Result>
  296. struct _Constant_void_fun {
  297. typedef _Result result_type;
  298. result_type _M_val;
  299. _Constant_void_fun(const result_type& __v) : _M_val(__v) {}
  300. const result_type& operator()() const { return _M_val; }
  301. };
  302. _STLP_MOVE_TO_STD_NAMESPACE
  303. template <class _Result>
  304. struct constant_void_fun : public _STLP_PRIV _Constant_void_fun<_Result> {
  305. constant_void_fun(const _Result& __v)
  306. : _STLP_PRIV _Constant_void_fun<_Result>(__v) {}
  307. };
  308. template <class _Result, _STLP_DFL_TMPL_PARAM( _Argument , _Result) >
  309. struct constant_unary_fun : public _STLP_PRIV _Constant_unary_fun<_Result, _Argument> {
  310. constant_unary_fun(const _Result& __v)
  311. : _STLP_PRIV _Constant_unary_fun<_Result, _Argument>(__v) {}
  312. };
  313. template <class _Result, _STLP_DFL_TMPL_PARAM( _Arg1 , _Result), _STLP_DFL_TMPL_PARAM( _Arg2 , _Arg1) >
  314. struct constant_binary_fun
  315. : public _STLP_PRIV _Constant_binary_fun<_Result, _Arg1, _Arg2> {
  316. constant_binary_fun(const _Result& __v)
  317. : _STLP_PRIV _Constant_binary_fun<_Result, _Arg1, _Arg2>(__v) {}
  318. };
  319. template <class _Result>
  320. inline constant_void_fun<_Result> constant0(const _Result& __val) {
  321. return constant_void_fun<_Result>(__val);
  322. }
  323. template <class _Result>
  324. inline constant_unary_fun<_Result,_Result> constant1(const _Result& __val) {
  325. return constant_unary_fun<_Result,_Result>(__val);
  326. }
  327. template <class _Result>
  328. inline constant_binary_fun<_Result,_Result,_Result>
  329. constant2(const _Result& __val) {
  330. return constant_binary_fun<_Result,_Result,_Result>(__val);
  331. }
  332. // subtractive_rng is an extension: it is not part of the standard.
  333. // Note: this code assumes that int is 32 bits.
  334. class subtractive_rng : public unary_function<_STLP_UINT32_T, _STLP_UINT32_T> {
  335. private:
  336. _STLP_UINT32_T _M_table[55];
  337. _STLP_UINT32_T _M_index1;
  338. _STLP_UINT32_T _M_index2;
  339. public:
  340. _STLP_UINT32_T operator()(_STLP_UINT32_T __limit) {
  341. _M_index1 = (_M_index1 + 1) % 55;
  342. _M_index2 = (_M_index2 + 1) % 55;
  343. _M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
  344. return _M_table[_M_index1] % __limit;
  345. }
  346. void _M_initialize(_STLP_UINT32_T __seed) {
  347. _STLP_UINT32_T __k = 1;
  348. _M_table[54] = __seed;
  349. _STLP_UINT32_T __i;
  350. for (__i = 0; __i < 54; __i++) {
  351. _STLP_UINT32_T __ii = (21 * (__i + 1) % 55) - 1;
  352. _M_table[__ii] = __k;
  353. __k = __seed - __k;
  354. __seed = _M_table[__ii];
  355. }
  356. for (int __loop = 0; __loop < 4; __loop++) {
  357. for (__i = 0; __i < 55; __i++)
  358. _M_table[__i] = _M_table[__i] - _M_table[(1 + __i + 30) % 55];
  359. }
  360. _M_index1 = 0;
  361. _M_index2 = 31;
  362. }
  363. subtractive_rng(unsigned int __seed) { _M_initialize(__seed); }
  364. subtractive_rng() { _M_initialize(161803398ul); }
  365. };
  366. #endif /* _STLP_NO_EXTENSIONS */
  367. _STLP_END_NAMESPACE
  368. #include <stl/_function_adaptors.h>
  369. #endif /* _STLP_INTERNAL_FUNCTION_H */
  370. // Local Variables:
  371. // mode:C++
  372. // End: