4 * @brief Declaration of class CompareStats
15 * @brief Class holding directory compare stats.
17 * This class is used for sharing compare stats between dir compare
18 * classes. CDiffContext updates statuses. GUI (compare statepane) tracks
19 * state changes and updates UI. If compare is fast (compared few small files)
20 * GUI might not be able to detect state change from IDLE back to IDLE. That's
21 * why there is IsCompareDone() which tells if new compare is ready.
28 * @brief Different states for compare procedure.
29 * These states form state-machine for directory compare. States go like:
30 * STATE_IDLE --> STATE_START --> STATE_COMPARE --> STATE_IDLE.
31 * @note GUI doesn't change state, but only backend code. GUI must track
32 * state changes to update itself.
36 STATE_IDLE, /**< No compare running */
37 STATE_START, /**< Start folder compare */
38 STATE_COMPARE, /**< Comparing collected items */
42 * @brief Resultcodes we store.
67 RESULT_COUNT //THIS MUST BE THE LAST ITEM
70 explicit CompareStats(int nDirs);
72 int GetCompareThreadCount()
74 return static_cast<int>(m_rgThreadState.size());
76 void SetCompareThreadCount(int nCompareThreads)
78 m_rgThreadState.clear();
79 m_rgThreadState.resize(nCompareThreads);
81 unsigned GetIdleCompareThreadCount() const
83 return m_nIdleCompareThreadCount;
85 void SetIdleCompareThreadCount(unsigned nIdleCompareThreadCount)
87 assert(nIdleCompareThreadCount < m_rgThreadState.size());
88 m_nIdleCompareThreadCount = nIdleCompareThreadCount;
90 bool IsIdleCompareThread(unsigned iCompareThread) const
92 return iCompareThread >= (m_rgThreadState.size() - m_nIdleCompareThreadCount);
94 void BeginCompare(const DIFFITEM *di, unsigned iCompareThread)
96 ThreadState &rThreadState = m_rgThreadState[iCompareThread];
97 rThreadState.m_nHitCount = 0;
98 rThreadState.m_pDiffItem = di;
100 void AddItem(int code);
101 void IncreaseTotalItems(int count = 1);
102 int GetCount(CompareStats::RESULT result) const;
103 int GetTotalItems() const;
104 int GetComparedItems() const { return m_nComparedItems; }
105 const DIFFITEM *GetCurDiffItem();
107 void SetCompareState(CompareStats::CMP_STATE state);
108 CompareStats::CMP_STATE GetCompareState() const;
109 bool IsCompareDone() const { return m_bCompareDone; }
110 CompareStats::RESULT GetResultFromCode(unsigned diffcode) const;
111 void Swap(int idx1, int idx2);
112 int GetCompareDirs() const { return m_nDirs; }
115 std::array<std::atomic_int, RESULT_COUNT> m_counts; /**< Table storing result counts */
116 std::atomic_int m_nTotalItems; /**< Total items found to compare */
117 std::atomic_int m_nComparedItems; /**< Compared items so far */
118 CMP_STATE m_state; /**< State for compare (idle, collect, compare,..) */
119 bool m_bCompareDone; /**< Have we finished last compare? */
120 int m_nDirs; /**< number of directories to compare */
123 ThreadState() : m_nHitCount(0), m_pDiffItem(nullptr) {}
124 ThreadState(const ThreadState& other) : m_nHitCount(other.m_nHitCount.load()), m_pDiffItem(other.m_pDiffItem) {}
125 std::atomic_int m_nHitCount;
126 const DIFFITEM *m_pDiffItem;
128 std::vector<ThreadState> m_rgThreadState;
129 unsigned m_nIdleCompareThreadCount;
133 * @brief Increase found items (dirs and files) count.
134 * @param [in] count Amount of items to add.
136 inline void CompareStats::IncreaseTotalItems(int count)
138 m_nTotalItems += count;
142 * @brief Return count by resultcode.
143 * @param [in] result Resultcode to return.
144 * @return Count of items for given resultcode.
146 inline int CompareStats::GetCount(CompareStats::RESULT result) const
148 return m_counts[result];
152 * @brief Return total count of items (so far) found.
154 inline int CompareStats::GetTotalItems() const
156 return m_nTotalItems;
160 * @brief Return current comparestate.
162 inline CompareStats::CMP_STATE CompareStats::GetCompareState() const