Triplet.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2010 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_TRIPLET_H
  36. #define D_TRIPLET_H
  37. #include <cstdlib>
  38. #include <utility>
  39. namespace aria2 {
  40. template<typename T1, typename T2, typename T3>
  41. struct Triplet {
  42. typedef T1 first_type;
  43. typedef T2 second_type;
  44. typedef T3 third_type;
  45. T1 first;
  46. T2 second;
  47. T3 third;
  48. Triplet() {}
  49. Triplet(const T1& t1, const T2& t2, const T3& t3):
  50. first(t1), second(t2), third(t3) {}
  51. template<typename U1, typename U2, typename U3>
  52. Triplet(const Triplet<U1, U2, U3>& t):
  53. first(t.first), second(t.second), third(t.third) {}
  54. Triplet& operator=(const Triplet& tri)
  55. {
  56. if(this != &tri) {
  57. first = tri.first;
  58. second = tri.second;
  59. third = tri.third;
  60. }
  61. return *this;
  62. }
  63. };
  64. template<typename T1, typename T2, typename T3>
  65. bool operator<(const Triplet<T1, T2, T3>& lhs, const Triplet<T1, T2, T3>& rhs)
  66. {
  67. return lhs.first < rhs.first ||
  68. (!(rhs.first < lhs.first) && (lhs.second < rhs.second ||
  69. (!(rhs.second < lhs.second) &&
  70. lhs.third < rhs.third)));
  71. }
  72. template<typename T1, typename T2, typename T3>
  73. Triplet<T1, T2, T3> makeTriplet(const T1& t1, const T2& t2, const T3& t3)
  74. {
  75. return Triplet<T1, T2, T3>(t1, t2, t3);
  76. }
  77. template<class Tuple, size_t N>
  78. struct TupleNthType;
  79. template<class Tuple>
  80. struct TupleNthType<Tuple, 1> {
  81. typedef typename Tuple::first_type type;
  82. };
  83. template<class Tuple>
  84. struct TupleNthType<Tuple, 2> {
  85. typedef typename Tuple::second_type type;
  86. };
  87. template<class Tuple>
  88. struct TupleNthType<Tuple, 3> {
  89. typedef typename Tuple::third_type type;
  90. };
  91. template<size_t N>
  92. struct TupleGet;
  93. template<>
  94. struct TupleGet<1> {
  95. template<class Tuple>
  96. static typename TupleNthType<Tuple, 1>::type get(const Tuple& tri)
  97. {
  98. return tri.first;
  99. }
  100. };
  101. template<>
  102. struct TupleGet<2> {
  103. template<class Tuple>
  104. static typename TupleNthType<Tuple, 2>::type get(const Tuple& tri)
  105. {
  106. return tri.second;
  107. }
  108. };
  109. template<>
  110. struct TupleGet<3> {
  111. template<class Tuple>
  112. static typename TupleNthType<Tuple, 3>::type get(const Tuple& tri)
  113. {
  114. return tri.third;
  115. }
  116. };
  117. template<size_t N1, size_t N2>
  118. class Tuple2Pair {
  119. public:
  120. template<class Tuple>
  121. std::pair<typename TupleNthType<Tuple, N1>::type,
  122. typename TupleNthType<Tuple, N2>::type>
  123. operator()(const Tuple& tri) const
  124. {
  125. return std::make_pair<typename TupleNthType<Tuple, N1>::type,
  126. typename TupleNthType<Tuple, N2>::type>
  127. (TupleGet<N1>::get(tri), TupleGet<N2>::get(tri));
  128. }
  129. };
  130. } // namespace aria2
  131. #endif // D_TRIPLET_H