Iterator.Extension.tlh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #pragma once
  2. #include <numeric>
  3. #include "Iterator.Base.tlh"
  4. #include "Iterator.Filter.tlh"
  5. #include "Iterator.Select.tlh"
  6. #include "Iterator.Indirect.tlh"
  7. #include "Iterator.Zip.tlh"
  8. #include "Iterator.Concat.tlh"
  9. #include "Iterator.Distinct.tlh"
  10. //-----------------------------------------------------------------------------
  11. // Helper
  12. //-----------------------------------------------------------------------------
  13. namespace Iterator
  14. {
  15. template <typename ForIterator>
  16. class Extension : public ForIterator
  17. {
  18. typedef ForIterator inherited;
  19. public:
  20. using data_type = typename ForIterator::data_type;
  21. using value_type = typename ForIterator::value_type;
  22. // using deref_type = typename ForIterator::deref_type;
  23. public:
  24. // Extension () = delete;
  25. // Extension (const Extension &) = delete;
  26. // Extension (Extension &&) = delete;
  27. // Extension & operator = (const Extension &) = delete;
  28. // Extension & operator = (Extension &&) = delete;
  29. // Extension (const Extension &) = default;
  30. // Extension (Extension &&) = default;
  31. // Extension & operator = (const Extension &) = default;
  32. // Extension & operator = (Extension &&) = default;
  33. public:
  34. Extension () { }
  35. Extension (ForIterator & Iter) : inherited (Iter) { }
  36. public:
  37. inline Extension begin ()
  38. {
  39. Extension Tmp (*this);
  40. Tmp.Restart ();
  41. return Tmp;
  42. }
  43. inline Extension end ()
  44. {
  45. Extension Tmp (*this);
  46. Tmp.GoToEnd ();
  47. return Tmp;
  48. }
  49. public:
  50. // 过滤
  51. template <typename Predicate>
  52. auto Where (const Predicate & pred) -> Extension <Filter_Iterator <typename Predicate, inherited>>
  53. {
  54. using EFT = Filter_Iterator <typename Predicate, inherited>;
  55. Extension <EFT> Ex (EFT (pred, *this));
  56. return Ex;
  57. }
  58. #if 1
  59. // 转换
  60. // 注: Select 中的 lambda 函数, 参数不能用 auto, 因为编译器无法推导类型, 从而导致编译错误
  61. template <typename Map>
  62. auto Select (Map map) -> Extension <Select_Iterator <Map, inherited, typename function_traits <Map>::return_type>>
  63. {
  64. using RetType = function_traits <Map>::return_type;
  65. using EFT = Select_Iterator <Map, inherited, RetType>;
  66. Extension <EFT> Ex (EFT (map, *this));
  67. return Ex;
  68. }
  69. #else
  70. template <typename Map, typename ...Args>
  71. auto Select (Map map ) //-> Extension <Select_Iterator <Map, inherited, typename std::result_of<Map (Args...)>::type>>
  72. {
  73. // using rt = std::result_of <decltype (fun)&(int)>::type;
  74. using RetType = typename std::result_of<decltype (map) & (Args...)>::type;
  75. // using RetType = typename std::result_of<typename Map (Args...)>::type;
  76. using EFT = Select_Iterator <Map, inherited, RetType>;
  77. Extension <EFT> Ex (EFT (map, *this));
  78. return Ex;
  79. }
  80. #endif
  81. // 累加器, 对每个元素进行一个运算
  82. template <typename Fun>
  83. auto Reduce (const Fun & fun) -> value_type
  84. {
  85. auto value = Current ();
  86. Next ();
  87. for (; ! IsEmpty (); Next ())
  88. value = fun (value, Current ());
  89. return std::move (value);
  90. }
  91. // 左折叠
  92. template <typename T, typename Fun>
  93. auto FoldLeft (const T & v0, const Fun & fun) -> T
  94. {
  95. auto value = v0;
  96. for (; ! IsEmpty (); Next ())
  97. value = fun (value, Current ());
  98. return (value);
  99. }
  100. // 右折叠
  101. template <typename T, typename Fun>
  102. auto FoldRight (const T & v0, const Fun & fun) -> T
  103. {
  104. auto value = v0;
  105. for (; ! IsEmpty (); Next ())
  106. value = fun (Current (), value);
  107. return (value);
  108. }
  109. // 左折叠
  110. template <typename T, typename Fun>
  111. auto FoldLeft (T && v0, const Fun & fun) -> T
  112. {
  113. auto value = std::move (v0);
  114. for (; ! IsEmpty (); Next ())
  115. value = fun (value, Current ());
  116. return (value);
  117. }
  118. // 右折叠
  119. template <typename T, typename Fun>
  120. auto FoldRight (T && v0, const Fun & fun) -> T
  121. {
  122. auto value = std::move (v0);
  123. for (; ! IsEmpty (); Next ())
  124. value = fun (Current (), value);
  125. return (value);
  126. }
  127. // 将指针或智能指针的指向的内容组成新集合
  128. auto Indirect () -> Extension <Indirect_Iterator <inherited, typename inherited::deref_type>>
  129. {
  130. using EFT = Indirect_Iterator <inherited, typename inherited::deref_type>;
  131. Extension <EFT> Ex (EFT (*this));
  132. return Ex;
  133. }
  134. // 把两个迭代器连起来,变成一个
  135. template <typename Other>
  136. auto Concat (Other & Iter) -> Extension <Concat_Iterator2 <Extension, Other> >
  137. {
  138. return MakeConcat (*this, Iter);
  139. }
  140. // 两个迭代器并行发送
  141. template <typename Other>
  142. auto Zip (Other & Iter) -> Extension <Zip_Iterator2 <Extension, Other> >
  143. {
  144. return MakeZip (*this, Iter);
  145. }
  146. // 去重
  147. auto Distinct () -> Extension <Distinct_Iterator <inherited>>
  148. {
  149. using EFT = Distinct_Iterator <inherited>;
  150. Extension <EFT> Ex (EFT (*this));
  151. return Ex;
  152. }
  153. template <typename Fun>
  154. auto Distinct (const Fun & fun) -> Extension <Distinct_Iterator2 <typename Fun, inherited>>
  155. {
  156. using EFT = Distinct_Iterator2 <typename Fun, inherited>;
  157. Extension <EFT> Ex (EFT (fun, *this));
  158. return Ex;
  159. }
  160. // 算术运算
  161. public:
  162. auto Count () -> int
  163. {
  164. return inherited::Count ();
  165. }
  166. template <typename Fun>
  167. auto Count (const Fun & fun) -> int
  168. {
  169. int count = 0;
  170. for (; ! IsEmpty (); Next ())
  171. if (fun (Current ()))
  172. count ++;
  173. return (count);
  174. }
  175. auto Min () -> value_type
  176. {
  177. auto smallest = Current ();
  178. if (IsEmpty ()) return std::move (smallest);
  179. Next ();
  180. for (; ! IsEmpty (); Next ())
  181. {
  182. auto value = Current ();
  183. if (smallest > value)
  184. smallest = value;
  185. }
  186. return std::move (smallest);
  187. }
  188. template <typename Fun>
  189. auto Min (const Fun & fun) -> value_type
  190. {
  191. auto smallest = Current ();
  192. if (IsEmpty ()) return std::move (smallest);
  193. Next ();
  194. for (; ! IsEmpty (); Next ())
  195. {
  196. auto value = Current ();
  197. if (fun (value, smallest))
  198. smallest = value;
  199. }
  200. return std::move (smallest);
  201. }
  202. auto Max () -> value_type
  203. {
  204. auto largest = Current ();
  205. if (IsEmpty ()) return std::move (largest);
  206. Next ();
  207. for (; ! IsEmpty (); Next ())
  208. {
  209. auto value = Current ();
  210. if (largest < value)
  211. largest = value;
  212. }
  213. return std::move (largest);
  214. }
  215. template <typename Fun>
  216. auto Max (const Fun & fun) -> value_type
  217. {
  218. auto largest = Current ();
  219. if (IsEmpty ()) return std::move (largest);
  220. Next ();
  221. for (; ! IsEmpty (); Next ())
  222. {
  223. auto value = Current ();
  224. if (fun (largest, value))
  225. largest = value;
  226. }
  227. return std::move (largest);
  228. }
  229. public:
  230. // 有一个满足指定的条件吗 ?
  231. template <typename Fun>
  232. inline bool Any (const Fun & fun)
  233. {
  234. for (; ! IsEmpty (); Next ())
  235. {
  236. if (fun (Current ()))
  237. return true;
  238. }
  239. return false;
  240. }
  241. // 所有元素都满足指定的条件吗 ?
  242. template <typename Fun>
  243. inline bool All (const Fun & fun)
  244. {
  245. for (; ! IsEmpty (); Next ())
  246. {
  247. if (! fun (Current ()))
  248. return false;
  249. }
  250. return true;
  251. }
  252. // 跳过 N 个元素
  253. inline Extension & Skip (int n)
  254. {
  255. inherited::Skip (n);
  256. return *this;
  257. }
  258. // 只要前面的 N 个元素
  259. inline Extension & Take (int n)
  260. {
  261. inherited::Take (n);
  262. return *this;
  263. }
  264. #if 1
  265. // 当 F 返回 true 时停止, 返回前面所有元素
  266. template <typename F>
  267. inline Extension & TakeWhile (const F & f)
  268. {
  269. for (auto Temp = *this; ! Temp.IsEmpty (); Temp.Next ())
  270. {
  271. auto value = Temp.Current ();
  272. if (f (value))
  273. {
  274. m_stdEnd = Temp.m_stdIter;
  275. break;
  276. }
  277. }
  278. return (*this);
  279. }
  280. #else
  281. template <typename F>
  282. inline Extension & TakeWhile (const F f)
  283. {
  284. for (auto Temp = m_Iter; Temp != _end; ++Temp)
  285. {
  286. auto value = inherited::CurrentOf (Temp);
  287. if (f (value))
  288. {
  289. _end = Temp;
  290. break;
  291. }
  292. }
  293. return (*this);
  294. }
  295. #endif
  296. // 当 F 返回 true 时停止, 返回后面所有元素
  297. template <typename F>
  298. inline Extension & SkipWhile (const F & f)
  299. {
  300. return First (f);
  301. }
  302. // 查找指定的元素
  303. template <typename T>
  304. inline Extension & Find (const T & v)
  305. {
  306. for (; ! IsEmpty (); Next ())
  307. {
  308. if (Current () == v)
  309. break;
  310. }
  311. return *this;
  312. }
  313. // 返回满足条件的第一个元素
  314. template <typename Fun>
  315. inline Extension & First (const Fun & fun)
  316. {
  317. for (; ! IsEmpty (); Next ())
  318. {
  319. if (fun (Current ()))
  320. break;
  321. }
  322. return *this;
  323. }
  324. public:
  325. template <typename Fun, typename... arg>
  326. inline Extension & ForEach (const Fun & fun, arg... e)
  327. {
  328. for (; ! IsEmpty (); Next ())
  329. fun (Current (), e...);
  330. return *this;
  331. }
  332. //-----------------------------------------------------------------------------
  333. // 转换成容器
  334. // CopyTo 和 MoveTo 是不同的
  335. // CopyTo 保证原来的容器中的内容不变, 而 MoveTo 会导致原来的容器中的内容可能改变
  336. //
  337. // DStringArray ar; ar.Add ("1"); ar.Add ("2");
  338. // auto N10 = Iterator::From (ar)
  339. // .MoveToVector ();
  340. //
  341. // 上述语句执行后, ar 的 size 不变, 但是每个 DString 元素都变成 空 了
  342. //-----------------------------------------------------------------------------
  343. public:
  344. auto CopyToVector () -> std::vector <typename inherited::value_type>
  345. {
  346. std::vector <typename inherited::value_type> to;
  347. #ifdef _DEBUG
  348. to.reserve (1000);
  349. #endif
  350. for (; ! IsEmpty (); Next ())
  351. to.push_back (Current ());
  352. return std::move (to);
  353. }
  354. auto MoveToVector () -> std::vector <typename inherited::value_type>
  355. {
  356. std::vector <typename inherited::value_type> to;
  357. #ifdef _DEBUG
  358. to.reserve (1000);
  359. #endif
  360. for (; ! IsEmpty (); Next ())
  361. to.push_back (std::move (Current ()));
  362. return std::move (to);
  363. }
  364. auto CopyToList () -> std::list <typename inherited::value_type>
  365. {
  366. std::list <typename inherited::value_type> to;
  367. for (; ! IsEmpty (); Next ())
  368. to.push_back (Current ());
  369. return std::move (to);
  370. }
  371. auto MoveToList () -> std::list <typename inherited::value_type>
  372. {
  373. std::list <typename inherited::value_type> to;
  374. for (; ! IsEmpty (); Next ())
  375. to.push_back (std::move (Current ()));
  376. return std::move (to);
  377. }
  378. };
  379. template <typename IterA, typename IterB>
  380. static auto MakeZip (IterA & a, IterB & b) -> Extension <Iterator::Zip_Iterator2 <IterA, IterB>>
  381. {
  382. using EFT = Iterator::Zip_Iterator2 <IterA, IterB>;
  383. Extension <EFT> Ex (EFT (a, b));
  384. return Ex;
  385. }
  386. template <typename IterA, typename IterB, typename IterC>
  387. static auto MakeZip (IterA & a, IterB & b, IterC & c) -> Extension <Iterator::Zip_Iterator3 <IterA, IterB, IterC>>
  388. {
  389. using EFT = Iterator::Zip_Iterator3 <IterA, IterB, IterC>;
  390. Extension <EFT> Ex (EFT (a, b, c));
  391. return Ex;
  392. }
  393. template <typename IterA, typename IterB>
  394. static auto MakeConcat (IterA & a, IterB & b) -> Extension <Iterator::Concat_Iterator2 <IterA, IterB>>
  395. {
  396. using EFT = Iterator::Concat_Iterator2 <IterA, IterB>;
  397. Extension <EFT> Ex (EFT (a, b));
  398. return Ex;
  399. }
  400. }