algorithm_stl.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #pragma once
  2. #include "ybase/define.h"
  3. #include "yutil/array.hpp"
  4. #include <vector>
  5. #include <map>
  6. /*
  7. * 结构体vector int 成员排序
  8. */
  9. #define SORT_VECTOR_MEMBER(VECTOR,MEMBER,PS) \
  10. { \
  11. for (uint32 i = 0; i < VECTOR.size(); i++) \
  12. { \
  13. for (uint32 x = i; x < VECTOR.size(); x++) \
  14. { \
  15. if (PS == true ? (uint64)VECTOR[i].MEMBER > (uint64)VECTOR[x].MEMBER:VECTOR[i].MEMBER < (uint64)VECTOR[x].MEMBER) \
  16. { \
  17. auto temp = VECTOR[i]; \
  18. VECTOR[i] = VECTOR[x]; \
  19. VECTOR[x] = temp; \
  20. } \
  21. } \
  22. } \
  23. }
  24. /*
  25. * 结构体vector int 成员排序
  26. */
  27. #define SORT_VECTOR_MEMBER_PTR(VECTOR,MEMBER,PS) \
  28. { \
  29. for (uint32 i = 0; i < VECTOR.size(); i++) \
  30. { \
  31. for (uint32 x = i; x < VECTOR.size(); x++) \
  32. { \
  33. if (PS == true ? VECTOR[i]->MEMBER > VECTOR[x]->MEMBER:VECTOR[i]->MEMBER < VECTOR[x]->MEMBER) \
  34. { \
  35. auto temp = VECTOR[i]; \
  36. VECTOR[i] = VECTOR[x]; \
  37. VECTOR[x] = temp; \
  38. } \
  39. } \
  40. } \
  41. }
  42. /*
  43. * 结构体vector int 成员排序
  44. */
  45. #define SORT_VECTOR_MEMBER_PPTR(VECTOR,MEMBER,PS) \
  46. { \
  47. for (uint32 i = 0; i < VECTOR->size(); i++) \
  48. { \
  49. for (uint32 x = i; x < VECTOR->size(); x++) \
  50. { \
  51. if (PS == true ? (*VECTOR)[i]->MEMBER > (*VECTOR)[x]->MEMBER:(*VECTOR)[i]->MEMBER < (*VECTOR)[x]->MEMBER) \
  52. { \
  53. auto temp = VECTOR[i]; \
  54. VECTOR[i] = VECTOR[x]; \
  55. VECTOR[x] = temp; \
  56. } \
  57. } \
  58. } \
  59. }
  60. namespace newobj
  61. {
  62. namespace stl
  63. {
  64. /*
  65. * 排序
  66. * @ps : 正序
  67. */
  68. inline void sort(std::vector<int>& value,bool ps)
  69. {
  70. int temp = 0;
  71. for (size_t i = 0; i < value.size(); i++)
  72. {
  73. for (size_t x = i; x < value.size(); x++)
  74. {
  75. if (ps==true?value[i] > value[x]:value[i] < value[x])
  76. {
  77. temp = value[i];
  78. value[i] = value[x];
  79. value[x] = temp;
  80. }
  81. }
  82. }
  83. }
  84. /*
  85. * 排序
  86. * @ps : 正序
  87. */
  88. template<typename T>
  89. inline void sort(std::vector<int>& value,std::vector<T> &extra, bool ps)
  90. {
  91. int temp = 0;
  92. for (uint32 i = 0; i < value.size(); i++)
  93. {
  94. for (uint32 x = i; x < value.size(); x++)
  95. {
  96. if (ps == true ? value[i] > value[x]:value[i] < value[x])
  97. {
  98. temp = value[i];
  99. value[i] = value[x];
  100. value[x] = temp;
  101. auto t = extra[i];
  102. extra[i] = extra[x];
  103. extra[x] = t;
  104. }
  105. }
  106. }
  107. }
  108. /*
  109. * 分页专用
  110. * @ps : 排序
  111. */
  112. template<typename KEY, typename VAL>
  113. inline std::map<KEY, VAL> limit(const std::map<KEY, VAL>& value,uint32 start,uint32 length,bool ps)
  114. {
  115. std::map<KEY, VAL> result;
  116. uint32 idx = 0;
  117. if (ps)
  118. {
  119. for_iter(iter, value)
  120. {
  121. if (idx >= start && idx < start + length)
  122. result.insert(std::pair<KEY, VAL>(iter->first, iter->second));
  123. else if (idx > start + length)
  124. break;
  125. idx++;
  126. }
  127. }
  128. else
  129. {
  130. for_riter(iter, value)
  131. {
  132. if (idx >= start && idx < start + length)
  133. result.insert(std::pair<KEY, VAL>(iter->first, iter->second));
  134. else if (idx > start + length)
  135. break;
  136. idx++;
  137. }
  138. }
  139. return result;
  140. }
  141. template<typename T>
  142. inline std::vector<T> limit(const std::vector<T>& value, uint32 start, uint32 length, bool ps)
  143. {
  144. std::vector<T> result;
  145. uint32 idx = 0;
  146. if (ps)
  147. {
  148. for_iter(iter, value)
  149. {
  150. if (idx >= start && idx < start + length)
  151. result.push_back(*iter);
  152. else if (idx > start + length)
  153. break;
  154. idx++;
  155. }
  156. }
  157. else
  158. {
  159. for_riter(iter, value)
  160. {
  161. if (idx >= start && idx < start + length)
  162. result.push_back(*iter);
  163. else if (idx > start + length)
  164. break;
  165. idx++;
  166. }
  167. }
  168. return result;
  169. }
  170. /*
  171. * std::map转std::vector
  172. */
  173. template<typename KEY,typename VAL>
  174. inline std::vector<VAL> to_vector_val(const std::map<KEY, VAL>& value)
  175. {
  176. std::vector<VAL> result;
  177. for_iter(iter, value)
  178. result.push_back(iter->second);
  179. return result;
  180. }
  181. }
  182. }