123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- #pragma once
- #include <numeric>
- #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 <typename ForIterator>
- 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 <typename Predicate>
- auto Where (const Predicate & pred) -> Extension <Filter_Iterator <typename Predicate, inherited>>
- {
- using EFT = Filter_Iterator <typename Predicate, inherited>;
- Extension <EFT> Ex (EFT (pred, *this));
- return Ex;
- }
- #if 1
- // 转换
- // 注: Select 中的 lambda 函数, 参数不能用 auto, 因为编译器无法推导类型, 从而导致编译错误
- template <typename Map>
- auto Select (Map map) -> Extension <Select_Iterator <Map, inherited, typename function_traits <Map>::return_type>>
- {
- using RetType = function_traits <Map>::return_type;
- using EFT = Select_Iterator <Map, inherited, RetType>;
- Extension <EFT> Ex (EFT (map, *this));
- return Ex;
- }
- #else
- template <typename Map, typename ...Args>
- auto Select (Map map ) //-> Extension <Select_Iterator <Map, inherited, typename std::result_of<Map (Args...)>::type>>
- {
- // using rt = std::result_of <decltype (fun)&(int)>::type;
- using RetType = typename std::result_of<decltype (map) & (Args...)>::type;
- // using RetType = typename std::result_of<typename Map (Args...)>::type;
- using EFT = Select_Iterator <Map, inherited, RetType>;
- Extension <EFT> Ex (EFT (map, *this));
- return Ex;
- }
- #endif
- // 累加器, 对每个元素进行一个运算
- template <typename Fun>
- auto Reduce (const Fun & fun) -> value_type
- {
- auto value = Current ();
- Next ();
- for (; ! IsEmpty (); Next ())
- value = fun (value, Current ());
- return std::move (value);
- }
- // 左折叠
- template <typename T, typename Fun>
- auto FoldLeft (const T & v0, const Fun & fun) -> T
- {
- auto value = v0;
- for (; ! IsEmpty (); Next ())
- value = fun (value, Current ());
- return (value);
- }
- // 右折叠
- template <typename T, typename Fun>
- auto FoldRight (const T & v0, const Fun & fun) -> T
- {
- auto value = v0;
- for (; ! IsEmpty (); Next ())
- value = fun (Current (), value);
- return (value);
- }
- // 左折叠
- template <typename T, typename Fun>
- auto FoldLeft (T && v0, const Fun & fun) -> T
- {
- auto value = std::move (v0);
- for (; ! IsEmpty (); Next ())
- value = fun (value, Current ());
- return (value);
- }
- // 右折叠
- template <typename T, typename Fun>
- 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 <Indirect_Iterator <inherited, typename inherited::deref_type>>
- {
- using EFT = Indirect_Iterator <inherited, typename inherited::deref_type>;
- Extension <EFT> Ex (EFT (*this));
- return Ex;
- }
- // 把两个迭代器连起来,变成一个
- template <typename Other>
- auto Concat (Other & Iter) -> Extension <Concat_Iterator2 <Extension, Other> >
- {
- return MakeConcat (*this, Iter);
- }
- // 两个迭代器并行发送
- template <typename Other>
- auto Zip (Other & Iter) -> Extension <Zip_Iterator2 <Extension, Other> >
- {
- return MakeZip (*this, Iter);
- }
- // 去重
- auto Distinct () -> Extension <Distinct_Iterator <inherited>>
- {
- using EFT = Distinct_Iterator <inherited>;
- Extension <EFT> Ex (EFT (*this));
- return Ex;
- }
- template <typename Fun>
- auto Distinct (const Fun & fun) -> Extension <Distinct_Iterator2 <typename Fun, inherited>>
- {
- using EFT = Distinct_Iterator2 <typename Fun, inherited>;
- Extension <EFT> Ex (EFT (fun, *this));
- return Ex;
- }
- // 算术运算
- public:
- auto Count () -> int
- {
- return inherited::Count ();
- }
- template <typename Fun>
- 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 <typename Fun>
- 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 <typename Fun>
- 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 <typename Fun>
- inline bool Any (const Fun & fun)
- {
- for (; ! IsEmpty (); Next ())
- {
- if (fun (Current ()))
- return true;
- }
- return false;
- }
- // 所有元素都满足指定的条件吗 ?
- template <typename Fun>
- 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 <typename F>
- 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 <typename F>
- 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 <typename F>
- inline Extension & SkipWhile (const F & f)
- {
- return First (f);
- }
- // 查找指定的元素
- template <typename T>
- inline Extension & Find (const T & v)
- {
- for (; ! IsEmpty (); Next ())
- {
- if (Current () == v)
- break;
- }
- return *this;
- }
- // 返回满足条件的第一个元素
- template <typename Fun>
- inline Extension & First (const Fun & fun)
- {
- for (; ! IsEmpty (); Next ())
- {
- if (fun (Current ()))
- break;
- }
- return *this;
- }
- public:
- template <typename Fun, typename... arg>
- 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 <typename inherited::value_type>
- {
- std::vector <typename inherited::value_type> to;
- #ifdef _DEBUG
- to.reserve (1000);
- #endif
- for (; ! IsEmpty (); Next ())
- to.push_back (Current ());
- return std::move (to);
- }
- auto MoveToVector () -> std::vector <typename inherited::value_type>
- {
- std::vector <typename inherited::value_type> to;
- #ifdef _DEBUG
- to.reserve (1000);
- #endif
- for (; ! IsEmpty (); Next ())
- to.push_back (std::move (Current ()));
- return std::move (to);
- }
- auto CopyToList () -> std::list <typename inherited::value_type>
- {
- std::list <typename inherited::value_type> to;
- for (; ! IsEmpty (); Next ())
- to.push_back (Current ());
- return std::move (to);
- }
- auto MoveToList () -> std::list <typename inherited::value_type>
- {
- std::list <typename inherited::value_type> to;
- for (; ! IsEmpty (); Next ())
- to.push_back (std::move (Current ()));
- return std::move (to);
- }
- };
- template <typename IterA, typename IterB>
- static auto MakeZip (IterA & a, IterB & b) -> Extension <Iterator::Zip_Iterator2 <IterA, IterB>>
- {
- using EFT = Iterator::Zip_Iterator2 <IterA, IterB>;
- Extension <EFT> Ex (EFT (a, b));
- return Ex;
- }
- template <typename IterA, typename IterB, typename IterC>
- static auto MakeZip (IterA & a, IterB & b, IterC & c) -> Extension <Iterator::Zip_Iterator3 <IterA, IterB, IterC>>
- {
- using EFT = Iterator::Zip_Iterator3 <IterA, IterB, IterC>;
- Extension <EFT> Ex (EFT (a, b, c));
- return Ex;
- }
- template <typename IterA, typename IterB>
- static auto MakeConcat (IterA & a, IterB & b) -> Extension <Iterator::Concat_Iterator2 <IterA, IterB>>
- {
- using EFT = Iterator::Concat_Iterator2 <IterA, IterB>;
- Extension <EFT> Ex (EFT (a, b));
- return Ex;
- }
- }
|