]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kitemset.h
SVN_SILENT made messages (.desktop file) - always resolve ours
[dolphin.git] / src / kitemviews / kitemset.h
index 5afe24df5db846ef7e46137ee7f0b0d3ec9a7cf2..d3562aca40b2c4a8d2fd082510231c23d5baee06 100644 (file)
@@ -1,28 +1,14 @@
-/***************************************************************************
- *   Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com>    *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2013 Frank Reininghaus <frank78ac@googlemail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
 
 #ifndef KITEMSET_H
 #define KITEMSET_H
 
 #include "dolphin_export.h"
-
-#include <kitemviews/kitemrange.h>
+#include "kitemviews/kitemrange.h"
 
 /**
  * @brief Stores a set of integer numbers in a space-efficient way.
@@ -46,9 +32,9 @@ class DOLPHIN_EXPORT KItemSet
 {
 public:
     KItemSet();
-    KItemSet(const KItemSetother);
+    KItemSet(const KItemSet &other);
     ~KItemSet();
-    KItemSet& operator=(const KItemSet& other);
+    KItemSet &operator=(const KItemSet &other);
 
     /**
      * Returns the number of items in the set.
@@ -59,25 +45,25 @@ public:
     bool isEmpty() const;
     void clear();
 
-    bool operator==(const KItemSetother) const;
-    bool operator!=(const KItemSetother) const;
+    bool operator==(const KItemSet &other) const;
+    bool operator!=(const KItemSet &other) const;
 
     class iterator
     {
-        iterator(const KItemRangeList::iterator& rangeIt, int offset) :
-            m_rangeIt(rangeIt),
-            m_offset(offset)
+        iterator(const KItemRangeList::iterator &rangeIt, int offset = 0)
+            : m_rangeIt(rangeIt)
+            m_offset(offset)
         {
         }
 
     public:
-        iterator(const iterator& other) :
-            m_rangeIt(other.m_rangeIt),
-            m_offset(other.m_offset)
+        iterator(const iterator &other)
+            : m_rangeIt(other.m_rangeIt)
+            m_offset(other.m_offset)
         {
         }
 
-        iterator& operator=(const iterator& other)
+        iterator &operator=(const iterator &other)
         {
             m_rangeIt = other.m_rangeIt;
             m_offset = other.m_offset;
@@ -91,17 +77,17 @@ public:
             return m_rangeIt->index + m_offset;
         }
 
-        inline bool operator==(const iteratorother) const
+        inline bool operator==(const iterator &other) const
         {
             return m_rangeIt == other.m_rangeIt && m_offset == other.m_offset;
         }
 
-        inline bool operator!=(const iteratorother) const
+        inline bool operator!=(const iterator &other) const
         {
             return !(*this == other);
         }
 
-        inline iteratoroperator++()
+        inline iterator &operator++()
         {
             ++m_offset;
 
@@ -120,7 +106,7 @@ public:
             return r;
         }
 
-        inline iteratoroperator--()
+        inline iterator &operator--()
         {
             if (m_offset == 0) {
                 --m_rangeIt;
@@ -147,29 +133,28 @@ public:
         friend class KItemSet;
     };
 
-
     class const_iterator
     {
-        const_iterator(KItemRangeList::const_iterator rangeIt, int offset) :
-            m_rangeIt(rangeIt),
-            m_offset(offset)
+        const_iterator(KItemRangeList::const_iterator rangeIt, int offset = 0)
+            : m_rangeIt(rangeIt)
+            m_offset(offset)
         {
         }
 
     public:
-        const_iterator(const const_iterator& other) :
-            m_rangeIt(other.m_rangeIt),
-            m_offset(other.m_offset)
+        const_iterator(const const_iterator &other)
+            : m_rangeIt(other.m_rangeIt)
+            m_offset(other.m_offset)
         {
         }
 
-        const_iterator(const iterator& other) :
-            m_rangeIt(other.m_rangeIt),
-            m_offset(other.m_offset)
+        explicit const_iterator(const iterator &other)
+            : m_rangeIt(other.m_rangeIt)
+            m_offset(other.m_offset)
         {
         }
 
-        const_iterator& operator=(const const_iterator& other)
+        const_iterator &operator=(const const_iterator &other)
         {
             m_rangeIt = other.m_rangeIt;
             m_offset = other.m_offset;
@@ -183,17 +168,17 @@ public:
             return m_rangeIt->index + m_offset;
         }
 
-        inline bool operator==(const const_iteratorother) const
+        inline bool operator==(const const_iterator &other) const
         {
             return m_rangeIt == other.m_rangeIt && m_offset == other.m_offset;
         }
 
-        inline bool operator!=(const const_iteratorother) const
+        inline bool operator!=(const const_iterator &other) const
         {
             return !(*this == other);
         }
 
-        inline const_iteratoroperator++()
+        inline const_iterator &operator++()
         {
             ++m_offset;
 
@@ -212,7 +197,7 @@ public:
             return r;
         }
 
-        inline const_iteratoroperator--()
+        inline const_iterator &operator--()
         {
             if (m_offset == 0) {
                 --m_rangeIt;
@@ -238,6 +223,70 @@ public:
         friend class KItemSet;
     };
 
+    class const_reverse_iterator
+    {
+    public:
+        const_reverse_iterator(KItemSet::const_iterator rangeIt)
+            : m_current(rangeIt)
+        {
+        }
+
+        const_reverse_iterator(const KItemSet::const_reverse_iterator &other)
+            : m_current(other.base())
+        {
+        }
+
+        int operator*() const
+        {
+            // analog to std::prev
+            auto t = const_iterator(m_current);
+            --t;
+            return *t;
+        }
+
+        inline bool operator==(const const_reverse_iterator &other) const
+        {
+            return m_current == other.m_current;
+        }
+
+        bool operator!=(const const_reverse_iterator &other) const
+        {
+            return !(*this == other);
+        }
+
+        const_reverse_iterator &operator++()
+        {
+            --m_current;
+            return *this;
+        }
+        const_reverse_iterator operator++(int)
+        {
+            auto tmp = *this;
+            ++(*this);
+            return tmp;
+        }
+
+        const_reverse_iterator &operator--()
+        {
+            ++m_current;
+            return *this;
+        }
+        const_reverse_iterator operator--(int)
+        {
+            auto tmp = *this;
+            --(*this);
+            return tmp;
+        }
+
+        KItemSet::const_iterator base() const
+        {
+            return m_current;
+        }
+
+    private:
+        KItemSet::const_iterator m_current;
+    };
+
     iterator begin();
     const_iterator begin() const;
     const_iterator constBegin() const;
@@ -245,6 +294,9 @@ public:
     const_iterator end() const;
     const_iterator constEnd() const;
 
+    const_reverse_iterator rend() const;
+    const_reverse_iterator rbegin() const;
+
     int first() const;
     int last() const;
 
@@ -259,16 +311,16 @@ public:
      * Returns a new set which contains all items that are contained in this
      * KItemSet, in \a other, or in both.
      */
