Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSortedHistoryList.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#ifndef KISSORTEDHISTORYLIST_H
8#define KISSORTEDHISTORYLIST_H
9
10#include <vector>
11#include <KisHistoryList.h>
12
13template <typename T>
15{
16public:
17 using const_iterator = typename std::vector<T>::const_iterator;
18 using compare_less = std::function<bool(const T&, const T&)>;
19
20public:
22 : m_list(size)
23 {
24 }
25
29 int size() const {
30 return m_list.size();
31 }
32
36 T at(int pos) const {
39
40 return m_sortedList.at(pos);
41 }
42
48 int append(const T& value) {
49 int newElementIndex = m_list.append(value);;
50 if (resortList()) {
51 auto it = std::find(m_sortedList.begin(), m_sortedList.end(), value);
53 it = m_sortedList.begin();
54 }
55 newElementIndex = std::distance(m_sortedList.begin(), it);
56 }
57
58 return newElementIndex;
59 }
60
64 void clear() {
65 m_list.clear();
66 m_sortedList.clear();
67 }
68
72 int maxSize() const {
73 return m_list.maxSize();
74 }
75
82 m_compareLess = func;
83 resortList();
84 }
85
87 return m_sortedList.cbegin();
88 }
89
91 return m_sortedList.cend();
92 }
93
94private:
95 bool resortList() {
96 m_sortedList.clear();
97 m_sortedList.reserve(m_list.size());
98 std::copy(m_list.cbegin(), m_list.cend(), std::back_inserter(m_sortedList));
99
100 if (m_compareLess) {
101 std::sort(m_sortedList.begin(), m_sortedList.end(), m_compareLess);
102 }
103
104 return bool(m_compareLess);
105 }
106
107private:
109 std::vector<T> m_sortedList;
111};
112
113
114#endif // KISSORTEDHISTORYLIST_H
float value(const T *src, size_t ch)
std::function< bool(const T &, const T &)> compare_less
const_iterator cend() const
std::vector< T > m_sortedList
void setCompareLess(compare_less func)
KisHistoryList< T > m_list
typename std::vector< T >::const_iterator const_iterator
int append(const T &value)
const_iterator cbegin() const
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129