void shift(IteratorT begin, IteratorT end, int offset){ for (auto& it = begin; it != end; it++) { *it = (*it).addDays(offset * 7); }}
void shift(IteratorT begin, IteratorT end, int offset){ while (begin != end) { *begin++ = (*begin).addDays(offset * 7); }}
class Week{public: explicit Week(QDate date) : date_(date) {} QDate date() const { return date_; }private: QDate date_;};
QList<Week*> weeks = { new Week(QDate::currentDate().addDays(-7)), new Week(QDate::currentDate()), new Week(QDate::currentDate().addDays(7)) };
C++ (Qt)void OffsetRange( QList<Week *> & lst, Iter beg, Iter end, int offset ){// calc sort range auto sortBeg = beg; auto sortEnd = end; if (offset > 0) { if (end != lst.end()) sortEnd = std::lower_bound(beg, lst.end(), (*end)->data().addDays(offset), CompDate1); } else { if (beg != lst.end()) sortBeg = std::lower_bound(lst.begin(), beg, (*beg)->data().addDays(offset), CompDate1); } // modify elements while (beg != end) { auto & dst= *beg; auto newP = new Week(dst->data().addDays(offset)); delete dst; dst = newP; ++beg; } // sort it std::sort(sortBeg, sortEnd, CompDate2);}
#include <QtCore/QCoreApplication>#include <QtCore/QDate>#include <QtCore/QDebug>#include <QtCore/QList>#include <QtCore/QSharedPointer>class Week{public: explicit Week(QDate date) : date_(date) { qDebug() << "Week: ctor()"; } ~Week() { qDebug() << "Week: dtor()"; } QDate date() const { return date_; }private: QDate date_;};//<<<<<<<<template <typename, typename = void>inline constexpr bool IsReverseIterator = false;template <typename T>inline constexpr bool IsReverseIterator<T, std::void_t<decltype(std::declval<T>().base())>> = true;/// Shifts the range of Week*s to left or right on \a offset positions (weeks). The shift direction/// is determined by the iterators kind - forward ones indicate shifting to left (with insertion on/// the right side future weeks) and reverse ones - to right (with insertion on the left side past/// weeks). \a offset may be any value (even greater then the range size). Shifted out Weeks/// are destroyed, shifted in ones are created with the proper dates.template <typename IteratorT>static void shift(IteratorT begin, IteratorT end, typename std::iterator_traits<IteratorT>::difference_type offset){ if (Q_UNLIKELY(!offset)) return; // std::copy() uses '!=', not '<' for iterator comparison, so values above end are invalid; we // decompose offset as offset + offsetExceeding, where the offset < end. auto size = std::distance(begin, end); auto offsetExceeding = offset <= size ? 0 : offset - size; offset -= offsetExceeding; // Date step and first shifting into element's date static const int DayStep = 7 * (IsReverseIterator<IteratorT> ? -1 : 1); QDate date = (*std::prev(end))->date().addDays(DayStep * (offsetExceeding + 1)); // Shift left/right, shifted out elements are freed qDeleteAll(begin, begin + offset); auto pos = std::copy(begin + offset, end, begin); // Create new elements placing them from left/right side for (; pos != end; date = date.addDays(DayStep)) *pos++ = new Week(date);}//>>>>>>>>// Just syntactic sugar for whole container. Shift direction is determined by offset's sign.template <typename ContainerT>static void shift(ContainerT& container, int offset){ if (offset > 0) shift(container.begin(), container.end(), offset); else if (offset < 0) shift(container.rbegin(), container.rend(), -offset);}static void print(const QList<Week*>& weeks){ for (auto week : weeks) qDebug() << week->date();}int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); QList<Week*> weeks = { new Week(QDate::currentDate().addDays(-7)), new Week(QDate::currentDate()), new Week(QDate::currentDate().addDays(7)) }; qDebug() << "\nOriginal:"; print(weeks); qDebug() << "\nShift left on 1:"; shift(weeks, 1); print(weeks); qDebug() << "\nShift right on 1, so we get the original range:"; shift(weeks, -1); print(weeks); qDebug() << "\nShift left on 4:"; shift(weeks, 4); print(weeks); qDebug() << "\nShift right on 4, so we get the original range:"; shift(weeks, -4); print(weeks); return a.exec();}
QT -= guiCONFIG += c++17 consoleCONFIG -= app_bundle# You can make your code fail to compile if it uses deprecated APIs.# In order to do so, uncomment the following line.#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0SOURCES += \ main.cpp# Default rules for deployment.qnx: target.path = /tmp/$${TARGET}/binelse: unix:!android: target.path = /opt/$${TARGET}/bin!isEmpty(target.path): INSTALLS += target