Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

476 lines
9.5 KiB

  1. #pragma once
  2. #ifndef _STLFUNC_H_
  3. #define _STLFUNC_H_
  4. //#include <xstddef>
  5. #include <stddef.h>
  6. #ifdef _MSC_VER
  7. #pragma pack(push,8)
  8. #endif /* _MSC_VER */
  9. _STD_BEGIN
  10. // TEMPLATE STRUCT unary_function
  11. template<class _A, class _R>
  12. struct unary_function
  13. {
  14. typedef _A argument_type;
  15. typedef _R result_type;
  16. };
  17. // TEMPLATE STRUCT binary_function
  18. template<class _A1, class _A2, class _R>
  19. struct binary_function
  20. {
  21. typedef _A1 first_argument_type;
  22. typedef _A2 second_argument_type;
  23. typedef _R result_type;
  24. };
  25. // TEMPLATE STRUCT plus
  26. template<class _Ty>
  27. struct plus : binary_function<_Ty, _Ty, _Ty>
  28. {
  29. _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  30. {
  31. return (_X + _Y);
  32. }
  33. };
  34. // TEMPLATE STRUCT minus
  35. template<class _Ty>
  36. struct minus : binary_function<_Ty, _Ty, _Ty>
  37. {
  38. _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  39. {
  40. return (_X - _Y);
  41. }
  42. };
  43. // TEMPLATE STRUCT multiplies
  44. template<class _Ty>
  45. struct multiplies : binary_function<_Ty, _Ty, _Ty>
  46. {
  47. _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  48. {
  49. return (_X * _Y);
  50. }
  51. };
  52. // TEMPLATE STRUCT divides
  53. template<class _Ty>
  54. struct divides : binary_function<_Ty, _Ty, _Ty>
  55. {
  56. _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  57. {
  58. return (_X / _Y);
  59. }
  60. };
  61. // TEMPLATE STRUCT modulus
  62. template<class _Ty>
  63. struct modulus : binary_function<_Ty, _Ty, _Ty>
  64. {
  65. _Ty operator()(const _Ty& _X, const _Ty& _Y) const
  66. {
  67. return (_X % _Y);
  68. }
  69. };
  70. // TEMPLATE STRUCT negate
  71. template<class _Ty>
  72. struct negate : unary_function<_Ty, _Ty>
  73. {
  74. _Ty operator()(const _Ty& _X) const
  75. {
  76. return (-_X);
  77. }
  78. };
  79. // TEMPLATE STRUCT equal_to
  80. template<class _Ty>
  81. struct equal_to : binary_function<_Ty, _Ty, bool>
  82. {
  83. bool operator()(const _Ty& _X, const _Ty& _Y) const
  84. {
  85. return (_X == _Y);
  86. }
  87. };
  88. // TEMPLATE STRUCT not_equal_to
  89. template<class _Ty>
  90. struct not_equal_to : binary_function<_Ty, _Ty, bool>
  91. {
  92. bool operator()(const _Ty& _X, const _Ty& _Y) const
  93. {
  94. return (_X != _Y);
  95. }
  96. };
  97. // TEMPLATE STRUCT greater
  98. template<class _Ty>
  99. struct greater : binary_function<_Ty, _Ty, bool>
  100. {
  101. bool operator()(const _Ty& _X, const _Ty& _Y) const
  102. {
  103. return (_X > _Y);
  104. }
  105. };
  106. // TEMPLATE STRUCT less
  107. template<class _Ty>
  108. struct less : binary_function<_Ty, _Ty, bool>
  109. {
  110. bool operator()(const _Ty& _X, const _Ty& _Y) const
  111. {
  112. return (_X < _Y);
  113. }
  114. };
  115. // TEMPLATE STRUCT greater_equal
  116. template<class _Ty>
  117. struct greater_equal : binary_function<_Ty, _Ty, bool>
  118. {
  119. bool operator()(const _Ty& _X, const _Ty& _Y) const
  120. {
  121. return (_X >= _Y);
  122. }
  123. };
  124. // TEMPLATE STRUCT less_equal
  125. template<class _Ty>
  126. struct less_equal : binary_function<_Ty, _Ty, bool>
  127. {
  128. bool operator()(const _Ty& _X, const _Ty& _Y) const
  129. {
  130. return (_X <= _Y);
  131. }
  132. };
  133. // TEMPLATE STRUCT logical_and
  134. template<class _Ty>
  135. struct logical_and : binary_function<_Ty, _Ty, bool>
  136. {
  137. bool operator()(const _Ty& _X, const _Ty& _Y) const
  138. {
  139. return (_X && _Y);
  140. }
  141. };
  142. // TEMPLATE STRUCT logical_or
  143. template<class _Ty>
  144. struct logical_or : binary_function<_Ty, _Ty, bool>
  145. {
  146. bool operator()(const _Ty& _X, const _Ty& _Y) const
  147. {
  148. return (_X || _Y);
  149. }
  150. };
  151. // TEMPLATE STRUCT logical_not
  152. template<class _Ty>
  153. struct logical_not : unary_function<_Ty, bool>
  154. {
  155. bool operator()(const _Ty& _X) const
  156. {
  157. return (!_X);
  158. }
  159. };
  160. // TEMPLATE CLASS unary_negate
  161. template<class _Ufn>
  162. class unary_negate
  163. : public unary_function<_Ufn::argument_type, bool>
  164. {
  165. public:
  166. explicit unary_negate(const _Ufn& _X)
  167. : _Fn(_X)
  168. {
  169. }
  170. bool operator()(const _Ufn::argument_type& _X) const
  171. {
  172. return (!_Fn(_X));
  173. }
  174. protected:
  175. _Ufn _Fn;
  176. };
  177. // TEMPLATE FUNCTION not1
  178. template<class _Ufn> inline
  179. unary_negate<_Ufn> not1(const _Ufn& _X)
  180. {
  181. return (unary_negate<_Ufn>(_X));
  182. }
  183. // TEMPLATE CLASS binary_negate
  184. template<class _Bfn>
  185. class binary_negate
  186. : public binary_function<_Bfn::first_argument_type,
  187. _Bfn::second_argument_type, bool>
  188. {
  189. public:
  190. explicit binary_negate(const _Bfn& _X)
  191. : _Fn(_X)
  192. {
  193. }
  194. bool operator()(const _Bfn::first_argument_type& _X,
  195. const _Bfn::second_argument_type& _Y) const
  196. {
  197. return (!_Fn(_X, _Y));
  198. }
  199. protected:
  200. _Bfn _Fn;
  201. };
  202. // TEMPLATE FUNCTION not2
  203. template<class _Bfn> inline
  204. binary_negate<_Bfn> not2(const _Bfn& _X)
  205. {
  206. return (binary_negate<_Bfn>(_X));
  207. }
  208. // TEMPLATE CLASS binder1st
  209. template<class _Bfn>
  210. class binder1st
  211. : public unary_function<_Bfn::second_argument_type,
  212. _Bfn::result_type>
  213. {
  214. public:
  215. binder1st(const _Bfn& _X,
  216. const _Bfn::first_argument_type& _Y)
  217. : op(_X), value(_Y)
  218. {
  219. }
  220. result_type operator()(const argument_type& _X) const
  221. {
  222. return (op(value, _X));
  223. }
  224. protected:
  225. _Bfn op;
  226. _Bfn::first_argument_type value;
  227. };
  228. // TEMPLATE FUNCTION bind1st
  229. template<class _Bfn, class _Ty> inline
  230. binder1st<_Bfn> bind1st(const _Bfn& _X, const _Ty& _Y)
  231. {
  232. return (binder1st<_Bfn>(_X,
  233. _Bfn::first_argument_type(_Y)));
  234. }
  235. // TEMPLATE CLASS binder2nd
  236. template<class _Bfn>
  237. class binder2nd
  238. : public unary_function<_Bfn::first_argument_type,
  239. _Bfn::result_type>
  240. {
  241. public:
  242. binder2nd(const _Bfn& _X,
  243. const _Bfn::second_argument_type& _Y)
  244. : op(_X), value(_Y)
  245. {
  246. }
  247. result_type operator()(const argument_type& _X) const
  248. {
  249. return (op(_X, value));
  250. }
  251. protected:
  252. _Bfn op;
  253. _Bfn::second_argument_type value;
  254. };
  255. // TEMPLATE FUNCTION bind2nd
  256. template<class _Bfn, class _Ty> inline
  257. binder2nd<_Bfn> bind2nd(const _Bfn& _X, const _Ty& _Y)
  258. {
  259. return (binder2nd<_Bfn>(_X,
  260. _Bfn::second_argument_type(_Y)));
  261. }
  262. // TEMPLATE CLASS pointer_to_unary_function
  263. template<class _A, class _R>
  264. class pointer_to_unary_function
  265. : public unary_function<_A, _R>
  266. {
  267. public:
  268. explicit pointer_to_unary_function(_R (__cdecl *_X)(_A))
  269. : _Fn(_X)
  270. {
  271. }
  272. _R operator()(_A _X) const
  273. {
  274. return (_Fn(_X));
  275. }
  276. protected:
  277. _R (__cdecl *_Fn)(_A);
  278. };
  279. // TEMPLATE CLASS pointer_to_binary_function
  280. template<class _A1, class _A2, class _R>
  281. class pointer_to_binary_function
  282. : public binary_function<_A1, _A2, _R>
  283. {
  284. public:
  285. explicit pointer_to_binary_function(
  286. _R (__cdecl *_X)(_A1, _A2))
  287. : _Fn(_X)
  288. {
  289. }
  290. _R operator()(_A1 _X, _A2 _Y) const
  291. {
  292. return (_Fn(_X, _Y));
  293. }
  294. protected:
  295. _R (__cdecl *_Fn)(_A1, _A2);
  296. };
  297. // TEMPLATE FUNCTION ptr_fun
  298. template<class _A, class _R> inline
  299. pointer_to_unary_function<_A, _R>
  300. ptr_fun(_R (__cdecl *_X)(_A))
  301. {
  302. return (pointer_to_unary_function<_A, _R>(_X));
  303. }
  304. template<class _A1, class _A2, class _R> inline
  305. pointer_to_binary_function<_A1, _A2, _R>
  306. ptr_fun(_R (__cdecl *_X)(_A1, _A2))
  307. {
  308. return (pointer_to_binary_function<_A1, _A2, _R>(_X));
  309. }
  310. // TEMPLATE CLASS mem_fun_t
  311. template<class _R, class _Ty>
  312. class mem_fun_t : public unary_function<_Ty *, _R>
  313. {
  314. public:
  315. explicit mem_fun_t(_R (_Ty::*_Pm)())
  316. : _Ptr(_Pm)
  317. {
  318. }
  319. _R operator()(_Ty *_P)
  320. {
  321. return ((_P->*_Ptr)());
  322. }
  323. private:
  324. _R (_Ty::*_Ptr)();
  325. };
  326. // TEMPLATE FUNCTION mem_fun
  327. template<class _R, class _Ty> inline
  328. mem_fun_t<_R, _Ty> mem_fun(_R (_Ty::*_Pm)())
  329. {
  330. return (mem_fun_t<_R, _Ty>(_Pm));
  331. }
  332. // TEMPLATE CLASS mem_fun1_t
  333. template<class _R, class _Ty, class _A>
  334. class mem_fun1_t : public binary_function<_Ty *, _A, _R>
  335. {
  336. public:
  337. explicit mem_fun1_t(_R (_Ty::*_Pm)(_A))
  338. : _Ptr(_Pm)
  339. {
  340. }
  341. _R operator()(_Ty *_P, _A _Arg)
  342. {
  343. return ((_P->*_Ptr)(_Arg));
  344. }
  345. private:
  346. _R (_Ty::*_Ptr)(_A);
  347. };
  348. // TEMPLATE FUNCTION mem_fun1
  349. template<class _R, class _Ty, class _A> inline
  350. mem_fun1_t<_R, _Ty, _A> mem_fun1(_R (_Ty::*_Pm)(_A))
  351. {
  352. return (mem_fun1_t<_R, _Ty, _A>(_Pm));
  353. }
  354. // TEMPLATE CLASS mem_fun_ref_t
  355. template<class _R, class _Ty>
  356. class mem_fun_ref_t : public unary_function<_Ty *, _R>
  357. {
  358. public:
  359. explicit mem_fun_ref_t(_R (_Ty::*_Pm)())
  360. : _Ptr(_Pm)
  361. {
  362. }
  363. _R operator()(_Ty& _X)
  364. {
  365. return ((_X.*_Ptr)());
  366. }
  367. private:
  368. _R (_Ty::*_Ptr)();
  369. };
  370. // TEMPLATE FUNCTION mem_fun_ref
  371. template<class _R, class _Ty> inline
  372. mem_fun_ref_t<_R, _Ty> mem_fun_ref(_R (_Ty::*_Pm)())
  373. {
  374. return (mem_fun_ref_t<_R, _Ty>(_Pm));
  375. }
  376. // TEMPLATE CLASS mem_fun1_ref_t
  377. template<class _R, class _Ty, class _A>
  378. class mem_fun1_ref_t : public binary_function<_Ty *, _A, _R>
  379. {
  380. public:
  381. explicit mem_fun1_ref_t(_R (_Ty::*_Pm)(_A))
  382. : _Ptr(_Pm)
  383. {
  384. }
  385. _R operator()(_Ty& _X, _A _Arg)
  386. {
  387. return ((_X.*_Ptr)(_Arg));
  388. }
  389. private:
  390. _R (_Ty::*_Ptr)(_A);
  391. };
  392. // TEMPLATE FUNCTION mem_fun1_ref
  393. template<class _R, class _Ty, class _A> inline
  394. mem_fun1_ref_t<_R, _Ty, _A> mem_fun1_ref(_R (_Ty::*_Pm)(_A))
  395. {
  396. return (mem_fun1_ref_t<_R, _Ty, _A>(_Pm));
  397. }
  398. _STD_END
  399. #ifdef _MSC_VER
  400. #pragma pack(pop)
  401. #endif /* _MSC_VER */
  402. #endif /* _STLFUNC_H_ */
  403. /*
  404. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  405. * Consult your license regarding permissions and restrictions.
  406. */
  407. /*
  408. * This file is derived from software bearing the following
  409. * restrictions:
  410. *
  411. * Copyright (c) 1994
  412. * Hewlett-Packard Company
  413. *
  414. * Permission to use, copy, modify, distribute and sell this
  415. * software and its documentation for any purpose is hereby
  416. * granted without fee, provided that the above copyright notice
  417. * appear in all copies and that both that copyright notice and
  418. * this permission notice appear in supporting documentation.
  419. * Hewlett-Packard Company makes no representations about the
  420. * suitability of this software for any purpose. It is provided
  421. * "as is" without express or implied warranty.
  422. */