swift
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 
11 namespace 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()) { obj.setOrder(c++); }
38  }
39 
41  bool needsOrder() const
42  {
43  for (const OBJ &obj : container())
44  {
45  if (!obj.hasValidOrder()) { return true; }
46  }
47  return false;
48  }
49 
51  QList<int> orderValues() const
52  {
53  QList<int> orders;
54  for (const OBJ &obj : container())
55  {
56  if (!obj.hasValidOrder()) { continue; }
57  orders.append(obj.getOrder());
58  }
59  return orders;
60  }
61 
63  CONTAINER withoutItemsOfSameOrder(const CONTAINER &items) const
64  {
65  const QList<int> orders = items.orderValues();
66  if (orders.isEmpty()) { return this->container(); }
67 
68  CONTAINER newContainer;
69  for (const OBJ &obj : container())
70  {
71  if (orders.contains(obj.getOrder())) { continue; }
72  newContainer.push_back(obj);
73  }
74  return newContainer;
75  }
76 
78  void removeItemsWithSameOrder(const CONTAINER &items)
79  {
80  const QList<int> orders = items.orderValues();
81  if (orders.isEmpty()) { return; }
82 
83  CONTAINER newContainer;
84  for (const OBJ &obj : container())
85  {
86  if (orders.contains(obj.getOrder())) { continue; }
87  newContainer.push_back(obj);
88  }
89  this->container() = newContainer;
90  }
91 
93  void moveTo(const CONTAINER &items, int targetOrder)
94  {
95  if (items.isEmpty()) { return; }
96  CONTAINER newContainer(this->withoutItemsOfSameOrder(items)); // this container without items
97  CONTAINER newItems(items);
98  const int shift = items.size();
99  newContainer.sortAscendingByOrder(); // sorted by old order
100  newContainer.resetOrder();
101  for (OBJ &obj : newContainer)
102  {
103  if (obj.getOrder() < targetOrder) { continue; }
104  obj.setOrder(obj.getOrder() + shift);
105  }
106  newItems.sortAscendingByOrder(); // sort by old order
107  newItems.resetOrder(targetOrder);
108  newContainer.push_back(newItems);
109  this->container() = newContainer;
110  }
111 
113  void freezeOrder()
114  {
115  int c = 0;
116  for (OBJ &obj : container()) { obj.setOrder(c++); }
117  }
118 
121  {
122  int c = this->container().size() - 1;
123  for (OBJ &obj : container()) { obj.setOrder(c--); }
124  }
125 
127  OBJ minOrderOrDefault() const
128  {
129  if (container().isEmpty()) { return OBJ(); }
130  OBJ min = container().front();
131  for (const OBJ &obj : container())
132  {
133  if (!obj.hasValidOrder()) { continue; }
134  if (obj.getOrder() < min.getOrder()) { min = obj; }
135  }
136  return min;
137  }
138 
140  OBJ maxOrderOrDefault() const
141  {
142  if (container().isEmpty()) { return OBJ(); }
143  OBJ max = container().front();
144  for (const OBJ &obj : container())
145  {
146  if (!obj.hasValidOrder()) { continue; }
147  if (obj.getOrder() > max.getOrder()) { max = obj; }
148  }
149  return max;
150  }
151 
152  protected:
154  IOrderableList() = default;
155 
157  const CONTAINER &container() const { return static_cast<const CONTAINER &>(*this); }
158 
160  CONTAINER &container() { return static_cast<CONTAINER &>(*this); }
161  };
162 } // namespace swift::misc
163 
164 #endif // SWIFT_MISC_ORDERABLELIST_H
List of orderable IOrderable objects.
Definition: orderablelist.h:19
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.
Definition: orderablelist.h:78
IOrderableList()=default
Constructor.
void sortAscendingByOrder()
Sort ascending.
Definition: orderablelist.h:24
void sortDescendingByOrder()
Sort descending.
Definition: orderablelist.h:27
CONTAINER & container()
Container.
void moveTo(const CONTAINER &items, int targetOrder)
Move items to given order.
Definition: orderablelist.h:93
QList< int > orderValues() const
All order values IOrderable::order.
Definition: orderablelist.h:51
OBJ maxOrderOrDefault() const
Object with max.order or default.
OBJ minOrderOrDefault() const
Object with min.order or default.
const CONTAINER & container() const
Container.
CONTAINER withoutItemsOfSameOrder(const CONTAINER &items) const
Items with order will not be included.
Definition: orderablelist.h:63
void resetOrder(int offset=0)
Set order member to current order.
Definition: orderablelist.h:34
void freezeOrder()
Current order of list will be new order values.
bool needsOrder() const
All order values set or missing some?
Definition: orderablelist.h:41
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:332
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:338