array_fun.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <cstdlib>
  38. #include <functional>
  39. namespace aria2 {
  40. template<typename T>
  41. class bit_negate:public std::unary_function<T, T> {
  42. public:
  43. T operator()(const T& t) const
  44. {
  45. return ~t;
  46. }
  47. };
  48. template<typename T>
  49. class bit_and:public std::binary_function<T, T, T> {
  50. public:
  51. T operator()(const T& t1, const T& t2) const
  52. {
  53. return t1&t2;
  54. }
  55. };
  56. template<typename R>
  57. class array_function_base {
  58. public:
  59. virtual ~array_function_base() {}
  60. virtual R operator[](size_t index) const = 0;
  61. virtual array_function_base* clone() const = 0;
  62. };
  63. template<typename A, typename F>
  64. class array_unary_function:public array_function_base<typename F::result_type> {
  65. private:
  66. A _a;
  67. F _f;
  68. public:
  69. array_unary_function(A a, F f):_a(a), _f(f) {}
  70. virtual typename F::result_type operator[](size_t index) const
  71. {
  72. return _f(_a[index]);
  73. }
  74. virtual array_function_base<typename F::result_type>* clone() const
  75. {
  76. return new array_unary_function(*this);
  77. }
  78. };
  79. template<typename A, typename B, typename F>
  80. class array_binary_function:public array_function_base<typename F::result_type>{
  81. private:
  82. A _a;
  83. B _b;
  84. F _f;
  85. public:
  86. array_binary_function(A a, B b, F f):_a(a), _b(b), _f(f) {}
  87. virtual typename F::result_type operator[](size_t index) const
  88. {
  89. return _f(_a[index], _b[index]);
  90. }
  91. virtual array_function_base<typename F::result_type>* clone() const
  92. {
  93. return new array_binary_function(*this);
  94. }
  95. };
  96. template<typename R>
  97. class array_fun {
  98. private:
  99. array_function_base<R>* _p;
  100. public:
  101. template<typename A, typename F>
  102. array_fun(A a, F f):_p(new array_unary_function<A, F>(a, f)) {}
  103. template<typename A, typename B, typename F>
  104. array_fun(A a, B b, F f):_p(new array_binary_function<A, B, F>(a, b, f)) {}
  105. array_fun(const array_fun& af):_p(af._p->clone()) {}
  106. ~array_fun()
  107. {
  108. delete _p;
  109. }
  110. array_fun& operator=(const array_fun& af)
  111. {
  112. if(this != &af) {
  113. delete _p;
  114. _p = af._p->clone();
  115. }
  116. return *this;
  117. }
  118. R operator[](size_t index) const
  119. {
  120. return (*_p)[index];
  121. }
  122. typedef R result_type;
  123. };
  124. template<typename R, typename A>
  125. array_fun<R>
  126. array_negate(A a)
  127. {
  128. return array_fun<R>(a, bit_negate<R>());
  129. }
  130. template<typename T>
  131. array_fun<T>
  132. array_negate(T* a)
  133. {
  134. return array_fun<T>(a, bit_negate<T>());
  135. }
  136. template<typename A>
  137. array_fun<typename A::result_type>
  138. array_negate(A a)
  139. {
  140. return array_fun<typename A::result_type>(a, bit_negate<typename A::result_type>());
  141. }
  142. template<typename R, typename A, typename B>
  143. array_fun<R>
  144. array_and(A a, B b)
  145. {
  146. return array_fun<R>(a, b, bit_and<R>());
  147. }
  148. template<typename T>
  149. array_fun<T>
  150. array_and(T* a, T* b)
  151. {
  152. return array_fun<T>(a, b, bit_and<T>());
  153. }
  154. template<typename T>
  155. array_fun<typename T::result_type>
  156. array_and(T a, T b)
  157. {
  158. return array_fun<typename T::result_type>(a, b, bit_and<typename T::result_type>());
  159. }
  160. template<typename A, typename B>
  161. array_fun<typename A::result_type>
  162. array_and(A a, B b)
  163. {
  164. return array_fun<typename A::result_type>(a, b, bit_and<typename A::result_type>());
  165. }
  166. template<typename A, typename B>
  167. array_fun<typename B::result_type>
  168. array_and(A a, B b)
  169. {
  170. return array_fun<typename B::result_type>(a, b, bit_and<typename B::result_type>());
  171. }
  172. // calculate length of array
  173. template<typename T, size_t N>
  174. char (&char_array_ref(T (&)[N]))[N];
  175. template<typename T, size_t N>
  176. size_t arrayLength(T (&a)[N])
  177. {
  178. return sizeof(char_array_ref(a));
  179. }
  180. template<typename T>
  181. size_t arrayLength(T (&a)[0u])
  182. {
  183. return 0;
  184. }
  185. } // namespace aria2
  186. #endif // _D_ARRAY_FUN_H_