array_fun.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "common.h"
  38. #include <cstdlib>
  39. #include <functional>
  40. namespace aria2 {
  41. template<typename T, size_t N>
  42. constexpr size_t arraySize(T (&)[N])
  43. {
  44. return N;
  45. }
  46. template<typename T, size_t N>
  47. class array_wrapper {
  48. private:
  49. T array_[N];
  50. public:
  51. array_wrapper() {}
  52. operator T*()
  53. {
  54. return array_;
  55. }
  56. operator const T*() const
  57. {
  58. return array_;
  59. }
  60. size_t size() const
  61. {
  62. return N;
  63. }
  64. };
  65. // Expression Template for array
  66. namespace expr {
  67. template<typename L, typename OpTag, typename R>
  68. struct BinExpr {
  69. BinExpr(L l, R r):l_(l), r_(r) {}
  70. typedef typename OpTag::returnType returnType;
  71. returnType operator[](size_t index) const
  72. {
  73. return OpTag::apply(l_[index], r_[index]);
  74. }
  75. const L l_;
  76. const R r_;
  77. };
  78. template<typename OpTag, typename A>
  79. struct UnExpr {
  80. UnExpr(A a):a_(a) {}
  81. typedef typename OpTag::returnType returnType;
  82. returnType operator[](size_t index) const
  83. {
  84. return OpTag::apply(a_[index]);
  85. }
  86. const A a_;
  87. };
  88. template<typename T>
  89. struct And
  90. {
  91. typedef T returnType;
  92. static inline returnType apply(T lhs, T rhs) { return lhs&rhs; }
  93. };
  94. template<typename T>
  95. struct Or
  96. {
  97. typedef T returnType;
  98. static inline returnType apply(T lhs, T rhs) { return lhs|rhs; }
  99. };
  100. template<typename T>
  101. struct Negate
  102. {
  103. typedef T returnType;
  104. static inline returnType apply(T a) { return ~a; }
  105. };
  106. template<typename T>
  107. struct Array
  108. {
  109. typedef T returnType;
  110. Array(const T* t):t_(t) {}
  111. const T* t_;
  112. returnType operator[](size_t index) const { return t_[index]; }
  113. };
  114. template<typename T>
  115. Array<T>
  116. array(const T* t) { return Array<T>(t); }
  117. template<typename L, typename R>
  118. BinExpr<L, And<typename L::returnType>, R>
  119. operator&(const L& l, const R& r)
  120. {
  121. return BinExpr<L, And<typename L::returnType>, R>(l, r);
  122. }
  123. template<typename L, typename R>
  124. BinExpr<L, Or<typename L::returnType>, R>
  125. operator|(const L& l, const R& r)
  126. {
  127. return BinExpr<L, Or<typename L::returnType>, R>(l, r);
  128. }
  129. template<typename A>
  130. UnExpr<Negate<typename A::returnType>, A>
  131. operator~(const A& a)
  132. {
  133. return UnExpr<Negate<typename A::returnType>, A>(a);
  134. }
  135. } // namespace expr
  136. } // namespace aria2
  137. #endif // D_ARRAY_FUN_H