Iterator.Extension.tlh 11 KB

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