Iterator.Distinct.tlh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #pragma once
  2. #include "Iterator.Base.tlh"
  3. //-----------------------------------------------------------------------------
  4. // Distinct_Iterator_Base
  5. //
  6. // 2019-3-13 :
  7. // 计算 m_Count 是对的.
  8. // 但是如果修改了 m_Count, 在 Satisfy () 函数执行过程中, 很容易出错.
  9. // 因为 Iter 的迭代是基于原始迭代器 - std::iterator 的,
  10. // m_Index 的变更是基于原始迭代器的,
  11. // 因此要求 m_Count 与 原始迭代器的数量相符
  12. // 如果修改了 m_Count, 在迭代未结束时, m_Count 将小于 m_Index
  13. // 导致迭代提前结束了
  14. //-----------------------------------------------------------------------------
  15. namespace Iterator
  16. {
  17. template <typename ForIterator>
  18. class Distinct_Iterator_Base : public ForIterator
  19. {
  20. typedef ForIterator inherited;
  21. public:
  22. using value_type = typename ForIterator::value_type;
  23. using data_type = typename ForIterator::data_type;
  24. using deref_type = value_type;
  25. public:
  26. Distinct_Iterator_Base () = delete;
  27. Distinct_Iterator_Base (inherited & Iter) : inherited (Iter)
  28. {
  29. }
  30. Distinct_Iterator_Base (const Distinct_Iterator_Base & from) : inherited (from)
  31. {
  32. m_arDup = from.m_arDup;
  33. m_BeginOfDup = m_arDup.cbegin ();
  34. m_EndOfDup = m_arDup.cend ();
  35. }
  36. Distinct_Iterator_Base (Distinct_Iterator_Base && from) : inherited (from)
  37. {
  38. m_arDup.swap (from.m_arDup);
  39. m_BeginOfDup = m_arDup.cbegin ();
  40. m_EndOfDup = m_arDup.cend ();
  41. }
  42. public:
  43. inline virtual void Next () override
  44. {
  45. if (! IsEmpty ())
  46. {
  47. inherited::Next ();
  48. Satisfy ();
  49. }
  50. }
  51. protected:
  52. virtual void FindDup () = 0;
  53. protected:
  54. void Satisfy ()
  55. {
  56. if (inherited::IsEmpty ()) return;
  57. if (m_arDup.empty ()) return;
  58. for (;! IsEmpty (); inherited::Next ())
  59. {
  60. auto it = std::find (m_BeginOfDup, m_EndOfDup, inherited::stdCurrent ());
  61. if (it == m_EndOfDup)
  62. return;
  63. }
  64. }
  65. protected:
  66. using dup_type = std::vector <typename inherited::IterType>;
  67. typename dup_type m_arDup;
  68. typename dup_type::const_iterator m_BeginOfDup;
  69. typename dup_type::const_iterator m_EndOfDup;
  70. };
  71. };
  72. //-----------------------------------------------------------------------------
  73. // Distinct_Iterator
  74. //-----------------------------------------------------------------------------
  75. namespace Iterator
  76. {
  77. template <typename ForIterator>
  78. class Distinct_Iterator : public Distinct_Iterator_Base <ForIterator>
  79. {
  80. typedef Distinct_Iterator_Base <ForIterator> inherited;
  81. // typedef ForIterator inherited;
  82. public:
  83. Distinct_Iterator () = delete;
  84. Distinct_Iterator (ForIterator & Iter) : inherited (Iter)
  85. {
  86. FindDup ();
  87. }
  88. Distinct_Iterator (const Distinct_Iterator & from) : inherited (from)
  89. {
  90. }
  91. Distinct_Iterator (Distinct_Iterator && from) : inherited (from)
  92. {
  93. }
  94. protected:
  95. virtual void FindDup () override
  96. {
  97. if (IsEmpty ()) return;
  98. ForIterator findIter = *this;
  99. typename dup_type arDup;
  100. for (; ! findIter.IsEmpty (); findIter.Next ())
  101. {
  102. auto & v0 = findIter.Current ();
  103. auto tmpIter = findIter;
  104. tmpIter ++;
  105. for (; ! tmpIter.IsEmpty (); tmpIter++)
  106. {
  107. if (v0 != tmpIter.Current ())
  108. continue;
  109. auto it = std::find (arDup.cbegin (), arDup.cend (), tmpIter.stdCurrent ());
  110. if (it != arDup.cend ())
  111. continue;
  112. TRACE ("\r\nfindIter.Index : %3d, tmpIter.Index : %3d ", findIter.Index (), tmpIter.Index ());
  113. arDup.push_back (tmpIter.stdCurrent ());
  114. }
  115. }
  116. TRACE ("\r\n------ END --------");
  117. m_arDup.swap (arDup);
  118. m_BeginOfDup = m_arDup.cbegin ();
  119. m_EndOfDup = m_arDup.cend ();
  120. // m_Count -= (int) m_arDup.size ();
  121. }
  122. };
  123. };
  124. //-----------------------------------------------------------------------------
  125. // Distinct_Iterator
  126. //-----------------------------------------------------------------------------
  127. namespace Iterator
  128. {
  129. template <typename Predicate, typename ForIterator>
  130. class Distinct_Iterator2 : public Distinct_Iterator_Base <ForIterator>
  131. {
  132. typedef Distinct_Iterator_Base <ForIterator> inherited;
  133. // typedef ForIterator inherited;
  134. public:
  135. Distinct_Iterator2 () = delete;
  136. Distinct_Iterator2 (const Predicate & f, ForIterator & Iter) :
  137. m_predicate (f),
  138. inherited (Iter)
  139. {
  140. FindDup ();
  141. }
  142. Distinct_Iterator2 (const Distinct_Iterator2 & from) :
  143. m_predicate (from.m_predicate),
  144. inherited (from)
  145. {
  146. }
  147. Distinct_Iterator2 (Distinct_Iterator2 && from) :
  148. m_predicate (from.m_predicate),
  149. inherited (from)
  150. {
  151. }
  152. protected:
  153. virtual void FindDup () override
  154. {
  155. if (IsEmpty ()) return;
  156. ForIterator findIter = *this;
  157. typename dup_type arDup;
  158. for (; ! findIter.IsEmpty (); findIter.Next ())
  159. {
  160. auto & v0 = findIter.Current ();
  161. auto tmpIter = findIter;
  162. tmpIter ++;
  163. for (; ! tmpIter.IsEmpty (); tmpIter++)
  164. {
  165. // if (v0 != value)
  166. if (! m_predicate (v0, tmpIter.Current ()))
  167. continue;
  168. auto it = std::find (arDup.cbegin (), arDup.cend (), tmpIter.stdCurrent ());
  169. if (it != arDup.cend ())
  170. continue;
  171. TRACE ("\r\nfindIter.Index : %3d, tmpIter.Index : %3d ", findIter.Index (), tmpIter.Index ());
  172. arDup.push_back (tmpIter.stdCurrent ());
  173. }
  174. }
  175. TRACE ("\r\n------ END --------");
  176. m_arDup.swap (arDup);
  177. m_BeginOfDup = m_arDup.cbegin ();
  178. m_EndOfDup = m_arDup.cend ();
  179. // m_Count -= (int) m_arDup.size ();
  180. }
  181. private:
  182. Predicate m_predicate;
  183. };
  184. };