array_fun.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #ifndef _D_ARRAY_FUN_H_
  36. #define _D_ARRAY_FUN_H_
  37. #include <functional>
  38. namespace aria2 {
  39. template<typename T>
  40. class bit_negate:public std::unary_function<T, T> {
  41. public:
  42. T operator()(const T& t) const
  43. {
  44. return ~t;
  45. }
  46. };
  47. template<typename T>
  48. class bit_and:public std::binary_function<T, T, T> {
  49. public:
  50. T operator()(const T& t1, const T& t2) const
  51. {
  52. return t1&t2;
  53. }
  54. };
  55. template<typename R>
  56. class array_function_base {
  57. public:
  58. virtual ~array_function_base() {}
  59. virtual R operator[](std::size_t index) const = 0;
  60. virtual array_function_base* clone() const = 0;
  61. };
  62. template<typename A, typename F>
  63. class array_unary_function:public array_function_base<typename F::result_type> {
  64. private:
  65. A _a;
  66. F _f;
  67. public:
  68. array_unary_function(A a, F f):_a(a), _f(f) {}
  69. virtual typename F::result_type operator[](std::size_t index) const
  70. {
  71. return _f(_a[index]);
  72. }
  73. virtual array_function_base<typename F::result_type>* clone() const
  74. {
  75. return new array_unary_function(*this);
  76. }
  77. };
  78. template<typename A, typename B, typename F>
  79. class array_binary_function:public array_function_base<typename F::result_type>{
  80. private:
  81. A _a;
  82. B _b;
  83. F _f;
  84. public:
  85. array_binary_function(A a, B b, F f):_a(a), _b(b), _f(f) {}
  86. virtual typename F::result_type operator[](std::size_t index) const
  87. {
  88. return _f(_a[index], _b[index]);
  89. }
  90. virtual array_function_base<typename F::result_type>* clone() const
  91. {
  92. return new array_binary_function(*this);
  93. }
  94. };
  95. template<typename R>
  96. class array_fun {
  97. private:
  98. array_function_base<R>* _p;
  99. public:
  100. template<typename A, typename F>
  101. array_fun(A a, F f):_p(new array_unary_function<A, F>(a, f)) {}
  102. template<typename A, typename B, typename F>
  103. array_fun(A a, B b, F f):_p(new array_binary_function<A, B, F>(a, b, f)) {}
  104. array_fun(const array_fun& af):_p(af._p->clone()) {}
  105. ~array_fun()
  106. {
  107. delete _p;
  108. }
  109. array_fun& operator=(const array_fun& af)
  110. {
  111. if(this != &af) {
  112. delete _p;
  113. _p = af._p->clone();
  114. }
  115. return *this;
  116. }
  117. R operator[](std::size_t index) const
  118. {
  119. return (*_p)[index];
  120. }
  121. typedef R result_type;
  122. };
  123. template<typename R, typename A>
  124. array_fun<R>
  125. array_negate(A a)
  126. {
  127. return array_fun<R>(a, bit_negate<R>());
  128. }
  129. template<typename T>
  130. array_fun<T>
  131. array_negate(T* a)
  132. {
  133. return array_fun<T>(a, bit_negate<T>());
  134. }
  135. template<typename A>
  136. array_fun<typename A::result_type>
  137. array_negate(A a)
  138. {
  139. return array_fun<typename A::result_type>(a, bit_negate<typename A::result_type>());
  140. }
  141. template<typename R, typename A, typename B>
  142. array_fun<R>
  143. array_and(A a, B b)
  144. {
  145. return array_fun<R>(a, b, bit_and<R>());
  146. }
  147. template<typename T>
  148. array_fun<T>
  149. array_and(T* a, T* b)
  150. {
  151. return array_fun<T>(a, b, bit_and<T>());
  152. }
  153. template<typename T>
  154. array_fun<typename T::result_type>
  155. array_and(T a, T b)
  156. {
  157. return array_fun<typename T::result_type>(a, b, bit_and<typename T::result_type>());
  158. }
  159. template<typename A, typename B>
  160. array_fun<typename A::result_type>
  161. array_and(A a, B b)
  162. {
  163. return array_fun<typename A::result_type>(a, b, bit_and<typename A::result_type>());
  164. }
  165. template<typename A, typename B>
  166. array_fun<typename B::result_type>
  167. array_and(A a, B b)
  168. {
  169. return array_fun<typename B::result_type>(a, b, bit_and<typename B::result_type>());
  170. }
  171. // calculate length of array
  172. template<typename T, std::size_t N>
  173. char (&char_array_ref(T (&)[N]))[N];
  174. template<typename T, std::size_t N>
  175. std::size_t
  176. arrayLength(T (&a)[N])
  177. {
  178. return sizeof(char_array_ref(a));
  179. }
  180. template<typename T>
  181. std::size_t
  182. arrayLength(T (&a)[0u])
  183. {
  184. return 0;
  185. }
  186. } // namespace aria2
  187. #endif // _D_ARRAY_FUN_H_