#pragma once #include #include "Iterator.Base.tlh" #include "Iterator.Filter.tlh" #include "Iterator.Select.tlh" #include "Iterator.Indirect.tlh" #include "Iterator.Zip.tlh" #include "Iterator.Concat.tlh" #include "Iterator.Distinct.tlh" //----------------------------------------------------------------------------- // Helper //----------------------------------------------------------------------------- namespace Iterator { template class Extension : public ForIterator { typedef ForIterator inherited; public: using data_type = typename ForIterator::data_type; using value_type = typename ForIterator::value_type; // using deref_type = typename ForIterator::deref_type; public: // Extension () = delete; // Extension (const Extension &) = delete; // Extension (Extension &&) = delete; // Extension & operator = (const Extension &) = delete; // Extension & operator = (Extension &&) = delete; // Extension (const Extension &) = default; // Extension (Extension &&) = default; // Extension & operator = (const Extension &) = default; // Extension & operator = (Extension &&) = default; public: Extension () { } Extension (ForIterator & Iter) : inherited (Iter) { } public: inline Extension begin () { Extension Tmp (*this); Tmp.Restart (); return Tmp; } inline Extension end () { Extension Tmp (*this); Tmp.GoToEnd (); return Tmp; } public: // 过滤 template auto Where (const Predicate & pred) -> Extension > { using EFT = Filter_Iterator ; Extension Ex (EFT (pred, *this)); return Ex; } #if 1 // 转换 // 注: Select 中的 lambda 函数, 参数不能用 auto, 因为编译器无法推导类型, 从而导致编译错误 template auto Select (Map map) -> Extension ::return_type>> { using RetType = function_traits ::return_type; using EFT = Select_Iterator ; Extension Ex (EFT (map, *this)); return Ex; } #else template auto Select (Map map ) //-> Extension ::type>> { // using rt = std::result_of ::type; using RetType = typename std::result_of::type; // using RetType = typename std::result_of::type; using EFT = Select_Iterator ; Extension Ex (EFT (map, *this)); return Ex; } #endif // 累加器, 对每个元素进行一个运算 template auto Reduce (const Fun & fun) -> value_type { auto value = Current (); Next (); for (; ! IsEmpty (); Next ()) value = fun (value, Current ()); return std::move (value); } // 左折叠 template auto FoldLeft (const T & v0, const Fun & fun) -> T { auto value = v0; for (; ! IsEmpty (); Next ()) value = fun (value, Current ()); return (value); } // 右折叠 template auto FoldRight (const T & v0, const Fun & fun) -> T { auto value = v0; for (; ! IsEmpty (); Next ()) value = fun (Current (), value); return (value); } // 左折叠 template auto FoldLeft (T && v0, const Fun & fun) -> T { auto value = std::move (v0); for (; ! IsEmpty (); Next ()) value = fun (value, Current ()); return (value); } // 右折叠 template auto FoldRight (T && v0, const Fun & fun) -> T { auto value = std::move (v0); for (; ! IsEmpty (); Next ()) value = fun (Current (), value); return (value); } // 将指针或智能指针的指向的内容组成新集合 auto Indirect () -> Extension > { using EFT = Indirect_Iterator ; Extension Ex (EFT (*this)); return Ex; } // 把两个迭代器连起来,变成一个 template auto Concat (Other & Iter) -> Extension > { return MakeConcat (*this, Iter); } // 两个迭代器并行发送 template auto Zip (Other & Iter) -> Extension > { return MakeZip (*this, Iter); } // 去重 auto Distinct () -> Extension > { using EFT = Distinct_Iterator ; Extension Ex (EFT (*this)); return Ex; } template auto Distinct (const Fun & fun) -> Extension > { using EFT = Distinct_Iterator2 ; Extension Ex (EFT (fun, *this)); return Ex; } // 算术运算 public: auto Count () -> int { return inherited::Count (); } template auto Count (const Fun & fun) -> int { int count = 0; for (; ! IsEmpty (); Next ()) if (fun (Current ())) count ++; return (count); } auto Min () -> value_type { auto smallest = Current (); if (IsEmpty ()) return std::move (smallest); Next (); for (; ! IsEmpty (); Next ()) { auto value = Current (); if (smallest > value) smallest = value; } return std::move (smallest); } template auto Min (const Fun & fun) -> value_type { auto smallest = Current (); if (IsEmpty ()) return std::move (smallest); Next (); for (; ! IsEmpty (); Next ()) { auto value = Current (); if (fun (value, smallest)) smallest = value; } return std::move (smallest); } auto Max () -> value_type { auto largest = Current (); if (IsEmpty ()) return std::move (largest); Next (); for (; ! IsEmpty (); Next ()) { auto value = Current (); if (largest < value) largest = value; } return std::move (largest); } template auto Max (const Fun & fun) -> value_type { auto largest = Current (); if (IsEmpty ()) return std::move (largest); Next (); for (; ! IsEmpty (); Next ()) { auto value = Current (); if (fun (largest, value)) largest = value; } return std::move (largest); } public: // 有一个满足指定的条件吗 ? template inline bool Any (const Fun & fun) { for (; ! IsEmpty (); Next ()) { if (fun (Current ())) return true; } return false; } // 所有元素都满足指定的条件吗 ? template inline bool All (const Fun & fun) { for (; ! IsEmpty (); Next ()) { if (! fun (Current ())) return false; } return true; } // 跳过 N 个元素 inline Extension & Skip (int n) { inherited::Skip (n); return *this; } // 只要前面的 N 个元素 inline Extension & Take (int n) { inherited::Take (n); return *this; } #if 1 // 当 F 返回 true 时停止, 返回前面所有元素 template inline Extension & TakeWhile (const F & f) { for (auto Temp = *this; ! Temp.IsEmpty (); Temp.Next ()) { auto value = Temp.Current (); if (f (value)) { m_stdEnd = Temp.m_stdIter; break; } } return (*this); } #else template inline Extension & TakeWhile (const F f) { for (auto Temp = m_Iter; Temp != _end; ++Temp) { auto value = inherited::CurrentOf (Temp); if (f (value)) { _end = Temp; break; } } return (*this); } #endif // 当 F 返回 true 时停止, 返回后面所有元素 template inline Extension & SkipWhile (const F & f) { return First (f); } // 查找指定的元素 template inline Extension & Find (const T & v) { for (; ! IsEmpty (); Next ()) { if (Current () == v) break; } return *this; } // 返回满足条件的第一个元素 template inline Extension & First (const Fun & fun) { for (; ! IsEmpty (); Next ()) { if (fun (Current ())) break; } return *this; } public: template inline Extension & ForEach (const Fun & fun, arg... e) { for (; ! IsEmpty (); Next ()) fun (Current (), e...); return *this; } //----------------------------------------------------------------------------- // 转换成容器 // CopyTo 和 MoveTo 是不同的 // CopyTo 保证原来的容器中的内容不变, 而 MoveTo 会导致原来的容器中的内容可能改变 // // DStringArray ar; ar.Add ("1"); ar.Add ("2"); // auto N10 = Iterator::From (ar) // .MoveToVector (); // // 上述语句执行后, ar 的 size 不变, 但是每个 DString 元素都变成 空 了 //----------------------------------------------------------------------------- public: auto CopyToVector () -> std::vector { std::vector to; #ifdef _DEBUG to.reserve (1000); #endif for (; ! IsEmpty (); Next ()) to.push_back (Current ()); return std::move (to); } auto MoveToVector () -> std::vector { std::vector to; #ifdef _DEBUG to.reserve (1000); #endif for (; ! IsEmpty (); Next ()) to.push_back (std::move (Current ())); return std::move (to); } auto CopyToList () -> std::list { std::list to; for (; ! IsEmpty (); Next ()) to.push_back (Current ()); return std::move (to); } auto MoveToList () -> std::list { std::list to; for (; ! IsEmpty (); Next ()) to.push_back (std::move (Current ())); return std::move (to); } }; template static auto MakeZip (IterA & a, IterB & b) -> Extension > { using EFT = Iterator::Zip_Iterator2 ; Extension Ex (EFT (a, b)); return Ex; } template static auto MakeZip (IterA & a, IterB & b, IterC & c) -> Extension > { using EFT = Iterator::Zip_Iterator3 ; Extension Ex (EFT (a, b, c)); return Ex; } template static auto MakeConcat (IterA & a, IterB & b) -> Extension > { using EFT = Iterator::Concat_Iterator2 ; Extension Ex (EFT (a, b)); return Ex; } }