-    KItemSet operator+(const KItemSetother) const;
+    KItemSet operator+(const KItemSet &other) const;
 
     /**
      * Returns a new set which contains all items that are contained either in
      * this KItemSet, or in \a other, but not in both (the symmetric difference
      * of both KItemSets).
      */
-    KItemSet operator^(const KItemSetother) const;
+    KItemSet operator^(const KItemSet &other) const;
 
-    KItemSetoperator<<(int i);
+    KItemSet &operator<<(int i);
 
 private:
     /**
@@ -295,28 +347,28 @@ private:
     friend class KItemSetTest;
 };
 
-inline KItemSet::KItemSet() :
-    m_itemRanges()
+inline KItemSet::KItemSet()
+    m_itemRanges()
 {
 }
 
-inline KItemSet::KItemSet(const KItemSet& other) :
-    m_itemRanges(other.m_itemRanges)
+inline KItemSet::KItemSet(const KItemSet &other)
+    m_itemRanges(other.m_itemRanges)
 {
 }
 
 inline KItemSet::~KItemSet() = default;
 
-inline KItemSet& KItemSet::operator=(const KItemSet& other)
+inline KItemSet &KItemSet::operator=(const KItemSet &other)
 {
-    m_itemRanges=other.m_itemRanges;
+    m_itemRanges = other.m_itemRanges;
     return *this;
 }
 
 inline int KItemSet::count() const
 {
     int result = 0;
-    foreach (const KItemRange& range, m_itemRanges) {
+    for (const KItemRange &range : std::as_const(m_itemRanges)) {
         result += range.count;
     }
     return result;
@@ -332,12 +384,12 @@ inline void KItemSet::clear()
     m_itemRanges.clear();
 }
 
-inline bool KItemSet::operator==(const KItemSetother) const
+inline bool KItemSet::operator==(const KItemSet &other) const
 {
     return m_itemRanges == other.m_itemRanges;
 }
 
-inline bool KItemSet::operator!=(const KItemSetother) const
+inline bool KItemSet::operator!=(const KItemSet &other) const
 {
     return m_itemRanges != other.m_itemRanges;
 }
@@ -381,32 +433,32 @@ inline bool KItemSet::remove(int i)
 
 inline KItemSet::iterator KItemSet::begin()
 {
-    return iterator(m_itemRanges.begin(), 0);
+    return iterator(m_itemRanges.begin());
 }
 
 inline KItemSet::const_iterator KItemSet::begin() const
 {
-    return const_iterator(m_itemRanges.begin(), 0);
+    return const_iterator(m_itemRanges.begin());
 }
 
 inline KItemSet::const_iterator KItemSet::constBegin() const
 {
-    return const_iterator(m_itemRanges.constBegin(), 0);
+    return const_iterator(m_itemRanges.constBegin());
 }
 
 inline KItemSet::iterator KItemSet::end()
 {
-    return iterator(m_itemRanges.end(), 0);
+    return iterator(m_itemRanges.end());
 }
 
 inline KItemSet::const_iterator KItemSet::end() const
 {
-    return const_iterator(m_itemRanges.end(), 0);
+    return const_iterator(m_itemRanges.end());
 }
 
 inline KItemSet::const_iterator KItemSet::constEnd() const
 {
-    return const_iterator(m_itemRanges.constEnd(), 0);
+    return const_iterator(m_itemRanges.constEnd());
 }
 
 inline int KItemSet::first() const
@@ -416,11 +468,21 @@ inline int KItemSet::first() const
 
 inline int KItemSet::last() const
 {
-    const KItemRangelastRange = m_itemRanges.last();
+    const KItemRange &lastRange = m_itemRanges.last();
     return lastRange.index + lastRange.count - 1;
 }
 
-inline KItemSet& KItemSet::operator<<(int i)
+inline KItemSet::const_reverse_iterator KItemSet::rend() const
+{
+    return KItemSet::const_reverse_iterator(constBegin());
+}
+
+inline KItemSet::const_reverse_iterator KItemSet::rbegin() const
+{
+    return KItemSet::const_reverse_iterator(constEnd());
+}
+
+inline KItemSet &KItemSet::operator<<(int i)
 {
     insert(i);
     return *this;