swift
Loading...
Searching...
No Matches
orderablelist.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_ORDERABLELIST_H
7#define SWIFT_MISC_ORDERABLELIST_H
8
9#include <QList>
10
11namespace swift::misc
12{
13 class IOrderable;
14
17 template <class OBJ, class CONTAINER>
19 {
20 static_assert(std::is_base_of_v<IOrderable, OBJ>, "OBJ needs to implement IOrderable");
21
22 public:
24 void sortAscendingByOrder() { this->container().sort(predicates::MemberLess(&OBJ::getOrder)); }
25
28 {
29 this->container().sortAscendingByOrder();
30 std::reverse(this->container().begin(), this->container().end());
31 }
32
34 void resetOrder(int offset = 0)
35 {
36 int c = offset;
37 for (OBJ &obj : container())
38 {
39 obj.setOrder(c++);
40 }
41 }
42
44 bool needsOrder() const
45 {
46 return std::any_of(this->container().begin(), this->container().end(),
47 [](const OBJ &obj) { return !obj.hasValidOrder(); });
48 }
49
52 {
53 QList<int> orders;
54 for (const OBJ &obj : container())
55 {
56 if (!obj.hasValidOrder())
57 {
58 continue;
59 }
60 orders.append(obj.getOrder());
61 }
62 return orders;
63 }
64
66 CONTAINER withoutItemsOfSameOrder(const CONTAINER &items) const
67 {
68 const QList<int> orders = items.orderValues();
69 if (orders.isEmpty())
70 {
71 return this->container();
72 }
73
74 CONTAINER newContainer;
75 for (const OBJ &obj : container())
76 {
77 if (orders.contains(obj.getOrder()))
78 {
79 continue;
80 }
81 newContainer.push_back(obj);
82 }
83 return newContainer;
84 }
85
87 void removeItemsWithSameOrder(const CONTAINER &items)
88 {
89 const QList<int> orders = items.orderValues();
90 if (orders.isEmpty())
91 {
92 return;
93 }
94
95 CONTAINER newContainer;
96 for (const OBJ &obj : container())
97 {
98 if (orders.contains(obj.getOrder()))
99 {
100 continue;
101 }
102 newContainer.push_back(obj);
103 }
104 this->container() = newContainer;
105 }
106
108 void moveTo(const CONTAINER &items, int targetOrder)
109 {
110 if (items.isEmpty())
111 {
112 return;
113 }
114 CONTAINER newContainer(this->withoutItemsOfSameOrder(items)); // this container without items
115 CONTAINER newItems(items);
116 const int shift = items.size();
117 newContainer.sortAscendingByOrder(); // sorted by old order
118 newContainer.resetOrder();
119 for (OBJ &obj : newContainer)
120 {
121 if (obj.getOrder() < targetOrder)
122 {
123 continue;
124 }
125 obj.setOrder(obj.getOrder() + shift);
126 }
127 newItems.sortAscendingByOrder(); // sort by old order
128 newItems.resetOrder(targetOrder);
129 newContainer.push_back(newItems);
130 this->container() = newContainer;
131 }
132
135 {
136 int c = 0;
137 for (OBJ &obj : container())
138 {
139 obj.setOrder(c++);
140 }
141 }
142
145 {
146 int c = this->container().size() - 1;
147 for (OBJ &obj : container())
148 {
149 obj.setOrder(c--);
150 }
151 }
152
155 {
156 if (container().isEmpty())
157 {
158 return OBJ();
159 }
160 OBJ min = container().front();
161 for (const OBJ &obj : container())
162 {
163 if (!obj.hasValidOrder())
164 {
165 continue;
166 }
167 if (obj.getOrder() < min.getOrder())
168 {
169 min = obj;
170 }
171 }
172 return min;
173 }
174
177 {
178 if (container().isEmpty())
179 {
180 return OBJ();
181 }
182 OBJ max = container().front();
183 for (const OBJ &obj : container())
184 {
185 if (!obj.hasValidOrder())
186 {
187 continue;
188 }
189 if (obj.getOrder() > max.getOrder())
190 {
191 max = obj;
192 }
193 }
194 return max;
195 }
196
197 protected:
199 IOrderableList() = default;
200
202 const CONTAINER &container() const { return static_cast<const CONTAINER &>(*this); }
203
205 CONTAINER &container() { return static_cast<CONTAINER &>(*this); }
206 };
207} // namespace swift::misc
208
209#endif // SWIFT_MISC_ORDERABLELIST_H
Entity with order attribute (can be manually ordered in views).
Definition orderable.h:19
const CONTAINER & container() const
Container.
void freezeOrderReverse()
Current reverse order of list will be new order values.
void removeItemsWithSameOrder(const CONTAINER &items)
Remove the items based on their order IOrderable::order.
IOrderableList()=default
Constructor.
void sortAscendingByOrder()
Sort ascending.
void sortDescendingByOrder()
Sort descending.
void moveTo(const CONTAINER &items, int targetOrder)
Move items to given order.
OBJ maxOrderOrDefault() const
Object with max.order or default.
OBJ minOrderOrDefault() const
Object with min.order or default.
CONTAINER withoutItemsOfSameOrder(const CONTAINER &items) const
Items with order will not be included.
void resetOrder(int offset=0)
Set order member to current order.
QList< int > orderValues() const
All order values IOrderable::order.
void freezeOrder()
Current order of list will be new order values.
CONTAINER & container()
Container.
bool needsOrder() const
All order values set or missing some?
auto MemberLess(Ts... vs)
Predicate which compares the return values of some member functions of two objects.
Definition predicates.h:43
Free functions in swift::misc.
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:258
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:264
void append(QList< T > &&value)
bool contains(const AT &value) const const
bool isEmpty() const const