[gnote] Add helper to efficiently remove item from std::vector



commit a84fbfd63080815d0fc76449688fa998d944ae50
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sat Apr 20 14:59:14 2019 +0300

    Add helper to efficiently remove item from std::vector

 src/Makefile.am             |  1 +
 src/test/unit/utiltests.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++
 src/utils.hpp               | 14 ++++++++++
 3 files changed, 80 insertions(+)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 630743b3..e132fb59 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -50,6 +50,7 @@ gnoteunittests_SOURCES = \
        test/unit/syncmanagerutests.cpp \
        test/unit/trieutests.cpp \
        test/unit/uriutests.cpp \
+       test/unit/utiltests.cpp \
        test/unit/xmlreaderutests.cpp \
        $(NULL)
 gnoteunittests_LDADD = libgnote.la @UNITTESTCPP_LIBS@
diff --git a/src/test/unit/utiltests.cpp b/src/test/unit/utiltests.cpp
new file mode 100644
index 00000000..76f61fc8
--- /dev/null
+++ b/src/test/unit/utiltests.cpp
@@ -0,0 +1,65 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2019 Aurimas Cernius
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <UnitTest++/UnitTest++.h>
+
+#include "utils.hpp"
+
+
+SUITE(Utis)
+{
+  int contains(const std::vector<int> & v, int val)
+  {
+    int count = 0;
+    for(auto i : v) {
+      if(i == val) {
+        ++count;
+      }
+    }
+
+    return count;
+  }
+
+  TEST(remove_swap_back)
+  {
+    std::vector<int> v;
+    CHECK(gnote::utils::remove_swap_back(v, 5) == false);
+
+    for(int i = 0; i < 100; ++i) {
+      v.push_back(i);
+    }
+    for(int i = 10; i < 100; i += 10) {
+      v.push_back(i);
+    }
+
+    CHECK(gnote::utils::remove_swap_back(v, 3));
+    CHECK_EQUAL(108, v.size());
+    CHECK_EQUAL(0, contains(v, 3));
+    CHECK(gnote::utils::remove_swap_back(v, 3) == false);
+    CHECK_EQUAL(108, v.size());
+
+    CHECK_EQUAL(2, contains(v, 20));
+    CHECK(gnote::utils::remove_swap_back(v, 20));
+    CHECK_EQUAL(1, contains(v, 20));
+    CHECK(gnote::utils::remove_swap_back(v, 20));
+    CHECK_EQUAL(0, contains(v, 20));
+  }
+}
+ 
diff --git a/src/utils.hpp b/src/utils.hpp
index 539c694e..d2480796 100644
--- a/src/utils.hpp
+++ b/src/utils.hpp
@@ -61,6 +61,20 @@ namespace gnote {
     void main_context_invoke(const sigc::slot<void> & slot);
     void main_context_call(const sigc::slot<void> & slot);
 
+    template <typename T>
+    bool remove_swap_back(std::vector<T> & v, const T & e)
+    {
+      for(typename std::vector<T>::iterator iter = v.begin(); iter != v.end(); ++iter) {
+        if(*iter == e) {
+          *iter = v.back();
+          v.pop_back();
+          return true;
+        }
+      }
+
+      return false;
+    }
+
     class GlobalKeybinder
     {
     public:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]